ezlogs-nextjs 0.1.4 → 0.2.0
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 +74 -3
- package/bin/install-triggers.cjs +4 -0
- package/dist/actors/index.d.cts +1 -1
- package/dist/actors/index.d.ts +1 -1
- package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
- package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
- package/dist/db/drizzle.cjs +36 -8
- package/dist/db/drizzle.js +36 -8
- package/dist/db/prisma.cjs +101 -18
- package/dist/db/prisma.d.cts +37 -1
- package/dist/db/prisma.d.ts +37 -1
- package/dist/db/prisma.js +100 -19
- package/dist/db/supabase.cjs +36 -8
- package/dist/db/supabase.js +36 -8
- package/dist/index.cjs +485 -222
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +485 -222
- package/dist/jobs/bullmq.cjs +0 -4
- package/dist/jobs/bullmq.js +0 -4
- package/dist/jobs/inngest.cjs +0 -4
- package/dist/jobs/inngest.js +0 -4
- package/dist/jobs/supabase-queues.cjs +0 -4
- package/dist/jobs/supabase-queues.js +0 -4
- package/dist/jobs/trigger.cjs +0 -4
- package/dist/jobs/trigger.js +0 -4
- package/dist/jobs/upstash-workflow.cjs +0 -4
- package/dist/jobs/upstash-workflow.js +0 -4
- package/dist/triggers/index.cjs +192 -63
- package/dist/triggers/index.d.cts +30 -40
- package/dist/triggers/index.d.ts +30 -40
- package/dist/triggers/index.js +191 -61
- package/package.json +3 -1
- package/sql/postgres/00_install.sql +244 -21
package/README.md
CHANGED
|
@@ -115,9 +115,36 @@ npx ezlogs-nextjs install-triggers
|
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
Migration tables, sessions, and the agent's own audit table are
|
|
118
|
-
skipped automatically.
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
skipped automatically. The trigger path needs the `pg` package in your
|
|
119
|
+
project (`npm i pg`); if `pg` or the audit table is missing, the agent
|
|
120
|
+
logs one warning at startup and leaves the trigger path off. Without
|
|
121
|
+
triggers, DB writes are captured only if you wire a per-ORM adapter
|
|
122
|
+
(`ezlogsPrisma`, `ezlogsDrizzleLogger`, `wrapSupabase`) — see
|
|
123
|
+
[docs/database.md](./docs/database.md) for trade-offs.
|
|
124
|
+
|
|
125
|
+
**Recommended setup for Prisma** (plain `new PrismaClient()`, no driver
|
|
126
|
+
adapter): triggers **plus** the extension, which tags each write with
|
|
127
|
+
the request so DB changes join the right action and actor.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
// lib/prisma.ts
|
|
131
|
+
import { PrismaClient } from "@prisma/client";
|
|
132
|
+
import { ezlogsPrisma } from "ezlogs-nextjs/prisma";
|
|
133
|
+
|
|
134
|
+
export const prisma = new PrismaClient().$extends(ezlogsPrisma());
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Each write inside a captured request runs as
|
|
138
|
+
`$transaction([set_config(correlation id), your query])` (Prisma's
|
|
139
|
+
documented RLS-extension pattern); reads are untouched. Writes inside
|
|
140
|
+
your own `prisma.$transaction(...)` are not tagged: they still show up,
|
|
141
|
+
as separate actions without an actor. Without the extension, a plain
|
|
142
|
+
PrismaClient's writes are never tagged.
|
|
143
|
+
|
|
144
|
+
**Upgrading from 0.1.x:** run `npx ezlogs-nextjs install-triggers --tables=none`
|
|
145
|
+
**before** deploying the new agent (functions only, no table locks),
|
|
146
|
+
deploy right after, and later re-run `npx ezlogs-nextjs install-triggers`
|
|
147
|
+
at a quiet moment. Details in [CHANGELOG.md](./CHANGELOG.md).
|
|
121
148
|
|
|
122
149
|
---
|
|
123
150
|
|
|
@@ -171,6 +198,50 @@ The agent is designed to be **invisible** to your application.
|
|
|
171
198
|
Your application's reliability is more important than capturing every
|
|
172
199
|
event. EZLogs is best-effort, not guaranteed delivery.
|
|
173
200
|
|
|
201
|
+
## Security: what is captured and redacted
|
|
202
|
+
|
|
203
|
+
- **Request params / Server Action args.** Keys matching a sensitive
|
|
204
|
+
name are sent as `"[FILTERED]"`: password, token, secret, api key,
|
|
205
|
+
credential, auth, signature, card number / cvv, ssn, … (substring),
|
|
206
|
+
and the short words `pin`, `otp`, `totp`, `iban`, `mfa`,
|
|
207
|
+
`passphrase` (whole word, so `shipping` / `options` are kept).
|
|
208
|
+
camelCase keys are covered (`apiKey`, `mdmUnlockPin`). Add your own
|
|
209
|
+
fragments with `excludedGraphqlVariableKeys`.
|
|
210
|
+
- **Unnamed Server Action arguments** (`signIn(email, password)`) have
|
|
211
|
+
no key to judge by, so every positional string is sent as
|
|
212
|
+
`"[FILTERED]"` unless it has a strict generated-id shape (integer,
|
|
213
|
+
UUID, cuid, cuid2, ULID, nanoid — with digit / mixed-case checks, so
|
|
214
|
+
words like `inactive` stay masked). Numbers and booleans are kept,
|
|
215
|
+
except Luhn-valid card numbers (15-16 digits always, 13-19 for payment-named actions) and 4-8
|
|
216
|
+
digit values when the action name has a secret word (`setPin`,
|
|
217
|
+
`verifyOtp`, `addCard`). Residual risk: a PIN passed to an innocently
|
|
218
|
+
named action (`update(4821)`) is shown. Object / FormData arguments
|
|
219
|
+
keep their field names and get the key rule above — pass an object
|
|
220
|
+
if you want values shown.
|
|
221
|
+
- **Database rows (trigger path).** Columns with sensitive names are
|
|
222
|
+
removed by the trigger itself, so they are never written to
|
|
223
|
+
`ezlogs_audit_log`. An update that only touches such columns writes
|
|
224
|
+
no audit row.
|
|
225
|
+
- **Retention of `ezlogs_audit_log`.** Rows are deleted as soon as the
|
|
226
|
+
agent sends them. Rows nobody claimed (untagged writes, or tagged
|
|
227
|
+
rows older than 5 minutes) are sent as their own actions by a
|
|
228
|
+
throttled sweep. Rows older than 24 hours are deleted unsent. The
|
|
229
|
+
agent's role needs `DELETE` on the table; without it the agent warns
|
|
230
|
+
once and you must schedule
|
|
231
|
+
`DELETE FROM ezlogs_audit_log WHERE captured_at < now() - interval '24 hours'`.
|
|
232
|
+
- **Access to `ezlogs_audit_log`.** Row-level security is on and all
|
|
233
|
+
privileges are revoked from `PUBLIC` and Supabase's `anon` /
|
|
234
|
+
`authenticated`, so the table is not reachable through PostgREST.
|
|
235
|
+
The installing role (owner) and the trigger are unaffected; a reader
|
|
236
|
+
connecting as another role gets one startup warning with the GRANT +
|
|
237
|
+
CREATE POLICY it needs.
|
|
238
|
+
- **Correlation is never guessed.** A DB write is grouped with a
|
|
239
|
+
request only when it carries that request's id (set by the agent on
|
|
240
|
+
`pg` connections, or via the `x-correlation-id` header PostgREST
|
|
241
|
+
exposes for supabase-js). Untagged writes show up as their own
|
|
242
|
+
actions without an actor. An inbound `x-ezlogs-correlation-id` is
|
|
243
|
+
honored only in the agent's own format and if under an hour old.
|
|
244
|
+
|
|
174
245
|
## Wire-format parity
|
|
175
246
|
|
|
176
247
|
The Node agent emits byte-identical events to the Ruby agent
|
package/bin/install-triggers.cjs
CHANGED
|
@@ -56,6 +56,10 @@
|
|
|
56
56
|
|
|
57
57
|
let exitCode = 1;
|
|
58
58
|
try {
|
|
59
|
+
// A statement stuck behind a long transaction would make the app's
|
|
60
|
+
// writes queue behind it. Give up after 5s instead; re-running the
|
|
61
|
+
// (idempotent) install finishes the job.
|
|
62
|
+
await client.query("SET lock_timeout = '5s'");
|
|
59
63
|
exitCode = await runInstall(args, {
|
|
60
64
|
out: (l) => console.log(l),
|
|
61
65
|
exec: (sql) => client.query(sql),
|
package/dist/actors/index.d.cts
CHANGED
package/dist/actors/index.d.ts
CHANGED
|
@@ -178,39 +178,30 @@ interface InitOptions {
|
|
|
178
178
|
*/
|
|
179
179
|
patchDatabaseClient?: unknown;
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* is NULL and `id` is greater than the snapshot) — i.e. rows the
|
|
188
|
-
* trigger fired during this request even though the customer's
|
|
189
|
-
* client couldn't tag them.
|
|
190
|
-
*
|
|
191
|
-
* Default false. Enable for supabase-js apps. Adds one cheap
|
|
192
|
-
* `SELECT MAX(id)` per request. See AGENT_PARITY_NOTES.md for the
|
|
193
|
-
* concurrency caveat (concurrent same-actor mutations may blur).
|
|
181
|
+
* @deprecated Since 0.2.0 this option is ignored. The old time-window
|
|
182
|
+
* fallback attributed untagged audit rows to whichever request was
|
|
183
|
+
* open, which under concurrency stamped other users' writes with the
|
|
184
|
+
* wrong actor. Untagged rows are now reported as their own actions.
|
|
185
|
+
* supabase-js writes are tagged exactly via the request header (re-run
|
|
186
|
+
* `install-triggers` after upgrading).
|
|
194
187
|
*/
|
|
195
188
|
databaseSnapshotMode?: boolean;
|
|
196
189
|
/**
|
|
197
190
|
* One-line install for the trigger-based capture. Pass your Postgres
|
|
198
191
|
* connection string; the agent does the rest:
|
|
199
192
|
* 1. Lazy-imports `pg` and constructs a Pool (4 connections, max).
|
|
200
|
-
* `pg` must be in your project's dependencies — the agent
|
|
201
|
-
*
|
|
193
|
+
* `pg` must be in your project's dependencies — the agent logs
|
|
194
|
+
* one warning at startup and skips the trigger path if it's
|
|
195
|
+
* missing, or if `ezlogs_audit_log` is not installed.
|
|
202
196
|
* 2. Wires that Pool as `databaseReader` for the audit-log drain.
|
|
203
197
|
* 3. Patches `pg.Client.prototype.query` so every customer query
|
|
204
198
|
* auto-injects SET LOCAL. Drizzle (node-postgres) and Prisma
|
|
205
199
|
* (`@prisma/adapter-pg`) both run on `pg` and inherit zero-touch
|
|
206
200
|
* capture without any extra wiring on the customer's side.
|
|
207
|
-
* 4. Enables `databaseSnapshotMode` — the time-window fallback
|
|
208
|
-
* that catches supabase-js / PostgREST writes whose audit row
|
|
209
|
-
* didn't get our SET LOCAL.
|
|
210
201
|
*
|
|
211
202
|
* Customers who want to override individual pieces can still pass
|
|
212
|
-
* `databaseReader` / `patchDatabaseClient`
|
|
213
|
-
*
|
|
203
|
+
* `databaseReader` / `patchDatabaseClient` directly; explicit values
|
|
204
|
+
* win over the auto-wired pieces.
|
|
214
205
|
*/
|
|
215
206
|
databaseUrl?: string;
|
|
216
207
|
logLevel?: LogLevel;
|
|
@@ -178,39 +178,30 @@ interface InitOptions {
|
|
|
178
178
|
*/
|
|
179
179
|
patchDatabaseClient?: unknown;
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* is NULL and `id` is greater than the snapshot) — i.e. rows the
|
|
188
|
-
* trigger fired during this request even though the customer's
|
|
189
|
-
* client couldn't tag them.
|
|
190
|
-
*
|
|
191
|
-
* Default false. Enable for supabase-js apps. Adds one cheap
|
|
192
|
-
* `SELECT MAX(id)` per request. See AGENT_PARITY_NOTES.md for the
|
|
193
|
-
* concurrency caveat (concurrent same-actor mutations may blur).
|
|
181
|
+
* @deprecated Since 0.2.0 this option is ignored. The old time-window
|
|
182
|
+
* fallback attributed untagged audit rows to whichever request was
|
|
183
|
+
* open, which under concurrency stamped other users' writes with the
|
|
184
|
+
* wrong actor. Untagged rows are now reported as their own actions.
|
|
185
|
+
* supabase-js writes are tagged exactly via the request header (re-run
|
|
186
|
+
* `install-triggers` after upgrading).
|
|
194
187
|
*/
|
|
195
188
|
databaseSnapshotMode?: boolean;
|
|
196
189
|
/**
|
|
197
190
|
* One-line install for the trigger-based capture. Pass your Postgres
|
|
198
191
|
* connection string; the agent does the rest:
|
|
199
192
|
* 1. Lazy-imports `pg` and constructs a Pool (4 connections, max).
|
|
200
|
-
* `pg` must be in your project's dependencies — the agent
|
|
201
|
-
*
|
|
193
|
+
* `pg` must be in your project's dependencies — the agent logs
|
|
194
|
+
* one warning at startup and skips the trigger path if it's
|
|
195
|
+
* missing, or if `ezlogs_audit_log` is not installed.
|
|
202
196
|
* 2. Wires that Pool as `databaseReader` for the audit-log drain.
|
|
203
197
|
* 3. Patches `pg.Client.prototype.query` so every customer query
|
|
204
198
|
* auto-injects SET LOCAL. Drizzle (node-postgres) and Prisma
|
|
205
199
|
* (`@prisma/adapter-pg`) both run on `pg` and inherit zero-touch
|
|
206
200
|
* capture without any extra wiring on the customer's side.
|
|
207
|
-
* 4. Enables `databaseSnapshotMode` — the time-window fallback
|
|
208
|
-
* that catches supabase-js / PostgREST writes whose audit row
|
|
209
|
-
* didn't get our SET LOCAL.
|
|
210
201
|
*
|
|
211
202
|
* Customers who want to override individual pieces can still pass
|
|
212
|
-
* `databaseReader` / `patchDatabaseClient`
|
|
213
|
-
*
|
|
203
|
+
* `databaseReader` / `patchDatabaseClient` directly; explicit values
|
|
204
|
+
* win over the auto-wired pieces.
|
|
214
205
|
*/
|
|
215
206
|
databaseUrl?: string;
|
|
216
207
|
logLevel?: LogLevel;
|
package/dist/db/drizzle.cjs
CHANGED
|
@@ -111,7 +111,6 @@ var Configuration = class {
|
|
|
111
111
|
displayNameFor = {};
|
|
112
112
|
isServerActionError = null;
|
|
113
113
|
databaseReader = null;
|
|
114
|
-
databaseSnapshotMode = false;
|
|
115
114
|
databaseUrl = null;
|
|
116
115
|
/**
|
|
117
116
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -163,9 +162,6 @@ var Configuration = class {
|
|
|
163
162
|
if (options.databaseReader !== void 0) {
|
|
164
163
|
this.databaseReader = options.databaseReader;
|
|
165
164
|
}
|
|
166
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
167
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
168
|
-
}
|
|
169
165
|
if (options.databaseUrl !== void 0) {
|
|
170
166
|
this.databaseUrl = options.databaseUrl;
|
|
171
167
|
}
|
|
@@ -493,6 +489,26 @@ var storage2 = createScopedStorage(
|
|
|
493
489
|
function getCurrentActor() {
|
|
494
490
|
return storage2.read();
|
|
495
491
|
}
|
|
492
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
493
|
+
"pin",
|
|
494
|
+
"otp",
|
|
495
|
+
"totp",
|
|
496
|
+
"iban",
|
|
497
|
+
"mfa",
|
|
498
|
+
"passphrase"
|
|
499
|
+
]);
|
|
500
|
+
function snakeLower(key) {
|
|
501
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
502
|
+
}
|
|
503
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
504
|
+
const lower = key.toLowerCase();
|
|
505
|
+
const snake = snakeLower(key);
|
|
506
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
510
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
511
|
+
}
|
|
496
512
|
|
|
497
513
|
// src/db/shared.ts
|
|
498
514
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -525,7 +541,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
525
541
|
"salt",
|
|
526
542
|
"digest",
|
|
527
543
|
"signature",
|
|
528
|
-
"hmac"
|
|
544
|
+
"hmac",
|
|
545
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
546
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
547
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
548
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
549
|
+
"credential",
|
|
550
|
+
"passwd",
|
|
551
|
+
"passcode",
|
|
552
|
+
"apikey",
|
|
553
|
+
"card_number",
|
|
554
|
+
"cardnumber",
|
|
555
|
+
"cvv",
|
|
556
|
+
"cvc"
|
|
529
557
|
]);
|
|
530
558
|
function emitDbEvent(input) {
|
|
531
559
|
try {
|
|
@@ -552,7 +580,8 @@ function emitDbEvent(input) {
|
|
|
552
580
|
correlation_id: current(),
|
|
553
581
|
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
554
582
|
context,
|
|
555
|
-
duration_ms: null
|
|
583
|
+
duration_ms: null,
|
|
584
|
+
timestamp: input.timestamp ?? null
|
|
556
585
|
});
|
|
557
586
|
pushEvent(event);
|
|
558
587
|
} catch (error) {
|
|
@@ -610,8 +639,7 @@ function isMeaningfulAttribute(name) {
|
|
|
610
639
|
return true;
|
|
611
640
|
}
|
|
612
641
|
function isDbSensitive(name) {
|
|
613
|
-
|
|
614
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
642
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
615
643
|
}
|
|
616
644
|
function isScalar(value) {
|
|
617
645
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
package/dist/db/drizzle.js
CHANGED
|
@@ -109,7 +109,6 @@ var Configuration = class {
|
|
|
109
109
|
displayNameFor = {};
|
|
110
110
|
isServerActionError = null;
|
|
111
111
|
databaseReader = null;
|
|
112
|
-
databaseSnapshotMode = false;
|
|
113
112
|
databaseUrl = null;
|
|
114
113
|
/**
|
|
115
114
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -161,9 +160,6 @@ var Configuration = class {
|
|
|
161
160
|
if (options.databaseReader !== void 0) {
|
|
162
161
|
this.databaseReader = options.databaseReader;
|
|
163
162
|
}
|
|
164
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
165
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
166
|
-
}
|
|
167
163
|
if (options.databaseUrl !== void 0) {
|
|
168
164
|
this.databaseUrl = options.databaseUrl;
|
|
169
165
|
}
|
|
@@ -491,6 +487,26 @@ var storage2 = createScopedStorage(
|
|
|
491
487
|
function getCurrentActor() {
|
|
492
488
|
return storage2.read();
|
|
493
489
|
}
|
|
490
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
491
|
+
"pin",
|
|
492
|
+
"otp",
|
|
493
|
+
"totp",
|
|
494
|
+
"iban",
|
|
495
|
+
"mfa",
|
|
496
|
+
"passphrase"
|
|
497
|
+
]);
|
|
498
|
+
function snakeLower(key) {
|
|
499
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
500
|
+
}
|
|
501
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
502
|
+
const lower = key.toLowerCase();
|
|
503
|
+
const snake = snakeLower(key);
|
|
504
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
508
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
509
|
+
}
|
|
494
510
|
|
|
495
511
|
// src/db/shared.ts
|
|
496
512
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -523,7 +539,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
523
539
|
"salt",
|
|
524
540
|
"digest",
|
|
525
541
|
"signature",
|
|
526
|
-
"hmac"
|
|
542
|
+
"hmac",
|
|
543
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
544
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
545
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
546
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
547
|
+
"credential",
|
|
548
|
+
"passwd",
|
|
549
|
+
"passcode",
|
|
550
|
+
"apikey",
|
|
551
|
+
"card_number",
|
|
552
|
+
"cardnumber",
|
|
553
|
+
"cvv",
|
|
554
|
+
"cvc"
|
|
527
555
|
]);
|
|
528
556
|
function emitDbEvent(input) {
|
|
529
557
|
try {
|
|
@@ -550,7 +578,8 @@ function emitDbEvent(input) {
|
|
|
550
578
|
correlation_id: current(),
|
|
551
579
|
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
552
580
|
context,
|
|
553
|
-
duration_ms: null
|
|
581
|
+
duration_ms: null,
|
|
582
|
+
timestamp: input.timestamp ?? null
|
|
554
583
|
});
|
|
555
584
|
pushEvent(event);
|
|
556
585
|
} catch (error) {
|
|
@@ -608,8 +637,7 @@ function isMeaningfulAttribute(name) {
|
|
|
608
637
|
return true;
|
|
609
638
|
}
|
|
610
639
|
function isDbSensitive(name) {
|
|
611
|
-
|
|
612
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
640
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
613
641
|
}
|
|
614
642
|
function isScalar(value) {
|
|
615
643
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
package/dist/db/prisma.cjs
CHANGED
|
@@ -122,6 +122,14 @@ function createScopedStorage(key = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:def
|
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
// src/correlation.ts
|
|
126
|
+
var storage = createScopedStorage(
|
|
127
|
+
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
128
|
+
);
|
|
129
|
+
function current() {
|
|
130
|
+
return storage.read();
|
|
131
|
+
}
|
|
132
|
+
|
|
125
133
|
// src/configuration.ts
|
|
126
134
|
var DEFAULT_EXCLUDED_PATHS = Object.freeze([
|
|
127
135
|
// Next.js / Vercel infrastructure noise
|
|
@@ -191,7 +199,6 @@ var Configuration = class {
|
|
|
191
199
|
displayNameFor = {};
|
|
192
200
|
isServerActionError = null;
|
|
193
201
|
databaseReader = null;
|
|
194
|
-
databaseSnapshotMode = false;
|
|
195
202
|
databaseUrl = null;
|
|
196
203
|
/**
|
|
197
204
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -243,9 +250,6 @@ var Configuration = class {
|
|
|
243
250
|
if (options.databaseReader !== void 0) {
|
|
244
251
|
this.databaseReader = options.databaseReader;
|
|
245
252
|
}
|
|
246
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
247
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
248
|
-
}
|
|
249
253
|
if (options.databaseUrl !== void 0) {
|
|
250
254
|
this.databaseUrl = options.databaseUrl;
|
|
251
255
|
}
|
|
@@ -488,14 +492,6 @@ function describe2(error) {
|
|
|
488
492
|
return String(error);
|
|
489
493
|
}
|
|
490
494
|
|
|
491
|
-
// src/correlation.ts
|
|
492
|
-
var storage = createScopedStorage(
|
|
493
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
494
|
-
);
|
|
495
|
-
function current() {
|
|
496
|
-
return storage.read();
|
|
497
|
-
}
|
|
498
|
-
|
|
499
495
|
// src/actor.ts
|
|
500
496
|
var storage2 = createScopedStorage(
|
|
501
497
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:actor-storage")
|
|
@@ -503,6 +499,26 @@ var storage2 = createScopedStorage(
|
|
|
503
499
|
function getCurrentActor() {
|
|
504
500
|
return storage2.read();
|
|
505
501
|
}
|
|
502
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
503
|
+
"pin",
|
|
504
|
+
"otp",
|
|
505
|
+
"totp",
|
|
506
|
+
"iban",
|
|
507
|
+
"mfa",
|
|
508
|
+
"passphrase"
|
|
509
|
+
]);
|
|
510
|
+
function snakeLower(key) {
|
|
511
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
512
|
+
}
|
|
513
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
514
|
+
const lower = key.toLowerCase();
|
|
515
|
+
const snake = snakeLower(key);
|
|
516
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
517
|
+
return true;
|
|
518
|
+
}
|
|
519
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
520
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
521
|
+
}
|
|
506
522
|
|
|
507
523
|
// src/db/shared.ts
|
|
508
524
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -535,7 +551,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
535
551
|
"salt",
|
|
536
552
|
"digest",
|
|
537
553
|
"signature",
|
|
538
|
-
"hmac"
|
|
554
|
+
"hmac",
|
|
555
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
556
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
557
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
558
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
559
|
+
"credential",
|
|
560
|
+
"passwd",
|
|
561
|
+
"passcode",
|
|
562
|
+
"apikey",
|
|
563
|
+
"card_number",
|
|
564
|
+
"cardnumber",
|
|
565
|
+
"cvv",
|
|
566
|
+
"cvc"
|
|
539
567
|
]);
|
|
540
568
|
function emitDbEvent(input) {
|
|
541
569
|
try {
|
|
@@ -562,7 +590,8 @@ function emitDbEvent(input) {
|
|
|
562
590
|
correlation_id: current(),
|
|
563
591
|
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
564
592
|
context,
|
|
565
|
-
duration_ms: null
|
|
593
|
+
duration_ms: null,
|
|
594
|
+
timestamp: input.timestamp ?? null
|
|
566
595
|
});
|
|
567
596
|
pushEvent(event);
|
|
568
597
|
} catch (error) {
|
|
@@ -620,8 +649,7 @@ function isMeaningfulAttribute(name) {
|
|
|
620
649
|
return true;
|
|
621
650
|
}
|
|
622
651
|
function isDbSensitive(name) {
|
|
623
|
-
|
|
624
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
652
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
625
653
|
}
|
|
626
654
|
function isScalar(value) {
|
|
627
655
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
|
@@ -715,6 +743,9 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
715
743
|
deleteMany: "destroy"
|
|
716
744
|
};
|
|
717
745
|
function ezlogsPrisma() {
|
|
746
|
+
return (client) => client.$extends(buildPrismaExtension(client));
|
|
747
|
+
}
|
|
748
|
+
function buildPrismaExtension(client) {
|
|
718
749
|
return {
|
|
719
750
|
name: "ezlogs",
|
|
720
751
|
query: {
|
|
@@ -723,7 +754,7 @@ function ezlogsPrisma() {
|
|
|
723
754
|
if (!CAPTURED_OPERATIONS.has(ctx.operation)) {
|
|
724
755
|
return ctx.query(ctx.args);
|
|
725
756
|
}
|
|
726
|
-
const result = await
|
|
757
|
+
const result = await runTagged(client, ctx);
|
|
727
758
|
try {
|
|
728
759
|
recordPrismaOperation(ctx, result);
|
|
729
760
|
} catch (error) {
|
|
@@ -731,10 +762,60 @@ function ezlogsPrisma() {
|
|
|
731
762
|
}
|
|
732
763
|
return result;
|
|
733
764
|
}
|
|
765
|
+
},
|
|
766
|
+
// Raw writes (`$executeRaw\`UPDATE …\``) aren't model operations.
|
|
767
|
+
async $allOperations(ctx) {
|
|
768
|
+
if (ctx.operation !== "$executeRaw" && ctx.operation !== "$executeRawUnsafe") {
|
|
769
|
+
return ctx.query(ctx.args);
|
|
770
|
+
}
|
|
771
|
+
return runTagged(client, ctx);
|
|
734
772
|
}
|
|
735
773
|
}
|
|
736
774
|
};
|
|
737
775
|
}
|
|
776
|
+
var SET_CORRELATION_SQL = "SELECT set_config('ezlogs.correlation_id', $1, TRUE)";
|
|
777
|
+
async function runTagged(client, ctx) {
|
|
778
|
+
if (!shouldTag(client, ctx)) return ctx.query(ctx.args);
|
|
779
|
+
try {
|
|
780
|
+
const results = await client.$transaction([
|
|
781
|
+
client.$executeRawUnsafe(SET_CORRELATION_SQL, current()),
|
|
782
|
+
ctx.query(ctx.args)
|
|
783
|
+
]);
|
|
784
|
+
return results[1];
|
|
785
|
+
} catch (error) {
|
|
786
|
+
if (!isTransactionUnsupported(error)) throw error;
|
|
787
|
+
taggingDisabled.add(client);
|
|
788
|
+
logger.warn(
|
|
789
|
+
`this Prisma client can't run transactions, so DB writes won't be linked to their request (they still show up as their own actions): ${describeError2(error)}`
|
|
790
|
+
);
|
|
791
|
+
return ctx.query(ctx.args);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
var taggingDisabled = /* @__PURE__ */ new WeakSet();
|
|
795
|
+
function _resetPrismaTaggingForTests() {
|
|
796
|
+
taggingDisabled = /* @__PURE__ */ new WeakSet();
|
|
797
|
+
}
|
|
798
|
+
function shouldTag(client, ctx) {
|
|
799
|
+
if (!client || taggingDisabled.has(client)) return false;
|
|
800
|
+
if (configuration().databaseReader == null) return false;
|
|
801
|
+
if (!isPostgresClient(client)) return false;
|
|
802
|
+
if (typeof client.$transaction !== "function" || typeof client.$executeRawUnsafe !== "function") {
|
|
803
|
+
return false;
|
|
804
|
+
}
|
|
805
|
+
if (!current()) return false;
|
|
806
|
+
const params = ctx.__internalParams;
|
|
807
|
+
return params != null && !params.transaction;
|
|
808
|
+
}
|
|
809
|
+
function isPostgresClient(client) {
|
|
810
|
+
const c = client;
|
|
811
|
+
const provider = c._activeProvider ?? c._engineConfig?.activeProvider;
|
|
812
|
+
if (typeof provider !== "string") return true;
|
|
813
|
+
return provider === "postgresql" || provider === "postgres";
|
|
814
|
+
}
|
|
815
|
+
function isTransactionUnsupported(error) {
|
|
816
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
817
|
+
return /transaction/i.test(message) && /not (?:be )?supported|unsupported|not available/i.test(message);
|
|
818
|
+
}
|
|
738
819
|
function recordPrismaOperation(ctx, result) {
|
|
739
820
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
740
821
|
if (!dbOperation) return;
|
|
@@ -923,7 +1004,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
923
1004
|
return prisma;
|
|
924
1005
|
}
|
|
925
1006
|
try {
|
|
926
|
-
return client.$extends(
|
|
1007
|
+
return client.$extends(buildPrismaExtension(null));
|
|
927
1008
|
} catch (error) {
|
|
928
1009
|
logger.debug(`patchPrismaClient: $extends failed: ${describeError2(error)}`);
|
|
929
1010
|
return prisma;
|
|
@@ -1016,6 +1097,8 @@ function isLikelyModelDelegate(value) {
|
|
|
1016
1097
|
return typeof candidate.create === "function" && typeof candidate.findMany === "function";
|
|
1017
1098
|
}
|
|
1018
1099
|
|
|
1100
|
+
exports._resetPrismaTaggingForTests = _resetPrismaTaggingForTests;
|
|
1101
|
+
exports.buildPrismaExtension = buildPrismaExtension;
|
|
1019
1102
|
exports.ezlogsPrisma = ezlogsPrisma;
|
|
1020
1103
|
exports.patchPrismaClient = patchPrismaClient;
|
|
1021
1104
|
exports.patchPrismaPgAdapter = patchPrismaPgAdapter;
|
package/dist/db/prisma.d.cts
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
|
+
/** The parts of a PrismaClient the correlation tagging needs. */
|
|
2
|
+
interface TaggingClient {
|
|
3
|
+
$extends: (ext: unknown) => unknown;
|
|
4
|
+
$transaction: (queries: unknown[]) => Promise<unknown[]>;
|
|
5
|
+
$executeRawUnsafe: (sql: string, ...values: unknown[]) => unknown;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Public factory for `prisma.$extends(ezlogsPrisma())`.
|
|
9
|
+
*
|
|
10
|
+
* Returns Prisma's callback-form extension (what
|
|
11
|
+
* `Prisma.defineExtension((client) => …)` returns) so the hook can
|
|
12
|
+
* reach the client for correlation tagging: every write made inside a
|
|
13
|
+
* captured request runs as
|
|
14
|
+
*
|
|
15
|
+
* client.$transaction([
|
|
16
|
+
* client.$executeRawUnsafe("SELECT set_config('ezlogs.correlation_id', $1, TRUE)", id),
|
|
17
|
+
* query(args),
|
|
18
|
+
* ])
|
|
19
|
+
*
|
|
20
|
+
* — Prisma's documented RLS client-extension pattern. The audit
|
|
21
|
+
* trigger reads that setting, so rows from a plain `PrismaClient`
|
|
22
|
+
* (default engine, no driver adapter) join the right request. Reads
|
|
23
|
+
* pass through with no extra query.
|
|
24
|
+
*
|
|
25
|
+
* Not tagged (rows ship as their own actions, never misattributed):
|
|
26
|
+
* writes inside a user `$transaction` (batch or interactive) — Prisma
|
|
27
|
+
* can't nest a transaction there.
|
|
28
|
+
*/
|
|
1
29
|
declare function ezlogsPrisma(): unknown;
|
|
30
|
+
/**
|
|
31
|
+
* The extension descriptor. `client` null = capture only, no tagging
|
|
32
|
+
* (the adapter-pg path tags via the patched `pg` driver instead).
|
|
33
|
+
* @internal exported for tests.
|
|
34
|
+
*/
|
|
35
|
+
declare function buildPrismaExtension(client: TaggingClient | null): unknown;
|
|
36
|
+
/** @internal test-only */
|
|
37
|
+
declare function _resetPrismaTaggingForTests(): void;
|
|
2
38
|
/**
|
|
3
39
|
* Wrap a `PrismaPg` adapter so its queryRaw/executeRaw/transactionContext
|
|
4
40
|
* methods run inside a snapshot the partnered `patchPrismaClient` deposits.
|
|
@@ -37,4 +73,4 @@ declare function patchPrismaPgAdapter<A extends object>(adapter: A): A;
|
|
|
37
73
|
*/
|
|
38
74
|
declare function patchPrismaClient<T extends object>(prisma: T, adapter?: object): T;
|
|
39
75
|
|
|
40
|
-
export { ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|
|
76
|
+
export { _resetPrismaTaggingForTests, buildPrismaExtension, ezlogsPrisma, patchPrismaClient, patchPrismaPgAdapter };
|