- Migration: er_ingest_keys.rate_limit_max and rate_limit_window_ms (nullable overrides). - Fingerprint policy v2: server-side digest includes normalized stacktrace (UUID/hex/line noise). - In-memory fixed-window limits: per API key after auth; per-IP for unauthenticated POSTs (INGEST_ANON_IP_*). Removed @fastify/rate-limit dependency. - OpenAPI documents 429; runbook at docs/operations/ingest-hardening-runbook.md. Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376 Paperclip-Issue: BIT-735 Paperclip-Run: 0a91be22-6f8f-4c60-b889-44b99048d3a2 Made-with: Cursor
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import {
|
|
computeFingerprintFromBasis,
|
|
extractLocationFromContext,
|
|
extractProjectStableKey,
|
|
normalizeMessageForFingerprint,
|
|
normalizeStacktraceForFingerprint,
|
|
} from "./fingerprintPolicy.js";
|
|
import { sha256Hex } from "./hash.js";
|
|
|
|
export function normalizeLevelToSeverity(level: string): string {
|
|
const _l = level.trim().toLowerCase();
|
|
if (_l === "fatal" || _l === "critical") {
|
|
return "critical";
|
|
}
|
|
if (_l === "error") {
|
|
return "high";
|
|
}
|
|
if (_l === "warn" || _l === "warning") {
|
|
return "medium";
|
|
}
|
|
if (_l === "info") {
|
|
return "info";
|
|
}
|
|
if (_l === "debug" || _l === "trace") {
|
|
return "low";
|
|
}
|
|
return "high";
|
|
}
|
|
|
|
export function computeFingerprint(input: {
|
|
clientFingerprint?: string;
|
|
message: string;
|
|
stacktrace?: string;
|
|
context?: Record<string, unknown> | null;
|
|
}): string {
|
|
if (typeof input.clientFingerprint === "string") {
|
|
const _fp = input.clientFingerprint.trim().slice(0, 128);
|
|
if (_fp.length > 0) {
|
|
return _fp;
|
|
}
|
|
}
|
|
const _ctx = input.context ?? undefined;
|
|
const _loc = extractLocationFromContext(_ctx);
|
|
const _proj = extractProjectStableKey(_ctx);
|
|
const _normMsg = normalizeMessageForFingerprint(input.message);
|
|
const _stackRaw =
|
|
typeof input.stacktrace === "string" && input.stacktrace.trim() !== ""
|
|
? normalizeStacktraceForFingerprint(input.stacktrace)
|
|
: "";
|
|
const _stackDigest = _stackRaw === "" ? "" : sha256Hex(_stackRaw);
|
|
return computeFingerprintFromBasis({
|
|
projectStableKey: _proj,
|
|
normalizedMessage: _normMsg,
|
|
filePath: _loc.filePath,
|
|
lineNumber: _loc.lineNumber,
|
|
stackDigest: _stackDigest,
|
|
});
|
|
}
|
|
|
|
export function incidentTitleFromMessage(message: string): string {
|
|
const _line = message.split(/\r?\n/)[0] ?? message;
|
|
const _trimmed = _line.trim();
|
|
if (_trimmed.length <= 300) {
|
|
return _trimmed;
|
|
}
|
|
return `${_trimmed.slice(0, 297)}...`;
|
|
}
|
|
|
|
export function parseOccurredAt(iso: string): number {
|
|
const _d = Date.parse(iso);
|
|
if (!Number.isFinite(_d)) {
|
|
throw new Error("invalid_occurred_at");
|
|
}
|
|
return _d;
|
|
}
|