Files
webmail/lib/error-reporting/stack.ts
T
Linus Rath cba1109444 feat: report browser and server errors to an error tracker the admin sets
Admins had logs and nothing else: client errors only reached the
browser console. An admin can now set a Sentry-protocol address (DSN)
for Bugsink, GlitchTip or Sentry under System > Monitoring, or with
`ERROR_REPORTING_DSN`, `ERROR_REPORTING_DSN_FILE`,
`ERROR_REPORTING_SAMPLE_RATE` and `ERROR_REPORTING_ENVIRONMENT`.
Bulwark Lite reads the same from `errorReporting` in policy.json.
Everything stays off while no address is set.

- browsers report errors from the error boundaries, window.onerror,
  unhandled rejections and failed JMAP requests (method and error type
  only), with route and JMAP method breadcrumbs and the chunks' debug IDs
- the server reports what Next hands to onRequestError and what routes
  log with logger.error
- every report is scrubbed first: quoted text, addresses, URLs, file
  names, tokens and the rest after the first line are removed, and the
  server scrubs what browsers post to /api/errors again
- reports are deduplicated and rate-limited per session and process
- errors are counted per day for "errors today" on the admin page
- startup messages and audit entries go through the logger, so
  `LOG_FORMAT=json` covers them
2026-11-01 02:50:19 +01:00

58 lines
2.1 KiB
TypeScript

import { scrubFramePath, scrubFunctionName } from './scrub';
/** One stack frame, already scrubbed. */
export interface Frame {
/** The file as scrubFramePath leaves it: `/_next/static/chunks/x.js`. */
path: string;
line?: number;
column?: number;
fn?: string;
}
export const MAX_FRAMES = 30;
// V8 (Chrome, Edge, Node): " at fn (file:1:2)", " at file:1:2",
// " at async fn (file:1:2)", " at new Foo (file:1:2)".
const V8 = /^\s*at (?:(.+?) \()?(.+?):(\d+):(\d+)\)?\s*$/;
// SpiderMonkey and JavaScriptCore (Firefox, Safari): "fn@file:1:2", "@file:1:2".
const GECKO = /^\s*([^@]*)@(.+?):(\d+):(\d+)\s*$/;
function number(value: string): number | undefined {
const n = Number(value);
return Number.isInteger(n) && n >= 0 && n < 10_000_000 ? n : undefined;
}
/** The frames of a stack, the frame that threw first, at most MAX_FRAMES. */
export function parseStack(stack: unknown): Frame[] {
if (typeof stack !== 'string') return [];
const frames: Frame[] = [];
for (const line of stack.split('\n')) {
const match = V8.exec(line) ?? GECKO.exec(line);
if (!match) continue;
const [, fn, file, lineNo, column] = match;
// "at fn (native)" and eval chains carry no file worth keeping.
frames.push({
path: scrubFramePath(file),
line: number(lineNo),
column: number(column),
fn: scrubFunctionName(fn?.replace(/^async /, '')),
});
if (frames.length >= MAX_FRAMES) break;
}
return frames;
}
/** Frames handed in from outside (a browser's report), checked and scrubbed again. */
export function sanitizeFrames(input: unknown): Frame[] {
if (!Array.isArray(input)) return [];
const out: Frame[] = [];
for (const raw of input.slice(0, MAX_FRAMES)) {
if (!raw || typeof raw !== 'object') continue;
const frame = raw as Record<string, unknown>;
const line = typeof frame.line === 'number' ? number(String(frame.line)) : undefined;
const column = typeof frame.column === 'number' ? number(String(frame.column)) : undefined;
out.push({ path: scrubFramePath(frame.path), line, column, fn: scrubFunctionName(frame.fn) });
}
return out;
}