fix: validate and rotate OAuth refresh tokens in one transaction #6
No reviewers
Labels
No labels
Compat/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ai-collab/agent-news-aggregator!6
Loading…
Reference in a new issue
No description provided.
Delete branch "0.7.2/oauth-refresh-hardening"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hardens
refresh_tokensinapp/oauth.py. Do not merge yet. Merging intodevrebuilds the preview host, so the merge needs a separate decision from Brandon. Base isdev, notmain, which keeps this diff out of the 12 unpromoteddevcommits. AGENTS.md says to work straight ondev, but a push todevauto-deploys the preview, and this task's scope says no deploy. The topic branch is that deviation.Defects confirmed on
origin/dev@a4387eb(old L315–341)UPDATE ... revoked = 1+commit()(L334–335) ran before the resource check (L339). A resource mismatch burned the client's only refresh token, and no replacement was issued.with locked()._issue_tokensthen took the lock again and committed separately. If issuance failed, the client was left with no valid refresh token. Across processes, theSELECTran outside any transaction. Two processes could both readrevoked = 0, and the old code had no compare-and-swap.row["revoked"] or expiredtook the family-revocation branch, so an idle client's whole family was revoked.Change
BEGIN IMMEDIATEtransaction under the RLock does the spend (a compare-and-swap:WHERE revoked = 0, rowcount must be 1) and inserts the new access + refresh rows, then commits. Any exception rolls the whole thing back._issue_tokensis split into_insert_token_rows(the caller owns the transaction) plus a committing wrapper.exchange_codebehaves the same as before.unknown_token,client_mismatch,reuse_family_revoked,expired,resource_mismatch,rotation_error_rolled_back) and the eventoutcome=rotated. No token or token hash is logged.client_idis sanitized ([A-Za-z0-9._-], ≤64 chars).v0.7.3tag is not pushed; it should be tagged when this lands ondev.Concurrency semantics
revoked = 1, which counts as replay, so the family (including the winner's new pair) is revoked. This strict behavior is unchanged on purpose; there is no grace window.BEGIN IMMEDIATEtakes the write lock before theSELECT, so the check and the spend are serialized, with the compare-and-swap as a second guard. A competitor waits up to the sqlite3 default 5 s busy timeout. If it times out, it getsOperationalError(HTTP 500) and nothing is mutated.Tests (
tests/test_oauth_refresh.py, 11 new; full suite 42 passed ×5 runs;ruff check+ruff format --checkclean)refresh token expired, family untouched, the access token still validinvalid_target, no rows written, and the token is not burned (it rotates afterwards)reused, family revokedreusedAgainst the old
oauth.py, 6 of the 11 new tests fail. The expiry, resource and rollback failures are real defects. The concurrency and log failures are partly the changed error description and the new log lines.Open risks and follow-ups
exchange_codehas the same split-commit pattern: it marks the code used and commits, then issues tokens in a second commit. Not changed here; this should be a follow-up issue.docs/design.md.