ezlogs-nextjs 0.1.4 → 0.3.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.
Files changed (40) hide show
  1. package/README.md +106 -11
  2. package/bin/install-triggers.cjs +49 -11
  3. package/dist/actors/index.d.cts +1 -1
  4. package/dist/actors/index.d.ts +1 -1
  5. package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
  6. package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
  7. package/dist/db/drizzle.cjs +154 -18
  8. package/dist/db/drizzle.js +154 -18
  9. package/dist/db/prisma.cjs +242 -45
  10. package/dist/db/prisma.d.cts +39 -1
  11. package/dist/db/prisma.d.ts +39 -1
  12. package/dist/db/prisma.js +241 -46
  13. package/dist/db/supabase.cjs +154 -18
  14. package/dist/db/supabase.js +154 -18
  15. package/dist/index.cjs +761 -245
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.js +761 -245
  19. package/dist/jobs/bullmq.cjs +973 -16
  20. package/dist/jobs/bullmq.d.cts +8 -3
  21. package/dist/jobs/bullmq.d.ts +8 -3
  22. package/dist/jobs/bullmq.js +973 -16
  23. package/dist/jobs/inngest.cjs +880 -14
  24. package/dist/jobs/inngest.d.cts +7 -5
  25. package/dist/jobs/inngest.d.ts +7 -5
  26. package/dist/jobs/inngest.js +880 -14
  27. package/dist/jobs/supabase-queues.cjs +878 -16
  28. package/dist/jobs/supabase-queues.js +878 -16
  29. package/dist/jobs/trigger.cjs +914 -42
  30. package/dist/jobs/trigger.js +914 -42
  31. package/dist/jobs/upstash-workflow.cjs +914 -14
  32. package/dist/jobs/upstash-workflow.d.cts +16 -2
  33. package/dist/jobs/upstash-workflow.d.ts +16 -2
  34. package/dist/jobs/upstash-workflow.js +914 -15
  35. package/dist/triggers/index.cjs +405 -76
  36. package/dist/triggers/index.d.cts +36 -41
  37. package/dist/triggers/index.d.ts +36 -41
  38. package/dist/triggers/index.js +404 -74
  39. package/package.json +5 -1
  40. package/sql/postgres/00_install.sql +244 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ezlogs-nextjs",
3
- "version": "0.1.4",
3
+ "version": "0.3.0",
4
4
  "description": "EZLogs agent for Next.js — capture HTTP, database, and background-job events as plain-English Action cards.",
5
5
  "license": "MIT",
6
6
  "author": "EZLogs",
@@ -24,6 +24,7 @@
24
24
  "README.md"
25
25
  ],
26
26
  "bin": {
27
+ "ezlogs-nextjs": "./bin/install-triggers.cjs",
27
28
  "install-triggers": "./bin/install-triggers.cjs"
28
29
  },
29
30
  "exports": {
@@ -138,9 +139,12 @@
138
139
  }
139
140
  },
