Skip to content
NEWMarketplace and your team: sell what you build, and get plans and changes that are tested before you see them. See what is new

Library / Security & Compliance

Security & Compliance

Write tamper-evident append-only audit logs to Supabase Postgres

Runs on manual trigger and records each event as a sequenced, hashed row in a Supabase audit_log table so changes stay traceable.

What it does

## SQL (run once in the Supabase SQL Editor) sql create extension if not exists pgcrypto with schema extensions; create table public.audit_log ( seq bigint primary key, ts timestamptz not null, event_type text not null, action text not null, status text not null check (status in ('success','error','pending','rejected')), actor text, actor_type text not null check (actor_type in ('human','system','ai_agent')), reason text, workflow_id text, workflow_name text, workflow_version text, execution_id text, correlation_id text, details jsonb, payload_hash text, schema_version int not null default 1, prev_hash text not null, hash text not null ); create or replace function public.audit_hash(r public.audit_log) returns text language sql immutable set search_path = '' as $$ select encode(extensions.digest(concat_ws('|', r.prev_hash, r.seq::text, to_char(r.ts at time zone 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US'), r.event_type, r.action, r.status, coalesce(r.actor,''), r.actor_type, coalesce(r.reason,''), coalesce(r.workflow_id,''), coalesce(r.workflow_name,''), coalesce(r.workflow_version,''), coalesce(r.execution_id,''), coalesce(r.correlation_id,''), coalesce(r.details::text,''), coalesce(r.payload_hash,''), r.schema_version::text), 'sha256'), 'hex'); $$; create or replace function public.audit_log_chain() returns trigger language plpgsql set search_path = '' as $$ declare last_row public.audit_log; begin perform pg_advisory_xact_lock(hashtext('audit_log_chain')); select * into last_row from public.audit_log order by seq desc limit 1; new.seq := coalesce(last_row.seq, 0) + 1; new.ts := clock_timestamp(); new.prev_hash := coalesce(last_row.hash, 'GENESIS'); new.hash := public.audit_hash(new); return new; end $$; create trigger trg_audit_chain before insert on public.audit_log for each row execute function public.audit_log_chain(); create or replace function public.audit_log_block() returns trigger language plpgsql set search_path = '' as $$ begin raise exception 'audit_log is append-only'; end $$; create trigger trg_audit_no_change before update or delete on public.audit_log for each row execute function public.audit_log_block(); create trigger trg_audit_no_truncate before truncate on public.audit_log for each statement execute function public.audit_log_block(); alter table public.audit_log enable row level security; revoke all on public.audit_log from anon, authenticated;

The steps

  1. Run demo
  2. Set demo configuration
  3. Create demo events
  4. Hash original payload
  5. Receive audit event
  6. Validate audit event
  7. Write audit event
  8. Check if run came from demo
  9. Verify hash chain
  10. Check if chain is intact
  11. Report chain intact
  12. Report chain broken
  13. Try to UPDATE a record
  14. Try to DELETE a record

21 steps in total.

Similar automations