Elffuss BraidHub

kikocis/continue public

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