140
141
  "devDependencies": {
142
+ "@prisma/client": "^5.22.0",
141
143
  "@types/node": "^20.11.0",
142
144
  "@types/pg": "^8.20.0",
145
+ "bullmq": "^5.76.10",
143
146
  "pg": "^8.20.0",
147
+ "prisma": "^5.22.0",
144
148
  "sucrase": "^3.35.0",
145
149
  "tsup": "^8.0.2",
146
150
  "typescript": "^5.4.0",
@@ -21,7 +21,21 @@
21
21
  -- Avoids per-table schema sprawl (Supabase audit-log pattern).
22
22
  -- ---------------------------------------------------------------------
23
23
 
24
- CREATE TABLE IF NOT EXISTS ezlogs_audit_log (
24
+ -- One-time upgrade from 0.1.x: those agents never deleted rows, so the
25
+ -- table holds history they already sent. Emptying it stops the new
26
+ -- agent's sweep from re-sending that history (and makes the index
27
+ -- builds below instant). The explicit table comment set further down
28
+ -- marks a 0.2+ table, so a re-run never truncates live rows.
29
+ DO $$
30
+ BEGIN
31
+ IF to_regclass('public.ezlogs_audit_log') IS NOT NULL
32
+ AND COALESCE(obj_description(to_regclass('public.ezlogs_audit_log'), 'pg_class'), '')
33
+ NOT LIKE 'ezlogs:0.2%' THEN
34
+ TRUNCATE public.ezlogs_audit_log;
35
+ END IF;
36
+ END $$;
37
+
38
+ CREATE TABLE IF NOT EXISTS public.ezlogs_audit_log (
25
39
  id bigserial PRIMARY KEY,
26
40
  table_name text NOT NULL,
27
41
  op text NOT NULL CHECK (op IN ('INSERT','UPDATE','DELETE')),
@@ -43,16 +57,103 @@ CREATE TABLE IF NOT EXISTS ezlogs_audit_log (
43
57
  captured_at timestamptz NOT NULL DEFAULT clock_timestamp()
44
58
  );
45
59
 
60
+ COMMENT ON TABLE public.ezlogs_audit_log IS
61
+ 'ezlogs:0.2 — EZLogs audit log. Rows are deleted once sent. Do not edit this comment (upgrade marker).';
62
+
46
63
  -- Hot-path index: agent reader scans WHERE correlation_id = $current.
47
64
  -- Partial index keeps it small — most rows have correlation_id set.
48
65
  CREATE INDEX IF NOT EXISTS ezlogs_audit_log_correlation_idx
49
- ON ezlogs_audit_log (correlation_id, captured_at)
66
+ ON public.ezlogs_audit_log (correlation_id, captured_at)
50
67
  WHERE correlation_id IS NOT NULL;
51
68
 
52
- -- Secondary index for cleanup / retention queries
53
- -- (DELETE FROM ezlogs_audit_log WHERE captured_at < now() - interval '7 days').
69
+ -- Secondary index for the agent's retention sweep (rows older than
70
+ -- 5 minutes are drained, rows older than 24 hours are deleted).
54
71
  CREATE INDEX IF NOT EXISTS ezlogs_audit_log_captured_at_idx
55
- ON ezlogs_audit_log (captured_at);
72
+ ON public.ezlogs_audit_log (captured_at);
73
+
74
+ -- Rows with no correlation (writes from outside any captured request,
75
+ -- or from a client that couldn't tag them) are drained by the agent
76
+ -- as their own actions. This keeps that scan cheap.
77
+ CREATE INDEX IF NOT EXISTS ezlogs_audit_log_uncorrelated_idx
78
+ ON public.ezlogs_audit_log (id)
79
+ WHERE correlation_id IS NULL;
80
+
81
+ -- ---------------------------------------------------------------------
82
+ -- 1b. Helpers used by the trigger.
83
+ -- ---------------------------------------------------------------------
84
+
85
+ -- Sensitive column names never reach ezlogs_audit_log. Mirrors the
86
+ -- agent's DB_SENSITIVE_PATTERNS (substring) + SENSITIVE_WORD_PATTERNS
87
+ -- (whole word) in src/db/shared.ts / src/http/sanitize.ts. camelCase
88
+ -- keys are split into words first (`mdmUnlockPin` -> `mdm_unlock_pin`).
89
+ -- A parity test keeps the two lists identical.
90
+ CREATE OR REPLACE FUNCTION public.ezlogs_is_sensitive_key(k text) RETURNS boolean
91
+ LANGUAGE sql IMMUTABLE
92
+ AS $$
93
+ SELECT s ~ '(password|token|secret|api_key|credit_card|ssn|social_security|encrypted|private_key|public_key|signing_key|pem|cipher|nonce|salt|digest|signature|hmac|credential|passwd|passcode|apikey|card_number|cardnumber|cvv|cvc)'
94
+ OR lower(k) ~ '(password|token|secret|api_key|credit_card|ssn|social_security|encrypted|private_key|public_key|signing_key|pem|cipher|nonce|salt|digest|signature|hmac|credential|passwd|passcode|apikey|card_number|cardnumber|cvv|cvc)'
95
+ OR s ~ '(^|[^a-z0-9])(pin|otp|totp|iban|mfa|passphrase)([^a-z0-9]|$)'
96
+ FROM (SELECT lower(regexp_replace(k, '([a-z0-9])([A-Z])', '\1_\2', 'g')) AS s) AS words
97
+ $$;
98
+
99
+ -- Version of the pattern lists above. BUMP IT whenever they change:
100
+ -- triggers attached under another version ignore their precomputed
101
+ -- column list (per-key check instead) and are re-attached by the next
102
+ -- `install-triggers` run.
103
+ CREATE OR REPLACE FUNCTION public.ezlogs_patterns_version() RETURNS text
104
+ LANGUAGE sql IMMUTABLE
105
+ AS $$ SELECT 'p1' $$;
106
+
107
+ CREATE OR REPLACE FUNCTION public.ezlogs_strip_sensitive(r jsonb) RETURNS jsonb
108
+ LANGUAGE sql IMMUTABLE
109
+ AS $$
110
+ SELECT CASE WHEN r IS NULL THEN NULL ELSE COALESCE(
111
+ (SELECT jsonb_object_agg(key, value)
112
+ FROM jsonb_each(r)
113
+ WHERE NOT public.ezlogs_is_sensitive_key(key)),
114
+ '{}'::jsonb) END
115
+ $$;
116
+
117
+ -- Correlation id for the current statement:
118
+ -- 1. `ezlogs.correlation_id` GUC — set by the agent's patched `pg`
119
+ -- client / withDatabaseContext (direct connections).
120
+ -- 2. PostgREST's `request.headers` GUC — supabase-js requests carry
121
+ -- the agent's `x-correlation-id` header (injected into every
122
+ -- outgoing fetch made inside a captured request). Only the
123
+ -- agent's own id format is accepted, so arbitrary client headers
124
+ -- can't land rows in an unrelated action.
125
+ -- Returns NULL when neither is present: the agent then reports the
126
+ -- row as its own action instead of guessing which request made it.
127
+ CREATE OR REPLACE FUNCTION public.ezlogs_request_correlation_id() RETURNS text
128
+ LANGUAGE plpgsql STABLE
129
+ SET search_path = public, pg_temp
130
+ AS $$
131
+ DECLARE
132
+ cid text;
133
+ raw text;
134
+ hdrs jsonb;
135
+ BEGIN
136
+ -- NULLIF: once a session has read a custom GUC, unset reads return ''.
137
+ cid := NULLIF(current_setting('ezlogs.correlation_id', true), '');
138
+ IF cid IS NOT NULL THEN
139
+ RETURN cid;
140
+ END IF;
141
+ raw := NULLIF(current_setting('request.headers', true), '');
142
+ IF raw IS NULL THEN
143
+ RETURN NULL;
144
+ END IF;
145
+ BEGIN
146
+ hdrs := raw::jsonb;
147
+ EXCEPTION WHEN others THEN
148
+ RETURN NULL;
149
+ END;
150
+ cid := COALESCE(hdrs->>'x-ezlogs-correlation-id', hdrs->>'x-correlation-id');
151
+ IF cid ~ '^ezl_[0-9]{1,16}_[0-9a-f]{8}$' THEN
152
+ RETURN cid;
153
+ END IF;
154
+ RETURN NULL;
155
+ END;
156
+ $$;
56
157
 
57
158
  -- ---------------------------------------------------------------------
58
159
  -- 2. Generic trigger function — one function audits every table.
@@ -76,6 +177,10 @@ AS $$
76
177
  DECLARE
77
178
  resource_id_value text;
78
179
  pk_column_name text;
180
+ old_raw jsonb;
181
+ new_raw jsonb;
182
+ old_json jsonb;
183
+ new_json jsonb;
79
184
  BEGIN
80
185
  -- The PK column name is passed as a per-trigger argument by
81
186
  -- ezlogs_track(). Defaults to 'id' when missing (older installs)
@@ -84,13 +189,42 @@ BEGIN
84
189
  -- looked up here, so this is a constant-time read.
85
190
  pk_column_name := COALESCE(NULLIF(TG_ARGV[0], ''), 'id');
86
191
 
192
+ -- Serialize each row once; everything below reads these.
193
+ IF TG_OP <> 'INSERT' THEN
194
+ old_raw := to_jsonb(OLD);
195
+ END IF;
196
+ IF TG_OP <> 'DELETE' THEN
197
+ new_raw := to_jsonb(NEW);
198
+ END IF;
199
+
200
+ -- Sensitive columns are dropped HERE, so they are never stored in
201
+ -- ezlogs_audit_log (not only filtered later by the agent).
202
+ -- Fast path: ezlogs_track() precomputed the table's sensitive column
203
+ -- names (TG_ARGV[1]), its full column-name set in jsonb key order
204
+ -- (TG_ARGV[2]) and the pattern version (TG_ARGV[3]); removing a fixed
205
+ -- key list costs a few µs. If the column NAMES changed since then
206
+ -- (add / drop / rename), the patterns changed, or the trigger predates
207
+ -- 0.2.0, fall back to checking every key by name (~60µs per row image).
208
+ IF TG_NARGS >= 4
209
+ AND TG_ARGV[3] = public.ezlogs_patterns_version()
210
+ AND ARRAY(SELECT jsonb_object_keys(COALESCE(new_raw, old_raw))) = TG_ARGV[2]::text[] THEN
211
+ old_json := old_raw - TG_ARGV[1]::text[];
212
+ new_json := new_raw - TG_ARGV[1]::text[];
213
+ ELSE
214
+ old_json := public.ezlogs_strip_sensitive(old_raw);
215
+ new_json := public.ezlogs_strip_sensitive(new_raw);
216
+ END IF;
217
+
87
218
  -- Prefer NEW's PK value when present (INSERT/UPDATE), fall back to
88
- -- OLD's (DELETE). `to_jsonb(NEW)->>X` returns NULL when X doesn't
89
- -- exist or is itself NULL — both are fine for our use.
90
- resource_id_value := COALESCE(
91
- (to_jsonb(NEW)->>pk_column_name),
92
- (to_jsonb(OLD)->>pk_column_name)
93
- );
219
+ -- OLD's (DELETE). Read from the STRIPPED rows, so a PK named like a
220
+ -- secret (a `token` table keyed by the token) yields NULL, not the
221
+ -- secret. `->>X` returns NULL when X doesn't exist or is NULL.
222
+ resource_id_value := COALESCE(new_json->>pk_column_name, old_json->>pk_column_name);
223
+ -- Nothing visible changed (only sensitive columns, or a no-op
224
+ -- UPDATE): skip the row.
225
+ IF TG_OP = 'UPDATE' AND old_json = new_json THEN
226
+ RETURN NEW;
227
+ END IF;
94
228
 
95
229
  -- Schema-qualified for double-safety. With search_path pinned above
96
230
  -- the unqualified form would also work, but explicit-public removes
@@ -102,8 +236,8 @@ BEGIN
102
236
  actor_id, correlation_id
103
237
  ) VALUES (
104
238
  TG_TABLE_NAME, TG_OP,
105
- CASE WHEN TG_OP = 'INSERT' THEN NULL ELSE to_jsonb(OLD) END,
106
- CASE WHEN TG_OP = 'DELETE' THEN NULL ELSE to_jsonb(NEW) END,
239
+ old_json,
240
+ new_json,
107
241
  resource_id_value,
108
242
  -- `current_setting(_, true)` returns NULL when the GUC is unset
109
243
  -- (the second arg = missing_ok). Without `true`, an unsignaled
@@ -117,7 +251,7 @@ BEGIN
117
251
  -- treats both as "no actor" — coercing here means downstream
118
252
  -- (TS code, dashboard) only has to check one shape.
119
253
  NULLIF(current_setting('ezlogs.actor_id', true), ''),
120
- NULLIF(current_setting('ezlogs.correlation_id', true), '')
254
+ public.ezlogs_request_correlation_id()
121
255
  );
