Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions server/src/middleware/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down
43 changes: 42 additions & 1 deletion server/src/utils/request.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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',
Comment on lines +39 to +48

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment

])('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);
});
});
13 changes: 13 additions & 0 deletions server/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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'];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why these keys and the ones on the spec are different ? e.g. pincode vs pinCode

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison is case insensitive by making it lowercase before comparing.


export const isSensitiveKey = (key: string) => SENSITIVE_KEYS.some((item) => key.toLowerCase().includes(item));
Loading