Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(api): tenant erasure crosses the audit-log immutability trigger (#1330)
beenuar2aa1265a631b
diff --git a/.github/workflows/audit-forgery-live.yml b/.github/workflows/audit-forgery-live.ymlindex 2c128d72b..0bef0bfb3 100644--- a/.github/workflows/audit-forgery-live.yml+++ b/.github/workflows/audit-forgery-live.yml@@ -103,6 +103,19 @@ jobs:DATABASE_URL: ${{ env.AUDIT_DSN }}run: python -m pytest ../../tests/isolation/test_audit_forgery_live.py -v --tb=short+ # The immutability suite ran in no workflow at all between #1231 and+ # here — a live suite attached to nothing grades nothing. It drives two+ # connections because the two refusal layers bind different roles: the+ # runtime role is refused by 117's privilege revoke, the owner only by+ # the trigger, and the tenant-erasure cases (issue #1330, ADR-0014)+ # must cross both.+ - name: Audit rows cannot be deleted or edited; tenant erasure is the one path through+ working-directory: services/api+ env:+ ISOLATION_AUDIT_DSN: ${{ env.AUDIT_DSN }}+ ISOLATION_AUDIT_OWNER_DSN: ${{ env.DATABASE_MIGRATION_URL }}+ run: python -m pytest ../../tests/isolation/test_audit_immutability_live.py -v --tb=short+# The negative control.## Reintroduce the vulnerability — trust the unverified Authorizationdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex eaf36de2a..b8f07ca37 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -31,6 +31,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- **Tenant deletion was impossible on any database holding audit rows**+ (#1330). `audit_log.tenant_id` has declared `ON DELETE CASCADE` since+ migration 004, and the same migration's immutability trigger refused every+ DELETE — a cascade is an ordinary DELETE on the child, so+ `DELETE FROM tenants` raised for any tenant that had ever written an audit+ row, and `tenant_deletion.py` (the GDPR erasure path) could never complete.+ The offboarding gate stayed green throughout because its test database held+ no audit rows. Migration `119` teaches the trigger the distinction+ migration `117` already used on its checkpoint table: the cascade runs+ after the parent is gone, a direct DELETE still finds its tenant and is+ refused (ADR-0014 records the trade — erasure costs the whole tenant,+ loudly, and selective tampering stays impossible at both the privilege and+ trigger layers). Proven by the acceptance tests the issue named, in+ `test_audit_immutability_live.py` — a suite that had been wired into **no+ workflow at all** since #1231 and now runs in `audit-forgery-live.yml`+ against real Postgres as the runtime role, with its teardown corrected to+ go through the two legal paths rather than around them.+- **A fix pass closed the gap between what the enterprise-identity surfacesclaimed and what they did.** Six items, each reproduced before it wasfixed. **White-label** reached the surfaces the docs listed: both reportdiff --git a/docs/audit/CLAIM_TO_GATE_MATRIX.md b/docs/audit/CLAIM_TO_GATE_MATRIX.mdindex 84ae5a8fe..0f5af74ce 100644--- a/docs/audit/CLAIM_TO_GATE_MATRIX.md+++ b/docs/audit/CLAIM_TO_GATE_MATRIX.md@@ -82,7 +82,7 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`| Detections can be backtested over historical lake data before promotion | README (detection engineering) | `ci.yml` api job (`test_backtest.py` — bounded/allow-listed SQL construction with injection-sanitised source filter, lake-row→event mapping with JSON payload expansion, exact would-fire count + hit-rate + samples; `test_backtest_gate.py` — opt-in `AISOC_BACKTEST_MAX_HIT_RATE` /decide gate) + web type-check (rule-editor backtest panel) + `isolation-live.yml` (`tests/isolation/test_backtest_lake_live.py` — the real `fetch_lake_events` against ClickHouse 23.8, table built from `clickhouse/001_init.sql` and rows seeded through the production `lake_writer.event_to_row`) | full | GATED (the gap this row used to name was wrong: nothing ran the live fetch, and the tenant-scoping path existed twice — `detection_rules.py` carried its own near-identical copy, now deleted, with a test asserting exactly one definition survives. The live path exposed a defect no unit could: the writer stores OCSF `raw_data` into a column named `raw_payload` and `rows_to_events` expanded neither, so the payload expansion its docstring promises had never run on a real lake row. `services/api/app/services/backtest.py` was also absent from the workflow's path filter, so a change to the function under test did not trigger the job) | — || Asking the affected person, then their manager, is a workflow a playbook branches on — not a message that is sent and forgotten | `services/agents/app/playbook/verification.py`; `playbooks/packs/v1/suspicious-signin/user-verification-containment.playbook.json`; depth plan 5.4 | `ci.yml :: python-lint` (`check_stateful_verification.py --self-test` then the gate: the answer is HMAC-signed over token, answer **and** stage — so a link minted for the subject stops verifying once the question escalates — compared with `compare_digest`; an unanswered prompt escalates to the manager and then resumes with `timed_out`, a value a playbook can read; the confirmed-true-positive refusal is checked **before** the opt-in, because that rule has no opt-out and the other order would let a tenant opt into messaging a compromised account; and `aisoc_verification_policies` has a writer, since a control that fails closed with no way to switch it on is a feature that reads as broken. Eleven injected violations, one per rule) + `ci.yml` agents job (`test_playbook_verification.py`, 20 cases driving the real engine end to end: ask, pause, signed answer, branch, approval — and the stale-link case, where the subject's own `denied` link is refused after the escalation and a manager-stage one is accepted) + `verification-live.yml` (`test_verification_pause_live.py`, 12 cases against real Postgres with every migration applied, because only a real transaction can show that the conditional UPDATEs claim a row once and that migration 102's CHECK admits all five outcomes the code writes) | core | GATED | — || The retention window a tenant chooses is the one the lake honours, and a legal hold outranks it | `services/api/app/services/retention.py`; `services/api/clickhouse/001_init.sql`; depth plan 4.3 | `ci.yml :: python-lint` (`check_retention_window.py --self-test` then the gate: the table TTL, the tiering DELETE and `MAX_LAKE_DAYS` are held to one number at or above 400 days, a `MODIFY TTL` migration exists so an existing volume converges — `001_init.sql` runs only on a fresh one — and the purge worker reads the holds in force, decides through `may_purge`, and reports what it withheld. Eight injected violations, one per rule) + `ci.yml` api job (`test_retention_legal_hold.py`, 13 cases driving the worker against recording doubles. **Half of them are the positive control**, because a purge stuck closed withholds everything on every deployment and passes every "did the hold work" assertion perfectly: a tenant with no hold is asserted to still lose its aged rows, and a hold on one tenant is asserted not to freeze another. Proven against the pre-fix worker, where five fail) | core | PARTIAL (the lake half is static: ClickHouse enforces a table TTL during merges and no test in CI watches a row survive past day 90 on a real server, so what is gated is that the three numbers agree and that the migration exists, not that ClickHouse honoured it. The purge half is gated against doubles rather than live Postgres and ClickHouse. And a hold currently stops the whole tenant's sweep rather than only its own subjects, because both purges are bulk statements — recorded as follow-up rather than approximated) | a live-store retention test |-| The audit retention window a tenant configures is enforced, and the surviving chain still verifies | `docs/decisions/0013-truncating-a-hash-chained-audit-log.md`; `services/api/app/workers/retention_purge.py`; depth plan 8.1 | `audit-retention-live.yml :: audit-retention-live` (`tests/isolation/test_audit_retention_live.py`, 12 cases against real Postgres as the DML-only `aisoc_app` role: the window removes the rows past it, the checkpoint agrees with the rows it describes from both sides, the remainder replays clean from the anchor, a direct DELETE and a hand-set purge flag are both refused by privilege, and the checkpoint is itself immutable. The job reads the DELETE privilege out of the catalogue, refuses a run that skipped a case, and carries a negative control that ignores the anchor and requires the suite to go red) | core | GATED | `retention_policies.audit_days` was storable since the table existed and purged by nothing, because `audit_log` is an append-only hash chain: delete the oldest rows and `verify_chain_breaks`, which replays from `prev_hash = NULL`, reports a mismatch at index 0 — an intact chain reads as tampered with, which is the worst outcome for a control whose only job is to tell those apart. Migration `111` adds `audit_retention_truncate`, a `SECURITY DEFINER` function that writes a checkpoint (the cutoff, the window, the count, the hash of the last row removed and the predecessor the oldest survivor names) and only then deletes, in one transaction; the verifier re-anchors on it. The application role holds **no DELETE on `audit_log`**, so the function is the only path rather than the intended one — the trigger's transaction-local setting is a second control, but a custom GUC can be set by anybody. **Does not unblock tenant deletion**: `audit_log`'s trigger from migration `101` refuses the cascade from `tenants` too, so `DELETE FROM tenants` raises for any tenant that has written a row. Pre-existing, tracked separately, and the checkpoint table deliberately does not repeat it |+| The audit retention window a tenant configures is enforced, and the surviving chain still verifies | `docs/decisions/0013-truncating-a-hash-chained-audit-log.md`; `services/api/app/workers/retention_purge.py`; depth plan 8.1 | `audit-retention-live.yml :: audit-retention-live` (`tests/isolation/test_audit_retention_live.py`, 12 cases against real Postgres as the DML-only `aisoc_app` role: the window removes the rows past it, the checkpoint agrees with the rows it describes from both sides, the remainder replays clean from the anchor, a direct DELETE and a hand-set purge flag are both refused by privilege, and the checkpoint is itself immutable. The job reads the DELETE privilege out of the catalogue, refuses a run that skipped a case, and carries a negative control that ignores the anchor and requires the suite to go red) | core | GATED | `retention_policies.audit_days` was storable since the table existed and purged by nothing, because `audit_log` is an append-only hash chain: delete the oldest rows and `verify_chain_breaks`, which replays from `prev_hash = NULL`, reports a mismatch at index 0 — an intact chain reads as tampered with, which is the worst outcome for a control whose only job is to tell those apart. Migration `111` adds `audit_retention_truncate`, a `SECURITY DEFINER` function that writes a checkpoint (the cutoff, the window, the count, the hash of the last row removed and the predecessor the oldest survivor names) and only then deletes, in one transaction; the verifier re-anchors on it. The application role holds **no DELETE on `audit_log`**, so the function is the only path rather than the intended one — the trigger's transaction-local setting is a second control, but a custom GUC can be set by anybody. Tenant deletion was blocked by the same trigger until migration `119` (issue #1330, ADR-0014) taught it to tell the cascade from `tenants` apart from a direct DELETE — the deliberate follow-up this row used to track, proven live by `audit-forgery-live.yml`'s immutability step || Detections can be authored in Python (`def rule(event)`) with a fixture gate | README (detection engineering) | `python-detections.yml` (`aisoc_detections.runner` fails a blind rule that misses a positive or a noisy one that fires on a negative; every bundled detection must ship ≥1 positive + ≥1 negative case) + `test_framework.py` (fail-closed evaluate, metadata validation) | core | GATED | - || AI Detection Builder: NL → rule + AUTO-generated fixtures → non-circular eval-gate → governed proposal | README (detection engineering) | `ci.yml` api job (`test_ai_detection_builder.py` — fixtures are DERIVED from the generated rule's own selection, not invented, and genuinely pass the non-circular gate: fires on the positive, quiet on the negative) | core | GATED | - || No-code / simple detection builder compiles to a valid rule | README (detection engineering) | `ci.yml` web type-check + lint (`SimpleRuleBuilder.tsx` `buildSigma` compiles field/operator/value rows to Sigma, dropped into the governed editor) | core | GATED | - |@@ -125,7 +125,7 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`| The agent's abstention rate and groundedness are published | `/metrics/funnel` | `ci.yml :: api tests` (`test_triage_quality_metrics.py` — asserts `mean_groundedness` averages only scored verdicts, so the deterministic path is not counted as zero, and that a missing column degrades rather than failing the funnel) | core | GATED | - || UEBA baselines non-human principals (service accounts, AI agents) | `apps/docs/docs/` (UEBA) | `ci.yml` ueba tests (`test_non_human_entities.py` — the entity-type list widened without becoming free text, and a zero-variance baseline returns None rather than a 0.0 that would read as permanently normal) | full | GATED | - || Envelope encryption: per-secret DEKs wrapped by a KEK in KMS | platform-threat-model.md (information disclosure) | `ci.yml` api job (`test_credential_vault_envelope.py`) — vault writes `vault:v2` when enabled, distinct DEK per secret, `vault:v1` rows still decrypt after the switch, a v2 row fails closed if envelope mode is later disabled, and an unrecognised mode refuses to boot rather than silently writing v1 | core | GATED | KMS backends beyond AWS are implemented against the same protocol but not integration-tested |-| Tenant offboarding erases data from every store | operations (data lifecycle) | `ci.yml` api job (`test_tenant_deletion.py`) — the Postgres table list is discovered from `information_schema` rather than hardcoded (14 of 72 tenant-scoped tables have no FK cascade, so a hardcoded list silently orphans them), the tenants row is deleted last, a failed satellite store rolls Postgres back and reports `complete: false`, and global MITRE reference nodes survive the graph purge | full | GATED | Neo4j MERGEs shared entities on `natural_key` with `tenant_id` as a property, so deleting by tenant can remove a node another tenant also referenced; it re-creates on their next ingest |+| Tenant offboarding erases data from every store | operations (data lifecycle) | `ci.yml` api job (`test_tenant_deletion.py`) — the Postgres table list is discovered from `information_schema` rather than hardcoded (14 of 72 tenant-scoped tables have no FK cascade, so a hardcoded list silently orphans them), the tenants row is deleted last, a failed satellite store rolls Postgres back and reports `complete: false`, and global MITRE reference nodes survive the graph purge; `audit-forgery-live.yml` immutability step (`test_audit_immutability_live.py::TestTenantErasureIsTheOnePathThrough`) proves the final `DELETE FROM tenants` completes **on real Postgres holding audit rows, as the runtime role** — the case the unit suite's empty audit table could never see, and the one that was broken until migration `119` (issue #1330) | full | GATED | Neo4j MERGEs shared entities on `natural_key` with `tenant_id` as a property, so deleting by tenant can remove a node another tenant also referenced; it re-creates on their next ingest || Per-tenant rate limiting on the public ingest inbox | operations (ingest) | `ci.yml` ingest job (`ratelimit_test.go`, run under `-race`) — separate request and event ceilings, one tenant cannot consume another's budget, a rejected batch consumes no quota, `Retry-After` always rounds up, and idle buckets are swept so the map cannot grow on an attacker-controlled dimension | core | GATED | limits are per replica (in-process), so the effective ceiling is rate x replicas; an exact global cap needs edge termination || A push to the ingest spine is authenticated and its tenant comes from the credential | README (connect real data), operations (ingest authentication) | `ci.yml` ingest job (`ingest_auth_test.go`) — two tenants are seeded and the outsider's own token is proved to write first, so the cross-tenant refusal cannot pass vacuously; `X-Tenant-ID` is intersected with the credential's scope rather than trusted, a service token with no declared tenant resolves to an empty scope and refuses instead of widening, a token minted for another template is refused, and a nil authenticator fails closed + `golden-pipeline.yml` (an unauthenticated push to the deployed service is refused, observed from outside) | core | GATED | - || Incident resolves to identity, asset, cloud, business and threat context | architecture (context graph) | `ci.yml` api job (`test_incident_context.py`) — the test parses the generated Cypher and asserts every matched variable is tenant-scoped or is a label holding no tenant data, that no variable-length pattern binds a global label (which would bridge tenants), that every dimension is row-capped, and that one failing or slow dimension neither hangs nor discards the others; plus `ci.yml` agents job (`test_bundle_incident_context.py`) for the bundle and prompt wiring | core | GATED | population of the new labels from connectors is separate work; the traversal returns empty until they are written |diff --git a/docs/decisions/0014-tenant-erasure-crosses-the-audit-trigger.md b/docs/decisions/0014-tenant-erasure-crosses-the-audit-trigger.mdnew file mode 100644index 000000000..f05febb7f--- /dev/null+++ b/docs/decisions/0014-tenant-erasure-crosses-the-audit-trigger.md@@ -0,0 +1,77 @@+# ADR-0014: tenant erasure crosses the audit-log immutability trigger; nothing smaller does++- **Status:** accepted+- **Date:** 2026-10-12+- **Related:** migration `004` (the trigger and the `ON DELETE CASCADE` it contradicts), migration `101` (the `actor_id` SET NULL exemption), migration `117` (the retention arm, and the same cascade arm on the checkpoint table), migration `119` (this decision), issue #1330+- **Implements:** the fix issue #1330 asked for, with the acceptance tests it named++## Context++`DELETE FROM tenants` raised for any tenant that had ever written an audit+row — in practice, every tenant. `audit_log.tenant_id` has declared+`REFERENCES tenants(id) ON DELETE CASCADE` since migration 004, and the same+migration put a `BEFORE DELETE` trigger on the table that refuses every+delete. A cascade is an ordinary DELETE on the child, so the foreign key's+own declared behaviour could never fire: the parent statement aborted, and+`tenant_deletion.py` — the path a GDPR erasure request goes through — could+not complete. The claim-to-gate matrix row for tenant offboarding stayed+green throughout, because its test database held no audit rows; the audit+retention row (ADR-0013) carried the honest caveat instead.++Migration 117 met the identical problem on the checkpoint table it added and+solved it there, deliberately leaving `audit_log` alone: relaxing a shipped+immutability control is a security decision that deserves its own change,+its own ADR and its own live test, rather than riding along in a retention+PR. This is that change.++## Decision++The trigger learns to tell a cascade from a direct delete, using the fact+PostgreSQL gives it for free: `ON DELETE CASCADE` executes as an AFTER+trigger on the parent, so by the time the child's BEFORE DELETE trigger+runs, the tenant row is already gone. A direct DELETE still finds its+tenant and is refused. `tenant_id` is NOT NULL, so a row whose tenant is+absent can only be mid-cascade. The identical arm has run on+`aisoc_audit_checkpoints` since 117.++## The trade, stated plainly++Erasing a tenant's audit history now has exactly one non-checkpointed path:+destroying the tenant, which takes every alert, case, connector and+credential with it. That is loud, irreversible, and useless for selective+tampering — an attacker who wants one incriminating row gone cannot get it+without deleting the customer, and the absence of a tenant is not a secret.+What stays impossible:++- **A direct `DELETE FROM audit_log` while the tenant exists**, refused+ twice over: migration 117 revoked DELETE from the runtime role, and the+ trigger refuses any role that does hold the privilege (including the+ table owner and a superuser, for whom the privilege layer does not+ apply).+- **Selective erasure.** The only row-level removal remains+ `audit_retention_truncate` (ADR-0013), which writes a checkpoint the+ chain verifier re-anchors on.+- **Editing.** The UPDATE arm is unchanged: `actor_id` value→NULL for the+ user-deletion FK, and nothing else.++One consequence worth naming: the cascade bypasses the hash chain's+bookkeeping entirely, which is correct — after erasure there is no tenant+left whose chain could verify, so there is nothing to re-anchor. A partial+failure cannot strand a half-erased chain either, because the cascade and+the parent delete are one statement in one transaction.++## How it is proven++`tests/isolation/test_audit_immutability_live.py` runs against real+Postgres as the DML-only `aisoc_app` role (wired into+`audit-forgery-live.yml`, which until now ran a different file and left+this suite attached to no workflow at all):++- deleting a tenant that holds audit rows succeeds as the runtime role, and+ the rows go with it — proving the referential action carries the owner's+ privilege past both the revoke and the trigger;+- a direct `DELETE FROM audit_log` while the tenant exists is still+ refused for the runtime role (privilege layer);+- the same direct DELETE is refused for the table owner too (trigger+ layer), via the owner DSN the workflow already has — so the control does+ not quietly become privilege-only.diff --git a/docs/decisions/README.md b/docs/decisions/README.mdindex f322f2569..3ed48ead1 100644--- a/docs/decisions/README.md+++ b/docs/decisions/README.md@@ -27,6 +27,7 @@ ADRs are immutable once accepted. If a decision changes, write a new ADR that su| [ADR-0011](./0011-behavioural-baselines-in-core.md) | Behavioural baselines in CORE are a fusion module; the UEBA service stays in `full` | accepted | 2026-10-11 || [ADR-0012](./0012-how-fusion-scales.md) | How fusion scales — required-field indexing, entity partitioning, and a scale hint the cluster acts on | accepted | 2026-10-11 || [ADR-0013](./0013-truncating-a-hash-chained-audit-log.md) | Truncating a hash-chained audit log without making an intact chain look tampered with | accepted | 2026-10-11 |+| [ADR-0014](./0014-tenant-erasure-crosses-the-audit-trigger.md) | Tenant erasure crosses the audit-log immutability trigger; nothing smaller does | accepted | 2026-10-12 |## Authoring conventionsdiff --git a/services/api/migrations/119_audit_log_tenant_erasure.sql b/services/api/migrations/119_audit_log_tenant_erasure.sqlnew file mode 100644index 000000000..b0d450a66--- /dev/null+++ b/services/api/migrations/119_audit_log_tenant_erasure.sql@@ -0,0 +1,88 @@+-- 119_audit_log_tenant_erasure.sql+--+-- Tenant deletion is impossible: the audit_log immutability trigger refuses+-- the cascade from tenants (issue #1330; ADR 0014).+--+-- ── The defect ───────────────────────────────────────────────────────────────+--+-- `audit_log.tenant_id` has carried `REFERENCES tenants(id) ON DELETE CASCADE`+-- since migration 004, and the immutability trigger (004, restated by 101 and+-- again by 117) refuses every DELETE that is not the retention purge. A+-- cascade is an ordinary DELETE on the child, so `DELETE FROM tenants` raises+-- for any tenant that has ever written an audit row — which is every tenant.+-- `services/api/app/services/tenant_deletion.py` ends with exactly that+-- statement, so the tenant-offboarding path (the one a GDPR erasure request+-- goes through) could never complete. The behaviour reads as the control+-- working, which is why nothing noticed until someone deleted a tenant+-- against a database that held audit rows.+--+-- ── The fix, and why it is safe ──────────────────────────────────────────────+--+-- Migration 117 already solved this exact problem for the checkpoint table it+-- added: PostgreSQL runs `ON DELETE CASCADE` as an AFTER trigger on the+-- parent, so by the time the child's BEFORE DELETE trigger runs, the parent+-- row is already gone — while a caller deleting child rows directly still has+-- their tenant. `tenant_id` is NOT NULL (004), so the probe cannot be+-- confused by an orphan row. The same arm is applied here.+--+-- What this deliberately does NOT relax (ADR 0014 records the trade):+-- * A direct `DELETE FROM audit_log` while the tenant exists is still+-- refused twice over — by privilege (117 revoked DELETE from the runtime+-- role) and by this trigger for any role that does hold the privilege.+-- * Selective erasure stays impossible. The only way to remove a tenant's+-- audit history outside the checkpointed retention window is to destroy+-- the whole tenant — every alert, case and credential with it — which is+-- visible, irreversible, and not tampering in any useful sense.+-- * The retention-purge arm is unchanged: `audit_retention_truncate`+-- (117) remains the only selective path, and it writes a checkpoint the+-- chain verifier re-anchors on.+--+-- Restated in full rather than patched, because a trigger function assembled+-- from two migrations is one nobody can read in one place. Byte-identical to+-- 117's statement apart from the cascade arm and this header.++CREATE OR REPLACE FUNCTION public.audit_log_immutable()+RETURNS trigger+LANGUAGE plpgsql+AS $fn$+DECLARE+ key text;+ old_row jsonb := to_jsonb(OLD);+ new_row jsonb := to_jsonb(NEW);+BEGIN+ IF TG_OP = 'DELETE' THEN+ -- The retention window, and nothing else. `audit_retention_truncate`+ -- is the only thing that sets this, it is `SET LOCAL` so it cannot+ -- outlive the statement that needed it, and the application role has+ -- no DELETE privilege here anyway — so a caller who sets the setting+ -- by hand is refused by Postgres before this trigger is reached.+ IF coalesce(current_setting('app.audit_retention_purge', TRUE), 'off') = 'on' THEN+ RETURN OLD;+ END IF;+ -- Tenant erasure. `ON DELETE CASCADE` runs as an AFTER trigger on+ -- `tenants`, so when this BEFORE trigger sees the child row the+ -- parent is already gone; a direct DELETE still finds its tenant and+ -- falls through to the refusal. `tenant_id` is NOT NULL, so a row+ -- whose tenant is absent can only be mid-cascade.+ IF NOT EXISTS (SELECT 1 FROM tenants WHERE id = OLD.tenant_id) THEN+ RETURN OLD;+ END IF;+ RAISE EXCEPTION 'audit_log rows are immutable (attempted %)', TG_OP;+ END IF;++ FOR key IN SELECT jsonb_object_keys(new_row) LOOP+ IF old_row -> key IS DISTINCT FROM new_row -> key THEN+ -- The ONE legal mutation, pinned to its column: only actor_id+ -- carries ON DELETE SET NULL.+ IF key = 'actor_id'+ AND jsonb_typeof(old_row -> key) <> 'null'+ AND jsonb_typeof(new_row -> key) = 'null'+ THEN+ CONTINUE;+ END IF;+ RAISE EXCEPTION 'audit_log rows are immutable (attempted UPDATE of %)', key;+ END IF;+ END LOOP;+ RETURN NEW;+END;+$fn$;diff --git a/tests/isolation/test_audit_immutability_live.py b/tests/isolation/test_audit_immutability_live.pyindex 5714cf396..842a45366 100644--- a/tests/isolation/test_audit_immutability_live.py+++ b/tests/isolation/test_audit_immutability_live.py@@ -6,8 +6,20 @@ user. Review on #1231 showed a value->NULL exemption that does not name thecolumn lets one UPDATE strip every forensic column at once (actor_email,actor_ip, resource, changes, metadata) while the trigger stays silent.-Every case runs inside a transaction that ROLLBACKs: no live audit row is-ever durably mutated, not even in the legal case.+Migration 119 (issue #1330, ADR-0014) added the second exemption: the+cascade from ``DELETE FROM tenants``, without which tenant offboarding — the+path a GDPR erasure request takes — could never complete. The erasure cases+here are the acceptance tests that issue named, and they commit real rows in+a tenant of their own; every other case runs inside a transaction that+ROLLBACKs, so no shared audit row is ever durably mutated, not even in the+legal case.++The suite drives two connections, because the two refusal layers bind+different roles: ``ISOLATION_AUDIT_DSN`` is the DML-only runtime role+(``aisoc_app``), refused by privilege since migration 117 revoked its+DELETE; ``ISOLATION_AUDIT_OWNER_DSN`` is the table owner, for whom only the+trigger stands. Proving both keeps the control two-layered rather than+quietly privilege-only."""from __future__ import annotations@@ -33,6 +45,24 @@ def _dsn() -> str:return value.replace("postgresql://", "postgresql+asyncpg://", 1) if value.startswith("postgresql://") else value+def _owner_dsn() -> str:+ """The table owner's DSN — required, not optional, once the suite runs.++ The trigger-layer cases can only be proven by a role that holds the+ DELETE privilege, and a live suite that silently skips them would read+ as the control passing. So an unset owner DSN is a failure with the+ remedy in the message, never a skip.+ """+ value = os.environ.get("ISOLATION_AUDIT_OWNER_DSN", "").strip()+ if not value:+ pytest.fail(+ "ISOLATION_AUDIT_DSN is set but ISOLATION_AUDIT_OWNER_DSN is not: the trigger-layer "+ "cases need the audit_log table owner (the role that applies migrations), and "+ "skipping them would prove the privilege layer only"+ )+ return value.replace("postgresql://", "postgresql+asyncpg://", 1) if value.startswith("postgresql://") else value++@pytest_asyncio.fixture(scope="module", loop_scope="module")async def engine():from sqlalchemy.ext.asyncio import create_async_engine@@ -44,6 +74,17 @@ async def engine():await eng.dispose()+@pytest_asyncio.fixture(scope="module", loop_scope="module")+async def owner_engine():+ from sqlalchemy.ext.asyncio import create_async_engine++ eng = create_async_engine(_owner_dsn())+ try:+ yield eng+ finally:+ await eng.dispose()++@pytest_asyncio.fixture(scope="module", loop_scope="module")async def row(engine):"""A fresh audit row to attack, deleted on teardown. Its actor is then@@ -91,9 +132,13 @@ async def row(engine):)yield {"id": aid, "user_id": user_id, "tenant_id": tenant_id}async with engine.begin() as conn:- await conn.execute(text("DELETE FROM audit_log WHERE id = :id"), {"id": aid})- # delete the actor user: FK ON DELETE SET NULL must fire through the- # trigger, exactly the path migration 101 exists for.+ # Teardown goes through the two legal paths rather than around them.+ # Deleting the user first fires ON DELETE SET NULL on actor_id through+ # the trigger — exactly the path migration 101 exists for — and+ # deleting the tenant then erases the audit row by cascade, the path+ # migration 119 exists for. The previous teardown deleted the audit+ # row directly, which 117's trigger refuses; it only looked fine while+ # this suite ran in no workflow at all.await conn.execute(text("DELETE FROM users WHERE id = :id"), {"id": user_id})await conn.execute(text("DELETE FROM tenants WHERE id = :id"), {"id": tenant_id})@@ -173,7 +218,7 @@ class TestAuditImmutabilityExemption:)assert outcome.startswith("REFUSED"), outcome- async def test_delete_is_still_refused_loudly(self, engine, row) -> None:+ async def test_delete_is_still_refused_loudly(self, owner_engine, row) -> None:"""The trigger is BEFORE UPDATE **OR DELETE** and both arms run thisone function, so rewriting it for the UPDATE shape has to keep theDELETE arm working.@@ -186,10 +231,15 @@ class TestAuditImmutabilityExemption:`DELETE 0` as success. Refusing loudly is the behaviour migration004 shipped and the one an operator can act on.+ Runs as the **owner**, deliberately: the runtime role is stopped one+ layer earlier by 117's privilege revoke (the case below), so only a+ role holding DELETE can prove the trigger arm still works — and the+ tenant is alive here, so 119's cascade arm must not open the door.+Asserted both ways round: the statement raises, and the row isstill there afterwards."""- outcome = await _attempt(engine, "DELETE FROM audit_log WHERE id = :id", {"id": row["id"]})+ outcome = await _attempt(owner_engine, "DELETE FROM audit_log WHERE id = :id", {"id": row["id"]})assert outcome.startswith("REFUSED"), outcomeassert "DELETE" in outcome, outcome@@ -198,6 +248,31 @@ class TestAuditImmutabilityExemption:# unrelated tests down in the offline collection job.from sqlalchemy import text+ async with owner_engine.connect() as conn:+ remaining = (+ await conn.execute(+ text("SELECT count(*) FROM audit_log WHERE id = :id"),+ {"id": row["id"]},+ )+ ).scalar()+ assert remaining == 1, "the row was removed even though the statement was refused"++ async def test_delete_as_the_runtime_role_is_refused_by_privilege(self, engine, row) -> None:+ """The privilege layer, proven on the role the product actually runs as.++ Migration 117 revoked DELETE on `audit_log` from `aisoc_app`, so the+ runtime role is refused before the trigger is ever consulted. Either+ message proves the row cannot be removed; which one arrives says+ which layer answered, and on this connection it must be the+ privilege layer unless a deployment re-granted DELETE — in which+ case the trigger still stands, so `immutable` is accepted too.+ """+ outcome = await _attempt(engine, "DELETE FROM audit_log WHERE id = :id", {"id": row["id"]})+ assert outcome.startswith("REFUSED"), outcome+ assert "permission denied" in outcome or "immutable" in outcome, outcome++ from sqlalchemy import text+async with engine.connect() as conn:remaining = (await conn.execute(@@ -206,3 +281,66 @@ class TestAuditImmutabilityExemption:)).scalar()assert remaining == 1, "the row was removed even though the statement was refused"+++class TestTenantErasureIsTheOnePathThrough:+ """Issue #1330's acceptance tests, verbatim: deleting a tenant that holds+ audit rows must succeed and take the rows with it, while a direct DELETE+ on those same rows stays refused for as long as the tenant exists."""++ async def test_deleting_the_tenant_erases_its_audit_rows(self, engine) -> None:+ """`DELETE FROM tenants` completes as the runtime role, and the+ cascade crosses both refusal layers.++ This is the exact statement `tenant_deletion.py` ends with, run as+ the role the product runs as. It proves two things no unit test can:+ that 119's trigger arm recognises the cascade (the parent is already+ gone when the child's BEFORE trigger runs), and that the referential+ action carries the table owner's privilege — `aisoc_app` holds no+ DELETE on `audit_log`, yet its DELETE on `tenants` must still erase+ the audit rows. Before 119 this statement raised `immutable` for any+ tenant that had ever written an audit row, which is every tenant.++ Committed rather than rolled back, in a tenant of its own: erasure+ that only happens inside an aborted transaction proves nothing.+ """+ import uuid++ from sqlalchemy import text++ tenant_id = str(uuid.uuid4())+ marker = f"erasure-{tenant_id[:8]}"+ async with engine.begin() as conn:+ await conn.execute(+ text("INSERT INTO tenants (id, name, slug) VALUES (:id, :n, :s)"),+ {"id": tenant_id, "n": marker, "s": marker},+ )+ for action in ("case:create", "case:close"):+ await conn.execute(+ text("INSERT INTO audit_log (id, tenant_id, action, resource) VALUES (:id, :t, :a, 'case/1')"),+ {"id": str(uuid.uuid4()), "t": tenant_id, "a": action},+ )++ # The negative control first, while the tenant is alive: the same+ # rows the cascade is about to erase cannot be deleted directly.+ outcome = await _attempt(engine, "DELETE FROM audit_log WHERE tenant_id = :t", {"t": tenant_id})+ assert outcome.startswith("REFUSED"), outcome++ async with engine.begin() as conn:+ await conn.execute(text("DELETE FROM tenants WHERE id = :t"), {"t": tenant_id})++ async with engine.connect() as conn:+ survivors = (+ await conn.execute(+ text("SELECT count(*) FROM audit_log WHERE tenant_id = :t"),+ {"t": tenant_id},+ )+ ).scalar()+ tenant_rows = (+ await conn.execute(+ text("SELECT count(*) FROM tenants WHERE id = :t"),+ {"t": tenant_id},+ )+ ).scalar()+ assert tenant_rows == 0, "the tenant row survived its own deletion"+ assert survivors == 0, "the tenant is gone but its audit rows survived — the cascade was refused or skipped"