diff --git a/server/src/middleware/logging.interceptor.ts b/server/src/middleware/logging.interceptor.ts index d1cd9c0e410a..028ec69559be 100644 --- a/server/src/middleware/logging.interceptor.ts +++ b/server/src/middleware/logging.interceptor.ts @@ -2,11 +2,12 @@ import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nes import { Request, Response } from 'express'; import { Observable, finalize } from 'rxjs'; import { LoggingRepository } from 'src/repositories/logging.repository.js'; +import { REDACTED, isSensitiveKey, redactUrl } from 'src/utils/request.js'; const maxArrayLength = 100; const replacer = (key: string, value: unknown) => { - if (key.toLowerCase().includes('password')) { - return '********'; + if (isSensitiveKey(key)) { + return REDACTED; } if (Array.isArray(value) && value.length > maxArrayLength) { @@ -37,7 +38,7 @@ export class LoggingInterceptor implements NestInterceptor { const duration = (finish - start).toFixed(2); const { statusCode } = res; - this.logger.debug(`${method} ${url} ${statusCode} ${duration}ms ${ip}`); + this.logger.debug(`${method} ${redactUrl(url)} ${statusCode} ${duration}ms ${ip}`); if (req.body && Object.keys(req.body).length > 0) { this.logger.verbose(JSON.stringify(req.body, replacer)); } diff --git a/server/src/utils/request.spec.ts b/server/src/utils/request.spec.ts index 740d313fc9f8..f6ee5d9dbeac 100644 --- a/server/src/utils/request.spec.ts +++ b/server/src/utils/request.spec.ts @@ -1,4 +1,4 @@ -import { getAppVersionFromUA } from 'src/utils/request.js'; +import { getAppVersionFromUA, isSensitiveKey, redactUrl } from 'src/utils/request.js'; describe(getAppVersionFromUA.name, () => { it('should get the app version for android', () => { @@ -13,3 +13,44 @@ describe(getAppVersionFromUA.name, () => { expect(getAppVersionFromUA('immich-unknown/1.123.4')).toEqual('1.123.4'); }); }); + +describe(redactUrl.name, () => { + it.each([ + ['/api/assets/1/video/playback?sessionKey=secret', '/api/assets/1/video/playback?sessionKey=********'], + ['/api/assets/1/original?key=secret&edited=true', '/api/assets/1/original?key=********&edited=true'], + [ + '/api/timeline/buckets?visibility=timeline&slug=secret', + '/api/timeline/buckets?visibility=timeline&slug=********', + ], + ['/api/albums?apiKey=secret', '/api/albums?apiKey=********'], + ['/api/oauth/mobile-redirect?code=secret&state=abc', '/api/oauth/mobile-redirect?code=********&state=abc'], + ])('should redact %s', (url, expected) => { + expect(redactUrl(url)).toEqual(expected); + }); + + it.each(['/api/albums', '/api/search/suggestions?type=country&monkey=1', '/api/assets?key'])( + 'should not change %s', + (url) => { + expect(redactUrl(url)).toEqual(url); + }, + ); +}); + +describe(isSensitiveKey.name, () => { + it.each([ + 'password', + 'newPassword', + 'pinCode', + 'newPinCode', + 'token', + 'logout_token', + 'clientSecret', + 'codeVerifier', + ])('should match %s', (key) => { + expect(isSensitiveKey(key)).toBe(true); + }); + + it.each(['key', 'name', 'email', 'assetIds'])('should not match %s', (key) => { + expect(isSensitiveKey(key)).toBe(false); + }); +}); diff --git a/server/src/utils/request.ts b/server/src/utils/request.ts index 5f4763bb343c..9edd1555d975 100644 --- a/server/src/utils/request.ts +++ b/server/src/utils/request.ts @@ -1,5 +1,6 @@ import { IncomingHttpHeaders } from 'node:http'; import { UAParser } from 'ua-parser-js'; +import { ImmichQuery } from 'src/enum.js'; export const fromChecksum = (checksum: string): Buffer => { return Buffer.from(checksum, checksum.length === 28 ? 'base64' : 'hex'); @@ -41,3 +42,15 @@ export const isCrossOriginRequest = (headers: IncomingHttpHeaders, method?: stri return !URL.canParse(origin) || new URL(origin).host !== host; }; + +export const REDACTED = '********'; + +const SENSITIVE_QUERY_PARAMS = [...Object.values(ImmichQuery), 'code']; +const sensitiveQueryParamRegex = new RegExp(`([?&](?:${SENSITIVE_QUERY_PARAMS.join('|')})=)[^&#]*`, 'g'); + +export const redactUrl = (url: string) => + url.replaceAll(sensitiveQueryParamRegex, (_, prefix: string) => prefix + REDACTED); + +const SENSITIVE_KEYS = ['password', 'pincode', 'secret', 'token', 'verifier', 'licensekey', 'activationkey']; + +export const isSensitiveKey = (key: string) => SENSITIVE_KEYS.some((item) => key.toLowerCase().includes(item));