mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 14:19:00 +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>
271 lines
12 KiB
TypeScript
271 lines
12 KiB
TypeScript
export const REDACTED_COMMAND_TEXT_VALUE = "***REDACTED***";
|
|
|
|
// Public Executor helper addresses retained for callers of this predicate.
|
|
// Dotted addresses alone do not meet the JWT credential heuristic.
|
|
const PUBLIC_EXECUTOR_TOOL_SELECTORS = new Set([
|
|
"executor.coreTools.integrations.list",
|
|
"executor.coreTools.connections.list",
|
|
"executor.coreTools.policies.list",
|
|
]);
|
|
export function isPublicExecutorToolSelector(value: string): boolean {
|
|
return PUBLIC_EXECUTOR_TOOL_SELECTORS.has(value);
|
|
}
|
|
|
|
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`(?<![A-Za-z0-9_-])`;
|
|
const SECRET_OPTION_NAME_PATTERN = String.raw`(?:[A-Za-z0-9_]|-(?!-))*${SECRET_NAME_WORDS}`;
|
|
|
|
const COMMAND_CLI_SECRET_OPTION_RE = new RegExp(
|
|
String.raw`(\B-{1,2}${SECRET_OPTION_NAME_PATTERN}(?:\s+|=)(["']?))[^\s"'` +
|
|
"`" +
|
|
String.raw`]+(\2)`,
|
|
"gi",
|
|
);
|
|
const COMMAND_ENV_SECRET_ASSIGNMENT_RE = new RegExp(
|
|
String.raw`(${SECRET_NAME_START}${SECRET_NAME_PATTERN}\s*=\s*)(?:(\\["'])([\s\S]*?)\2|(["'])([^"'` +
|
|
"`" +
|
|
String.raw`\r\n]*)\4|([^\s"'` +
|
|
"`" +
|
|
String.raw`]+))`,
|
|
"gi",
|
|
);
|
|
const COMMAND_AUTHORIZATION_BEARER_RE =
|
|
/(\bAuthorization\s*:\s*Bearer\s+)[^\s"'`]+/gi;
|
|
const COMMAND_OPENAI_KEY_RE = /\bsk-[A-Za-z0-9_-]{12,}\b/g;
|
|
const COMMAND_GITHUB_TOKEN_RE = /\bgh[pousr]_[A-Za-z0-9_]{20,}\b/g;
|
|
const COMMAND_JWT_RE =
|
|
/\b(?<![A-Za-z0-9_]-*)[A-Za-z0-9_-]{8,}(?:\.[A-Za-z0-9_-]{8,}){2}(?:\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})?\b/g;
|
|
/** Recognize encoded JSON headers, without treating dotted identifiers as tokens. */
|
|
export function looksLikeCredentialJwt(value: string): boolean {
|
|
const segments = value.split(".");
|
|
if (![3, 5].includes(segments.length)) return false;
|
|
if (segments.some((part, index) => {
|
|
// Compact JWE direct encryption has no encrypted-key value.
|
|
if (segments.length === 5 && index === 1 && part === "") return false;
|
|
return !/^[A-Za-z0-9_-]+$/.test(part);
|
|
})) return false;
|
|
if (segments[0].startsWith("eyJ")) return true;
|
|
try {
|
|
const header = JSON.parse(atob(segments[0].replace(/-/g, "+").replace(/_/g, "/")));
|
|
return typeof header === "object" && header !== null && typeof header.alg === "string";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const COMMAND_SECRET_HINTS = [
|
|
"api",
|
|
"key",
|
|
"token",
|
|
"auth",
|
|
"bearer",
|
|
"secret",
|
|
"pass",
|
|
"credential",
|
|
"jwt",
|
|
"private",
|
|
"cookie",
|
|
"connectionstring",
|
|
"sk-",
|
|
"ghp_",
|
|
"gho_",
|
|
"ghu_",
|
|
"ghs_",
|
|
"ghr_",
|
|
] as const;
|
|
|
|
function maybeContainsSecretText(command: string) {
|
|
const lower = command.toLowerCase();
|
|
return (
|
|
COMMAND_SECRET_HINTS.some((hint) => lower.includes(hint)) ||
|
|
command.includes(".")
|
|
);
|
|
}
|
|
|
|
export function redactCommandText(
|
|
command: string,
|
|
redactedValue = REDACTED_COMMAND_TEXT_VALUE,
|
|
): string {
|
|
if (!maybeContainsSecretText(command)) return command;
|
|
return command
|
|
.replace(COMMAND_AUTHORIZATION_BEARER_RE, `$1${redactedValue}`)
|
|
.replace(COMMAND_CLI_SECRET_OPTION_RE, `$1${redactedValue}$3`)
|
|
.replace(
|
|
COMMAND_ENV_SECRET_ASSIGNMENT_RE,
|
|
(
|
|
_match,
|
|
prefix: string,
|
|
escapedQuote: string | undefined,
|
|
_escapedValue: string | undefined,
|
|
rawQuote: string | undefined,
|
|
) => {
|
|
const quote = escapedQuote ?? rawQuote;
|
|
return quote
|
|
? `${prefix}${quote}${redactedValue}${quote}`
|
|
: `${prefix}${redactedValue}`;
|
|
},
|
|
)
|
|
.replace(COMMAND_OPENAI_KEY_RE, redactedValue)
|
|
.replace(COMMAND_GITHUB_TOKEN_RE, redactedValue)
|
|
.replace(COMMAND_JWT_RE, (match) => looksLikeCredentialJwt(match) ? redactedValue : match);
|
|
}
|
|
|
|
// A JSON secret field is a key/value pair such as `"token":"opaque-value"`. The
|
|
// command redaction handles shell `KEY=value` syntax only. A sandbox diagnostic
|
|
// can also carry a serialized JSON error, so the sanitizer must redact the JSON
|
|
// form too. The value body consumes JSON escape sequences. An escaped quote
|
|
// (`\"`) inside the value does not end the match early.
|
|
const JSON_SECRET_FIELD_RE = new RegExp(
|
|
String.raw`("(?:${SECRET_NAME_PATTERN})"\s*:\s*")(?:\\[\s\S]|[^"\\])*(")`,
|
|
"gi",
|
|
);
|
|
// An escaped JSON secret field is the same pair inside a JSON string. The double
|
|
// quote appears as `\"` and a backslash appears as `\\`. The value body
|
|
// consumes the doubled escape sequences. An escaped quote inside the value does
|
|
// not end the match early. The value ends at the next unescaped `\"`.
|
|
const JSON_ESCAPED_SECRET_FIELD_RE = new RegExp(
|
|
String.raw`(\\"(?:${SECRET_NAME_PATTERN})\\"\s*:\s*\\")(?:\\\\\\\\|\\\\\\"|\\\\[\s\S]|[^\\"])*(\\")`,
|
|
"gi",
|
|
);
|
|
|
|
/**
|
|
* Redact secrets from an untrusted diagnostic string.
|
|
*
|
|
* The function first runs the command redaction. The command redaction handles
|
|
* shell `KEY=value` assignments, CLI secret options, bearer headers, and common
|
|
* token shapes. The function then redacts JSON and escaped-JSON secret fields,
|
|
* because a sandbox diagnostic can carry a serialized JSON error such as
|
|
* `{"token":"opaque-value"}`. The caller must still bound the length after this
|
|
* step.
|
|
*/
|
|
export function redactDiagnosticText(
|
|
text: string,
|
|
redactedValue = REDACTED_COMMAND_TEXT_VALUE,
|
|
): string {
|
|
return redactCommandText(text, redactedValue)
|
|
.replace(JSON_ESCAPED_SECRET_FIELD_RE, `$1${redactedValue}$2`)
|
|
.replace(JSON_SECRET_FIELD_RE, `$1${redactedValue}$2`);
|
|
}
|
|
|
|
const PUBLIC_ENV_KEYS = new Set([
|
|
"PATH", "PATHEXT", "SYSTEMROOT", "WINDIR", "COMSPEC", "HOME", "USERPROFILE",
|
|
"HOMEDRIVE", "HOMEPATH", "USER", "USERNAME", "LOGNAME", "SHELL", "LANG",
|
|
"LANGUAGE", "LC_ALL", "LC_CTYPE", "TZ", "TMPDIR", "TEMP", "TMP", "NODE_ENV",
|
|
"XDG_CONFIG_HOME", "XDG_CACHE_HOME", "XDG_DATA_HOME",
|
|
"PWD", "OLDPWD", "INIT_CWD", "SHLVL", "_", "TERM", "TERM_PROGRAM", "TERM_PROGRAM_VERSION", "COLORTERM",
|
|
"NPM_LIFECYCLE_EVENT", "NPM_COMMAND", "NPM_EXEC_PATH", "NPM_EXECPATH", "NPM_NODE_EXECPATH",
|
|
"NPM_PACKAGE_NAME", "NPM_PACKAGE_VERSION", "NPM_PACKAGE_JSON", "NPM_CONFIG_USER_AGENT",
|
|
"XPC_SERVICE_NAME", "VITEST_POOL_ID", "VITEST_WORKER_ID",
|
|
"REGION", "AWS_REGION", "AWS_DEFAULT_REGION", "KEYBOARD_LAYOUT", "PUBLIC_KEY_PATH", "PUBLIC_KEY",
|
|
"SORT_KEY", "CACHE_KEY", "ROUTING_KEY", "PARTITION_KEY", "IDEMPOTENCY_KEY",
|
|
"PAPERCLIP_AGENT_ID", "PAPERCLIP_COMPANY_ID", "PAPERCLIP_RUN_ID", "PAPERCLIP_TASK_ID",
|
|
]);
|
|
const PUBLIC_BOOLEAN_ENV_KEYS = new Set([
|
|
"OPENCODE_ALLOW_ALL_MODELS", "CLAUDE_CODE_USE_BEDROCK", "GOOGLE_GENAI_USE_GCA",
|
|
"CI", "NO_COLOR", "FORCE_COLOR",
|
|
"VITEST", "MALLOCNANOZONE", "CODEX_CI", "CODEX_SHELL", "CODEX_SAGE_BACKFILL_TRACKER_TAB_REUSE",
|
|
"DEV", "PROD", "SSR", "PAPERCLIP_REQUIRE_SENTRY_TEST_SDK",
|
|
"PAPERCLIP_DB_BACKUP_ENABLED", "PAPERCLIP_WORKSPACE_COMPACTION_ENABLED", "PAPERCLIP_AUTH_DISABLE_SIGN_UP", "PAPERCLIP_ENABLE_COMPANY_DELETION",
|
|
"PAPERCLIP_STORAGE_S3_FORCE_PATH_STYLE", "PAPERCLIP_SECRETS_STRICT_MODE",
|
|
"SERVE_UI", "PAPERCLIP_UI_DEV_MIDDLEWARE", "HEARTBEAT_SCHEDULER_ENABLED", "PAPERCLIP_ANNOUNCEMENTS_ENABLED",
|
|
"GITHUB_ACTIONS", "GITHUB_REF_PROTECTED", "RUNNER_DEBUG", "ACTIONS_RUNNER_DEBUG", "ACTIONS_STEP_DEBUG",
|
|
]);
|
|
const PUBLIC_NUMBER_ENV_KEYS = new Set([
|
|
"PORT", "PAPERCLIP_DB_BACKUP_INTERVAL_MINUTES", "PAPERCLIP_DB_BACKUP_RETENTION_DAYS",
|
|
"PAPERCLIP_WORKSPACE_REAPER_COOLDOWN_DAYS", "PAPERCLIP_WORKSPACE_COMPACTION_DELAY_HOURS",
|
|
"HEARTBEAT_SCHEDULER_INTERVAL_MS",
|
|
"GITHUB_RUN_ATTEMPT", "GITHUB_RUN_NUMBER", "GITHUB_RUN_ID", "GITHUB_RETENTION_DAYS",
|
|
]);
|
|
|
|
// A setting name and a bounded value must agree before it is public. Unknown
|
|
// opaque values remain private; declared bindings always override these rules.
|
|
function isOrdinaryDiagnosticEnvSetting(key: string, value: string): boolean {
|
|
const name = key.toUpperCase();
|
|
if (PUBLIC_ENV_KEYS.has(name)) return true;
|
|
if ((PUBLIC_BOOLEAN_ENV_KEYS.has(name) || /(?:^|_)(?:FLAG|ENABLED|DISABLED|DEBUG)$/.test(name)) &&
|
|
/^(?:0|1|true|false)$/i.test(value)) return true;
|
|
if ((PUBLIC_NUMBER_ENV_KEYS.has(name) || /(?:^|_)(?:COUNT|WORKERS|PORT|MAX_TOKENS)$/.test(name)) &&
|
|
/^\d+$/.test(value)) return true;
|
|
return (name === "BASE_URL" && value === "/") ||
|
|
(/(?:^|_)MODE$/.test(name) && /^(?:test|development|production|safe)$/.test(value)) ||
|
|
(name === "PAPERCLIP_SENTRY_RUN_FAILURES" && /^(?:off|unexpected|all)$/.test(value));
|
|
}
|
|
|
|
/** In the host process environment, an undeclared value under an
|
|
* unrecognized name is redacted as a substring only if it could be a
|
|
* credential: at least 8 characters, and not a bare number, hex number or
|
|
* boolean-like word. Shorter values ("vi", "RUN") are redacted only as whole
|
|
* tokens (`collectHostEnvironmentTokenSecrets`): as substrings they corrupted
|
|
* unrelated report text ("service" became "ser***REDACTED***ce"). Declared,
|
|
* secret-named and credential-shaped values are always substring-private. An
|
|
* agent's configured environment keeps the fail-closed substring rule. */
|
|
function plausibleUndeclaredCredential(key: string, value: string): boolean {
|
|
if (credentialNameSegments(key)) return true;
|
|
return value.length >= 8 && !/^(?:\d+|0x[0-9a-f]+|true|false|on|off|yes|no)$/i.test(value);
|
|
}
|
|
|
|
const CREDENTIAL_SEGMENTS = new Set(["TOKEN", "TOKENS", "SECRET", "SECRETS", "PASSWORD", "PASSWD", "PASSPHRASE",
|
|
"CREDENTIAL", "CREDENTIALS", "PRIVATE", "BEARER", "AUTHORIZATION", "APIKEY"]);
|
|
|
|
/** A name with a credential segment (AWS_SECRET_ACCESS_KEY, AWS_BEARER_TOKEN_BEDROCK),
|
|
* or a KEY segment that is last or followed only by ID (KEY, AWS_ACCESS_KEY_ID).
|
|
* Such a value stays private whatever its length. */
|
|
function credentialNameSegments(key: string): boolean {
|
|
const segments = key.toUpperCase().split(/[^A-Z0-9]+/).filter(Boolean);
|
|
return segments.some((segment, index) => CREDENTIAL_SEGMENTS.has(segment) ||
|
|
(segment === "KEY" && (index === segments.length - 1 || (index === segments.length - 2 && segments[index + 1] === "ID"))));
|
|
}
|
|
|
|
const BOOLEAN_WORD = /^(?:true|false|on|off|yes|no)$/i;
|
|
|
|
/**
|
|
* Host process values under unknown names that are too short or too plain to
|
|
* redact as substrings (see `plausibleUndeclaredCredential`). They are still
|
|
* private: callers redact them where they appear as a whole token, numbers
|
|
* included. Only flag values are exempt (boolean words, and `0` or `1`): they
|
|
* cannot hold a credential, and as tokens they would erase ordinary words
|
|
* ("on", "no") and exit codes from every report.
|
|
*/
|
|
export function collectHostEnvironmentTokenSecrets(env: unknown): string[] {
|
|
if (!env || typeof env !== "object") return [];
|
|
const substringValues = new Set(collectDiagnosticEnvironmentSecrets(env, [], { hostProcess: true }));
|
|
const values = new Set<string>();
|
|
for (const [key, value] of Object.entries(env)) {
|
|
if (typeof value !== "string" || !value.trim() || substringValues.has(value)) continue;
|
|
if (isOrdinaryDiagnosticEnvSetting(key, value) || BOOLEAN_WORD.test(value) || /^[01]$/.test(value)) continue;
|
|
values.add(value);
|
|
}
|
|
return [...values].sort((a, b) => b.length - a.length);
|
|
}
|
|
|
|
/** Select private values while preserving explicit ordinary process settings. */
|
|
export function collectDiagnosticEnvironmentSecrets(
|
|
env: unknown,
|
|
secretKeys: Iterable<string> = [],
|
|
options: { hostProcess?: boolean } = {},
|
|
): string[] {
|
|
if (!env || typeof env !== "object") return [];
|
|
const declared = new Set(secretKeys);
|
|
const secretName = new RegExp(`^(?:${SECRET_NAME_PATTERN})$`, "i");
|
|
const values = new Set<string>();
|
|
for (const [key, value] of Object.entries(env)) {
|
|
if (typeof value !== "string" || !value) continue;
|
|
if (declared.has(key) || secretName.test(key) ||
|
|
redactDiagnosticText(value) !== value || looksLikeCredentialJwt(value) ||
|
|
/-----BEGIN (?:[A-Z]+ )*PRIVATE KEY-----/.test(value) ||
|
|
(!isOrdinaryDiagnosticEnvSetting(key, value) && (!options.hostProcess || plausibleUndeclaredCredential(key, value)))) {
|
|
values.add(value);
|
|
}
|
|
try {
|
|
const url = new URL(value);
|
|
if (url.password) {
|
|
values.add(value);
|
|
values.add(url.password);
|
|
values.add(decodeURIComponent(url.password));
|
|
}
|
|
} catch { /* ordinary settings and malformed URLs need no URL projection */ }
|
|
}
|
|
return [...values].sort((a, b) => b.length - a.length);
|
|
}
|