From ac76271772e4816c09b59a61e5e5b0070c206558 Mon Sep 17 00:00:00 2001 From: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:40:58 -0700 Subject: [PATCH] Redact paths on windows in more cases --- ts/test-both/util/privacy_test.ts | 4 +-- ts/util/privacy.ts | 57 ++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/ts/test-both/util/privacy_test.ts b/ts/test-both/util/privacy_test.ts index d671a2edfa8..55abba5f698 100644 --- a/ts/test-both/util/privacy_test.ts +++ b/ts/test-both/util/privacy_test.ts @@ -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); }); diff --git a/ts/util/privacy.ts b/ts/util/privacy.ts index 8b24be454a8..4b8d13aa15a 100644 --- a/ts/util/privacy.ts +++ b/ts/util/privacy.ts @@ -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; }