mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
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
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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`(?<![^\s/\\'"])[^\s/\\'"]{${RUN_KEPT + 1},}`, "g");
|
||||
|
||||
function cutAtWhitespace(value: string): string {
|
||||
if (value.length <= MAX_REDACTED_INPUT) return value;
|
||||
let end = MAX_REDACTED_INPUT;
|
||||
while (end >= 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user