import type { CommandStatus, JsonPath, SensitiveDataPolicy, } from './types.js'; const REDACTED = '[REDACTED]'; const DEFAULT_SENSITIVE_FIELD_NAMES = [ 'api_key', 'apiKey', 'clawKey', 'authorization', 'cookie', 'set-cookie', 'token', 'access_token', 'refresh_token', 'session_token', 'secret', 'credential', 'password', 'invite_code', 'signature', ] as const; export interface FilterContext { status: CommandStatus; policy?: SensitiveDataPolicy; } export interface FilterResult { value: T; filteredFields: string[]; } function normalizedFieldName(value: string): string { return value.toLowerCase().replace(/[^a-z0-9]/g, ''); } function pathKey(path: JsonPath): string { return JSON.stringify(path); } export function formatJsonPath(path: JsonPath): string { if (path.length === 0) return '$'; return `$${path.map((part) => ( typeof part === 'number' ? `[${part}]` : /^[A-Za-z_$][\w$]*$/.test(part) ? `.${part}` : `[${JSON.stringify(part)}]` )).join('')}`; } function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function redactString(value: string, sensitiveValues: readonly string[]): string { let out = value; out = out.replace(/\bBearer\s+[^\s,;]+/gi, `Bearer ${REDACTED}`); out = out.replace( /((?:[?&]|\b)(?:token|api[_-]?key|clawkey|access[_-]?token|refresh[_-]?token|signature|sig)=)([^&#\s]*)/gi, `$1${REDACTED}`, ); out = out.replace(/\bclaw_[A-Za-z0-9._~+/=-]{4,}\b/g, REDACTED); for (const secret of sensitiveValues) { if (!secret) continue; out = out.replace(new RegExp(escapeRegExp(secret), 'g'), REDACTED); } return out; } export class SensitiveDataFilter { filter(input: T, context: FilterContext): FilterResult { const policy = context.policy ?? {}; const sensitiveNames = new Set( [...DEFAULT_SENSITIVE_FIELD_NAMES, ...(policy.sensitiveFieldNames ?? [])] .map(normalizedFieldName), ); const sensitivePaths = new Set((policy.sensitivePaths ?? []).map(pathKey)); const capabilityPaths = new Set((policy.successCapabilityPaths ?? []).map(pathKey)); const filtered = new Set(); const visit = (value: unknown, path: (string | number)[], fieldName?: string): unknown => { const exactPath = pathKey(path); const capabilityAllowed = context.status === 'success' && capabilityPaths.has(exactPath); if (capabilityAllowed) return value; const explicitlySensitive = sensitivePaths.has(exactPath); const nameSensitive = fieldName !== undefined && sensitiveNames.has(normalizedFieldName(fieldName)); if ((explicitlySensitive || nameSensitive) && value !== null && value !== undefined) { filtered.add(formatJsonPath(path)); return REDACTED; } if (typeof value === 'string') { const redacted = redactString(value, policy.sensitiveValues ?? []); if (redacted !== value) filtered.add(formatJsonPath(path)); return redacted; } if (Array.isArray(value)) { return value.map((item, index) => visit(item, [...path, index])); } if (value && typeof value === 'object') { const out: Record = {}; for (const [key, item] of Object.entries(value)) { out[key] = visit(item, [...path, key], key); } return out; } return value; }; return { value: visit(input, []) as T, filteredFields: [...filtered], }; } } export { REDACTED as REDACTED_VALUE };