evlog 2.13.0 → 2.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -15
- package/dist/{_http-CHSsrWDJ.mjs → _http-BY1e9pwC.mjs} +9 -2
- package/dist/_http-BY1e9pwC.mjs.map +1 -0
- package/dist/adapters/axiom.d.mts +1 -1
- package/dist/adapters/axiom.mjs +1 -1
- package/dist/adapters/axiom.mjs.map +1 -1
- package/dist/adapters/better-stack.d.mts +1 -1
- package/dist/adapters/better-stack.mjs +1 -1
- package/dist/adapters/better-stack.mjs.map +1 -1
- package/dist/adapters/datadog.d.mts +1 -1
- package/dist/adapters/datadog.mjs +1 -1
- package/dist/adapters/datadog.mjs.map +1 -1
- package/dist/adapters/fs.d.mts +1 -1
- package/dist/adapters/fs.mjs.map +1 -1
- package/dist/adapters/hyperdx.d.mts +1 -1
- package/dist/adapters/hyperdx.mjs +1 -1
- package/dist/adapters/otlp.d.mts +1 -1
- package/dist/adapters/otlp.mjs +1 -1
- package/dist/adapters/otlp.mjs.map +1 -1
- package/dist/adapters/posthog.d.mts +1 -1
- package/dist/adapters/posthog.mjs +1 -1
- package/dist/adapters/posthog.mjs.map +1 -1
- package/dist/adapters/sentry.d.mts +1 -1
- package/dist/adapters/sentry.mjs +1 -1
- package/dist/adapters/sentry.mjs.map +1 -1
- package/dist/ai/index.d.mts +106 -5
- package/dist/ai/index.d.mts.map +1 -1
- package/dist/ai/index.mjs +28 -5
- package/dist/ai/index.mjs.map +1 -1
- package/dist/{types-DbzDln7O.d.mts → audit-CTIviX3P.d.mts} +509 -3
- package/dist/audit-CTIviX3P.d.mts.map +1 -0
- package/dist/{logger-DnobymUQ.mjs → audit-DQoBo7Dl.mjs} +758 -16
- package/dist/audit-DQoBo7Dl.mjs.map +1 -0
- package/dist/better-auth/index.d.mts +1 -1
- package/dist/better-auth/index.mjs.map +1 -1
- package/dist/browser.d.mts +1 -1
- package/dist/elysia/index.d.mts +2 -2
- package/dist/elysia/index.mjs +2 -2
- package/dist/enrichers.d.mts +1 -1
- package/dist/enrichers.mjs.map +1 -1
- package/dist/{error-B9CiGK_i.d.mts → error-C7gSQVqk.d.mts} +2 -2
- package/dist/{error-B9CiGK_i.d.mts.map → error-C7gSQVqk.d.mts.map} +1 -1
- package/dist/error.d.mts +1 -1
- package/dist/{errors-Dr0r4OpR.d.mts → errors-4MPmTzjY.d.mts} +2 -2
- package/dist/{errors-Dr0r4OpR.d.mts.map → errors-4MPmTzjY.d.mts.map} +1 -1
- package/dist/express/index.d.mts +2 -2
- package/dist/express/index.mjs +2 -2
- package/dist/fastify/index.d.mts +2 -2
- package/dist/fastify/index.mjs +2 -2
- package/dist/{fork-Y4z8iHti.mjs → fork-D1j1Fuzy.mjs} +3 -3
- package/dist/{fork-Y4z8iHti.mjs.map → fork-D1j1Fuzy.mjs.map} +1 -1
- package/dist/hono/index.d.mts +2 -2
- package/dist/hono/index.mjs +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/http.d.mts.map +1 -1
- package/dist/http.mjs +3 -2
- package/dist/http.mjs.map +1 -1
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +2 -2
- package/dist/{logger-Dp6nYWjH.d.mts → logger-DttRJRGa.d.mts} +23 -4
- package/dist/logger-DttRJRGa.d.mts.map +1 -0
- package/dist/logger.d.mts +1 -1
- package/dist/logger.mjs +1 -1
- package/dist/{middleware-FgC1OdOD.d.mts → middleware-CTnDsST-.d.mts} +2 -2
- package/dist/{middleware-FgC1OdOD.d.mts.map → middleware-CTnDsST-.d.mts.map} +1 -1
- package/dist/{middleware-BtBuosFV.mjs → middleware-oAccqyPp.mjs} +2 -2
- package/dist/{middleware-BtBuosFV.mjs.map → middleware-oAccqyPp.mjs.map} +1 -1
- package/dist/nestjs/index.d.mts +2 -2
- package/dist/nestjs/index.mjs +2 -2
- package/dist/next/client.d.mts +1 -1
- package/dist/next/index.d.mts +4 -4
- package/dist/next/index.mjs +2 -2
- package/dist/next/index.mjs.map +1 -1
- package/dist/next/instrumentation.d.mts +1 -1
- package/dist/next/instrumentation.mjs +1 -1
- package/dist/next/instrumentation.mjs.map +1 -1
- package/dist/nitro/errorHandler.mjs.map +1 -1
- package/dist/nitro/module.d.mts +2 -2
- package/dist/nitro/plugin.mjs +1 -1
- package/dist/nitro/plugin.mjs.map +1 -1
- package/dist/nitro/v3/index.d.mts +2 -2
- package/dist/nitro/v3/middleware.mjs.map +1 -1
- package/dist/nitro/v3/module.d.mts +1 -1
- package/dist/nitro/v3/plugin.mjs +1 -1
- package/dist/nitro/v3/plugin.mjs.map +1 -1
- package/dist/nitro/v3/useLogger.d.mts +1 -1
- package/dist/nitro/v3/useLogger.mjs.map +1 -1
- package/dist/{nitro-CDHLfRdw.d.mts → nitro-CPPRCPbG.d.mts} +2 -2
- package/dist/{nitro-CDHLfRdw.d.mts.map → nitro-CPPRCPbG.d.mts.map} +1 -1
- package/dist/nuxt/module.d.mts +1 -1
- package/dist/nuxt/module.mjs +1 -1
- package/dist/{parseError-DM-lyezZ.d.mts → parseError-o1GpZEOR.d.mts} +2 -2
- package/dist/parseError-o1GpZEOR.d.mts.map +1 -0
- package/dist/pipeline.mjs.map +1 -1
- package/dist/react-router/index.d.mts +2 -2
- package/dist/react-router/index.mjs +2 -2
- package/dist/runtime/client/log.d.mts +1 -1
- package/dist/runtime/client/log.mjs +2 -2
- package/dist/runtime/client/log.mjs.map +1 -1
- package/dist/runtime/client/plugin.mjs.map +1 -1
- package/dist/runtime/server/routes/_evlog/ingest.post.mjs +1 -1
- package/dist/runtime/server/routes/_evlog/ingest.post.mjs.map +1 -1
- package/dist/runtime/server/useLogger.d.mts +1 -1
- package/dist/runtime/server/useLogger.mjs.map +1 -1
- package/dist/runtime/utils/parseError.d.mts +2 -2
- package/dist/source-location-DRvDDqfq.mjs.map +1 -1
- package/dist/sveltekit/index.d.mts +2 -2
- package/dist/sveltekit/index.mjs +2 -2
- package/dist/sveltekit/index.mjs.map +1 -1
- package/dist/toolkit.d.mts +3 -3
- package/dist/toolkit.mjs +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/{useLogger-N5A-d5l9.d.mts → useLogger-CyPP1sVB.d.mts} +2 -2
- package/dist/{useLogger-N5A-d5l9.d.mts.map → useLogger-CyPP1sVB.d.mts.map} +1 -1
- package/dist/{utils-DnX6VMNi.d.mts → utils-Dmin7wVL.d.mts} +4 -3
- package/dist/utils-Dmin7wVL.d.mts.map +1 -0
- package/dist/utils.d.mts +2 -2
- package/dist/utils.mjs +7 -1
- package/dist/utils.mjs.map +1 -1
- package/dist/vite/index.d.mts +1 -1
- package/dist/vite/index.mjs.map +1 -1
- package/dist/workers.d.mts +48 -4
- package/dist/workers.d.mts.map +1 -1
- package/dist/workers.mjs +32 -5
- package/dist/workers.mjs.map +1 -1
- package/package.json +17 -21
- package/dist/_http-CHSsrWDJ.mjs.map +0 -1
- package/dist/logger-DnobymUQ.mjs.map +0 -1
- package/dist/logger-Dp6nYWjH.d.mts.map +0 -1
- package/dist/parseError-DM-lyezZ.d.mts.map +0 -1
- package/dist/types-DbzDln7O.d.mts.map +0 -1
- package/dist/utils-DnX6VMNi.d.mts.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { colors, cssColors, detectEnvironment, escapeFormatString, formatDuration, getConsoleMethod, getCssLevelColor, getLevelColor,
|
|
1
|
+
import { colors, cssColors, detectEnvironment, escapeFormatString, formatDuration, getConsoleMethod, getCssLevelColor, getLevelColor, isBrowser, isDev, isLevelEnabled, matchesPattern } from "./utils.mjs";
|
|
2
2
|
//#region src/redact.ts
|
|
3
3
|
const DEFAULT_REPLACEMENT = "[REDACTED]";
|
|
4
4
|
/**
|
|
@@ -6,10 +6,12 @@ const DEFAULT_REPLACEMENT = "[REDACTED]";
|
|
|
6
6
|
* Each builtin preserves just enough signal for debugging while scrubbing PII.
|
|
7
7
|
*/
|
|
8
8
|
const builtinPatterns = {
|
|
9
|
+
/** Credit card numbers → ****1111 (PCI DSS: last 4 allowed) */
|
|
9
10
|
creditCard: {
|
|
10
11
|
pattern: /\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/g,
|
|
11
12
|
mask: (m) => `****${m.replace(/[\s-]/g, "").slice(-4)}`
|
|
12
13
|
},
|
|
14
|
+
/** Email addresses → a***@***.com */
|
|
13
15
|
email: {
|
|
14
16
|
pattern: /[\w.+-]+@[\w-]+\.[\w.]+/g,
|
|
15
17
|
mask: (m) => {
|
|
@@ -18,12 +20,22 @@ const builtinPatterns = {
|
|
|
18
20
|
return `${m[0]}***@***${tld}`;
|
|
19
21
|
}
|
|
20
22
|
},
|
|
23
|
+
/** IPv4 addresses → ***.***.***.100 (last octet only) */
|
|
21
24
|
ipv4: {
|
|
22
25
|
pattern: /\b(?!0\.0\.0\.0\b)(?!127\.0\.0\.1\b)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,
|
|
23
26
|
mask: (m) => `***.***.***.${m.split(".").pop()}`
|
|
24
27
|
},
|
|
28
|
+
/**
|
|
29
|
+
* International phone numbers → `+33******78` (country code + last 2 digits).
|
|
30
|
+
*
|
|
31
|
+
* Requires an explicit phone signal (`+countryCode` prefix or `(areaCode)`
|
|
32
|
+
* parens) to avoid false positives on digit-rich identifiers (UUIDs,
|
|
33
|
+
* idempotency keys, order ids, hex hashes). Bare digit runs like `12345678`
|
|
34
|
+
* are intentionally not matched — opt in via custom `patterns` if your app
|
|
35
|
+
* stores phones in unformatted form.
|
|
36
|
+
*/
|
|
25
37
|
phone: {
|
|
26
|
-
pattern: /(?:\+\d{1,3}[\s.-]
|
|
38
|
+
pattern: /(?:\+\d{1,3}[\s.-]?\(?\d{1,4}\)?|\(\d{1,4}\))(?:[\s.-]?\d{2,4}){2,4}\b/g,
|
|
27
39
|
mask: (m) => {
|
|
28
40
|
const digits = m.replace(/[^\d]/g, "");
|
|
29
41
|
if (m.startsWith("+") && digits.length > 4) {
|
|
@@ -34,14 +46,17 @@ const builtinPatterns = {
|
|
|
34
46
|
return "***";
|
|
35
47
|
}
|
|
36
48
|
},
|
|
49
|
+
/** JWT tokens → eyJ***.*** */
|
|
37
50
|
jwt: {
|
|
38
51
|
pattern: /\beyJ[\w-]*\.[\w-]*\.[\w-]*\b/g,
|
|
39
52
|
mask: () => "eyJ***.***"
|
|
40
53
|
},
|
|
54
|
+
/** Bearer tokens → Bearer *** */
|
|
41
55
|
bearer: {
|
|
42
56
|
pattern: /\bBearer\s+[\w\-.~+/]{8,}=*/gi,
|
|
43
57
|
mask: () => "Bearer ***"
|
|
44
58
|
},
|
|
59
|
+
/** IBAN → FR76****189 (country + check digits + last 3) */
|
|
45
60
|
iban: {
|
|
46
61
|
pattern: /\b[A-Z]{2}\d{2}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{4}[\s-]?[\dA-Z]{0,4}[\s-]?[\dA-Z]{0,4}[\s-]?[\dA-Z]{0,4}\b/g,
|
|
47
62
|
mask: (m) => {
|
|
@@ -288,7 +303,8 @@ function shouldKeep(ctx) {
|
|
|
288
303
|
return false;
|
|
289
304
|
});
|
|
290
305
|
}
|
|
291
|
-
function emitWideEvent(level, event,
|
|
306
|
+
function emitWideEvent(level, event, options = {}) {
|
|
307
|
+
const { deferDrain = false, ownsEvent = false, waitUntil } = options;
|
|
292
308
|
if (!globalEnabled) return null;
|
|
293
309
|
if (!ownsEvent) {
|
|
294
310
|
if (!isLevelEnabled(level, globalMinLevel)) return null;
|
|
@@ -310,13 +326,17 @@ function emitWideEvent(level, event, deferDrain = false, ownsEvent = false) {
|
|
|
310
326
|
...globalEnv,
|
|
311
327
|
...event
|
|
312
328
|
};
|
|
329
|
+
finalizeAudit(formatted);
|
|
313
330
|
if (globalRedact) redactEvent(formatted, globalRedact);
|
|
314
331
|
if (!globalSilent) if (globalPretty) prettyPrintWideEvent(formatted);
|
|
315
332
|
else if (globalStringify) console[getConsoleMethod(level)](JSON.stringify(formatted));
|
|
316
333
|
else console[getConsoleMethod(level)](formatted);
|
|
317
|
-
if (globalDrain && !deferDrain)
|
|
318
|
-
|
|
319
|
-
|
|
334
|
+
if (globalDrain && !deferDrain) {
|
|
335
|
+
const drainPromise = Promise.resolve(globalDrain({ event: formatted })).catch((err) => {
|
|
336
|
+
console.error("[evlog] drain failed:", err);
|
|
337
|
+
});
|
|
338
|
+
if (waitUntil) waitUntil(drainPromise);
|
|
339
|
+
}
|
|
320
340
|
return formatted;
|
|
321
341
|
}
|
|
322
342
|
function emitTaggedLog(level, tag, message) {
|
|
@@ -324,7 +344,7 @@ function emitTaggedLog(level, tag, message) {
|
|
|
324
344
|
if (globalPretty && !globalSilent) {
|
|
325
345
|
if (!isLevelEnabled(level, globalMinLevel)) return;
|
|
326
346
|
if (!shouldSample(level)) return;
|
|
327
|
-
if (
|
|
347
|
+
if (isBrowser()) {
|
|
328
348
|
const levelColor = getCssLevelColor(level);
|
|
329
349
|
const timestamp = isoNow().slice(11, 23);
|
|
330
350
|
console.log(`%c${timestamp}%c %c[${escapeFormatString(tag)}]%c ${escapeFormatString(message)}`, cssColors.dim, cssColors.reset, levelColor, cssColors.reset);
|
|
@@ -490,7 +510,7 @@ function buildAIEntries(ai) {
|
|
|
490
510
|
function prettyPrintWideEvent(event) {
|
|
491
511
|
const { timestamp, level, service, environment, version, ...rest } = event;
|
|
492
512
|
const ts = timestamp.slice(11, 23);
|
|
493
|
-
const browser =
|
|
513
|
+
const browser = isBrowser();
|
|
494
514
|
const parts = [];
|
|
495
515
|
const styles = [];
|
|
496
516
|
if (browser) {
|
|
@@ -584,7 +604,8 @@ const noopLogger = {
|
|
|
584
604
|
},
|
|
585
605
|
getContext() {
|
|
586
606
|
return {};
|
|
587
|
-
}
|
|
607
|
+
},
|
|
608
|
+
audit: Object.assign(() => {}, { deny: () => {} })
|
|
588
609
|
};
|
|
589
610
|
/**
|
|
590
611
|
* Create a scoped logger for building wide events.
|
|
@@ -604,6 +625,7 @@ const noopLogger = {
|
|
|
604
625
|
function createLogger(initialContext = {}, internalOptions) {
|
|
605
626
|
if (!globalEnabled) return noopLogger;
|
|
606
627
|
const deferDrain = internalOptions?._deferDrain ?? false;
|
|
628
|
+
const waitUntil = internalOptions?.waitUntil;
|
|
607
629
|
const startTime = Date.now();
|
|
608
630
|
const context = { ...initialContext };
|
|
609
631
|
let hasError = false;
|
|
@@ -617,7 +639,25 @@ function createLogger(initialContext = {}, internalOptions) {
|
|
|
617
639
|
timestamp: isoNow()
|
|
618
640
|
});
|
|
619
641
|
}
|
|
642
|
+
const auditMethod = function audit(input) {
|
|
643
|
+
if (emitted) {
|
|
644
|
+
warnPostEmit("log.audit()", `Audit dropped: action=${input.action}.`);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const fields = buildAuditFields(input);
|
|
648
|
+
if (!isPlainObject(context.audit)) context.audit = fields;
|
|
649
|
+
else mergeInto(context.audit, fields);
|
|
650
|
+
context._auditForceKeep = true;
|
|
651
|
+
};
|
|
652
|
+
auditMethod.deny = function deny(reason, input) {
|
|
653
|
+
auditMethod({
|
|
654
|
+
...input,
|
|
655
|
+
outcome: "denied",
|
|
656
|
+
reason
|
|
657
|
+
});
|
|
658
|
+
};
|
|
620
659
|
return {
|
|
660
|
+
audit: auditMethod,
|
|
621
661
|
set(data) {
|
|
622
662
|
if (emitted) {
|
|
623
663
|
const keys = Object.keys(data);
|
|
@@ -684,6 +724,7 @@ function createLogger(initialContext = {}, internalOptions) {
|
|
|
684
724
|
const level = hasError ? "error" : hasWarn ? "warn" : "info";
|
|
685
725
|
let forceKeep = false;
|
|
686
726
|
if (overrides?._forceKeep) forceKeep = true;
|
|
727
|
+
else if (consumeAuditForceKeep(context)) forceKeep = true;
|
|
687
728
|
else if (globalSampling.keep?.length) forceKeep = shouldKeep({
|
|
688
729
|
status: overrides?.status ?? context.status,
|
|
689
730
|
duration: durationMs,
|
|
@@ -700,7 +741,11 @@ function createLogger(initialContext = {}, internalOptions) {
|
|
|
700
741
|
for (const key in obj) if (key !== "_forceKeep") context[key] = obj[key];
|
|
701
742
|
}
|
|
702
743
|
context.duration = formatDuration(durationMs);
|
|
703
|
-
const wide = emitWideEvent(level, context,
|
|
744
|
+
const wide = emitWideEvent(level, context, {
|
|
745
|
+
deferDrain,
|
|
746
|
+
ownsEvent: true,
|
|
747
|
+
waitUntil
|
|
748
|
+
});
|
|
704
749
|
emitted = true;
|
|
705
750
|
return wide;
|
|
706
751
|
},
|
|
@@ -720,13 +765,32 @@ function createLogger(initialContext = {}, internalOptions) {
|
|
|
720
765
|
* log.set({ cart: { items: 3 } })
|
|
721
766
|
* log.emit()
|
|
722
767
|
* ```
|
|
768
|
+
*
|
|
769
|
+
* @example Cloudflare Workers — pass `waitUntil` so `initLogger({ drain })` completes after the response:
|
|
770
|
+
* ```ts
|
|
771
|
+
* export default {
|
|
772
|
+
* async fetch(request, env, ctx) {
|
|
773
|
+
* const log = createRequestLogger({
|
|
774
|
+
* method: request.method,
|
|
775
|
+
* path: new URL(request.url).pathname,
|
|
776
|
+
* waitUntil: ctx.waitUntil.bind(ctx),
|
|
777
|
+
* })
|
|
778
|
+
* log.emit()
|
|
779
|
+
* return new Response('ok')
|
|
780
|
+
* },
|
|
781
|
+
* }
|
|
782
|
+
* ```
|
|
723
783
|
*/
|
|
724
784
|
function createRequestLogger(options = {}, internalOptions) {
|
|
785
|
+
const { method, path, requestId, waitUntil: optionsWaitUntil } = options;
|
|
725
786
|
const initial = {};
|
|
726
|
-
if (
|
|
727
|
-
if (
|
|
728
|
-
if (
|
|
729
|
-
return createLogger(initial,
|
|
787
|
+
if (method !== void 0) initial.method = method;
|
|
788
|
+
if (path !== void 0) initial.path = path;
|
|
789
|
+
if (requestId !== void 0) initial.requestId = requestId;
|
|
790
|
+
return createLogger(initial, {
|
|
791
|
+
...internalOptions,
|
|
792
|
+
waitUntil: internalOptions?.waitUntil ?? optionsWaitUntil
|
|
793
|
+
});
|
|
730
794
|
}
|
|
731
795
|
/**
|
|
732
796
|
* Get the current environment context.
|
|
@@ -736,6 +800,684 @@ function getEnvironment() {
|
|
|
736
800
|
}
|
|
737
801
|
if (typeof __EVLOG_CONFIG__ !== "undefined") initLogger(__EVLOG_CONFIG__);
|
|
738
802
|
//#endregion
|
|
739
|
-
|
|
803
|
+
//#region src/audit.ts
|
|
804
|
+
/**
|
|
805
|
+
* Current version of the audit envelope. Bumped when `AuditFields` evolves
|
|
806
|
+
* in a backward-incompatible way so downstream pipelines can branch on it.
|
|
807
|
+
*/
|
|
808
|
+
const AUDIT_SCHEMA_VERSION = 1;
|
|
809
|
+
/**
|
|
810
|
+
* @internal Stable JSON stringification with deterministic key order.
|
|
811
|
+
* Used by `idempotencyKey` and `hash-chain` so the same logical event always
|
|
812
|
+
* produces the same digest, regardless of how object keys were added.
|
|
813
|
+
*/
|
|
814
|
+
function stableStringify(value) {
|
|
815
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
816
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
817
|
+
return `{${Object.keys(value).sort().map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(",")}}`;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* @internal Sync, isomorphic 32-bit FNV-1a. Used to derive the idempotency
|
|
821
|
+
* key without pulling `node:crypto` into the static import graph (which would
|
|
822
|
+
* break browser / Cloudflare Workers bundles that import `evlog` for types
|
|
823
|
+
* or shared utilities). Idempotency keys are dedup tokens, not security
|
|
824
|
+
* primitives — collision resistance at 128 bits is more than sufficient.
|
|
825
|
+
*/
|
|
826
|
+
function fnv1a32(input, seed) {
|
|
827
|
+
let h = seed >>> 0;
|
|
828
|
+
for (let i = 0; i < input.length; i++) {
|
|
829
|
+
h ^= input.charCodeAt(i) & 255;
|
|
830
|
+
h = Math.imul(h, 16777619) >>> 0;
|
|
831
|
+
}
|
|
832
|
+
return h >>> 0;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* @internal Compute the deterministic idempotency key for an audit event.
|
|
836
|
+
* Includes `action`, `actor.{type,id}`, `target.{type,id}`, `outcome`, and
|
|
837
|
+
* `timestamp` rounded to the second so retries within the same second collapse.
|
|
838
|
+
*
|
|
839
|
+
* Uses four interleaved FNV-1a 32-bit hashes (128-bit output, 32 hex chars)
|
|
840
|
+
* so the implementation stays sync and isomorphic across Node, browsers,
|
|
841
|
+
* Bun, Deno, and Cloudflare Workers.
|
|
842
|
+
*/
|
|
843
|
+
function computeIdempotencyKey(audit, timestamp) {
|
|
844
|
+
const seconds = timestamp.slice(0, 19);
|
|
845
|
+
const payload = stableStringify({
|
|
846
|
+
action: audit.action,
|
|
847
|
+
actor: {
|
|
848
|
+
type: audit.actor.type,
|
|
849
|
+
id: audit.actor.id
|
|
850
|
+
},
|
|
851
|
+
target: audit.target ? {
|
|
852
|
+
type: audit.target.type,
|
|
853
|
+
id: audit.target.id
|
|
854
|
+
} : void 0,
|
|
855
|
+
outcome: audit.outcome,
|
|
856
|
+
timestamp: seconds
|
|
857
|
+
});
|
|
858
|
+
const a = fnv1a32(payload, 2166136261).toString(16).padStart(8, "0");
|
|
859
|
+
const b = fnv1a32(payload, 3735928559).toString(16).padStart(8, "0");
|
|
860
|
+
const c = fnv1a32(payload, 528734635).toString(16).padStart(8, "0");
|
|
861
|
+
const d = fnv1a32(payload, 1541459225).toString(16).padStart(8, "0");
|
|
862
|
+
return a + b + c + d;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Build a normalised {@link AuditFields} from caller input. Defaults:
|
|
866
|
+
* - `outcome` → `'success'`
|
|
867
|
+
* - `version` → {@link AUDIT_SCHEMA_VERSION}
|
|
868
|
+
*
|
|
869
|
+
* `idempotencyKey` is filled at emit time with the event timestamp so retries
|
|
870
|
+
* stay deterministic.
|
|
871
|
+
*/
|
|
872
|
+
function buildAuditFields(input) {
|
|
873
|
+
return {
|
|
874
|
+
action: input.action,
|
|
875
|
+
actor: input.actor,
|
|
876
|
+
target: input.target,
|
|
877
|
+
outcome: input.outcome ?? "success",
|
|
878
|
+
reason: input.reason,
|
|
879
|
+
changes: input.changes,
|
|
880
|
+
causationId: input.causationId,
|
|
881
|
+
correlationId: input.correlationId,
|
|
882
|
+
version: input.version ?? 1
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* @internal Test-collector hook installed by {@link mockAudit}. When set, every
|
|
887
|
+
* audit event flowing through `log.audit()` / `audit()` is also pushed to it.
|
|
888
|
+
*/
|
|
889
|
+
let _testCollector = null;
|
|
890
|
+
/** @internal Emit-time decoration: assign timestamp-based idempotency key. */
|
|
891
|
+
function decorateAudit(audit, timestamp) {
|
|
892
|
+
if (audit.idempotencyKey) return audit;
|
|
893
|
+
return {
|
|
894
|
+
...audit,
|
|
895
|
+
idempotencyKey: computeIdempotencyKey(audit, timestamp)
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Add audit semantics to an existing {@link RequestLogger}.
|
|
900
|
+
*
|
|
901
|
+
* Mutates the logger in place by adding an `audit` method (with a `.deny()`
|
|
902
|
+
* sub-method). Strictly equivalent to calling `log.set({ audit: ... })` plus
|
|
903
|
+
* `_forceKeep` on emit. Idempotent: calling twice on the same logger only
|
|
904
|
+
* attaches the methods once.
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```ts
|
|
908
|
+
* const log = withAuditMethods(createLogger())
|
|
909
|
+
* log.audit({
|
|
910
|
+
* action: 'invoice.refund',
|
|
911
|
+
* actor: { type: 'user', id: user.id },
|
|
912
|
+
* target: { type: 'invoice', id: 'inv_889' },
|
|
913
|
+
* })
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
function withAuditMethods(logger) {
|
|
917
|
+
const target = logger;
|
|
918
|
+
if (target.audit) return target;
|
|
919
|
+
const audit = function audit(input) {
|
|
920
|
+
const fields = buildAuditFields(input);
|
|
921
|
+
target.set({ audit: fields });
|
|
922
|
+
markForceKeep(target);
|
|
923
|
+
};
|
|
924
|
+
audit.deny = function deny(reason, input) {
|
|
925
|
+
audit({
|
|
926
|
+
...input,
|
|
927
|
+
outcome: "denied",
|
|
928
|
+
reason
|
|
929
|
+
});
|
|
930
|
+
};
|
|
931
|
+
target.audit = audit;
|
|
932
|
+
return target;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* @internal Mark a logger so its next `emit()` is force-kept past tail sampling.
|
|
936
|
+
* Implemented by stamping a hidden flag on the accumulated context which
|
|
937
|
+
* `emit()` reads via `_forceKeep`.
|
|
938
|
+
*/
|
|
939
|
+
function markForceKeep(logger) {
|
|
940
|
+
const ctx = logger.getContext();
|
|
941
|
+
ctx._auditForceKeep = true;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Standalone audit emitter for non-request contexts (jobs, scripts, CLIs).
|
|
945
|
+
*
|
|
946
|
+
* Creates a one-shot logger, sets the audit fields, and emits immediately.
|
|
947
|
+
* The event is force-kept past tail sampling. Returns the emitted wide event,
|
|
948
|
+
* or `null` if logging is globally disabled.
|
|
949
|
+
*
|
|
950
|
+
* @example
|
|
951
|
+
* ```ts
|
|
952
|
+
* import { audit } from 'evlog'
|
|
953
|
+
*
|
|
954
|
+
* audit({
|
|
955
|
+
* action: 'cron.cleanup',
|
|
956
|
+
* actor: { type: 'system', id: 'cron' },
|
|
957
|
+
* target: { type: 'job', id: 'cleanup-stale-sessions' },
|
|
958
|
+
* outcome: 'success',
|
|
959
|
+
* })
|
|
960
|
+
* ```
|
|
961
|
+
*/
|
|
962
|
+
function audit(input) {
|
|
963
|
+
const fields = buildAuditFields(input);
|
|
964
|
+
const wide = createLogger({ audit: fields }).emit({ _forceKeep: true });
|
|
965
|
+
_testCollector?.(fields, wide);
|
|
966
|
+
return wide;
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Wrap a function so its outcome (success / failure / denied) is automatically
|
|
970
|
+
* audited.
|
|
971
|
+
*
|
|
972
|
+
* Behaviour:
|
|
973
|
+
* - If `fn` resolves, an audit event with `outcome: 'success'` is emitted.
|
|
974
|
+
* - If `fn` throws an `EvlogError` (or any error) with `status === 403`, the
|
|
975
|
+
* audit event is recorded as `'denied'` with the error message as `reason`.
|
|
976
|
+
* - Any other thrown error produces `outcome: 'failure'` and re-throws.
|
|
977
|
+
*
|
|
978
|
+
* Use {@link AuditDeniedError} to signal denial without an HTTP status.
|
|
979
|
+
*
|
|
980
|
+
* @example
|
|
981
|
+
* ```ts
|
|
982
|
+
* const refundInvoice = withAudit(
|
|
983
|
+
* { action: 'invoice.refund', target: (input) => ({ type: 'invoice', id: input.id }) },
|
|
984
|
+
* async (input: { id: string }, ctx: { actor: AuditActor }) => {
|
|
985
|
+
* await db.invoices.refund(input.id)
|
|
986
|
+
* }
|
|
987
|
+
* )
|
|
988
|
+
*
|
|
989
|
+
* await refundInvoice({ id: 'inv_889' }, { actor: { type: 'user', id: user.id } })
|
|
990
|
+
* ```
|
|
991
|
+
*/
|
|
992
|
+
function withAudit(options, fn) {
|
|
993
|
+
return async (input, ctx) => {
|
|
994
|
+
const target = typeof options.target === "function" ? options.target(input) : options.target;
|
|
995
|
+
try {
|
|
996
|
+
const result = await fn(input, ctx);
|
|
997
|
+
audit({
|
|
998
|
+
action: options.action,
|
|
999
|
+
actor: ctx.actor,
|
|
1000
|
+
target,
|
|
1001
|
+
outcome: "success",
|
|
1002
|
+
causationId: ctx.causationId,
|
|
1003
|
+
correlationId: ctx.correlationId
|
|
1004
|
+
});
|
|
1005
|
+
return result;
|
|
1006
|
+
} catch (err) {
|
|
1007
|
+
const error = err;
|
|
1008
|
+
const status = error.status ?? error.statusCode;
|
|
1009
|
+
const denied = err instanceof AuditDeniedError || status === 403;
|
|
1010
|
+
audit({
|
|
1011
|
+
action: options.action,
|
|
1012
|
+
actor: ctx.actor,
|
|
1013
|
+
target,
|
|
1014
|
+
outcome: denied ? "denied" : "failure",
|
|
1015
|
+
reason: error.message,
|
|
1016
|
+
causationId: ctx.causationId,
|
|
1017
|
+
correlationId: ctx.correlationId
|
|
1018
|
+
});
|
|
1019
|
+
throw err;
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Throw inside a {@link withAudit} body to mark the action as `outcome: 'denied'`
|
|
1025
|
+
* regardless of the underlying HTTP status. The constructor message becomes the
|
|
1026
|
+
* audit `reason`.
|
|
1027
|
+
*/
|
|
1028
|
+
var AuditDeniedError = class extends Error {
|
|
1029
|
+
constructor(reason) {
|
|
1030
|
+
super(reason);
|
|
1031
|
+
this.name = "AuditDeniedError";
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* Compute a compact, redact-aware diff between two objects for the
|
|
1036
|
+
* `changes` field. Output is a JSON Patch-style array (RFC 6902 subset:
|
|
1037
|
+
* `add`, `remove`, `replace`) — small enough to ship over the wire.
|
|
1038
|
+
*
|
|
1039
|
+
* Object keys whose name matches one of the `redactPaths` (dot-notation, e.g.
|
|
1040
|
+
* `'user.password'`, `'card.cvv'`) are replaced with `'[REDACTED]'` so PII
|
|
1041
|
+
* never leaks through the diff.
|
|
1042
|
+
*
|
|
1043
|
+
* @example
|
|
1044
|
+
* ```ts
|
|
1045
|
+
* log.audit({
|
|
1046
|
+
* action: 'user.update',
|
|
1047
|
+
* actor: { type: 'user', id: user.id },
|
|
1048
|
+
* target: { type: 'user', id: 'usr_42' },
|
|
1049
|
+
* changes: auditDiff(before, after, { redactPaths: ['password'] }),
|
|
1050
|
+
* })
|
|
1051
|
+
* ```
|
|
1052
|
+
*/
|
|
1053
|
+
function auditDiff(before, after, options = {}) {
|
|
1054
|
+
const replacement = options.replacement ?? "[REDACTED]";
|
|
1055
|
+
const redactSet = new Set((options.redactPaths ?? []).map((p) => p));
|
|
1056
|
+
const patch = [];
|
|
1057
|
+
function isRedacted(path) {
|
|
1058
|
+
if (redactSet.size === 0) return false;
|
|
1059
|
+
if (redactSet.has(path)) return true;
|
|
1060
|
+
for (const p of redactSet) if (path.endsWith(`.${p}`)) return true;
|
|
1061
|
+
return false;
|
|
1062
|
+
}
|
|
1063
|
+
function diff(a, b, path) {
|
|
1064
|
+
if (a === b) return;
|
|
1065
|
+
if (a === void 0 && b !== void 0) {
|
|
1066
|
+
patch.push({
|
|
1067
|
+
op: "add",
|
|
1068
|
+
path: path || "/",
|
|
1069
|
+
value: redactValue(b, path)
|
|
1070
|
+
});
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
if (a !== void 0 && b === void 0) {
|
|
1074
|
+
patch.push({
|
|
1075
|
+
op: "remove",
|
|
1076
|
+
path: path || "/"
|
|
1077
|
+
});
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
if (a !== null && b !== null && typeof a === "object" && typeof b === "object" && !Array.isArray(a) && !Array.isArray(b)) {
|
|
1081
|
+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
1082
|
+
for (const key of keys) diff(a[key], b[key], `${path}/${key}`);
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
patch.push({
|
|
1086
|
+
op: "replace",
|
|
1087
|
+
path: path || "/",
|
|
1088
|
+
value: redactValue(b, path)
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
function redactValue(value, path) {
|
|
1092
|
+
if (value === null || typeof value !== "object") {
|
|
1093
|
+
const segs = path.split("/").filter(Boolean);
|
|
1094
|
+
const last = segs[segs.length - 1];
|
|
1095
|
+
if (last && isRedacted(last)) return replacement;
|
|
1096
|
+
return value;
|
|
1097
|
+
}
|
|
1098
|
+
if (Array.isArray(value)) return value.map((v, i) => redactValue(v, `${path}/${i}`));
|
|
1099
|
+
const out = {};
|
|
1100
|
+
for (const [k, v] of Object.entries(value)) out[k] = isRedacted(k) ? replacement : redactValue(v, `${path}/${k}`);
|
|
1101
|
+
return out;
|
|
1102
|
+
}
|
|
1103
|
+
diff(before, after, "");
|
|
1104
|
+
const result = { patch };
|
|
1105
|
+
if (options.includeBefore) result.before = redactValue(before, "");
|
|
1106
|
+
if (options.includeAfter) result.after = redactValue(after, "");
|
|
1107
|
+
return result;
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Define a typed audit action with an optional fixed target type.
|
|
1111
|
+
*
|
|
1112
|
+
* Returns a curried helper that fills in the action name (and target shape
|
|
1113
|
+
* if provided) so call sites stay terse and the action set is discoverable
|
|
1114
|
+
* in one place.
|
|
1115
|
+
*
|
|
1116
|
+
* @example
|
|
1117
|
+
* ```ts
|
|
1118
|
+
* const refund = defineAuditAction('invoice.refund', { target: 'invoice' })
|
|
1119
|
+
*
|
|
1120
|
+
* log.audit(refund({
|
|
1121
|
+
* actor: { type: 'user', id: user.id },
|
|
1122
|
+
* target: { id: 'inv_889' }, // type inferred as 'invoice'
|
|
1123
|
+
* outcome: 'success',
|
|
1124
|
+
* }))
|
|
1125
|
+
* ```
|
|
1126
|
+
*/
|
|
1127
|
+
function defineAuditAction(action, options) {
|
|
1128
|
+
const targetType = options?.target;
|
|
1129
|
+
return (input) => {
|
|
1130
|
+
const merged = {
|
|
1131
|
+
...input,
|
|
1132
|
+
action
|
|
1133
|
+
};
|
|
1134
|
+
if (targetType && input.target && !input.target.type) merged.target = {
|
|
1135
|
+
...input.target,
|
|
1136
|
+
type: targetType
|
|
1137
|
+
};
|
|
1138
|
+
return merged;
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
/**
|
|
1142
|
+
* Test helper that captures every audit event emitted while it is active.
|
|
1143
|
+
*
|
|
1144
|
+
* Returns `{ events, restore, expect }`:
|
|
1145
|
+
* - `events` — live array of captured `AuditFields`, populated as audits fire.
|
|
1146
|
+
* - `restore()` — uninstall the collector. Call from `afterEach()`.
|
|
1147
|
+
* - `expect.toIncludeAuditOf(matcher)` — assertion helper used inside `expect`
|
|
1148
|
+
* blocks, returns `true` if at least one captured event matches.
|
|
1149
|
+
*
|
|
1150
|
+
* Only captures audits going through `log.audit()` and the standalone
|
|
1151
|
+
* `audit()` function. Events emitted via raw `log.set({ audit })` skip the
|
|
1152
|
+
* collector by design — wrap them with `log.audit()` to make them visible to
|
|
1153
|
+
* tests.
|
|
1154
|
+
*
|
|
1155
|
+
* @example
|
|
1156
|
+
* ```ts
|
|
1157
|
+
* const captured = mockAudit()
|
|
1158
|
+
* await refundInvoice('inv_889')
|
|
1159
|
+
* expect(captured.events).toHaveLength(1)
|
|
1160
|
+
* expect(captured.toIncludeAuditOf({ action: 'invoice.refund' })).toBe(true)
|
|
1161
|
+
* captured.restore()
|
|
1162
|
+
* ```
|
|
1163
|
+
*/
|
|
1164
|
+
function mockAudit() {
|
|
1165
|
+
const events = [];
|
|
1166
|
+
const previous = _testCollector;
|
|
1167
|
+
_testCollector = (event) => {
|
|
1168
|
+
events.push(event);
|
|
1169
|
+
};
|
|
1170
|
+
return {
|
|
1171
|
+
events,
|
|
1172
|
+
restore() {
|
|
1173
|
+
_testCollector = previous;
|
|
1174
|
+
},
|
|
1175
|
+
toIncludeAuditOf(matcher) {
|
|
1176
|
+
return events.some((event) => matchesAudit(event, matcher));
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
function matchesAudit(event, matcher) {
|
|
1181
|
+
if (matcher.action !== void 0) {
|
|
1182
|
+
if (matcher.action instanceof RegExp) {
|
|
1183
|
+
if (!matcher.action.test(event.action)) return false;
|
|
1184
|
+
} else if (event.action !== matcher.action) return false;
|
|
1185
|
+
}
|
|
1186
|
+
if (matcher.outcome !== void 0 && event.outcome !== matcher.outcome) return false;
|
|
1187
|
+
if (matcher.actor) {
|
|
1188
|
+
for (const [k, v] of Object.entries(matcher.actor)) if (event.actor[k] !== v) return false;
|
|
1189
|
+
}
|
|
1190
|
+
if (matcher.target) {
|
|
1191
|
+
if (!event.target) return false;
|
|
1192
|
+
for (const [k, v] of Object.entries(matcher.target)) if (event.target[k] !== v) return false;
|
|
1193
|
+
}
|
|
1194
|
+
return true;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* @internal Hook used by `RequestLogger.emit()` to detect audit-driven
|
|
1198
|
+
* force-keep flags on the accumulated context. Returns whether the event was
|
|
1199
|
+
* marked by `log.audit()` and clears the flag.
|
|
1200
|
+
*/
|
|
1201
|
+
function consumeAuditForceKeep(context) {
|
|
1202
|
+
if (context._auditForceKeep) {
|
|
1203
|
+
delete context._auditForceKeep;
|
|
1204
|
+
return true;
|
|
1205
|
+
}
|
|
1206
|
+
if (context.audit) return true;
|
|
1207
|
+
return false;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* @internal Decorate the audit field on an event right before drain — fills
|
|
1211
|
+
* in the deterministic idempotency key. Called by the logger pipeline so
|
|
1212
|
+
* it works for both `log.audit()` and direct `log.set({ audit })` paths.
|
|
1213
|
+
*/
|
|
1214
|
+
function finalizeAudit(event) {
|
|
1215
|
+
const a = event.audit;
|
|
1216
|
+
if (!a) return;
|
|
1217
|
+
event.audit = decorateAudit(a, String(event.timestamp));
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Enrich audit-bearing wide events with request, runtime, and tenant context.
|
|
1221
|
+
*
|
|
1222
|
+
* Runs only when `event.audit` is present — every other event passes through
|
|
1223
|
+
* untouched. Populates:
|
|
1224
|
+
* - `event.audit.context.requestId` from `ctx.request.requestId`
|
|
1225
|
+
* - `event.audit.context.traceId` from `event.traceId`
|
|
1226
|
+
* - `event.audit.context.ip` from `x-forwarded-for` / `x-real-ip`
|
|
1227
|
+
* - `event.audit.context.userAgent` from `user-agent`
|
|
1228
|
+
* - `event.audit.context.tenantId` from `options.tenantId(ctx)`
|
|
1229
|
+
*
|
|
1230
|
+
* Optionally fills `event.audit.actor` from the better-auth bridge when the
|
|
1231
|
+
* caller did not provide one. Anything else (custom actor strategies,
|
|
1232
|
+
* extra context) belongs in a custom enricher — replace this one entirely.
|
|
1233
|
+
*/
|
|
1234
|
+
function auditEnricher(options = {}) {
|
|
1235
|
+
return async (ctx) => {
|
|
1236
|
+
const event = ctx.event;
|
|
1237
|
+
const a = event.audit;
|
|
1238
|
+
if (!a) return;
|
|
1239
|
+
const context = { ...a.context ?? {} };
|
|
1240
|
+
function setIfMissing(key, value) {
|
|
1241
|
+
if (value === void 0) return;
|
|
1242
|
+
if (options.overwrite || context[key] === void 0) context[key] = value;
|
|
1243
|
+
}
|
|
1244
|
+
setIfMissing("requestId", ctx.request?.requestId);
|
|
1245
|
+
setIfMissing("traceId", typeof event.traceId === "string" ? event.traceId : void 0);
|
|
1246
|
+
setIfMissing("ip", getHeader(ctx.headers, "x-forwarded-for")?.split(",")[0]?.trim() ?? getHeader(ctx.headers, "x-real-ip"));
|
|
1247
|
+
setIfMissing("userAgent", getHeader(ctx.headers, "user-agent"));
|
|
1248
|
+
if (options.tenantId) {
|
|
1249
|
+
const tid = options.tenantId(ctx);
|
|
1250
|
+
if (tid !== void 0) setIfMissing("tenantId", tid);
|
|
1251
|
+
}
|
|
1252
|
+
let { actor } = a;
|
|
1253
|
+
if (!actor && options.bridge) {
|
|
1254
|
+
const fromBridge = await options.bridge.getSession(ctx);
|
|
1255
|
+
if (fromBridge) actor = fromBridge;
|
|
1256
|
+
}
|
|
1257
|
+
event.audit = {
|
|
1258
|
+
...a,
|
|
1259
|
+
context,
|
|
1260
|
+
actor: actor ?? a.actor
|
|
1261
|
+
};
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
function getHeader(headers, name) {
|
|
1265
|
+
if (!headers) return void 0;
|
|
1266
|
+
if (headers[name] !== void 0) return headers[name];
|
|
1267
|
+
const lower = name.toLowerCase();
|
|
1268
|
+
if (headers[lower] !== void 0) return headers[lower];
|
|
1269
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === lower) return v;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Wrap any drain so it only receives events that carry an `audit` field.
|
|
1273
|
+
*
|
|
1274
|
+
* Use to route audit events to dedicated storage (separate Axiom dataset,
|
|
1275
|
+
* append-only Postgres table, FS journal) without affecting your main drain.
|
|
1276
|
+
*
|
|
1277
|
+
* Per-sink failure isolation comes from `initLogger({ drain: [...] })`: each
|
|
1278
|
+
* drain in the array is invoked independently, so a crashed Axiom call never
|
|
1279
|
+
* blocks the FS audit drain.
|
|
1280
|
+
*
|
|
1281
|
+
* @example
|
|
1282
|
+
* ```ts
|
|
1283
|
+
* import { initLogger, auditOnly } from 'evlog'
|
|
1284
|
+
* import { createAxiomDrain } from 'evlog/axiom'
|
|
1285
|
+
* import { createFsDrain } from 'evlog/fs'
|
|
1286
|
+
*
|
|
1287
|
+
* initLogger({
|
|
1288
|
+
* drain: [
|
|
1289
|
+
* createAxiomDrain({ dataset: 'logs' }),
|
|
1290
|
+
* auditOnly(createFsDrain({ dir: '.audit' }), { await: true }),
|
|
1291
|
+
* ],
|
|
1292
|
+
* })
|
|
1293
|
+
* ```
|
|
1294
|
+
*/
|
|
1295
|
+
function auditOnly(drain, options = {}) {
|
|
1296
|
+
return async (ctx) => {
|
|
1297
|
+
if (!ctx.event.audit) return;
|
|
1298
|
+
if (options.await) {
|
|
1299
|
+
await drain(ctx);
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
drain(ctx);
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Wrap a drain so every event passing through gains tamper-evident integrity.
|
|
1307
|
+
*
|
|
1308
|
+
* - `'hmac'` — adds `event.audit.signature` (HMAC of the canonical event).
|
|
1309
|
+
* - `'hash-chain'` — adds `event.audit.prevHash` and `event.audit.hash` so the
|
|
1310
|
+
* sequence of events forms a verifiable chain. State persists in memory
|
|
1311
|
+
* by default; pass a `state: { load, save }` for cross-process / durable
|
|
1312
|
+
* chains (Redis, file, Postgres).
|
|
1313
|
+
*
|
|
1314
|
+
* The signature is computed before the event is forwarded to the wrapped
|
|
1315
|
+
* drain — combine with {@link auditOnly} when you only want integrity for
|
|
1316
|
+
* audit events.
|
|
1317
|
+
*
|
|
1318
|
+
* @example
|
|
1319
|
+
* ```ts
|
|
1320
|
+
* import { initLogger, auditOnly, signed } from 'evlog'
|
|
1321
|
+
* import { createFsDrain } from 'evlog/fs'
|
|
1322
|
+
*
|
|
1323
|
+
* initLogger({
|
|
1324
|
+
* drain: auditOnly(
|
|
1325
|
+
* signed(createFsDrain({ dir: '.audit' }), { strategy: 'hash-chain' }),
|
|
1326
|
+
* { await: true },
|
|
1327
|
+
* ),
|
|
1328
|
+
* })
|
|
1329
|
+
* ```
|
|
1330
|
+
*/
|
|
1331
|
+
function signed(drain, options) {
|
|
1332
|
+
if (options.strategy === "hmac") {
|
|
1333
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
1334
|
+
const { secret } = options;
|
|
1335
|
+
return async (ctx) => {
|
|
1336
|
+
const a = ctx.event.audit;
|
|
1337
|
+
if (a) {
|
|
1338
|
+
const signature = await hmacHex(algorithm, secret, stableStringify(stripIntegrity(ctx.event)));
|
|
1339
|
+
ctx.event.audit = {
|
|
1340
|
+
...a,
|
|
1341
|
+
signature
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
await drain(ctx);
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
const algorithm = options.algorithm ?? "sha256";
|
|
1348
|
+
const { state } = options;
|
|
1349
|
+
let inMemoryPrev = null;
|
|
1350
|
+
let initialised = !state;
|
|
1351
|
+
let queue = Promise.resolve();
|
|
1352
|
+
return (ctx) => {
|
|
1353
|
+
queue = queue.then(async () => {
|
|
1354
|
+
const a = ctx.event.audit;
|
|
1355
|
+
if (a) {
|
|
1356
|
+
if (!initialised && state) {
|
|
1357
|
+
inMemoryPrev = await state.load() ?? null;
|
|
1358
|
+
initialised = true;
|
|
1359
|
+
}
|
|
1360
|
+
const prevHash = inMemoryPrev ?? void 0;
|
|
1361
|
+
const hash = await digestHex(algorithm, stableStringify({
|
|
1362
|
+
...stripIntegrity(ctx.event),
|
|
1363
|
+
audit: {
|
|
1364
|
+
...stripIntegrity(ctx.event).audit,
|
|
1365
|
+
prevHash
|
|
1366
|
+
}
|
|
1367
|
+
}));
|
|
1368
|
+
ctx.event.audit = {
|
|
1369
|
+
...a,
|
|
1370
|
+
prevHash,
|
|
1371
|
+
hash
|
|
1372
|
+
};
|
|
1373
|
+
inMemoryPrev = hash;
|
|
1374
|
+
await state?.save(hash);
|
|
1375
|
+
}
|
|
1376
|
+
await drain(ctx);
|
|
1377
|
+
}).catch((err) => {
|
|
1378
|
+
console.error("[evlog/audit] signed drain failed:", err);
|
|
1379
|
+
});
|
|
1380
|
+
return queue;
|
|
1381
|
+
};
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* @internal Resolve the Web Crypto SubtleCrypto interface. Available natively
|
|
1385
|
+
* in browsers, Node 19+, Bun, Deno, and Cloudflare Workers. Falls back to
|
|
1386
|
+
* Node's `webcrypto` for Node 18 (where `globalThis.crypto` is gated behind
|
|
1387
|
+
* a flag). The dynamic import keeps `node:crypto` out of browser bundles.
|
|
1388
|
+
*/
|
|
1389
|
+
async function getSubtle() {
|
|
1390
|
+
const c = globalThis.crypto;
|
|
1391
|
+
if (c?.subtle) return c.subtle;
|
|
1392
|
+
return (await import(
|
|
1393
|
+
/* @vite-ignore */
|
|
1394
|
+
"node:crypto"
|
|
1395
|
+
)).webcrypto.subtle;
|
|
1396
|
+
}
|
|
1397
|
+
function normalizeAlgo(algorithm) {
|
|
1398
|
+
switch (algorithm.toLowerCase()) {
|
|
1399
|
+
case "sha1":
|
|
1400
|
+
case "sha-1": return "SHA-1";
|
|
1401
|
+
case "sha256":
|
|
1402
|
+
case "sha-256": return "SHA-256";
|
|
1403
|
+
case "sha384":
|
|
1404
|
+
case "sha-384": return "SHA-384";
|
|
1405
|
+
case "sha512":
|
|
1406
|
+
case "sha-512": return "SHA-512";
|
|
1407
|
+
default: return "SHA-256";
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
function bufToHex(buf) {
|
|
1411
|
+
let out = "";
|
|
1412
|
+
for (const byte of new Uint8Array(buf)) out += byte.toString(16).padStart(2, "0");
|
|
1413
|
+
return out;
|
|
1414
|
+
}
|
|
1415
|
+
async function digestHex(algorithm, data) {
|
|
1416
|
+
return bufToHex(await (await getSubtle()).digest(normalizeAlgo(algorithm), new TextEncoder().encode(data)));
|
|
1417
|
+
}
|
|
1418
|
+
async function hmacHex(algorithm, secret, data) {
|
|
1419
|
+
const subtle = await getSubtle();
|
|
1420
|
+
const hash = normalizeAlgo(algorithm);
|
|
1421
|
+
const key = await subtle.importKey("raw", new TextEncoder().encode(secret), {
|
|
1422
|
+
name: "HMAC",
|
|
1423
|
+
hash
|
|
1424
|
+
}, false, ["sign"]);
|
|
1425
|
+
return bufToHex(await subtle.sign("HMAC", key, new TextEncoder().encode(data)));
|
|
1426
|
+
}
|
|
1427
|
+
/** @internal Strip integrity fields before hashing so signatures stay stable. */
|
|
1428
|
+
function stripIntegrity(event) {
|
|
1429
|
+
const a = event.audit;
|
|
1430
|
+
if (!a) return event;
|
|
1431
|
+
const { signature, prevHash, hash, ...rest } = a;
|
|
1432
|
+
return {
|
|
1433
|
+
...event,
|
|
1434
|
+
audit: rest
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
/**
|
|
1438
|
+
* Strict redact preset for audit events.
|
|
1439
|
+
*
|
|
1440
|
+
* Combine with the user's existing redact configuration via spread:
|
|
1441
|
+
* `initLogger({ redact: { paths: [...auditRedactPreset.paths!, ...mine] } })`.
|
|
1442
|
+
*
|
|
1443
|
+
* Hardens PII handling:
|
|
1444
|
+
* - Drops `Authorization` and `Cookie` headers anywhere they appear.
|
|
1445
|
+
* - Drops common credential field names (`password`, `passwordHash`, `token`,
|
|
1446
|
+
* `apiKey`, `secret`, `accessToken`, `refreshToken`, `cardNumber`, `cvv`,
|
|
1447
|
+
* `ssn`).
|
|
1448
|
+
*
|
|
1449
|
+
* Built-in pattern maskers (email, credit card, …) keep their default
|
|
1450
|
+
* behaviour — partial masking, not full redaction — so audit trails retain
|
|
1451
|
+
* enough signal to be useful.
|
|
1452
|
+
*/
|
|
1453
|
+
const auditRedactPreset = { paths: [
|
|
1454
|
+
"audit.changes.before.password",
|
|
1455
|
+
"audit.changes.before.passwordHash",
|
|
1456
|
+
"audit.changes.before.token",
|
|
1457
|
+
"audit.changes.before.apiKey",
|
|
1458
|
+
"audit.changes.before.secret",
|
|
1459
|
+
"audit.changes.before.accessToken",
|
|
1460
|
+
"audit.changes.before.refreshToken",
|
|
1461
|
+
"audit.changes.before.cardNumber",
|
|
1462
|
+
"audit.changes.before.cvv",
|
|
1463
|
+
"audit.changes.before.ssn",
|
|
1464
|
+
"audit.changes.after.password",
|
|
1465
|
+
"audit.changes.after.passwordHash",
|
|
1466
|
+
"audit.changes.after.token",
|
|
1467
|
+
"audit.changes.after.apiKey",
|
|
1468
|
+
"audit.changes.after.secret",
|
|
1469
|
+
"audit.changes.after.accessToken",
|
|
1470
|
+
"audit.changes.after.refreshToken",
|
|
1471
|
+
"audit.changes.after.cardNumber",
|
|
1472
|
+
"audit.changes.after.cvv",
|
|
1473
|
+
"audit.changes.after.ssn",
|
|
1474
|
+
"headers.authorization",
|
|
1475
|
+
"headers.cookie",
|
|
1476
|
+
"headers.set-cookie",
|
|
1477
|
+
"audit.context.headers.authorization",
|
|
1478
|
+
"audit.context.headers.cookie"
|
|
1479
|
+
] };
|
|
1480
|
+
//#endregion
|
|
1481
|
+
export { shouldKeep as C, resolveRedactConfig as E, lockLogger as S, redactEvent as T, getEnvironment as _, auditEnricher as a, isEnabled as b, buildAuditFields as c, signed as d, withAudit as f, createRequestLogger as g, createLogger as h, auditDiff as i, defineAuditAction as l, _log as m, AuditDeniedError as n, auditOnly as o, withAuditMethods as p, audit as r, auditRedactPreset as s, AUDIT_SCHEMA_VERSION as t, mockAudit as u, getGlobalDrain as v, normalizeRedactConfig as w, isLoggerLocked as x, initLogger as y };
|
|
740
1482
|
|
|
741
|
-
//# sourceMappingURL=
|
|
1483
|
+
//# sourceMappingURL=audit-DQoBo7Dl.mjs.map
|