Skip to content

fix(ui-server): check reference-grant login expiry against the clock inside the writer transaction (#4169) - #4177

Merged
schlessera merged 5 commits into
mainfrom
fix/4169-grant-expiry-clock
Nov 17, 2026
Merged

schlessera merged 5 commits into
mainfrom
fix/4169-grant-expiry-clock

Conversation

@schlessera

@schlessera schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #4169

What changed

POST /api/auth/principals/:id/reference-grants stamps now: Date.now() and then waits for the write worker. Inside the worker's transaction, createReferenceGrant checked the target and the creating owner with isUsablePrincipal(…, input.now), so a login that expired while the request was queued behind other writes still got a grant. The check now reads the clock inside the writer transaction, and such a login is refused with ReferenceGrantPrincipalError, which the route answers as Unknown principal (404). input.now still records created_at, drives retention of revoked grants and feeds the live-grant capacity count, exactly as before.

Why this shape

This follows the fix in #4168 (second review finding): check authority against the clock at the check, and keep the request's stamp for recorded times. Replacing the stamp everywhere would also have moved created_at and the retention cutoff, which the issue keeps unchanged.

Revocation. revokeReferenceGrant uses input.now only as the revoked_at value it writes (revokeReferenceGrant, packages/ui-server/src/db/reference-grants.ts:162-165). It makes no principal or expiry check. The caller's owner authority is checked by the route guard before the await, the same as for every route. So the stamp there is a recorded time, not an authority check, and it is unchanged.

Audit of ui-server for the same class. I grepped every isUsablePrincipal( and expires_at > ? site in packages/ui-server/src. Most read Date.now() or a now() clock at the check, or run synchronously with no await between stamp and check (inbox escalation, inbox notify, route guards, authGuard). Safety reviews already read the clock inside their transaction. Chat deletion is #4168. Three sites reuse a pre-wait stamp inside a writer procedure: session worker-profile save and bind, push subscription binding, and storage-notice dispatch. Issue scope excludes other callers, so they are filed as #4176 with held-writer acceptance criteria.

Proportionate containment

  • Trigger: the target or creating owner's row, read inside the write transaction, is revoked or expired at that moment. It does not cover expiry after the commit. Live-use checks (coveringReferenceGrant, isLiveReferenceGrant) cover that and are unchanged.
  • Restricts: this one grant creation request. Nothing narrower exists.
  • Restricted mode: none. A grant either exists or it does not, and the refusal is the existing one.
  • Clears: automatically. The owner signs in again and retries.
  • Cause: the 404 answer to the request. No new state.
  • Scope test: the existing route and record tests in reference-grants.test.ts still create grants for other, usable principals.

Proof

Failing-first, before the change (new describe block "reference grant creation behind a held writer" in packages/ui-server/tests/reference-grants.test.ts, real file-backed SQLite, real write worker held with holdUntilReleased, expiry set to stamp + 1 from a second connection after createGrant was posted):

error: REFERENCE_GRANT_TARGET_EXPIRY_CHECKED_AT_COMMIT
Expected constructor: [class ReferenceGrantPrincipalError extends Error]
Received value: "created"
(fail) … a target login that expires while the request waits for the writer is refused by the current time
error: REFERENCE_GRANT_CREATOR_EXPIRY_CHECKED_AT_COMMIT
Received value: "created"
(fail) … a creator login that expires while the request waits for the writer is refused by the current time
 16 pass
 2 fail

After the change: 18 pass, 0 fail.

Mutations on the fixed tree (each restored after):

Mutation Fails
target check back to input.now REFERENCE_GRANT_TARGET_EXPIRY_CHECKED_AT_COMMIT (17 pass, 1 fail)
creator check back to input.now REFERENCE_GRANT_CREATOR_EXPIRY_CHECKED_AT_COMMIT (17 pass, 1 fail)
retention cutoff from the in-transaction clock REFERENCE_GRANT_RETENTION_USES_THE_STAMP (17 pass, 1 fail)
createdAt from the in-transaction clock REFERENCE_GRANT_CREATED_AT_IS_THE_STAMP (17 pass, 1 fail)

Also: bun run test on reference-grants, principal-routes, principals and github-reference-access tests gave 64 pass, 0 fail. Root bun run typecheck is clean. No exports changed.

Pinned-clock fixture (second commit)

Candidate confirmation on 04762013b failed in unit (4): eight tests in packages/ui-server/tests/reference-writes-offloop.test.ts threw ReferenceGrantPrincipalError while building their fixture. That fixture runs on the pinned 2026-07-12 clock and gives its owner login expires_at = AT + 1 day, so in real time the login expired months ago. The old check passed only because it trusted the caller's now: AT. One of its tests also advances the fake clock two days to end that authority, so the expiry cannot simply move into the real future.

The fixture was wrong, not the check. The only production caller is the write-worker procedure behind the route, which runs on the real clock, so an injectable clock would be a seam that only tests use. The fixture needs a recorded grant, not a request, so it now inserts the grant row at AT, as it already inserts the principal. Production code is unchanged in this commit.

  • Before: 0 pass, 8 fail. After: 8 pass, 0 fail.
  • The row carries the authority: recording it under another brain fails REFERENCE_OPERATION_REACHED_POOL_BOUNDARY in "revocation sees a first private publication…" (7 pass, 1 fail). Restored afterwards.
  • Every other grant creator uses principals relative to the real clock and passes Date.now(), so none has this problem. These are 9 test files, experiments/git-read-qualification.test.ts and scripts/native-proof/reference-efficiency.ts, whose clock starts at Date.now().
  • Grant-touching test files run directly, since check:pr does not select them. reference-grants 18/0, reference-writes-offloop 8/0, reference-source 30/0, reference-worktrees 25/0, reference-tools 35/0, reference-worktree-hygiene 36/0, github-reference-access 19/0, account-writes-offloop 24/0.
  • Sandbox-only failures: github-reference-git (offline git child), reference-transport and http-supported-mounts (bubblewrap/user namespaces unavailable) fail identically with main's reference-grants.ts. None of their failures involve the grant refusal.
  • NODE_OPTIONS=--max-old-space-size=8192 bun run typecheck is clean.
bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"abe191496ff08d18db649ecb0c1804b3c0ac3a10","base":"origin/main","mode":"focused","startedAt":"2026-11-17T18:19:21.767Z","finishedAt":"2026-11-17T18:20:16.940Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":16598.572792},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":170.4759050000066},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":155.73637700000108},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":27.89847000000009},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":38199.580267}]}

