mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 04:21:24 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - People can export a feedback bundle that holds run records for
review
> - The bundle is redacted before export, and one rule removes phone
numbers
> - The phone rule also matched two all-digit groups inside a run UUID
> - A run id such as `9224d1c8-1dbf-4314-9924-e16fcf3ad1fa` became
`9224d1c8-1dbf-[REDACTED_PHONE]-e16fcf3ad1fa`, so the bundle no longer
named its run
> - This pull request keeps UUIDs and ISO dates out of the phone rule
> - The benefit is that exported ids and dates stay correct and real
phone numbers stay redacted
## Linked Issues or Issue Description
**What happened?**
In the paid chat eval `cli-operations.feedback-bundle`, the exported
bundle's `envelope.targetId` was
`9224d1c8-1dbf-[REDACTED_PHONE]-e16fcf3ad1fa`. The phone rule
`(?<!\w)(?:\+?\d[\d ()-]{7,}\d)(?!\w)` matches two all-digit UUID groups
joined by `-`. About 2 to 3 percent of random UUIDs have that shape. The
same rule also matched a bare date such as `2026-11-12`.
**Steps to reproduce**
1. Call `sanitizeFeedbackText` with text that contains
`9224d1c8-1dbf-4314-9924-e16fcf3ad1fa`.
2. Read the output.
**Expected behavior**
The UUID stays unchanged. A real phone number such as `+1 (415)
555-1234` is still redacted.
## What Changed
- `server/src/services/feedback-redaction.ts`: the phone rule now runs
only on the text between public identifiers. UUIDs and ISO-8601 dates
and timestamps are copied through unchanged. Other rules are unchanged.
- `server/src/__tests__/feedback-redaction.test.ts`: a regression test
with the `-4314-9924-` UUID, an all-digit UUID, a date and a timestamp,
next to a real phone number that must stay redacted.
## Verification
- `cd server && npx vitest run src/__tests__/feedback-redaction.test.ts`
passes (13 tests). The new test fails on master: the UUID and the date
are replaced with `[REDACTED_PHONE]`.
## Risks
- Low risk. A phone number written in the exact shape of a UUID or an
ISO date is no longer redacted. Those shapes are not phone numbers.
## 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
- [x] All Paperclip CI gates are green
- [x] 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>
222 lines
7.2 KiB
TypeScript
222 lines
7.2 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { looksLikeCredentialJwt } from "@paperclipai/adapter-utils/command-redaction";
|
|
import { redactCurrentUserText } from "../log-redaction.js";
|
|
import { sanitizeRecord } from "../redaction.js";
|
|
|
|
export type FeedbackRedactionState = {
|
|
redactedFields: Set<string>;
|
|
truncatedFields: Set<string>;
|
|
omittedFields: Set<string>;
|
|
notes: Set<string>;
|
|
counts: Map<string, number>;
|
|
};
|
|
|
|
type PatternReplacement = string | ((match: string, ...args: string[]) => string);
|
|
|
|
type RedactionPattern = {
|
|
kind: string;
|
|
regex: RegExp;
|
|
replacement: PatternReplacement;
|
|
preservesIdentifiers?: boolean;
|
|
};
|
|
|
|
const PUBLIC_IDENTIFIER_RE =
|
|
/\b(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?)\b/gi;
|
|
|
|
const SECRET_ASSIGNMENT_RE =
|
|
/\b(api[-_]?key|access[-_]?token|auth(?:_?token)?|authorization|bearer|secret|passwd|password|credential|jwt|private[-_]?key|cookie|connectionstring)\s*[:=]\s*([^\s,;]+)/gi;
|
|
|
|
const FREE_TEXT_PATTERNS: RedactionPattern[] = [
|
|
{
|
|
kind: "pem_block",
|
|
regex: /-----BEGIN [^-]+-----[\s\S]+?-----END [^-]+-----/g,
|
|
replacement: "[REDACTED_PEM_BLOCK]",
|
|
},
|
|
{
|
|
kind: "secret_assignment",
|
|
regex: SECRET_ASSIGNMENT_RE,
|
|
replacement: (_match, key: string) => `${key}=[REDACTED]`,
|
|
},
|
|
{
|
|
kind: "bearer_token",
|
|
regex: /\bBearer\s+(?!(?:tokens?|authentication|authorization|credentials?|schemes?|flows?)[.,;:!?]?(?:\s|[)\]}]|$))[A-Za-z0-9._~+/-]+=*/gi,
|
|
replacement: "Bearer [REDACTED_TOKEN]",
|
|
},
|
|
{
|
|
kind: "github_token",
|
|
regex: /\bgh[pousr]_[A-Za-z0-9_]{20,}\b/g,
|
|
replacement: "[REDACTED_GITHUB_TOKEN]",
|
|
},
|
|
{
|
|
kind: "provider_api_key",
|
|
regex: /\bsk-(?:ant-)?[A-Za-z0-9_-]{12,}\b/g,
|
|
replacement: "[REDACTED_API_KEY]",
|
|
},
|
|
{
|
|
kind: "jwt",
|
|
regex: /\b[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)\b/g,
|
|
replacement: (match) => looksLikeCredentialJwt(match) ? "[REDACTED_JWT]" : match,
|
|
},
|
|
{
|
|
kind: "dsn",
|
|
regex: /\b(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|amqp|kafka|nats|mssql):\/\/[^\s<>'")]+/gi,
|
|
replacement: "[REDACTED_CONNECTION_STRING]",
|
|
},
|
|
{
|
|
kind: "email",
|
|
regex: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi,
|
|
replacement: "[REDACTED_EMAIL]",
|
|
},
|
|
{
|
|
kind: "phone",
|
|
regex: /(?<!\w)(?:\+?\d[\d ()-]{7,}\d)(?!\w)/g,
|
|
replacement: "[REDACTED_PHONE]",
|
|
preservesIdentifiers: true,
|
|
},
|
|
];
|
|
|
|
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function increment(state: FeedbackRedactionState, kind: string, count: number) {
|
|
if (count <= 0) return;
|
|
state.counts.set(kind, (state.counts.get(kind) ?? 0) + count);
|
|
}
|
|
|
|
function recordField(state: FeedbackRedactionState, fieldPath: string) {
|
|
if (fieldPath.trim().length === 0) return;
|
|
state.redactedFields.add(fieldPath);
|
|
}
|
|
|
|
function applyPattern(input: string, pattern: RedactionPattern) {
|
|
if (pattern.preservesIdentifiers) return applyPatternOutsideIdentifiers(input, pattern);
|
|
return applyPatternToSegment(input, pattern);
|
|
}
|
|
|
|
function applyPatternOutsideIdentifiers(input: string, pattern: RedactionPattern) {
|
|
let matches = 0;
|
|
let output = "";
|
|
let cursor = 0;
|
|
for (const identifier of input.matchAll(PUBLIC_IDENTIFIER_RE)) {
|
|
const start = identifier.index ?? 0;
|
|
const segment = applyPatternToSegment(input.slice(cursor, start), pattern);
|
|
output += segment.output + identifier[0];
|
|
matches += segment.matches;
|
|
cursor = start + identifier[0].length;
|
|
}
|
|
const tail = applyPatternToSegment(input.slice(cursor), pattern);
|
|
return { output: output + tail.output, matches: matches + tail.matches };
|
|
}
|
|
|
|
function applyPatternToSegment(input: string, pattern: RedactionPattern) {
|
|
let matches = 0;
|
|
const output = input.replace(pattern.regex, (match: string, ...args: string[]) => {
|
|
const replacement = typeof pattern.replacement === "function"
|
|
? pattern.replacement(match, ...args)
|
|
: pattern.replacement;
|
|
if (replacement !== match) matches += 1;
|
|
return replacement;
|
|
});
|
|
pattern.regex.lastIndex = 0;
|
|
return { output, matches };
|
|
}
|
|
|
|
export function createFeedbackRedactionState(): FeedbackRedactionState {
|
|
return {
|
|
redactedFields: new Set<string>(),
|
|
truncatedFields: new Set<string>(),
|
|
omittedFields: new Set<string>(),
|
|
notes: new Set<string>(),
|
|
counts: new Map<string, number>(),
|
|
};
|
|
}
|
|
|
|
export function sanitizeFeedbackText(
|
|
input: string,
|
|
state: FeedbackRedactionState,
|
|
fieldPath: string,
|
|
maxLength: number,
|
|
) {
|
|
let output = redactCurrentUserText(input);
|
|
if (output !== input) {
|
|
recordField(state, fieldPath);
|
|
increment(state, "current_user", 1);
|
|
}
|
|
|
|
for (const pattern of FREE_TEXT_PATTERNS) {
|
|
const result = applyPattern(output, pattern);
|
|
if (result.matches > 0) {
|
|
output = result.output;
|
|
recordField(state, fieldPath);
|
|
increment(state, pattern.kind, result.matches);
|
|
}
|
|
}
|
|
|
|
if (output.length > maxLength) {
|
|
output = `${output.slice(0, Math.max(0, maxLength - 1))}...`;
|
|
state.truncatedFields.add(fieldPath);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
export function sanitizeFeedbackValue(
|
|
value: unknown,
|
|
state: FeedbackRedactionState,
|
|
fieldPath: string,
|
|
maxStringLength: number,
|
|
): unknown {
|
|
if (typeof value === "string") {
|
|
return sanitizeFeedbackText(value, state, fieldPath, maxStringLength);
|
|
}
|
|
if (Array.isArray(value)) {
|
|
return value.map((entry, index) =>
|
|
sanitizeFeedbackValue(entry, state, `${fieldPath}[${index}]`, maxStringLength));
|
|
}
|
|
if (!isPlainRecord(value)) {
|
|
return value;
|
|
}
|
|
|
|
const structurallySanitized = sanitizeRecord(value);
|
|
if (stableStringify(structurallySanitized) !== stableStringify(value)) {
|
|
recordField(state, fieldPath);
|
|
increment(state, "structured_secret", 1);
|
|
}
|
|
|
|
const output: Record<string, unknown> = {};
|
|
for (const [key, entry] of Object.entries(structurallySanitized)) {
|
|
output[key] = sanitizeFeedbackValue(entry, state, `${fieldPath}.${key}`, maxStringLength);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
export function finalizeFeedbackRedactionSummary(state: FeedbackRedactionState) {
|
|
return {
|
|
strategy: "deterministic_feedback_v2",
|
|
redactedFields: Array.from(state.redactedFields).sort(),
|
|
truncatedFields: Array.from(state.truncatedFields).sort(),
|
|
omittedFields: Array.from(state.omittedFields).sort(),
|
|
notes: Array.from(state.notes).sort(),
|
|
counts: Object.fromEntries(Array.from(state.counts.entries()).sort(([left], [right]) => left.localeCompare(right))),
|
|
} satisfies Record<string, unknown>;
|
|
}
|
|
|
|
export function stableStringify(value: unknown): string {
|
|
if (value === null || typeof value !== "object") {
|
|
return JSON.stringify(value);
|
|
}
|
|
if (Array.isArray(value)) {
|
|
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
}
|
|
|
|
const entries = Object.entries(value as Record<string, unknown>)
|
|
.sort(([left], [right]) => left.localeCompare(right))
|
|
.map(([key, entry]) => `${JSON.stringify(key)}:${stableStringify(entry)}`);
|
|
return `{${entries.join(",")}}`;
|
|
}
|
|
|
|
export function sha256Digest(value: unknown) {
|
|
return createHash("sha256").update(stableStringify(value)).digest("hex");
|
|
}
|