Skip to content

Commit

Permalink
fix(logger): sanitize urls in strings (#15102)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Apr 14, 2022
1 parent ee582fe commit 6ef2b8c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/logger/__snapshots__/err-serializer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Object {
"http2": false,
"method": "POST",
"password": "***********",
"url": "https://:**redacted**@github.com/api",
"url": "https://**redacted**@github.com/api",
"username": "",
},
"response": Object {
Expand Down
2 changes: 1 addition & 1 deletion lib/logger/err-serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('logger/err-serializer', () => {
options: {
method: 'POST',
password: '***********',
url: 'https://:**redacted**@github.com/api',
url: 'https://**redacted**@github.com/api',
username: '',
},
});
Expand Down
15 changes: 14 additions & 1 deletion lib/logger/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateLogLevel } from './utils';
import { sanitizeValue, validateLogLevel } from './utils';

describe('logger/utils', () => {
afterEach(() => {
Expand Down Expand Up @@ -30,4 +30,17 @@ describe('logger/utils', () => {
}).toThrow();
expect(mockExit).toHaveBeenCalledWith(1);
});

it.each`
input | output
${' https://somepw@domain.com/gitlab/org/repo?go-get'} | ${' https://**redacted**@domain.com/gitlab/org/repo?go-get'}
${'https://someuser:somepw@domain.com'} | ${'https://**redacted**@domain.com'}
${'https://someuser:@domain.com'} | ${'https://**redacted**@domain.com'}
${'redis://:somepw@172.32.11.71:6379/0'} | ${'redis://**redacted**@172.32.11.71:6379/0'}
${'some text with\r\n url: https://somepw@domain.com\nand some more'} | ${'some text with\r\n url: https://**redacted**@domain.com\nand some more'}
${'[git://domain.com](git://pw@domain.com)'} | ${'[git://domain.com](git://**redacted**@domain.com)'}
${'user@domain.com'} | ${'user@domain.com'}
`('sanitizeValue("$input") == "$output"', ({ input, output }) => {
expect(sanitizeValue(input)).toBe(output);
});
});
12 changes: 11 additions & 1 deletion lib/logger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function sanitizeValue(
seen = new WeakMap<NestedValue, unknown>()
): any {
if (is.string(value)) {
return sanitize(value);
return sanitize(sanitizeUrls(value));
}

if (is.date(value)) {
Expand Down Expand Up @@ -243,3 +243,13 @@ export function validateLogLevel(logLevelToCheck: string | undefined): void {
logger.fatal(`${logLevelToCheck} is not a valid log level. terminating...`);
process.exit(1);
}

// Can't use `util/regex` because of circular reference to logger
const urlRe = /[a-z]{3,9}:\/\/[-;:&=+$,\w]+@[a-z0-9.-]+/gi;
const urlCredRe = /\/\/[^@]+@/g;

export function sanitizeUrls(text: string): string {
return text.replace(urlRe, (url) => {
return url.replace(urlCredRe, '//**redacted**@');
});
}

0 comments on commit 6ef2b8c

Please sign in to comment.