mirror of
https://github.com/block/buzz.git
synced 2026-11-30 13:12:38 +00:00
fix(buzz-db): add NIP-FI Carl r2 guards — policy monotonicity, admission result cardinality, denial attempt binding
Finding 1 (policy revision monotonicity): add identity_enrollment_policy_revision_guard_v1() BEFORE INSERT on identity_enrollment_policies. Uses a per-community advisory lock via hashtextextended so concurrent writers serialize the max-revision read, then asserts both policy_revision and effective_at strictly exceed the current community maximum (FI-INV-06 — stable assertion policy). Finding 2 (admission result ↔ kind-11 receipt cardinality): add authorization_admission_result_guard_v1(), bidirectional deferred constraint trigger on both authorization_operation_receipts (kind-11 receipt must have exactly one result) and authorization_admission_results (result must attach to a kind-11 receipt). Mirrors the pattern of the existing authorization_operation_receipt_event_guard_v1. Finding 3 (denial event ↔ attempt binding): add authorization_denial_attempt_guard_v1(), bidirectional deferred constraint trigger on both authorization_events (kind-9 event must have exactly one denial attempt) and authorization_authentication_denial_attempts (attempt must reference an existing kind-9 event). The existing FK binds (audit_event_kind=9) but does not require a kind-9 event to have a matching attempt row; this guard closes that gap. All three fixes applied identically in migrations/0041, migrations/0042, and schema/schema.sql; the parity assertion continues to pass. Tests added (all three mutation-sensitive, two-sided): - identity_enrollment_policy_revision_is_monotonic - authorization_admission_result_requires_kind_11_receipt_bidirectional - authorization_denial_attempt_requires_kind_9_event_bidirectional No new tables; fence exclusion list unchanged; #[ignore] deletion suite need not rerun. Co-authored-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Hayt <9e1c23a3fd83f61da34420e4e88ff1b16e45cafcc0cd9019eb07d4ecfa8ca9b0@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -2308,6 +2308,56 @@ CREATE INDEX identity_lifecycle_selectors_asserted_history
|
||||
ON identity_lifecycle_selectors
|
||||
(community_id, asserted_history_id, selector_kind);
|
||||
|
||||
-- Serializes policy-revision inserts per community: each new revision must
|
||||
-- strictly exceed the current maximum, and each effective_at must strictly
|
||||
-- exceed the current maximum effective_at (FI-INV-06 — stable assertion
|
||||
-- policy; a revision that moves either coordinate backward is incoherent).
|
||||
-- The per-community advisory lock prevents two concurrent writers from both
|
||||
-- passing a plain SELECT MAX() check and committing conflicting revisions.
|
||||
CREATE FUNCTION identity_enrollment_policy_revision_guard_v1() RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
lock_key BIGINT;
|
||||
max_revision BIGINT;
|
||||
max_effective_at TIMESTAMPTZ;
|
||||
BEGIN
|
||||
-- Acquire a per-community exclusive transaction-scoped advisory lock so
|
||||
-- that concurrent insertions serialize here. The key is a stable hash of
|
||||
-- the namespace string and the community_id bytes.
|
||||
lock_key := hashtextextended(
|
||||
'buzz:enrollment-policy-revision:v1:' || NEW.community_id::text,
|
||||
0
|
||||
);
|
||||
PERFORM pg_advisory_xact_lock(lock_key);
|
||||
|
||||
SELECT MAX(policy_revision), MAX(effective_at)
|
||||
INTO max_revision, max_effective_at
|
||||
FROM identity_enrollment_policies
|
||||
WHERE community_id = NEW.community_id;
|
||||
|
||||
IF max_revision IS NOT NULL
|
||||
AND NEW.policy_revision <= max_revision
|
||||
THEN
|
||||
RAISE EXCEPTION
|
||||
'policy_revision % does not strictly exceed current maximum % for community %',
|
||||
NEW.policy_revision, max_revision, NEW.community_id
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'identity_enrollment_policy_revision_monotonic';
|
||||
END IF;
|
||||
|
||||
IF max_effective_at IS NOT NULL
|
||||
AND NEW.effective_at <= max_effective_at
|
||||
THEN
|
||||
RAISE EXCEPTION
|
||||
'effective_at % does not strictly exceed current maximum % for community %',
|
||||
NEW.effective_at, max_effective_at, NEW.community_id
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'identity_enrollment_policy_revision_monotonic';
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION nip_fi_reject_row_mutation_v1() RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION '% is immutable', TG_TABLE_NAME
|
||||
@@ -2728,6 +2778,9 @@ CREATE TRIGGER authorization_operation_receipts_no_truncate
|
||||
BEFORE TRUNCATE ON authorization_operation_receipts
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION nip_fi_reject_truncate_v1();
|
||||
|
||||
CREATE TRIGGER identity_enrollment_policies_revision_guard
|
||||
BEFORE INSERT ON identity_enrollment_policies
|
||||
FOR EACH ROW EXECUTE FUNCTION identity_enrollment_policy_revision_guard_v1();
|
||||
CREATE TRIGGER identity_enrollment_policies_immutable
|
||||
BEFORE UPDATE OR DELETE ON identity_enrollment_policies
|
||||
FOR EACH ROW EXECUTE FUNCTION nip_fi_reject_row_mutation_v1();
|
||||
@@ -3244,6 +3297,85 @@ CREATE TRIGGER authorization_authentication_denial_attempts_no_truncate
|
||||
BEFORE TRUNCATE ON authorization_authentication_denial_attempts
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION nip_fi_reject_truncate_v1();
|
||||
|
||||
-- Bidirectional deferred guard: a kind-9 (pre-authentication denial) audit
|
||||
-- event must commit with exactly one denial attempt; a denial attempt must
|
||||
-- commit with its audit event present and kind-9. Both directions deferred so
|
||||
-- event and attempt may be inserted in any order inside one transaction.
|
||||
CREATE FUNCTION authorization_denial_attempt_guard_v1()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
found_event_kind SMALLINT;
|
||||
attempt_count BIGINT;
|
||||
BEGIN
|
||||
IF TG_TABLE_NAME = 'authorization_events' THEN
|
||||
-- Firing from the event side: only kind-9 events require a denial row.
|
||||
IF NEW.event_kind <> 9 THEN
|
||||
RETURN NULL;
|
||||
END IF;
|
||||
|
||||
SELECT count(*) INTO attempt_count
|
||||
FROM authorization_authentication_denial_attempts
|
||||
WHERE community_id = NEW.community_id
|
||||
AND audit_event_id = NEW.event_id;
|
||||
|
||||
IF attempt_count <> 1 THEN
|
||||
RAISE EXCEPTION
|
||||
'kind-9 audit event requires exactly one denial attempt, found %',
|
||||
attempt_count
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_denial_attempt_event_cardinality';
|
||||
END IF;
|
||||
ELSE
|
||||
-- Firing from the denial-attempt side: verify the audit event is kind-9
|
||||
-- and that exactly one denial attempt references it.
|
||||
SELECT event_kind INTO found_event_kind
|
||||
FROM authorization_events
|
||||
WHERE community_id = NEW.community_id
|
||||
AND event_id = NEW.audit_event_id;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION
|
||||
'denial attempt references non-existent audit event %',
|
||||
NEW.audit_event_id
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_denial_attempt_event_kind';
|
||||
END IF;
|
||||
|
||||
IF found_event_kind <> 9 THEN
|
||||
RAISE EXCEPTION
|
||||
'denial attempt audit event must be kind 9, got %',
|
||||
found_event_kind
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_denial_attempt_event_kind';
|
||||
END IF;
|
||||
|
||||
SELECT count(*) INTO attempt_count
|
||||
FROM authorization_authentication_denial_attempts
|
||||
WHERE community_id = NEW.community_id
|
||||
AND audit_event_id = NEW.audit_event_id;
|
||||
|
||||
IF attempt_count <> 1 THEN
|
||||
RAISE EXCEPTION
|
||||
'exactly one denial attempt must reference audit event %, found %',
|
||||
NEW.audit_event_id, attempt_count
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_denial_attempt_event_cardinality';
|
||||
END IF;
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE CONSTRAINT TRIGGER authorization_denial_attempt_event_cardinality
|
||||
AFTER INSERT ON authorization_events
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW EXECUTE FUNCTION authorization_denial_attempt_guard_v1();
|
||||
|
||||
CREATE CONSTRAINT TRIGGER authorization_denial_event_attempt_cardinality
|
||||
AFTER INSERT ON authorization_authentication_denial_attempts
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW EXECUTE FUNCTION authorization_denial_attempt_guard_v1();
|
||||
|
||||
CREATE FUNCTION authorization_operation_version_delta_cardinality_guard_v1()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
@@ -3382,6 +3514,75 @@ CREATE TRIGGER authorization_admission_results_no_truncate
|
||||
BEFORE TRUNCATE ON authorization_admission_results
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION nip_fi_reject_truncate_v1();
|
||||
|
||||
-- Bidirectional deferred cardinality guard: a kind-11 (protected-mutation)
|
||||
-- receipt must commit with exactly one admission result; an admission result
|
||||
-- must commit against a kind-11 receipt. Deferred so receipt and result may
|
||||
-- be inserted in any order inside one transaction.
|
||||
CREATE FUNCTION authorization_admission_result_guard_v1()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
receipt authorization_operation_receipts%ROWTYPE;
|
||||
result_count BIGINT;
|
||||
BEGIN
|
||||
IF TG_TABLE_NAME = 'authorization_operation_receipts' THEN
|
||||
receipt := NEW;
|
||||
ELSE
|
||||
-- Firing from authorization_admission_results: look up the receipt.
|
||||
SELECT * INTO receipt
|
||||
FROM authorization_operation_receipts
|
||||
WHERE community_id = NEW.community_id
|
||||
AND operation_id = NEW.operation_id;
|
||||
IF NOT FOUND THEN
|
||||
-- FK on the result table already guards the non-existent receipt
|
||||
-- case; this path should not occur in normal operation.
|
||||
RAISE EXCEPTION
|
||||
'admission result references non-existent receipt for operation %',
|
||||
NEW.operation_id
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_admission_result_receipt_kind';
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
-- Non-kind-11 receipts require no admission result.
|
||||
IF receipt.operation_kind <> 11 THEN
|
||||
-- If this fired from the result side and the receipt is not kind 11,
|
||||
-- the result is attaching to the wrong receipt kind.
|
||||
IF TG_TABLE_NAME = 'authorization_admission_results' THEN
|
||||
RAISE EXCEPTION
|
||||
'admission result may only attach to a kind-11 (protected-mutation) receipt, got kind %',
|
||||
receipt.operation_kind
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_admission_result_receipt_kind';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END IF;
|
||||
|
||||
SELECT count(*) INTO result_count
|
||||
FROM authorization_admission_results
|
||||
WHERE community_id = receipt.community_id
|
||||
AND operation_id = receipt.operation_id;
|
||||
|
||||
IF result_count <> 1 THEN
|
||||
RAISE EXCEPTION
|
||||
'kind-11 receipt requires exactly one admission result, found %',
|
||||
result_count
|
||||
USING ERRCODE = 'check_violation',
|
||||
CONSTRAINT = 'authorization_admission_result_cardinality';
|
||||
END IF;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE CONSTRAINT TRIGGER authorization_admission_result_receipt_cardinality
|
||||
AFTER INSERT ON authorization_operation_receipts
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW EXECUTE FUNCTION authorization_admission_result_guard_v1();
|
||||
|
||||
CREATE CONSTRAINT TRIGGER authorization_admission_result_result_cardinality
|
||||
AFTER INSERT ON authorization_admission_results
|
||||
DEFERRABLE INITIALLY DEFERRED
|
||||
FOR EACH ROW EXECUTE FUNCTION authorization_admission_result_guard_v1();
|
||||
|
||||
-- Every successful/no-op core lifecycle receipt has exactly one privacy-safe
|
||||
-- audit event with the closed transition-kind mapping. Both directions are
|
||||
-- deferred so receipt, history, event, selectors, and binding may be inserted
|
||||
|
||||
Reference in New Issue
Block a user