diff --git a/packages/adapter-utils/src/command-redaction.test.ts b/packages/adapter-utils/src/command-redaction.test.ts index 321866bf7c7..2afd383a6d9 100644 --- a/packages/adapter-utils/src/command-redaction.test.ts +++ b/packages/adapter-utils/src/command-redaction.test.ts @@ -145,3 +145,42 @@ second-line\" status=401`; 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}`); + } + }); +}); diff --git a/packages/adapter-utils/src/command-redaction.ts b/packages/adapter-utils/src/command-redaction.ts index 46eeb6f5040..451f0ccf58e 100644 --- a/packages/adapter-utils/src/command-redaction.ts +++ b/packages/adapter-utils/src/command-redaction.ts @@ -11,16 +11,19 @@ export function isPublicExecutorToolSelector(value: string): boolean { return PUBLIC_EXECUTOR_TOOL_SELECTORS.has(value); } -const SECRET_NAME_PATTERN = String.raw`[A-Za-z0-9_-]*(?:api[-_]?key|(?:access[-_]?|auth[-_]?)?token|token|authorization(?:[-_]?code)?|bearer|secrets?|passwd|passwords?|credentials?|jwt|private[-_]?key|cookie|connectionstring)(?:[-_]?(?:value|header|prod(?:uction)?|dev(?:elopment)?|test|staging|primary|secondary))*`; +const SECRET_NAME_WORDS = String.raw`(?:api[-_]?key|(?:access[-_]?|auth[-_]?)?token|token|authorization(?:[-_]?code)?|bearer|secrets?|passwd|passwords?|credentials?|jwt|private[-_]?key|cookie|connectionstring)(?:[-_]?(?:value|header|prod(?:uction)?|dev(?:elopment)?|test|staging|primary|secondary))*`; +const SECRET_NAME_PATTERN = String.raw`[A-Za-z0-9_-]*${SECRET_NAME_WORDS}`; +const SECRET_NAME_START = String.raw`(? { expect(compacted).not.toContain("paperclip-flag-secret"); }); }); + +describe("compactRunLogChunk with long base64 commands", () => { + it("redacts a tool call that writes base64 of source text without stalling", () => { + const typescriptSource = "export const limit = 65536;\nexport function keep(value: string) {\n return value.slice(0, limit);\n}\n"; + const blob = Buffer.from(typescriptSource.repeat(1_000)).toString("base64").slice(0, 48 * 1024); + expect(blob).toMatch(/^[A-Za-z0-9]+$/); + const command = `printf '%s' '${blob}' | base64 -d > src/keep.ts && echo '{"GITHUB_TOKEN":"tool-call-secret"}'`; + const chunk = `${JSON.stringify({ type: "assistant", message: { content: [{ type: "tool_use", input: { command } }] } })}\n`; + + const startedAt = performance.now(); + const compacted = compactRunLogChunk(chunk); + + expect(performance.now() - startedAt).toBeLessThan(1_000); + expect(compacted).toContain(blob); + expect(compacted).not.toContain("tool-call-secret"); + expect(compacted).toContain("***REDACTED***"); + }); +}); diff --git a/server/src/__tests__/redaction.test.ts b/server/src/__tests__/redaction.test.ts index 4ab09bbd80e..5d060d67d1a 100644 --- a/server/src/__tests__/redaction.test.ts +++ b/server/src/__tests__/redaction.test.ts @@ -899,3 +899,73 @@ second-line\" status=401`, }); }); }); + +describe("redaction of long letter/digit runs", () => { + const typescriptSource = [ + "export function compactRunLogChunk(chunk: string, maxChars = 65536) {", + " const normalized = redactSensitiveText(chunk);", + " if (normalized.length <= maxChars) return normalized;", + " return normalized.slice(0, maxChars);", + "}", + "", + ].join("\n"); + + function base64OfTypeScriptSource(length: number) { + const repeatedSource = typescriptSource.repeat(Math.ceil(length / typescriptSource.length)); + return Buffer.from(repeatedSource).toString("base64").slice(0, length); + } + + function redactWithinTimeBudget(input: string) { + const startedAt = performance.now(); + const output = redactSensitiveText(input); + expect(performance.now() - startedAt).toBeLessThan(1_000); + return output; + } + + it("redacts secret field names that carry a prefix in plain and escaped JSON", () => { + const cases = [ + [`{"myApiKey": "plain-prefixed-secret"}`, `{"myApiKey": "${REDACTED_EVENT_VALUE}"}`], + [`{"x_auth_token":"snake-prefixed-secret","status":"ok"}`, `{"x_auth_token":"${REDACTED_EVENT_VALUE}","status":"ok"}`], + [`GITHUB_TOKEN: "upper-prefixed-secret"`, `GITHUB_TOKEN: "${REDACTED_EVENT_VALUE}"`], + [`{'db_password': 'single-quoted-secret'}`, `{'db_password': '${REDACTED_EVENT_VALUE}'}`], + [`text"apiKey":"glued-quote-secret"`, `text"apiKey":"${REDACTED_EVENT_VALUE}"`], + [`aGVsbG8apiKey:"glued-run-secret"`, `aGVsbG8apiKey:"${REDACTED_EVENT_VALUE}"`], + [String.raw`{\"myApiKey\":\"escaped-prefixed-secret\"}`, String.raw`{\"myApiKey\":\"${REDACTED_EVENT_VALUE}\"}`], + [ + String.raw`{\"x_auth_token\":\"escaped-snake-secret\",\"status\":\"ok\"}`, + String.raw`{\"x_auth_token\":\"${REDACTED_EVENT_VALUE}\",\"status\":\"ok\"}`, + ], + [String.raw`aGVsbG8\"GITHUB_TOKEN\":\"escaped-glued-secret\"`, String.raw`aGVsbG8\"GITHUB_TOKEN\":\"${REDACTED_EVENT_VALUE}\"`], + ]; + + for (const [input, expected] of cases) { + expect(redactSensitiveText(input)).toBe(expected); + expect(redactSensitiveText(expected)).toBe(expected); + } + }); + + it("keeps base64 of source text intact and fast while redacting a secret glued to its end", () => { + const blob = base64OfTypeScriptSource(48 * 1024); + expect(blob).toMatch(/^[A-Za-z0-9]+$/); + + const shellCommand = `printf '%s' '${blob}' | base64 -d > src/compact.ts`; + expect(redactWithinTimeBudget(shellCommand)).toBe(shellCommand); + + const toolCallLine = JSON.stringify({ type: "tool_use", input: { command: shellCommand } }); + expect(redactWithinTimeBudget(toolCallLine)).toBe(toolCallLine); + const serializedToolCallLine = JSON.stringify(toolCallLine); + expect(redactWithinTimeBudget(serializedToolCallLine)).toBe(serializedToolCallLine); + + const gluedSecret = redactWithinTimeBudget(`${blob}apiKey:"run-glued-secret"`); + expect(gluedSecret).toBe(`${blob}apiKey:"${REDACTED_EVENT_VALUE}"`); + }); + + it("scans long dash-separated runs in linear time", () => { + for (const run of ["a-".repeat(60_000), "a--".repeat(40_000)]) { + expect(redactWithinTimeBudget(`${run} Authorization: Bearer dash-run-secret`)) + .toBe(`${run} Authorization: ${REDACTED_EVENT_VALUE}`); + expect(redactWithinTimeBudget(`${run}Authorization: Bearer glued-run-secret`)) + .toBe(`${run}Authorization: ${REDACTED_EVENT_VALUE}`); + } + }); +}); diff --git a/server/src/redaction.ts b/server/src/redaction.ts index 28693d13fa0..dd3edf95680 100644 --- a/server/src/redaction.ts +++ b/server/src/redaction.ts @@ -2,6 +2,7 @@ import { redactCommandText } from "@paperclipai/adapter-utils"; import { isPublicExecutorToolSelector, looksLikeCredentialJwt } from "@paperclipai/adapter-utils/command-redaction"; const SECRET_FIELD_NAME_PATTERN = String.raw`[A-Za-z0-9_-]*(?:api[-_]?key|access[-_]?token|auth(?:_?token)?|token|authorization(?:[-_]?code)?|bearer|secrets?|passwd|passwords?|credentials?|jwt|private[-_]?key|cookie|connectionstring|browser[-_]?code|login[-_]?url)(?:[-_]?(?:value|header|prod(?:uction)?|dev(?:elopment)?|test|staging|primary|secondary))*`; +const SECRET_FIELD_NAME_START = String.raw`(?