Skip to content

Store salted commitments for erasable entry data - #75

Open
WiktorB2004 wants to merge 15 commits into
mainfrom
add-integrity-commitments
Open

WiktorB2004 wants to merge 15 commits into
mainfrom
add-integrity-commitments

Conversation

@WiktorB2004

Copy link
Copy Markdown
Owner

Summary

First step of tamper evidence: writes store a transaction id and salted commitments, and scrubs leave a pointer to their own record. Nothing is sealed yet, and the feature is private: audit_trail.integrity is not exported or documented.

  • audit_trail/integrity/: a canonical JSON encoder (keys in UTF-16 order, numbers as plain decimal text, -0 as 0), SHA-256 commitments with a 16-byte salt per group, the Xid8 column type, and the Integrity option. The encoder refuses \u0000, lone surrogates, numbers over 4,300 digits (checked before a Decimal is formatted) and values nested past the recursion limit.
  • AuditTrail(integrity=Integrity()) / build_tables(integrity=True) declare xid xid8 NOT NULL DEFAULT pg_current_xact_id(), seal_commitments, seal_salts and seal_erased on both tables, each with an (xid, id) index. Without the option (the default) the tables, DDL and queries stay exactly as they were.
  • The reads in query.py and testing.py list their columns instead of selecting the whole table, so they never return salts. A test keeps those lists equal to the table columns minus the integrity ones.
  • The writer commits to the erasable groups (label, changes, payload and actor) for the flush listener, log(), durable entries and the audit.scrubbed record. Every row has the same keys, because executemany takes its columns from the first row. A retried durable write draws new salts.
  • remote_addr is committed to as the text PostgreSQL stores. Addresses PostgreSQL would store as a different text (01.2.3.4, 10/8, 1.2.3.4.) are refused rather than guessed.
  • A value the encoder refuses follows on_error like a database error: logged and skipped under "log", raised under "raise", AuditWriteError for durable and fail_closed writes. With integrity on and the columns missing, the failure names migrations.add_integrity() under every policy.
  • scrub / scrub_actor remove the salt of each erased group and set seal_erased[group] to the scrub's xid, which is also the xid of its audit.scrubbed row. Rows written before integrity are not stamped.
  • Existing installs upgrade in two steps. migrations.add_integrity runs the ALTERs (ADD COLUMN, then SET DEFAULT on its own) in a savepoint with lock_timeout and jittered retries, and restores lock_timeout even when it fails. Old rows keep xid NULL. migrations.add_integrity_indexes then builds the indexes online on an AUTOCOMMIT connection: ON ONLY on each parent, CREATE INDEX CONCURRENTLY on each partition, then ATTACH PARTITION. It is idempotent and rebuilds a failed partition build.
  • Golden vectors for the encoding, commitments and leaf hashes are in tests/fixtures/integrity/vectors/. hypothesis is added as a dev dependency.

Closes #69

Test plan

  • Tests added or updated: unit tests for the encoder (Decimal digit limit, deep nesting), vectors, writer key sets, read column lists, DDL and Xid8. DB tests cover commitments under sync, asyncpg and psycopg async sessions, durable retry salts, scrub stamps (including rows written before integrity), encoder refusals and missing columns under "log", "raise" and fail_closed, add_integrity (upgrade, idempotent, lock_timeout restored, retry and give-up), add_integrity_indexes (valid index tree, idempotent, failed build rebuilt, lock timeout retry), executemany key behaviour, the Hypothesis JSONB round trip, and inet text against PostgreSQL for strict and loose inputs
  • uv run pytest passes locally, including the PostgreSQL tests in tests/db
  • Schema, SQL or partition changes checked on PostgreSQL 14 (AUDIT_TEST_PG_IMAGE=postgres:14) as well as the default image
  • ruff check, ruff format --check and mypy pass
  • Docs and docstrings updated if the public API changed: N/A, the feature stays private until the migration and guide PR
  • CHANGELOG.md Unreleased bullet if callers should notice: N/A, there is no user-facing change yet

The encoder is a custom format close to JCS: keys in UTF-16 order,
numbers as plain decimal text without trailing zeros, and -0 as 0, so
values read back from JSONB encode to the same bytes. Golden vectors
freeze the encoding, the commitment preimage and the leaf hash.
With AuditTrail(integrity=Integrity()) both tables get xid (xid8,
defaulting to pg_current_xact_id()), seal_commitments, seal_salts and
seal_erased, plus an (xid, id) index. Without it the tables are
unchanged. Reads list their columns instead of selecting the table, so
they never return salts and work on schemas without the new columns.
insert_transaction and insert_activities commit to the erasable groups
of each row with fresh salts, so the flush listener, log(), durable
entries and the scrub record all get them, and a retried durable write
draws new salts. Every activity row carries the same keys, since
executemany takes its columns from the first row.
scrub and scrub_actor remove the salt of each group they erase and
record the group in seal_erased with pg_current_xact_id(), the xid of
their own audit.scrubbed entry. coalesce keeps the stamp on a NULL
seal_erased.
It adds the columns, then the xid default in a second statement (the
default is STABLE, so one statement would stamp every old row), and the
(xid, id) indexes. Each attempt runs in a savepoint with a short
lock_timeout and is retried after a random pause.
The DB tests recompute every commitment from the row as read back, under
the sync and both async drivers. Hypothesis checks that the canonical
encoding survives a JSONB round trip on asyncpg and psycopg, and that
the inet text used in commitments matches PostgreSQL's output.
@WiktorB2004 WiktorB2004 self-assigned this Nov 26, 2026
@WiktorB2004 WiktorB2004 added the enhancement New feature or request label Nov 26, 2026
@WiktorB2004
WiktorB2004 added this pull request to stack #77 November 26, 2026 22:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Store salted commitments for erasable entry data

1 participant