| 1 | import * as Sentry from "@sentry/node"; |
| 2 | import { type Event } from "@sentry/core"; |
| 3 | /** |
| 4 | * Minimalist Sentry anonymization utilities |
| 5 | */ |
| 6 | |
| 7 | // Browser-compatible hash function (avoids Node.js crypto dependency) |
| 8 | function simpleHash(input: string): string { |
| 9 | let hash = 0; |
| 10 | for (let i = 0; i < input.length; i++) { |
| 11 | const char = input.charCodeAt(i); |
| 12 | hash = (hash << 5) - hash + char; |
| 13 | hash = hash & hash; // Convert to 32-bit integer |
| 14 | } |
| 15 | return Math.abs(hash).toString(16).substring(0, 8); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Anonymize file paths - keep package names, remove user paths |
| 20 | */ |
| 21 | export function anonymizeFilePath(filePath: string): string { |
| 22 | if (!filePath) return filePath; |
| 23 | |
| 24 | const normalized = filePath.replace(/\\/g, "/"); |
| 25 | |
| 26 | // Keep node_modules package names for debugging |
| 27 | if (normalized.includes("node_modules")) { |
| 28 | const match = normalized.match(/node_modules\/([^\/]+)/); |
| 29 | if (match) { |
| 30 | return `node_modules/${match[1]}/<file>`; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Replace absolute paths with generic identifier |
| 35 | if (normalized.startsWith("/") || normalized.match(/^[A-Za-z]:/)) { |
| 36 | return "<file>"; |
| 37 | } |
| 38 | |
| 39 | return normalized; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Clean stack trace frames - remove sensitive data but keep the event |
| 44 | */ |
| 45 | export function anonymizeStackTrace(frames: any[]): any[] { |
| 46 | if (!Array.isArray(frames)) return frames; |
| 47 | |
| 48 | return frames.map((frame) => ({ |
| 49 | ...frame, |
| 50 | filename: frame.filename |
| 51 | ? anonymizeFilePath(frame.filename) |
| 52 | : frame.filename, |
| 53 | abs_path: "", |
| 54 | // Remove local variables and source code context |
| 55 | vars: undefined, |
| 56 | pre_context: undefined, |
| 57 | post_context: undefined, |
| 58 | context_line: frame.context_line ? "<code>" : frame.context_line, |
| 59 | })); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Anonymize user information - hash ID, remove PII |
| 64 | */ |
| 65 | export function anonymizeUserInfo(user: any): any { |
| 66 | if (!user) return user; |
| 67 | |
| 68 | return { |
| 69 | id: user.id ? simpleHash(String(user.id)) : user.id, |
| 70 | username: undefined, |
| 71 | email: undefined, |
| 72 | ip_address: undefined, |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Main anonymization function - minimalist approach like Rasa |
| 78 | */ |
| 79 | export function anonymizeSentryEvent(event: any): any | null { |
| 80 | try { |
| 81 | // Deep copy to avoid mutating the original event |
| 82 | const anonymized = structuredClone(event); |
| 83 | |
| 84 | // Clean exception stack traces |
| 85 | if (anonymized.exception?.values) { |
| 86 | anonymized.exception.values = anonymized.exception.values.map( |
| 87 | (exception: any) => ({ |
| 88 | ...exception, |
| 89 | stacktrace: exception.stacktrace |
| 90 | ? { |
| 91 | ...exception.stacktrace, |
| 92 | frames: exception.stacktrace.frames |
| 93 | ? anonymizeStackTrace(exception.stacktrace.frames) |
| 94 | : exception.stacktrace.frames, |
| 95 | } |
| 96 | : exception.stacktrace, |
| 97 | }), |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | // Clean thread stack traces |
| 102 | if (anonymized.threads?.values) { |
| 103 | anonymized.threads.values = anonymized.threads.values.map( |
| 104 | (thread: any) => ({ |
| 105 | ...thread, |
| 106 | stacktrace: thread.stacktrace |
| 107 | ? { |
| 108 | ...thread.stacktrace, |
| 109 | frames: thread.stacktrace.frames |
| 110 | ? anonymizeStackTrace(thread.stacktrace.frames) |
| 111 | : thread.stacktrace.frames, |
| 112 | } |
| 113 | : thread.stacktrace, |
| 114 | }), |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | // Anonymize user info |
| 119 | if (anonymized.user) { |
| 120 | anonymized.user = anonymizeUserInfo(anonymized.user); |
| 121 | } |
| 122 | |
| 123 | // Remove OS environment variables |
| 124 | if (anonymized.contexts?.os?.environment) { |
| 125 | anonymized.contexts.os.environment = undefined; |
| 126 | } |
| 127 | |
| 128 | return anonymized; |
| 129 | } catch (error) { |
| 130 | console.error("Error anonymizing Sentry event:", error); |
| 131 | return null; // Drop event if anonymization fails |
| 132 | } |
| 133 | } |