From f76f47b1f9c59f1c12c42b07fd9aeab08fff5872 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 3 Nov 2025 19:02:19 -0600 Subject: [PATCH] Censor nsec in Sentry --- src/lib/sentry.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/sentry.ts b/src/lib/sentry.ts index 79091f73..fdfd7cec 100644 --- a/src/lib/sentry.ts +++ b/src/lib/sentry.ts @@ -51,6 +51,36 @@ export async function initializeSentry(dsn: string): Promise { environment: import.meta.env.MODE, // Release release: import.meta.env.VITE_APP_VERSION || 'development', + // Censor sensitive data before sending to Sentry + beforeSend(event) { + // Regex to match Nostr nsec private keys + const NSEC_REGEX = /nsec1[023456789acdefghjklmnpqrstuvwxyz]{58}/g; + + /** Recursively censors nsec values in any value (string, object, array, etc.) */ + function censorNsec(value: unknown): unknown { + if (typeof value === 'string') { + return value.replace(NSEC_REGEX, 'nsec1**********************************************************'); + } + if (Array.isArray(value)) { + return value.map(censorNsec); + } + if (value && typeof value === 'object') { + const result: Record = {}; + for (const [key, val] of Object.entries(value)) { + result[key] = censorNsec(val); + } + return result; + } + return value; + } + + /** Censors nsec values from Sentry events before sending */ + function censorNsecValues(event: T): T | null { + return censorNsec(event) as T; + } + + return censorNsecValues(event); + }, }); isInitialized = true;