Redact paths on windows in more cases

This commit is contained in:
Jamie Kyle 2023-06-16 11:40:58 -07:00 committed by GitHub
parent a5c6db096b
commit ac76271772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 18 deletions

View file

@ -94,7 +94,7 @@ describe('Privacy', () => {
'path1 [REDACTED]/main.js\n' +
'phone1 +[REDACTED]455 ipsum\n' +
'group1 group([REDACTED]789) doloret\n' +
'path2 file:///[REDACTED]/js/background.js.' +
'path2 [REDACTED]/js/background.js.' +
'phone2 +[REDACTED]566 lorem\n' +
'group2 group([REDACTED]hij) doloret\n' +
'path3 [REDACTED]/attachment.noindex\n';
@ -134,7 +134,7 @@ describe('Privacy', () => {
'path1 [REDACTED]/main.js\n' +
'phone1 +12223334455 ipsum\n' +
'group1 group(123456789) doloret\n' +
'path2 file:///[REDACTED]/js/background.js.';
'path2 [REDACTED]/js/background.js.';
assert.equal(actual, expected);
});

View file

@ -3,12 +3,12 @@
/* eslint-env node */
import { join as pathJoin } from 'path';
import path from 'path';
import { compose } from 'lodash/fp';
import { escapeRegExp, isString, isRegExp } from 'lodash';
export const APP_ROOT_PATH = pathJoin(__dirname, '..', '..');
export const APP_ROOT_PATH = path.join(__dirname, '..', '..');
const PHONE_NUMBER_PATTERN = /\+\d{7,12}(\d{3})/g;
const UUID_PATTERN =
@ -41,20 +41,45 @@ export const _redactPath = (filePath: string): RedactFunction => {
export const _pathToRegExp = (filePath: string): RegExp | undefined => {
try {
const pathWithNormalizedSlashes = filePath.replace(/\//g, '\\');
const pathWithEscapedSlashes = filePath.replace(/\\/g, '\\\\');
const urlEncodedPath = encodeURI(filePath);
// Safe `String::replaceAll`:
// https://github.com/lodash/lodash/issues/1084#issuecomment-86698786
const patternString = [
filePath,
pathWithNormalizedSlashes,
pathWithEscapedSlashes,
urlEncodedPath,
]
.map(escapeRegExp)
.join('|');
return new RegExp(patternString, 'g');
return new RegExp(
// Any possible prefix that we want to include
`(${escapeRegExp('file:///')})?${
// The rest of the file path
filePath
// Split by system path seperator ("/" or "\\")
// (split by both for tests)
.split(/\/|\\/)
// Escape all special characters in each part
.map(part => {
// This segment may need to be URI encoded
const urlEncodedPart = encodeURI(part);
// If its the same, then we don't need to worry about it
if (urlEncodedPart === part) {
return escapeRegExp(part);
}
// Otherwise, we need to test against both
return `(${escapeRegExp(part)}|${escapeRegExp(urlEncodedPart)})`;
})
// Join the parts back together with any possible path seperator
.join(
`(${[
// Posix (Linux, macOS, etc.)
path.posix.sep,
// Windows
path.win32.sep,
// Windows (URI encoded)
encodeURI(path.win32.sep),
]
// Escape the parts for use in a RegExp (e.g. "/" -> "\/")
.map(sep => escapeRegExp(sep))
// In case separators are repeated in the path (e.g. "\\\\")
.map(sep => `${sep}+`)
// Join all the possible separators together
.join('|')})`
)
}`,
'g'
);
} catch (error) {
return undefined;
}