Checklist

  • Focused local preflight/behavioral receipts passed. Selected hosted proof is pending.
  • Acceptance criteria from the issue are all met.
  • A PR touching packages/ui-server/ carries a recorded independent review of its head. The orchestrator requests it.
  • The PR and ui-server: reference-grant creation checks login expiry against a time stamped before the writer wait #4169 carry milestone 0.45.0.
  • A changeset is included (.changeset/4169-reference-grant-expiry-clock.md).
  • No personal data anywhere in the diff.
  • Nothing in the machine surface moved (no shape change; a request that used to succeed on an expired login is now refused).
  • Nothing documented moved.
  • The changed guard answers the proportionate-containment checklist above.
  • Nothing on a turn's path changed.

Anything left open

🤖 Generated with Claude Code

…inside the writer transaction (#4169)

Grant creation compared the target and creating owner's expiry with the
time the route stamped before it waited for the write worker, so a login
that expired while queued still passed. Read the clock at the check; the
stamp still records created_at and drives retention.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 17, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. security Auth, isolation, egress, secrets, or anything in the threat model. labels Nov 17, 2026
@schlessera

schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner Author

Collision check, bun scripts/collisions.ts --json --pr 4177 at head bb8e83fc10ec87423fe6a74f3299c664423d92f9 (graph built at bb8e83fc1):

@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head bb8e83f against base c26fb22. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4177,
  "head": "bb8e83fc10ec87423fe6a74f3299c664423d92f9",
  "base": "c26fb229fa1ef920ce219e77256e859e9982c263",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

@schlessera
schlessera marked this pull request as ready for review November 17, 2026 12:52
… data (#4169)

The reference worker fixture runs on the pinned 2026-07-12 clock, and its
owner login expires a day later. Creating its grant through
createReferenceGrant now checks that login against the real clock, which
refuses it. The fixture needs a recorded grant, not a request, so insert
the row at AT, as it already does for the principal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head b939714 against base 94f5580. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4177,
  "head": "b939714e9d6945758b9db9b0c95658c64d1cbf7c",
  "base": "94f55806c3fe2686c5e026d368fbe0683fac265a",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

@schlessera
schlessera marked this pull request as draft November 17, 2026 18:44
@schlessera
schlessera marked this pull request as ready for review November 17, 2026 18:44
@schlessera
schlessera merged commit 18da31b into main Nov 17, 2026
60 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. security Auth, isolation, egress, secrets, or anything in the threat model. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-server: reference-grant creation checks login expiry against a time stamped before the writer wait

1 participant