122
256
 
123
257
  -- AFTER triggers ignore the return value, but plpgsql requires one.
@@ -139,7 +273,13 @@ $$;
139
273
 
140
274
  -- ---------------------------------------------------------------------
141
275
  -- 3. ezlogs_track() — attach the trigger to a named table.
142
- -- Idempotent: drops + recreates so re-running is safe.
276
+ -- Idempotent: a table whose trigger already has the current arguments
277
+ -- (PK, column names, pattern version) is left alone, so re-running
278
+ -- install-triggers takes no table lock on it (DROP TRIGGER needs
279
+ -- ACCESS EXCLUSIVE). The trigger calls the function by name, so
280
+ -- CREATE OR REPLACE above already upgrades its behavior; an outdated
281
+ -- attachment (pre-0.2.0, schema change, new patterns) is re-created,
282
+ -- restoring the fast sensitive-column path.
143
283
  -- ---------------------------------------------------------------------
144
284
 
145
285
  CREATE OR REPLACE FUNCTION public.ezlogs_track(target_table text) RETURNS void
@@ -150,7 +290,10 @@ CREATE OR REPLACE FUNCTION public.ezlogs_track(target_table text) RETURNS void
150
290
  SET search_path = public, pg_temp
151
291
  AS $$
152
292
  DECLARE
