mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 22:56:58 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - When an agent run fails, Paperclip sends a redacted failure report to Sentry so the team can fix the cause > - Redaction treats the server's own environment values as possible secrets and removes them wherever they appear in the report > - Since #19461, every value under an environment name the allowlist does not know counts as a secret, even very short ones such as `vi`, `RUN`, `off` or `1` > - On a host with such settings, the report text is damaged: `service` becomes `ser***REDACTED***ce`, and a `1` anywhere can vanish. One server test failed on every such host, while CI (which has no such values) stayed green > - This pull request keeps the fail-closed rule for an agent's configured environment, and requires a plausible credential in the server's own environment > - The benefit is that Sentry reports stay readable on every host, and real credentials stay private ## Linked Issues or Issue Description Refs #19461 (introduced the catch-all), #14585 (added the failing test). **What happened?** `heartbeat-process-recovery.test.ts` › "redacts opaque environment-bound credentials from Sentry diagnostics: result" failed locally on `master`: `expected 'ser***REDACTED***ce' to be 'service'`. Instrumentation showed that the server's own `process.env` contributed these redaction values: `["false","67591","true","0x0","RUN","off","vi","64","1","0","8"]`. They came from a desktop agent session's environment. **Expected behavior** Short host settings are not credentials. Declared, secret-named and credential-shaped values stay private. An agent's configured environment keeps its fail-closed rule. **Steps to reproduce** Run that test on a machine whose environment contains `SOME_UNKNOWN_NAME=vi`. ## What Changed - `packages/adapter-utils/src/command-redaction.ts`: `collectDiagnosticEnvironmentSecrets` gets an option, `{ hostProcess: true }`. With it, an undeclared value under an unrecognized name is collected only in these cases: - it has at least 8 characters and is not a bare number, hex number or boolean-like word; or - its name has a credential segment: `TOKEN`, `SECRET`, `PASSWORD`, `BEARER` or `PRIVATE`, or a final `KEY` or `KEY_ID`. Without the option, behavior is unchanged. - `server/src/services/run-failure-report.ts`: the report collects the server's own `process.env` with `hostProcess: true`. The agent's configured environment (`heartbeat.ts`) and the provider environment (`terminal-session-failure.ts`) keep the default. ## Verification - `command-redaction.test.ts` (42 pass). The new test fails on `master` and passes here. It checks three things: the host's short values are not collected; a short value under `AWS_ACCESS_KEY_ID` and a long opaque value stay private; and an agent binding `CUSTOM_BINDING: "1"` stays private. - `run-failure-diagnostics.test.ts` (135 pass, unchanged). These tests pin the agent-environment fail-closed rule. - `heartbeat-process-recovery.test.ts` › "redacts opaque environment-bound credentials…": both modes pass here. The `result` mode failed on unchanged `master` on this host. - The adapter-utils and server typechecks pass. ## Risks - The server's own environment now leaves out a non-credential-named value that is shorter than 8 characters or is a bare number or boolean, for example `MY_PIN=1234`. Values under credential names, declared secrets and credential-shaped values are still redacted. - No Telemetry path changes; this is the Sentry run-failure report. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
255 lines
12 KiB
TypeScript
255 lines
12 KiB
TypeScript
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"]));
|
|
});
|
|
});
|