Skip to content

fix(ui-server): check login expiry against the clock inside the writer for profile, push, storage-notice and hygiene writes (#4176) - #4181

Merged
schlessera merged 2 commits into
mainfrom
fix/4176-writer-clock-expiry
Nov 17, 2026
Merged

schlessera merged 2 commits into
mainfrom
fix/4176-writer-clock-expiry

Conversation

@schlessera

@schlessera schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #4176

What changed

Five write-worker procedures checked a login's expiry against a time the caller stamped before it waited for the write worker. So a login that expired while the request was queued behind other writes still passed. Each check now reads the clock inside the writer:

  • Session worker profiles. saveProfile and bindDraftProfile call authorizeProfile(db, principalId, Date.now()) inside their transaction. A login that expired while queued is refused with SessionProfileError 401, and nothing is written. A bind leaves its draft in place.
  • Push subscription binding. The worker's INSERT … SELECT … WHERE expires_at > ? binds Date.now() for the expiry check. Such a login binds no device, and subscribe answers false.
  • Storage notice dispatch. begin decides the recipient with owner(sub.principal_id, Date.now()). Such a login gets no attempt (null), and no receipt row is written.
  • Hygiene review (a fourth site, found in this audit and fixed here). hygiene-writes.ts's worker authority() checked isUsablePrincipal(p, now()), and now() is the time that hygiene-review.ts stamps with now() before write(...). Every hygiene worker procedure that calls authority now checks against Date.now(), and refuses with HygieneAuthorityError.

At every site, the stamp is still used wherever it records a time: updated_at (profiles), created_at (push), attempted_at (storage receipts) and the hygiene card times. It still drives the draft-preference cleanup, and it still runs the storage notifier's reversed-clock guard, eligibility windows and quiet hours. Revocation is unchanged in every case, because it was already read from the row inside the transaction.

Why this shape

This is the same pattern as #4177 (#4169) and the second review finding on #4168: authority is checked against the clock at the check, and the request's stamp is kept for recorded times. Replacing the stamp everywhere would also move recorded timestamps, retention and the storage notifier's time windows, which the issue keeps unchanged. Tests also inject a clock as the stamp, so the tests that drive quiet hours and retry windows with a fixed clock still do.

Audit for the same class. I read every write-worker module under packages/ui-server/src (export const url = import.meta.url) that checks isUsablePrincipal, expires_at or principal ownership:

The only further site was the hygiene review, which is fixed here.

Proportionate containment

  • Trigger: the principal row, read inside the writer at the moment it runs, is expired. Expiry after the commit is covered by the existing live-use checks, which are unchanged.
  • Restricts: the one write request. Nothing narrower exists.
  • Restricted mode: none. Each refusal is the one the site already had (401, false, null, HygieneAuthorityError).
  • Clears: automatically. The user signs in again and retries. A storage notice is retried on the next pass for a usable owner.
  • Cause: each request's existing refusal answer. No new state.
  • Scope test: the existing tests in the touched files still pass for other, usable principals.

Proof

Held-writer tests run on real file-backed SQLite and the real write worker, held with holdUntilReleased. A new shared helper, packages/ui-server/tests/helpers/held-writer.ts, spies on Worker.prototype.postMessage to see when the procedure is posted behind the hold. It then sets the login's expires_at to one millisecond past the latest possible stamp from a second connection, and waits for the clock to pass it.

Failing-first, before the change:

error: NO_PROFILE_RECORDED   (save committed; the later read() refused, the row was written)
(fail) a login that expires while a profile save waits for the writer is refused by the current time (#4176)
error: PROFILE_BIND_EXPIRY_CHECKED_AT_COMMIT: expected a rejection, but the promise resolved
(fail) a login that expires while a profile bind waits for the writer is refused by the current time (#4176)
error: PUSH_SUBSCRIBE_EXPIRY_CHECKED_AT_COMMIT
(fail) push subscription behind a held writer > a login that expires while the subscription waits for the writer binds nothing (#4176)
error: STORAGE_NOTICE_OWNER_EXPIRY_CHECKED_AT_COMMIT
(fail) an owner login that expires while a storage notice waits for the writer records no attempt (#4176)
error: HYGIENE_AUTHORITY_EXPIRY_CHECKED_AT_COMMIT: expected a rejection, but the promise resolved
(fail) a reviewer login that expires while a hygiene command waits for the writer is refused by the current time (#4176)
 3 pass
 5 fail

The three stamp tests pass both before and after the change. After the change, the four touched test files (app-writes-offloop, push, storage-offloop, inbox-writes-offloop) give 56 pass, 0 fail.

Mutations on the fixed tree. Each was restored afterwards, and each failed only the named assertion:

Mutation File Fails
save authority back to the stamp src/ws/session-profile-writes.ts NO_PROFILE_RECORDED (7 pass 1 fail)
bind authority back to the stamp src/ws/session-profile-writes.ts PROFILE_BIND_EXPIRY_CHECKED_AT_COMMIT (7 pass 1 fail)
subscribe expiry back to the stamp src/activity/push-writes.ts PUSH_SUBSCRIBE_EXPIRY_CHECKED_AT_COMMIT (7 pass 1 fail)
storage notice owner back to the stamp src/storage/notify-writes.ts STORAGE_NOTICE_OWNER_EXPIRY_CHECKED_AT_COMMIT (7 pass 1 fail)
hygiene authority back to the stamp src/inbox/hygiene-writes.ts HYGIENE_AUTHORITY_EXPIRY_CHECKED_AT_COMMIT (7 pass 1 fail)
draft save updated_at from the clock src/ws/session-profile-writes.ts PROFILE_SAVE_UPDATED_AT_IS_THE_STAMP (7 pass 1 fail)
draft cleanup from the clock src/ws/session-profile-writes.ts PROFILE_DRAFT_CLEANUP_USES_THE_STAMP (7 pass 1 fail)
bind updated_at from the clock src/ws/session-profile-writes.ts PROFILE_BIND_UPDATED_AT_IS_THE_STAMP (7 pass 1 fail)
subscribe created_at from the clock src/activity/push-writes.ts PUSH_SUBSCRIBE_CREATED_AT_IS_THE_STAMP (7 pass 1 fail)
storage attempted_at from the clock src/storage/notify-writes.ts STORAGE_NOTICE_ATTEMPTED_AT_IS_THE_STAMP (7 pass 1 fail)

Also:

  • Root bun run typecheck is clean. No exports changed.
  • hygiene-review.test.ts and governance-policy.test.ts fail the same set of test names with origin/main's source as with this change. Those failures come from the sandbox: the agent worker boundary is unavailable because unshare is not permitted.
bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"3164ec215c98b15904e6ccb2915bbdf0ae70a1de","base":"origin/main","mode":"focused","startedAt":"2026-11-17T11:36:12.338Z","finishedAt":"2026-11-17T11:37:03.159Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":16356.592958999998},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":134.7701920000036},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":117.87566099999822},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":21.399405000003753},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":34171.427644999996}]}

Checklist

  • Focused local preflight/behavioral receipts passed. Selected hosted proof is pending.
  • Acceptance criteria from the issue are all met. A fourth site (hygiene review) is fixed and proven the same way.
  • A PR touching packages/ui-server/ carries a recorded independent review of its head. The orchestrator requests it.
  • The PR and ui-server: profile, push-subscribe and storage-notice writes check login expiry against a time stamped before the writer wait #4176 carry milestone 0.45.0.
  • A changeset is included (.changeset/4176-writer-clock-expiry.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 guards answer the proportionate-containment checklist above.
  • Nothing on a turn's path changed.

Anything left open

🤖 Generated with Claude Code

…r for profile, push, storage-notice and hygiene writes (#4176)

Session worker-profile save and bind, push subscription binding, storage
notice dispatch and the hygiene review's worker procedures checked a
login's expiry against a time the caller stamped before waiting for the
write worker, so a login that expired while queued still passed. Each
check now reads the clock inside the writer; the stamp still records
updated_at, created_at and attempted_at, drives the draft cleanup and
orders the storage notifier's time windows.

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

Copy link
Copy Markdown
Owner Author

Collisions at head 3164ec2 (bun scripts/collisions.ts --json --pr 4181): closure-only overlaps with open PRs #3056, #3059, #3069, #3581, #3622, #4002, #4151, #4157, #4166, #4168, #4172, #4173, #4175, #4177, #4179, #4180 (shared downstream test consumers). No path or unknown collisions: no other open PR changes a file this PR changes.

@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 3164ec2 against base b77f5d6. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4181,
  "head": "3164ec215c98b15904e6ccb2915bbdf0ae70a1de",
  "base": "b77f5d6ece1bb2d82f9ed4daaea21d2a679db280",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

@schlessera
schlessera marked this pull request as ready for review November 17, 2026 15:17
@schlessera
schlessera merged commit 1cd5fd4 into main Nov 17, 2026
57 of 60 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: profile, push-subscribe and storage-notice writes check login expiry against a time stamped before the writer wait

1 participant