153
- pk_column_name text;
293
+ pk_column_name text;
294
+ sensitive_columns text[];
295
+ all_columns text[];
296
+ trigger_args text;
154
297
  BEGIN
155
298
  -- Detect the table's single-column primary key so the trigger can
156
299
  -- populate `resource_id` correctly regardless of how the customer
@@ -172,18 +315,48 @@ BEGIN
172
315
  AND array_length(i.indkey, 1) = 1
173
316
  LIMIT 1;
174
317
 
318
+ -- Precompute for the trigger's fast path (see ezlogs_audit_trigger):
319
+ -- the sensitive column names, and every column name in jsonb key
320
+ -- order (what `to_jsonb(row)` keys come out as).
321
+ SELECT COALESCE(array_agg(a.attname::text)
322
+ FILTER (WHERE public.ezlogs_is_sensitive_key(a.attname)), '{}'),
323
+ ARRAY(SELECT jsonb_object_keys(jsonb_object_agg(a.attname, NULL::int)))
324
+ INTO sensitive_columns, all_columns
325
+ FROM pg_attribute a
326
+ WHERE a.attrelid = format('public.%I', target_table)::regclass
327
+ AND a.attnum > 0
328
+ AND NOT a.attisdropped;
329
+
330
+ trigger_args := format(
331
+ '%L, %L, %L, %L',
332
+ COALESCE(pk_column_name, ''),
333
+ sensitive_columns::text,
334
+ all_columns::text,
335
+ public.ezlogs_patterns_version()
336
+ );
337
+
338
+ -- Already attached with exactly these arguments (same PK, columns and
339
+ -- pattern version): leave it alone — no DROP TRIGGER, no table lock.
340
+ IF EXISTS (
341
+ SELECT 1 FROM pg_trigger t
342
+ WHERE t.tgname = 'ezlogs_audit'
343
+ AND t.tgrelid = format('public.%I', target_table)::regclass
344
+ AND position('ezlogs_audit_trigger(' || trigger_args || ')' IN pg_get_triggerdef(t.oid)) > 0
345
+ ) THEN
346
+ RETURN;
347
+ END IF;
348
+
175
349
  EXECUTE format(
176
350
  'DROP TRIGGER IF EXISTS ezlogs_audit ON %I', target_table
177
351
  );
