import { describe, expect, it } from "vitest"; describe("explicit diagnostic credential forms", () => { it("masks suffixed CLI options and encoded JSON headers with whitespace", () => { const jwt = `${Buffer.from(' {"alg":"HS256","typ":"JWT"}').toString("base64url")}.abcdefghijk.abcdefghijkl`; expect(redactCommandText("tool --api-key-prod sensitivevalue --token-policy readable")) .not.toContain("sensitivevalue"); expect(redactCommandText(`provider ${jwt}`)).not.toContain(jwt); expect(redactCommandText("plan.security.credentials.md")).toBe("plan.security.credentials.md"); }); }); import { REDACTED_COMMAND_TEXT_VALUE, collectDiagnosticEnvironmentSecrets, collectHostEnvironmentTokenSecrets, looksLikeCredentialJwt, redactDiagnosticText, redactCommandText, } from "./command-redaction.js"; describe("looksLikeCredentialJwt", () => { it("recognizes encoded headers even with short payloads and signatures", () => { const header = Buffer.from('{"alg":"HS256"}').toString("base64url"); expect(looksLikeCredentialJwt(`${header}.e30.c2ln`)).toBe(true); }); it("recognizes direct-encryption JWTs with an empty encrypted-key segment", () => { const header = Buffer.from('{"alg":"dir","enc":"A256GCM"}').toString("base64url"); expect(looksLikeCredentialJwt(`${header}..abcdefghijk.abcdefghijkl.abcdefghijk`)).toBe(true); }); it.each([ "eyJ..signature", "eyJ.key..ciphertext.tag", "eyJ.key.iv..tag", "eyJ.key.iv.ciphertext.", ])("rejects empty segments outside the JWE encrypted key: %s", (input) => { expect(looksLikeCredentialJwt(input)).toBe(false); }); it.each([ "api.openai.com", "agents.staging.example.com", "deployment.credentials.example.md", "paperclip.native-model-envelope.v3", ])("rejects public dotted identifiers: %s", (input) => { expect(looksLikeCredentialJwt(input)).toBe(false); }); }); describe("redactDiagnosticText", () => { it("preserves credential metadata and dotted identifiers", () => { for (const text of [ '"tokenBudget":4000 --token-budget 4000 SECRET_STORAGE=vault', '{"credentialHandling":"harness","authorizationRequired":true}', "executor.customTools.integrations.list deployment.credentials.example.md api.openai.com", "Use a private key and secret manager with credential handling.", "Use bearer tokens and bearer authentication.", ]) expect(redactDiagnosticText(text)).toBe(text); }); it("redacts a JSON secret field value", () => { const input = '{"token":"opaque-value","status":"error"}'; const output = redactDiagnosticText(input); expect(output).not.toContain("opaque-value"); expect(output).toContain(`"token":"${REDACTED_COMMAND_TEXT_VALUE}"`); // The non-secret field keeps its value. expect(output).toContain('"status":"error"'); }); it("redacts an api_key JSON field with whitespace around the colon", () => { const input = '{ "api_key" : "sk-secret-123" }'; const output = redactDiagnosticText(input); expect(output).not.toContain("sk-secret-123"); expect(output).toContain(REDACTED_COMMAND_TEXT_VALUE); }); it("redacts an escaped-JSON secret field value", () => { // A diagnostic can carry a JSON string, so the double quotes appear as `\"`. const input = '{\\"token\\":\\"opaque-value\\"}'; const output = redactDiagnosticText(input); expect(output).not.toContain("opaque-value"); expect(output).toContain( `\\"token\\":\\"${REDACTED_COMMAND_TEXT_VALUE}\\"`, ); }); it("still redacts a shell KEY=value secret", () => { const input = "ANTHROPIC_API_KEY=super-secret-value claude --print"; const output = redactDiagnosticText(input); expect(output).not.toContain("super-secret-value"); expect(output).toContain(REDACTED_COMMAND_TEXT_VALUE); }); it("redacts an escaped quoted assignment across a literal newline", () => { const input = String.raw`authorization=\"Bearer first-line second-line\" status=401`; const expected = String.raw`authorization=\"***REDACTED***\" status=401`; const output = redactDiagnosticText(input); expect(output).toBe(expected); expect(redactDiagnosticText(output)).toBe(expected); }); it("keeps non-secret text and non-secret JSON fields intact", () => { const input = '{"status":"ok","message":"probe finished"}'; expect(redactDiagnosticText(input)).toBe(input); }); it("redacts the secret but keeps a non-secret marker in the same string", () => { const input = 'DIAGMARKER1234 said {"authorization":"Bearer opaque"}'; const output = redactDiagnosticText(input); expect(output).toContain("DIAGMARKER1234"); expect(output).not.toContain("opaque"); }); it("redacts a JSON secret value that contains an escaped quote", () => { // The value holds an escaped quote, so a naive matcher stops at the `\"` and // leaves the rest of the credential. The marker sits after the escaped quote. const input = '{"token":"pre\\"MARKERQUOTE_A"}'; const output = redactDiagnosticText(input); expect(output).not.toContain("MARKERQUOTE_A"); expect(output).toContain(`"token":"${REDACTED_COMMAND_TEXT_VALUE}"`); }); it("redacts a JSON secret value that contains an escaped backslash", () => { const input = '{"secret":"pre\\\\MARKERBACKSLASH_A"}'; const output = redactDiagnosticText(input); expect(output).not.toContain("MARKERBACKSLASH_A"); expect(output).toContain(`"secret":"${REDACTED_COMMAND_TEXT_VALUE}"`); }); it("redacts an escaped-JSON secret value that contains an escaped quote", () => { // A diagnostic can carry a serialized JSON string, so the whole JSON is // escaped a second time. The inner value still holds an escaped quote. const innerJson = '{"token":"pre\\"MARKERQUOTE_B"}'; const input = JSON.stringify(innerJson); const output = redactDiagnosticText(input); expect(output).not.toContain("MARKERQUOTE_B"); expect(output).toContain(REDACTED_COMMAND_TEXT_VALUE); }); it("redacts an escaped-JSON secret value that contains an escaped backslash", () => { const innerJson = '{"password":"pre\\\\MARKERBACKSLASH_B"}'; const input = JSON.stringify(innerJson); const output = redactDiagnosticText(input); expect(output).not.toContain("MARKERBACKSLASH_B"); expect(output).toContain(REDACTED_COMMAND_TEXT_VALUE); }); }); describe("redactCommandText on long runs", () => { const jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; function redactWithinTimeBudget(input: string) { const startedAt = performance.now(); const output = redactCommandText(input); expect(performance.now() - startedAt).toBeLessThan(1_000); return output; } it("keeps redacting options, assignments, and tokens inside dash-separated runs", () => { const cases = [ ["x--token flag-secret", `x--token ${REDACTED_COMMAND_TEXT_VALUE}`], ["--x--token flag-secret", `--x--token ${REDACTED_COMMAND_TEXT_VALUE}`], [ "a-b--api-key=assigned-secret --password 'quoted-secret'", `a-b--api-key=${REDACTED_COMMAND_TEXT_VALUE} --password '${REDACTED_COMMAND_TEXT_VALUE}'`, ], ["prefix-MY_API_KEY=env-secret", `prefix-MY_API_KEY=${REDACTED_COMMAND_TEXT_VALUE}`], [`--${jwt}`, `--${REDACTED_COMMAND_TEXT_VALUE}`], [`session=${jwt}`, `session=${REDACTED_COMMAND_TEXT_VALUE}`], ["my-token is here", "my-token is here"], ]; for (const [input, expected] of cases) { expect(redactCommandText(input)).toBe(expected); } }); it("scans long letter/digit and dash-separated runs in linear time", () => { const base64Url = Buffer.from(Array.from({ length: 90_000 }, (_, index) => (index * 7919) % 256)).toString("base64url"); for (const run of ["a".repeat(120_000), "a-".repeat(60_000), "a--".repeat(40_000), base64Url]) { expect(redactWithinTimeBudget(`${run} --token run-secret`)).toBe(`${run} --token ${REDACTED_COMMAND_TEXT_VALUE}`); expect(redactWithinTimeBudget(`${run}--token run-secret`)).toBe(`${run}--token ${REDACTED_COMMAND_TEXT_VALUE}`); } }); }); describe("diagnostic environment secret selection", () => { it("does not infer secrets from ordinary configuration values", () => { expect(collectDiagnosticEnvironmentSecrets({ FEATURE_FLAG: "1", WORKER_COUNT: "2", MODE: "safe", REGION: "us-east-1", NODE_ENV: "production", HOME: "/home/operator", PORT: "3100", MAX_TOKENS: "4096", PUBLIC_KEY_PATH: "/workspace/key.pem", KEYBOARD_LAYOUT: "us", SORT_KEY: "created_at", CACHE_KEY: "cache-v1", ROUTING_KEY: "jobs", PARTITION_KEY: "tenant_id", IDEMPOTENCY_KEY: "operation-123", PUBLIC_KEY: "public-material", })).toEqual([]); }); it.each(["API_KEY", "KEY", "ACCESS_TOKEN", "AWS_SECRET_ACCESS_KEY", "AWS_ACCESS_KEY_ID", "CLIENT_SECRET", "PASSWORD", "AUTHORIZATION", "PRIVATE_KEY", "API_KEY_PRODUCTION", "AWS_BEARER_TOKEN_BEDROCK"])( "keeps short credentials private under %s", key => { expect(collectDiagnosticEnvironmentSecrets({ [key]: "1" })).toEqual(["1"]); }, ); it("recognizes credential content and declared bindings under arbitrary names", () => { const jwt = `${Buffer.from('{"alg":"HS256"}').toString("base64url")}.e30.c2ln`; const pem = "-----BEGIN PRIVATE KEY-----\nprivate-canary\n-----END PRIVATE KEY-----"; const key = "sk-fixturecredential12345"; const selected = collectDiagnosticEnvironmentSecrets({ A: jwt, B: pem, C: key, D: "safe", FEATURE_FLAG: "false" }, ["D"]); expect(new Set(selected)).toEqual(new Set([jwt, pem, key, "safe"])); }); // Host env of a desktop agent session: short ordinary values under names // the allowlist does not know. Treating "vi" as a credential redacted it // inside "service" in Sentry reports. it("does not treat short or bare-number host process values under unknown names as credentials", () => { expect(collectDiagnosticEnvironmentSecrets({ CHILD_EDITOR: "vi", RUN_PHASE: "RUN", FEATURE_STATE: "off", SESSION_PID: "67591", XPC_FLAGS: "0x0", ASK_TOOL: "true", BATCH_SIZE: "8", LONG_NUMBER: "123456789012", CLAUDE_CODE_SDK_HAS_HOST_AUTH_REFRESH: "1" }, [], { hostProcess: true })).toEqual([]); // Credential names keep short values private, and long opaque values stay private. expect(collectDiagnosticEnvironmentSecrets({ AWS_ACCESS_KEY_ID: "1", CUSTOM_BINDING: "opaque-host-secret" }, [], { hostProcess: true }).sort()).toEqual(["1", "opaque-host-secret"]); // An agent's configured environment keeps the fail-closed rule. expect(collectDiagnosticEnvironmentSecrets({ CUSTOM_BINDING: "1" })).toEqual(["1"]); }); it("keeps short host values private as whole tokens", () => { const env = { CHILD_EDITOR: "vi", OPAQUE_SHORT: "k9Q", RUN_PHASE: "RUN", FEATURE_STATE: "off", LEVEL: "2", SESSION_PID: "67591", LONG_SECRET: "opaque-host-secret", AWS_ACCESS_KEY_ID: "1" }; // Substring-private values are not repeated; only flag values (booleans, 0, 1) are exempt. expect(collectHostEnvironmentTokenSecrets(env)).toEqual(["67591", "k9Q", "RUN", "vi", "2"]); expect(collectHostEnvironmentTokenSecrets({ SHORT_PIN: "482", COUNT_FLAG: "1", OFF_FLAG: "0", MODE_FLAG: "yes" })).toEqual(["482"]); expect(collectHostEnvironmentTokenSecrets(null)).toEqual([]); }); it("keeps unknown opaque values private without declarations", () => { expect(collectDiagnosticEnvironmentSecrets({ CUSTOM_BINDING: "opaque-host-secret" })).toEqual(["opaque-host-secret"]); }); it("does not exempt credentials on ordinary-setting names", () => { expect(collectDiagnosticEnvironmentSecrets({ SORT_KEY: "opaque-secret", FEATURE_FLAG: "1" }, ["SORT_KEY", "FEATURE_FLAG"])) .toEqual(["opaque-secret", "1"]); }); it("extracts encoded URL passwords and deduplicates selected values", () => { const url = "postgres://user:private%20password@db.test/database"; expect(new Set(collectDiagnosticEnvironmentSecrets({ DATABASE_URL: url, PASSWORD: "private password" }))) .toEqual(new Set([url, "private%20password", "private password"])); }); });