From 52e565ace0c818a20834ac9fb129371ac3e9b59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 09:42:54 +0000 Subject: [PATCH] fix(core): telemetry redaction stays fast on a huge token (#8672) * fix(core): telemetry redaction stays fast on a huge token * fix(core): a bounded redaction still redacts a path a long run or the cut touches * fix(core): a shortened run keeps its end, which callers reading the tail need --- packages/core/src/telemetryRedaction.test.ts | 29 ++++++++++++++++++++ packages/core/src/telemetryRedaction.ts | 19 ++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/core/src/telemetryRedaction.test.ts b/packages/core/src/telemetryRedaction.test.ts index 5d5552061d..9d2ee87b6d 100644 --- a/packages/core/src/telemetryRedaction.test.ts +++ b/packages/core/src/telemetryRedaction.test.ts @@ -69,6 +69,35 @@ describe("redactTelemetryString", () => { ); }); + // fails without the fix (never finishes): every path pattern rescanned the token from each delimiter in it. + it.each([["=,([".repeat(1024 * 1024)], ["=.".repeat(2 * 1024 * 1024)]])( + "redacts a multi-megabyte token of delimiters in milliseconds", + (token) => { + const start = performance.now(); + const out = redactTelemetryString(`Conflicting script integrity requirements for ${token}`); + expect(performance.now() - start).toBeLessThan(1000); + expect(out.length).toBeLessThanOrEqual(240); + }, + ); + + it("still redacts a path after a run too long to be a path segment", () => { + const out = redactTelemetryString(`${"=".repeat(300)} /Users/kim/secret.mp4`, Infinity); + expect(out).not.toContain("secret"); + }); + + // fails without the fix: a long run must keep the file name that makes the rest of it a path. + it.each(["/", "\\"])("redacts a path whose file name starts a long run (%s)", (separator) => { + const message = `failed: acme-secret${separator}clip.mp4?&${"x".repeat(300)}`; + expect(redactTelemetryString(message, 240)).toBe("failed: [path]"); + }); + + // fails without the fix: the cut fell inside the path and left its start, which no longer reads as a path. + it("cuts long input at whitespace, so a path on the cut is never left half", () => { + const filler = "a ".repeat(8187); + const out = redactTelemetryString(`${filler}customer/acme-secret/video.mp4 tail`, Infinity); + expect(out).not.toMatch(/customer|acme/); + }); + it("truncates after redacting, so a long path cannot survive by being cut", () => { const out = redactTelemetryString(`/data/${"x".repeat(500)}/a.mp4`, 40); expect(out).not.toContain("xxx"); diff --git a/packages/core/src/telemetryRedaction.ts b/packages/core/src/telemetryRedaction.ts index fa923534f0..d547ae0fe8 100644 --- a/packages/core/src/telemetryRedaction.ts +++ b/packages/core/src/telemetryRedaction.ts @@ -150,9 +150,26 @@ function redactFilePaths(value: string): string { ); } +// The path patterns cost up to the square of a run's length. Before any of them runs, input is cut at whitespace so +// no path is split, and a long run keeps only its two ends: its start still names a path, its end what follows. +const MAX_REDACTED_INPUT = 16 * 1024; +const RUN_KEPT = 256; +const LONG_RUN = new RegExp(String.raw`(?= 0 && !/\s/.test(value.charAt(end))) end--; + return `${value.slice(0, end + 1)}…`; +} + export function redactTelemetryString( value: string, maxLength = MAX_TELEMETRY_STRING_LENGTH, ): string { - return truncateTelemetryString(redactFilePaths(redactUrlQueryStrings(value)), maxLength); + const shortRuns = redactUrlQueryStrings(cutAtWhitespace(value)).replace( + LONG_RUN, + (run) => `${run.slice(0, RUN_KEPT / 2)}…${run.slice(-RUN_KEPT / 2)}`, + ); + return truncateTelemetryString(redactFilePaths(shortRuns), maxLength); }