178
- -- Pass the resolved PK column name as a literal trigger argument.
179
- -- format(%L) quotes safely; NULL → 'null'-like which the trigger's
180
- -- `COALESCE(NULLIF(TG_ARGV[0], ''), 'id')` falls back from to 'id'.
352
+ -- format(%L) quotes every argument safely. An empty PK argument makes
353
+ -- the trigger's `COALESCE(NULLIF(TG_ARGV[0], ''), 'id')` fall back to 'id'.
181
354
  EXECUTE format(
182
355
  'CREATE TRIGGER ezlogs_audit '
183
356
  'AFTER INSERT OR UPDATE OR DELETE ON %I '
184
- 'FOR EACH ROW EXECUTE FUNCTION public.ezlogs_audit_trigger(%L)',
357
+ 'FOR EACH ROW EXECUTE FUNCTION public.ezlogs_audit_trigger(%s)',
185
358
  target_table,
186
- COALESCE(pk_column_name, '')
359
+ trigger_args
187
360
  );
188
361
  END;
189
362
  $$;
@@ -203,3 +376,53 @@ BEGIN
203
376
  );
204
377
  END;
205
378
  $$;
379
+
380
+ -- ---------------------------------------------------------------------
381
+ -- 5. Lock the audit log down.
382
+ -- It holds row data from every tracked table. On Supabase the
383
+ -- `public` schema is exposed through PostgREST and `anon` /
384
+ -- `authenticated` get default grants on new tables, so without this
385
+ -- anyone with the anon key could read, forge or delete rows.
386
+ --
387
+ -- - RLS on, no policies: every non-owner role sees nothing. The
388
+ -- table owner (the role that ran this install — the agent's
389
+ -- reader normally connects as it) and the SECURITY DEFINER trigger
390
+ -- (runs as the function owner = same role) bypass RLS.
391
+ -- - Explicit revokes for PUBLIC and the Supabase API roles, on the
392
+ -- table, its sequence, ezlogs_track/untrack, and the trigger
393
+ -- function itself (EXECUTE is checked when a trigger is CREATED,
394
+ -- not when it fires: already-tracked tables keep auditing, but no
395
+ -- other role can attach it to its own table and forge rows).
396
+ -- A reader connecting as a DIFFERENT role needs:
397
+ -- GRANT SELECT, DELETE ON ezlogs_audit_log TO <role>;
398
+ -- CREATE POLICY ezlogs_reader ON ezlogs_audit_log TO <role>
399
+ -- USING (true);
400
+ -- ---------------------------------------------------------------------
401
+
402
+ -- Skip when already on: ALTER TABLE takes an ACCESS EXCLUSIVE lock.
403
+ DO $$
404
+ BEGIN
405
+ IF NOT (SELECT relrowsecurity FROM pg_class WHERE oid = 'public.ezlogs_audit_log'::regclass) THEN
406
+ ALTER TABLE public.ezlogs_audit_log ENABLE ROW LEVEL SECURITY;
407
+ END IF;
408
+ END $$;
409
+
410
+ DO $$
411
+ DECLARE
412
+ seq text := pg_get_serial_sequence('public.ezlogs_audit_log', 'id');
413
+ role text;
414
+ BEGIN
415
+ EXECUTE 'REVOKE ALL ON TABLE public.ezlogs_audit_log FROM PUBLIC';
416
+ EXECUTE format('REVOKE ALL ON SEQUENCE %s FROM PUBLIC', seq);
417
+ EXECUTE 'REVOKE EXECUTE ON FUNCTION public.ezlogs_track(text), public.ezlogs_untrack(text), public.ezlogs_audit_trigger() FROM PUBLIC';
418
+ FOREACH role IN ARRAY ARRAY['anon', 'authenticated'] LOOP
419
+ IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = role) THEN
420
+ EXECUTE format('REVOKE ALL ON TABLE public.ezlogs_audit_log FROM %I', role);
421
+ EXECUTE format('REVOKE ALL ON SEQUENCE %s FROM %I', seq, role);
422
+ EXECUTE format(
423
+ 'REVOKE EXECUTE ON FUNCTION public.ezlogs_track(text), public.ezlogs_untrack(text), public.ezlogs_audit_trigger() FROM %I',
424
+ role
425
+ );
426
+ END IF;
427
+ END LOOP;
428
+ END $$;