Skip to content

Refuse /delegate and /revoke requests without standing before any lock (TC-3000) - #610

Draft
samgbafa wants to merge 2 commits into
mainfrom
feat/tc-3000-prelock-checks
Draft

samgbafa wants to merge 2 commits into
mainfrom
feat/tc-3000-prelock-checks

Conversation

@samgbafa

@samgbafa samgbafa commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

/delegate and /revoke: refuse requests without standing before taking any lock (TC-3000)

Follow-up to #595 (TC-2986 E). That PR moved the signature check ahead of every lock. A validly signed request with no authority could still take the victim's locks before it was refused: anyone can sign with a fresh did:key. Such a request is:

  • a delegation naming another tenant's space or parent;
  • a revocation of another tenant's delegation;
  • a delegation or revocation replayed after it expired.

Before being refused, it took the guards of the delegation chain it cited and queued for the write order of the spaces it named. On PostgreSQL it also held a pooled connection in an open transaction while waiting for the space advisory lock.

F1: a pre-check on the pool, before any lock

After the signature, and before the chain guards, the spaces' write order or a transaction, /delegate and /revoke run the checks that need no lock. They run on the pool and are read-only.

  • Delegation (delegation::precheck):
    • the signed time window;
    • the renewer-identity refusal;
    • renewal-lease parents;
    • the capability rules that hold whoever signs (named keys, app-id binding, SQL caveats, key.delete);
    • standing: the issuer is the root authority for every capability, or holds an owner revoke grant (owner_controls_target) for the rest, or every cited parent is stored and was granted to the issuer. The parents are read in one query by exact id, the same lookup the transaction's graph snapshot uses. Policy roots and policy-session shapes are treated exactly as in validate_with.
  • Revocation (revocation::precheck): the same check_standing the transaction runs. That is the time window, a stored target, and revoker_is_authorized. process_with now calls the same function, so the two cannot drift.
  • Renewal lease: the pure shape checks of save_owner_renewal_lease (time window, no parents, an original to renew). preverify_lease_original, which already reads the stored original before the locks, also refuses a lease not issued by the original's delegator.

Every check runs again inside the transaction, which stays the authority. The pre-check refuses only what the transaction would refuse. The checks are the same functions, run in the same order. The one difference is that a cited parent is read directly instead of through the graph snapshot.

Unchanged behaviour:

  • A delegation already stored still answers as registered when posted again, even after it has expired. The pre-check refusal falls back to the existing-row read before returning, which costs nothing on the accepting path.
  • Delegations with standing still queue for their locks as before, and are admitted.

Accepted edges:

  • A child posted while its parent's own write is still in flight was previously able to wait on the parent's chain guard and succeed. It is now refused at once as Cannot find parent delegation. Clients that pipeline a parent and its child without waiting for the parent's response must retry. The CHANGELOG says so.
  • A stranger's renewal lease is refused as unauthorized even when its window or shape was also wrong.
  • If a parent is missing while another cited chain exceeds the traversal limit, the refusal reads Cannot find parent delegation rather than the traversal-limit error.

F6: /delegate checks the signature before the Policy/v3 admission

The route now calls preverify_delegation before ordinary_admission_allowed. That admission does one primary-key read per cited parent, and for policy-session shapes a walk of up to 9 hops. The route then registers through the new SpaceDatabase::delegate_verified, so the signature is checked once. When the PreVerified it is given does not cover the delegation's bytes, delegate_verified checks the signature itself before any read or lock (review N5). delegate() is delegate_verified with an empty set.

Visible change: a badly signed delegation citing a policy root answers 401 Failed to verify signature instead of 403.

F5: a test pins the stored-original hash check

a_stored_original_whose_bytes_do_not_match_its_address_is_refused covers stored_original, which both preverify_lease_original and the in-transaction lease check use. It fails if the hash comparison is removed.

Tests

New tests only; no existing test changed.

  • db/signature_before_locks_tests.rs, Bobbie's fix: check /delegate and /revoke signatures before any lock (TC-2986 E) #595 residual probes inverted. Each asserts the refusal arrives within 5 s while the victim's lock is held:
    • a stranger's delegation, refused as MissingParents while the space write order is held;
    • a stranger's revocation, refused as UnauthorizedRevoker;
    • an expired delegation and an expired revocation, refused as InvalidTime;
    • a child citing a grant made to someone else (UnauthorizedDelegator, while that grant's chain guard is held) and a child citing a missing parent (MissingParents);
    • the PostgreSQL case, while a second session holds the space advisory lock.
  • Guards against over-refusal:
    • delegations_with_standing_still_wait_for_their_locks_and_are_admitted;
    • a_stored_delegation_posted_again_after_it_expired_is_still_registered.
  • F6: delegate_checks_the_signature_before_policy_admission, on both backends.
  • F5: the test above.
  • From Bobbie's Refuse /delegate and /revoke requests without standing before any lock (TC-3000) #610 review:
    • a_revocation_whose_control_grant_is_revoked_while_it_waits_is_refused (her R4) re-pins the in-transaction revoker check (N2). The revocation passes the pre-check through a revoke control grant and waits on its target's chain guard. The grant is revoked meanwhile, and the transaction refuses Unauthorized Revoker.
    • a_stranger_renewal_lease_is_refused_before_its_locks (her R2): the lease is refused while its own chain guard and the SQLite writer lock are held (N3).
    • an_expired_delegation_is_refused_without_a_pre_check: delegate_batch, which has no pre-check, still refuses an expired delegation inside the transaction. This pins verify_time in process_with.

Mutation checks:

  • With both pre-checks disabled, the 6 F1 tests fail and the rest pass.
  • With the in-transaction revoker check removed, the in-transaction verify_time removed, or the lease delegator check in preverify_lease_original removed, exactly the three review tests fail.

Follow-up, not in this PR: TC-3086 (review N1). The pre-check accepts any stored parent granted to the issuer; it should also require that parent's abilities to cover each capability, ignoring caveats and time.

Details: TC-3000.

…before any lock; check the signature before policy admission
… stranger-lease pre-check; preverify uncovered bytes in delegate_verified

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant