/** * File path username sanitizer. * * Replaces usernames in file paths with a placeholder before forwarding upstream, * stores the mapping, and can reverse-replace in responses. * * Supported patterns: * - /home//... (Linux) * - /Users//... (macOS) * - C:\Users\\... (Windows) */ // Global mapping storage: requestId → Map const pathMappings = new Map>(); /** * Clear all path mappings (for testing). */ export function clearPathMappings(): void { pathMappings.clear(); } /** * Replace usernames in file paths with placeholder. * Works on any JSON-serializable body. Mutates the body in place. */ export function sanitizePaths( body: unknown, placeholder: string, requestId: string ): void { const mapping = new Map(); const jsonStr = JSON.stringify(body); const replaced = replaceUsernames(jsonStr, mapping, placeholder); if (mapping.size === 0) return; pathMappings.set(requestId, mapping); const parsed = JSON.parse(replaced); mutateInPlace(body, parsed); } function replaceUsernames( jsonStr: string, mapping: Map, placeholder: string ): string { let result = jsonStr; result = result.replace( /(\/home\/)([^/"]+)(\/)/g, replacerFn(mapping, placeholder) ); result = result.replace( /(\/Users\/)([^/"]+)(\/)/g, replacerFn(mapping, placeholder) ); // In JSON strings, backslashes are escaped as \\, so we match double backslashes result = result.replace( /([A-Za-z]:\\\\Users\\\\)([^\\\\"]+)(\\\\)/g, replacerFn(mapping, placeholder) ); return result; } function replacerFn( mapping: Map, placeholder: string ): (match: string, prefix: string, username: string, suffix: string) => string { return (_match: string, prefix: string, username: string, suffix: string) => { const placeholderPath = prefix + placeholder + suffix; const realPath = prefix + username + suffix; mapping.set(placeholderPath, realPath); return placeholderPath; }; } function mutateInPlace(original: unknown, replacement: unknown): void { if (Array.isArray(original) && Array.isArray(replacement)) { original.length = 0; original.push(...replacement); } else if ( original && typeof original === 'object' && replacement && typeof replacement === 'object' && !Array.isArray(original) && !Array.isArray(replacement) ) { const origObj = original as Record; for (const key of Object.keys(origObj)) { delete origObj[key]; } for (const [key, value] of Object.entries(replacement)) { origObj[key] = value; } } } /** * Reverse-replace placeholders with real usernames in response body. * Clears the mapping after use. Mutates body in place. */ export function restorePaths( body: unknown, requestId: string ): void { const mapping = pathMappings.get(requestId); if (!mapping) return; const jsonStr = JSON.stringify(body); let result = jsonStr; for (const [placeholderPath, realPath] of mapping) { result = result.split(placeholderPath).join(realPath); } pathMappings.delete(requestId); const parsed = JSON.parse(result); mutateInPlace(body, parsed); } /** * Sanitize a single SSE chunk string by reverse-replacing placeholders. * Used in streaming responses. Does NOT clear the mapping (stream has multiple chunks). */ export function sanitizeSSEChunk( sseLine: string, requestId: string ): string { const mapping = pathMappings.get(requestId); if (!mapping) return sseLine; let result = sseLine; for (const [placeholderPath, realPath] of mapping) { result = result.split(placeholderPath).join(realPath); } return result; }