Files
webmail/lib/error-reporting/settings.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

54 lines
2.0 KiB
TypeScript

import { configManager } from '@/lib/admin/config-manager';
import { parseDsn } from './envelope';
import type { ClientSettings } from './client';
/**
* The error-reporting settings as the server resolves them: admin config,
* then ERROR_REPORTING_DSN(_FILE) / ERROR_REPORTING_SAMPLE_RATE /
* ERROR_REPORTING_ENVIRONMENT, then off. Bulwark Lite reads the same three
* from policy.json (lib/error-reporting/lite.ts).
*/
export interface ErrorReportingSettings {
/** A valid DSN, or '' when none is set or the value doesn't parse. */
dsn: string;
sampleRate: number;
environment: string;
}
export function parseSampleRate(value: unknown): number {
const n = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
if (!Number.isFinite(n)) return 1;
return Math.min(1, Math.max(0, n));
}
export function parseEnvironment(value: unknown): string {
const text = typeof value === 'string' ? value.trim() : '';
return /^[\w.-]{1,64}$/.test(text) ? text : 'production';
}
export function errorReportingSettings(): ErrorReportingSettings {
const raw = configManager.get<string>('errorReportingDsn', '');
return {
dsn: parseDsn(raw) ? raw.trim() : '',
sampleRate: parseSampleRate(configManager.get<string>('errorReportingSampleRate', '1')),
environment: parseEnvironment(configManager.get<string>('errorReportingEnvironment', 'production')),
};
}
/**
* Whether browsers should post their errors to /api/errors: when there is a
* DSN to report to, a metrics endpoint that counts them, or consent to send
* error counts with the telemetry heartbeat.
*/
export async function shouldCollectErrors(): Promise<boolean> {
await configManager.ensureLoaded();
return !!errorReportingSettings().dsn;
}
/** What /api/config hands the browser. */
export async function browserErrorSettings(): Promise<ClientSettings> {
const settings = errorReportingSettings();
return { ...settings, collect: await shouldCollectErrors() };
}