From 4e0745c569e89c3c56fbc34d0af0786c84837bdf Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 12:17:37 +0900 Subject: [PATCH] fix(telemetry): redact credentials anywhere in a string, URL secrets and emails (R-9.3) The value check was anchored with ^, so "request failed: Authorization: Bearer abc" passed through, as did ?access_token=... in a URL and email addresses. The key deny-list also missed pwd/pass/auth/otp/pin. Changes in src/platform/telemetry/redact-telemetry.ts: - Bearer/Basic credentials and JWTs are masked wherever they occur; only the match is replaced with [redacted], the rest of the message stays. A JWT cut short still matches. - URL query/fragment parameters (?, &, #) are masked when the name is a secret key (isSecretTelemetryKey) or one of the URL-only names code, key, sig. Non-secret parameters (limit, cursor, state) are kept. - Email addresses are masked, including one cut in half by truncation. - Keys: pwd, pass, auth, otp, pin match as whole key tokens (camelCase, snake, kebab, PINCode split), so spinner/author/compass/isPinned are kept; passcode and passphrase are added as fragments. - Strings are truncated before the patterns run, so the scan is bounded by maxStringLength. No regex lookbehind (Vite's default target includes Safari 16.0). Public exports and signatures are unchanged. Pinned first (passed before and after): "keeps legitimate telemetry values and innocent keys untouched" (enum codes, ULID/UUID/hex ids, route ids, numbers, booleans, BASIC_PLAN, react@19.1.0, a URL without secrets, code=... in prose, keys spinner/author/code/key). RED before fix: - "redacts the short secret key %s as a whole key token" (11 cases, key kept) - "redacts credentials in the middle of a string" (values unchanged) - "masks secret URL query and fragment parameters" (tokens kept) - "masks email addresses" (email kept) - "does not leak a secret cut in half by truncation" (partial secrets kept) Gates: check:architecture, check:types:app, check:types:test, lint, test:component, check:release-coherence, test:platform pass. test:unit passes except tests/unit/removal-fixture.test.ts, which needs generated release evidence under artifacts/ that a fresh worktree does not have (unrelated to this change). Co-Authored-By: Claude Opus 5.5 --- src/platform/telemetry/redact-telemetry.ts | 62 +++++++++++-- .../telemetry/redact-telemetry.test.ts | 93 +++++++++++++++++++ 2 files changed, 147 insertions(+), 8 deletions(-) diff --git a/src/platform/telemetry/redact-telemetry.ts b/src/platform/telemetry/redact-telemetry.ts index ad9257d3..5c850d7b 100644 --- a/src/platform/telemetry/redact-telemetry.ts +++ b/src/platform/telemetry/redact-telemetry.ts @@ -40,15 +40,60 @@ const SECRET_KEY_FRAGMENTS = Object.freeze([ "session", "privatekey", "signature", + "passcode", + "passphrase", ]); +/** + * Short secret words, matched only as a whole key token (`pin`, `userPin`, + * `pin_code`, `PINCode`) so innocent keys like `spinner` or `author` survive. + */ +const SECRET_KEY_TOKENS: ReadonlySet = new Set(["pwd", "pass", "auth", "otp", "pin"]); + +function keyTokens(key: string): string[] { + return key + .replace(/([a-z0-9])([A-Z])/gu, "$1 $2") + .replace(/([A-Z])([A-Z][a-z])/gu, "$1 $2") + .toLowerCase() + .split(/[^a-z0-9]+/u); +} + export function isSecretTelemetryKey(key: string): boolean { const normalized = key.toLowerCase().replace(/[^a-z0-9]/gu, ""); - return SECRET_KEY_FRAGMENTS.some((fragment) => normalized.includes(fragment)); + return ( + SECRET_KEY_FRAGMENTS.some((fragment) => normalized.includes(fragment)) || + keyTokens(key).some((token) => SECRET_KEY_TOKENS.has(token)) + ); } -/** Values that look like bearer credentials are redacted wherever they occur. */ -const SECRET_VALUE = /^(?:bearer|basic)\s+\S+|^eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\./iu; +/** + * Credential-shaped substrings are masked wherever they occur in a string: + * bearer/basic credentials, JWTs (also one cut short) and email addresses. + */ +const SECRET_VALUES: readonly RegExp[] = [ + /\b(?:bearer|basic)\s+\S+/giu, + /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_.-]*/gu, + /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}/gu, +]; + +/** A URL query or fragment parameter: `?name=value`, `&name=value`, `#name=value`. */ +const URL_PARAMETER = /([?&#])([^=&#?\s]+)=([^&#\s]+)/gu; + +/** Secret URL parameter names too generic to deny as payload keys. */ +const SECRET_URL_PARAMETERS: ReadonlySet = new Set(["code", "key", "sig"]); + +/** An email address cut by truncation, left at the very end of the string. */ +const CUT_EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]*$/u; + +function redactString(value: string): string { + let text = value.replace(URL_PARAMETER, (match, separator: string, name: string) => + isSecretTelemetryKey(name) || SECRET_URL_PARAMETERS.has(name.toLowerCase()) + ? `${separator}${name}=${REDACTED}` + : match, + ); + for (const pattern of SECRET_VALUES) text = text.replace(pattern, REDACTED); + return text; +} function readOwnData( source: object, @@ -61,11 +106,12 @@ function readOwnData( function sanitizeLeaf(value: unknown, budget: TelemetryPayloadBudget): unknown { switch (typeof value) { - case "string": - if (SECRET_VALUE.test(value)) return REDACTED; - return value.length > budget.maxStringLength - ? `${value.slice(0, budget.maxStringLength)}${TRUNCATED}` - : value; + case "string": { + if (value.length <= budget.maxStringLength) return redactString(value); + // Truncate first so the patterns only ever scan a bounded string. + const head = value.slice(0, budget.maxStringLength).replace(CUT_EMAIL, REDACTED); + return `${redactString(head)}${TRUNCATED}`; + } case "number": return Number.isFinite(value) ? value : null; case "boolean": diff --git a/tests/platform/telemetry/redact-telemetry.test.ts b/tests/platform/telemetry/redact-telemetry.test.ts index 10ccb0aa..13da3112 100644 --- a/tests/platform/telemetry/redact-telemetry.test.ts +++ b/tests/platform/telemetry/redact-telemetry.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; import { + DEFAULT_TELEMETRY_PAYLOAD_BUDGET, REDACTED, redactTelemetryPayload, TRUNCATED, @@ -34,6 +35,98 @@ describe("redactTelemetryPayload", () => { expect(result).toEqual({ note: REDACTED, jwt: REDACTED, list: [REDACTED] }); }); + it("keeps legitimate telemetry values and innocent keys untouched", () => { + const legitimate = { + reason: "REQUEST_TIMEOUT", + operation: "LIST_REFERENCE_RESOURCES", + routeId: "reference.resources.detail", + featureId: "reference-feature", + resourceId: "01J8Z3K7Q9W4N2B6C5D1E0F7GH", + uuid: "3f2a9c1e-5b7d-4e8f-9a0b-1c2d3e4f5a6b", + traceHex: "4bf92f3577b34da6a3ce929d0e0e4736", + plan: "BASIC_PLAN", + dependency: "react@19.1.0", + path: "/api/resources?limit=20&cursor=c1#top", + message: "error code=REQUEST_TIMEOUT after 3 attempts", + status: 404, + durationMs: 12.5, + retried: false, + spinner: "visible", + author: "system", + compass: "north", + opinion: "none", + shipping: "standard", + isPinned: true, + code: "E_CONFLICT", + key: "sort-order", + }; + expect(redactTelemetryPayload(legitimate)).toEqual(legitimate); + }); + + it.each([ + "pwd", + "pass", + "auth", + "otp", + "pin", + "userPwd", + "pin_code", + "PINCode", + "x-otp", + "authHeader", + "passcode", + ])("redacts the short secret key %s as a whole key token", (secretKey) => { + const result = redactTelemetryPayload({ [secretKey]: "s3cr3t", safe: "ok" }); + expect(result).toEqual({ safe: "ok" }); + }); + + it("redacts credentials in the middle of a string", () => { + expect( + redactTelemetryPayload({ + header: "request failed: Authorization: Bearer abc", + basic: "proxy said Basic dXNlcjpwYXNz was rejected", + jwt: "retry with eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl now", + }), + ).toEqual({ + header: `request failed: Authorization: ${REDACTED}`, + basic: `proxy said ${REDACTED} was rejected`, + jwt: `retry with ${REDACTED} now`, + }); + }); + + it("masks secret URL query and fragment parameters", () => { + expect( + redactTelemetryPayload( + "GET https://api.example.com/cb?code=abc123&state=xyz&access_token=tok456&key=k1#id_token=jwt789", + ), + ).toBe( + `GET https://api.example.com/cb?code=${REDACTED}&state=xyz&access_token=${REDACTED}&key=${REDACTED}#id_token=${REDACTED}`, + ); + expect(redactTelemetryPayload("/login?user=a&password=hunter2&otp=123456")).toBe( + `/login?user=a&password=${REDACTED}&otp=${REDACTED}`, + ); + }); + + it("masks email addresses", () => { + expect( + redactTelemetryPayload({ note: "invite failed for jane.doe+test@example.co.uk twice" }), + ).toEqual({ note: `invite failed for ${REDACTED} twice` }); + }); + + it("does not leak a secret cut in half by truncation", () => { + const budget = { ...DEFAULT_TELEMETRY_PAYLOAD_BUDGET, maxStringLength: 32 }; + const cut = redactTelemetryPayload( + { + email: `${"x".repeat(20)} jane.doe@example.com`, + query: `/cb${"x".repeat(10)}?access_token=abcdefghijklmnopqrstuvwxyz`, + bearer: `${"x".repeat(20)} Bearer abcdefghijklmnopqrstuvwxyz`, + }, + budget, + ); + expect(JSON.stringify(cut)).not.toMatch(/jane|abcd/u); + expect(JSON.stringify(cut)).toContain(TRUNCATED); + }); + it("never invokes getters, toJSON or foreign prototypes", () => { let invoked = false; class Carrier {