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

View file

@ -3,12 +3,12 @@
/* eslint-env node */ /* eslint-env node */
import { join as pathJoin } from 'path'; import path from 'path';
import { compose } from 'lodash/fp'; import { compose } from 'lodash/fp';
import { escapeRegExp, isString, isRegExp } from 'lodash'; 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 PHONE_NUMBER_PATTERN = /\+\d{7,12}(\d{3})/g;
const UUID_PATTERN = const UUID_PATTERN =
@ -41,20 +41,45 @@ export const _redactPath = (filePath: string): RedactFunction => {
export const _pathToRegExp = (filePath: string): RegExp | undefined => { export const _pathToRegExp = (filePath: string): RegExp | undefined => {
try { try {
const pathWithNormalizedSlashes = filePath.replace(/\//g, '\\'); return new RegExp(
const pathWithEscapedSlashes = filePath.replace(/\\/g, '\\\\'); // Any possible prefix that we want to include
const urlEncodedPath = encodeURI(filePath); `(${escapeRegExp('file:///')})?${
// Safe `String::replaceAll`: // The rest of the file path
// https://github.com/lodash/lodash/issues/1084#issuecomment-86698786 filePath
const patternString = [ // Split by system path seperator ("/" or "\\")
filePath, // (split by both for tests)
pathWithNormalizedSlashes, .split(/\/|\\/)
pathWithEscapedSlashes, // Escape all special characters in each part
urlEncodedPath, .map(part => {
] // This segment may need to be URI encoded
.map(escapeRegExp) const urlEncodedPart = encodeURI(part);
.join('|'); // If its the same, then we don't need to worry about it
return new RegExp(patternString, 'g'); 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) { } catch (error) {
return undefined; return undefined;
} }