Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/audit-chain-head.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'seamless-auth-api': minor
---

The audit chain head is protected and can be signed (#595).

- A new migration adds a trigger to `auth_event_chain_head` that refuses INSERT, DELETE and TRUNCATE, and any change to `seq` or `last_hash` outside the `auth_events` insert trigger, which now runs as SECURITY DEFINER. A role that deletes the newest audit events can no longer move the head back to hide it, so `GET /admin/auth-events/integrity` reports the loss as `head_mismatch`.
- New optional `AUDIT_CHAIN_KEY`. When set, every audit insert signs the new head with an HMAC under it, and the integrity check fails a head whose signature is missing (`head_unsigned`, a new `reason`) or does not verify (`head_mismatch`). Set it on every instance. The migration signs the head when the key is set while it runs. Otherwise sign it once with `node dist/scripts/sealAuditHead.js`, and again after rotating the key. Production warns at boot while it is unset.
- The integrity report gains `headSigned`.
- `docs/security-posture.md` now recommends revoking writes on `auth_event_chain_head` as well, and notes that the retention setting is open to any session, so the DELETE privilege is the control.
11 changes: 6 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ so it does nothing at all without an archive directory. The job runs a minute af
then daily, and each run also logs the audit chain head as an external anchor. See
[Audit trail integrity](./security-posture.md#audit-trail-integrity).

| Variable | Required | Default | Notes |
| ------------------------------ | -------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AUDIT_RETENTION_DAYS` | No | - | Whole days to keep audit events. Unset, or anything that is not a positive whole number, keeps everything. Set it from the records retention schedule that applies to the deployment, not from a product default: authentication audit events are records. |
| `AUDIT_ARCHIVE_DIR` | With retention | - | Where expired events are written before they are deleted, as `auth-events-<firstSeq>-<lastSeq>.ndjson` with a `.sha256` file beside each. Use durable storage, such as a mounted volume, since a file here is the only copy once its rows are gone. |
| `AUDIT_RETENTION_DATABASE_URL` | No | `DATABASE_URL` | A connection for the retention job alone, so it can run as a role that holds DELETE on `auth_events` while the application role does not. |
| Variable | Required | Default | Notes |
| ------------------------------ | ------------------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AUDIT_RETENTION_DAYS` | No | - | Whole days to keep audit events. Unset, or anything that is not a positive whole number, keeps everything. Set it from the records retention schedule that applies to the deployment, not from a product default: authentication audit events are records. |
| `AUDIT_ARCHIVE_DIR` | With retention | - | Where expired events are written before they are deleted, as `auth-events-<firstSeq>-<lastSeq>.ndjson` with a `.sha256` file beside each. Use durable storage, such as a mounted volume, since a file here is the only copy once its rows are gone. |
| `AUDIT_RETENTION_DATABASE_URL` | No | `DATABASE_URL` | A connection for the retention job alone, so it can run as a role that holds DELETE on `auth_events` while the application role does not. |
| `AUDIT_CHAIN_KEY` | Recommended in production | - | HMAC key that signs the audit chain head, so a head moved back over deleted events fails the integrity check. Every instance needs the same value. Set it before migrating, or sign the head once with `node dist/scripts/sealAuditHead.js` afterwards, and again after rotating it. Unset, nothing is signed and production warns at boot. See [A signed head](./security-posture.md#a-signed-head). |

Retention never deletes an event an [audit sink](#siem-streaming) has yet to deliver, so a sink
that has fallen behind, or is turned off, holds retention back until it catches up or is
Expand Down
92 changes: 74 additions & 18 deletions docs/security-posture.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,46 +682,102 @@ not cover them, because lockout only ever reads a short window of them.
one-row table, `auth_event_chain_head`, advanced with an UPDATE so that concurrent
inserts queue on its row lock rather than linking to the same predecessor.
- **Append-only.** A trigger refuses every UPDATE and TRUNCATE on the table, and every
DELETE unless the transaction has set `seamless.audit_retention = 'on'`, which only
the retention job does.
DELETE unless the transaction has set `seamless.audit_retention = 'on'`.
- **A protected head.** A trigger on `auth_event_chain_head` refuses INSERT, DELETE and
TRUNCATE, and refuses any change to `seq` or `last_hash` that does not come from the
insert trigger. The insert trigger runs as SECURITY DEFINER, so the application role
needs no write access to those columns. Without this, a role that could delete the
newest events could move the head back to match and the chain still verified.
- **No user foreign key.** It used to null `user_id` when a user was deleted. A row now
keeps the id of the user it was about after that user is gone. The id is a UUID, and
removing old rows is a job for retention, not for user deletion.

The retention setting is not an access control. `seamless.audit_retention` is a custom
setting, and any session can set it, so the trigger only stops the application deleting
events by accident. What decides who can delete events is the DELETE privilege, which is
why the grants below take it away from the application role.

### A signed head

With `AUDIT_CHAIN_KEY` set (see [configuration](./configuration.md#audit-retention)), the
head also carries `signature`, an HMAC-SHA256 under that key of the head's `seq` and hash,
and `signed_seq`, the `seq` it was made for. The database never sees the key. Every insert
moves the signature onto the new head in the same transaction, while the head is still
locked.

A head moved back over a truncated tail, or onto forged events, then has no valid
signature, and making one takes the key. This holds even against a role that owns the
tables and can disable their triggers.

The application only signs forward from a signature that verifies. A head that is
unsigned, or whose signature is wrong, stays that way and keeps failing the check, since
signing over it would erase the evidence. The head trigger also refuses clearing the
signature or moving `signed_seq` backwards.

Setting it up:

- **New deployment.** Set `AUDIT_CHAIN_KEY` before the first migration. The migration
that adds the signature signs the head when the key is set while it runs.
- **Existing deployment.** Set `AUDIT_CHAIN_KEY` on every instance, then run the upgrade.
If the key was not set while the migration ran, sign the head once by hand with
`node dist/scripts/sealAuditHead.js`, run with the server's database environment. It
refuses a chain that does not verify.
- **Every instance needs the same key.** An event written by an instance without it leaves
the head unsigned (`head_unsigned`) until an instance with the key writes the next one.
- **Rotating the key.** Set the new key on every instance and run the seal script once.
Until then the check reports `head_mismatch`, and new events are not signed.

Without the key nothing is signed, the check works as before, and its report carries
`headSigned: false`. Production logs a warning at boot while the key is unset.

### Checking it

`GET /admin/auth-events/integrity` (admin, read) recomputes every hash and reports the
first failure:

| `reason` | Meaning |
| --------------- | ---------------------------------------------------------------- |
| `hash_mismatch` | the row's content no longer matches its hash: it was edited |
| `sequence_gap` | rows are missing from the middle of the chain |
| `broken_link` | `prev_hash` does not match the previous row: rows were reordered |
| `head_mismatch` | the newest rows are missing, or the head itself was changed |
| `reason` | Meaning |
| --------------- | ------------------------------------------------------------------------------------------ |
| `hash_mismatch` | the row's content no longer matches its hash: it was edited |
| `sequence_gap` | rows are missing from the middle of the chain |
| `broken_link` | `prev_hash` does not match the previous row: rows were reordered |
| `head_mismatch` | the newest rows are missing, the head itself was changed, or its signature does not verify |
| `head_unsigned` | `AUDIT_CHAIN_KEY` is set and the head has no signature, or one for an older `seq` |

It also returns `head` (the latest `seq` and hash) and `anchorHash`, which is the
`prev_hash` of the oldest remaining row. Once retention has removed old rows, that is the
hash of the last row archived, and it ties the remaining chain to the archive.
It also returns `head` (the latest `seq` and hash), `headSigned`, and `anchorHash`, which
is the `prev_hash` of the oldest remaining row. Once retention has removed old rows, that is
the hash of the last row archived, and it ties the remaining chain to the archive.

### What it does not stop

A role that owns the table can disable the triggers and rewrite the whole chain
consistently. Nothing inside the database can detect that, so two things outside it close
the gap:
A role that owns the tables can disable the triggers and rewrite the whole chain
consistently. Without the key it can also move the head to match, and with the key it can
still put back an older head and signature that it copied while they were current. Nothing
inside the database can detect either, so two things outside it close the gap:

1. **Least privilege.** Run migrations as the table owner and the application as a role
without UPDATE, DELETE or TRUNCATE on `auth_events`:
that cannot change the trail or its head:

```sql
REVOKE UPDATE, DELETE, TRUNCATE ON public.auth_events FROM seamless_app;
REVOKE INSERT, UPDATE, DELETE, TRUNCATE ON public.auth_event_chain_head FROM seamless_app;
GRANT SELECT ON public.auth_event_chain_head TO seamless_app;
GRANT UPDATE (signed_seq, signature) ON public.auth_event_chain_head TO seamless_app;
REVOKE CREATE ON SCHEMA public FROM PUBLIC, seamless_app;
```

The retention job then needs DELETE, from a separate role.
Postgres 15 and later no longer grant CREATE on `public` to everyone, but a cluster
upgraded from an older version keeps that grant. No role but the owner should create
objects in the schema the audit functions live in.

The application keeps INSERT on `auth_events`, which the insert trigger chains, and may
only write the signature columns of the head, which it cannot forge without the key.
The retention job needs DELETE on `auth_events`, from a separate role
(`AUDIT_RETENTION_DATABASE_URL`). That role can still delete the newest events, but it
cannot move the head, so the check reports the loss as `head_mismatch`.

2. **External anchors.** Record `head` somewhere the database cannot reach: the evidence
package, a ticket, or a log store. A later check whose chain does not pass through a
recorded head shows the history was rewritten.
package, a ticket, or a log store. The retention job logs it daily. A later check whose
chain does not pass through a recorded head shows the history was rewritten.

### Retention and export

Expand Down
11 changes: 10 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7785,6 +7785,7 @@
"lastSeq": 0,
"anchorHash": "string",
"head": { "seq": 0, "hash": "string" },
"headSigned": true,
"firstFailure": { "seq": 0, "id": "string", "reason": null }
},
"schema": {
Expand All @@ -7805,6 +7806,7 @@
},
"required": ["seq", "hash"]
},
"headSigned": { "type": "boolean" },
"firstFailure": {
"type": "object",
"nullable": true,
Expand All @@ -7813,7 +7815,13 @@
"id": { "type": "string", "nullable": true },
"reason": {
"type": "string",
"enum": ["hash_mismatch", "broken_link", "sequence_gap", "head_mismatch"]
"enum": [
"hash_mismatch",
"broken_link",
"sequence_gap",
"head_mismatch",
"head_unsigned"
]
}
},
"required": ["seq", "id", "reason"]
Expand All @@ -7827,6 +7835,7 @@
"lastSeq",
"anchorHash",
"head",
"headSigned",
"firstFailure"
]
}
Expand Down
9 changes: 8 additions & 1 deletion src/generated/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7393,6 +7393,7 @@ export interface paths {
* "seq": 0,
* "hash": "string"
* },
* "headSigned": true,
* "firstFailure": {
* "seq": 0,
* "id": "string",
Expand All @@ -7411,11 +7412,17 @@ export interface paths {
seq: number;
hash: string | null;
} | null;
headSigned: boolean;
firstFailure: {
seq: number;
id: string | null;
/** @enum {string} */
reason: 'hash_mismatch' | 'broken_link' | 'sequence_gap' | 'head_mismatch';
reason:
| 'hash_mismatch'
| 'broken_link'
| 'sequence_gap'
| 'head_mismatch'
| 'head_unsigned';
} | null;
};
};
Expand Down
Loading