Keep a late refresh answer from replacing a newer stored refresh token; pr edit never moves the base (CLS-1061, CLS-1063) #302
Loading…
Reference in a new issue
No description provided.
Delete branch "stephen/cls-1061-refresh-cas"
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?
Two fixes in one PR, since the lead asked for them together and fj gets one PR at a time: CLS-1061 (a late refresh answer must not replace a newer stored refresh token) and CLS-1063 (a
fj pr editwas seen to retarget a PR's base).CLS-1063:
fj pr editand the PR's baseSeen by the web lane: after
fj pr edit 1345 -R rasterstate/fjord-platform --body-file <file>, the PR's base had moved fromdeveloptomain. In fjord-platform and paragon only promotions go tomain, so a silent retarget can send work reviewed fordevelopstraight to production.I could not reproduce it from this code.
EditPullalready skipsNonefields, andfj --debug pr editfor a body-only edit sends exactly{"body": "..."}. I created a PR on a non-default base in the privatestephen/fj-cli-testrepo and ran body, title, label and--attachedits through the installed fj and the gateway: the base stayed put every time (the PR is closed and its branches deleted again). So whatever moved #1345 was not this request body on this build: an older binary, or the forge. The change makes that not matter:api::pull::edit(the shared PATCH every caller uses:pr edit,pr attachand attachment delivery, close and reopen,ready, stack annotations) now keeps the base for any change that is not a retarget. It reads the PR first, and if the base on the answer differs it puts the base back and fails with a message that says what happened (the base of PR #N changed from develop to main during the edit; the base was put back). If the answer to the PATCH is lost, the PR is looked at again, because the forge may have applied it. If the base cannot be put back the error says how:fj pr edit N --base develop. A retarget (basegiven) goes straight through.fj pr edit --base <branch>is a new explicit flag;baseis sent only when it is given.run_editis split out ofeditso a test can drive the command against a mocked API.Tests:
fjord::cli::pr::tests::edit_requestsasserts the request bodies on the wire. A body-only edit of a PR ondevelopsends only{"body": ...}(and nobase), a title-only edit sends only the title,--basesends only{"base": ...}, a base the forge moved anyway is put back and reported, one that cannot be put back says how to set it, a lost answer after the forge moved the base is reconciled, and closing goes through the same guard. Three existing tests that mocked only the PATCH (pull_edit_can_close,pull_ready_strips_the_wip_prefix_from_the_title,annotate_prs_patches_only_changed_bodies) now also mock the GET the guard makes. I also ran the built binary against the sandbox PR: a body edit keeps the base,--base mainretargets and--base <old>restores it.cargo testpasses (1369, plus the integration tests).The cost is one extra GET per edit.
CLS-1061: refresh answers
A refresh answer can no longer overwrite a newer stored Fjord refresh token with a spent one (CLS-1061, from PLAT-688 / fjord-platform#1350). The iOS and Android clients got the same rule (fjord-ios #2203, fjord-android #1065).
Paragon now answers a retired refresh token presented inside its 30 s reuse window with a fresh access token and the token it was sent. So if two refreshes of one chain race, the loser's answer carries the spent token, and storing it over the winner's replacement leaves the chain on a token that is revoked once the window closes: a forced sign-out.
Was fj exposed? Not through its own refreshes.
refresh_sessionholds the cross-process lock across the exchange and the persist, so two fj processes never overlap, and a peer that already replaced the bearer short-circuits the exchange. What it did not do is look at the stored record again once the answer was back, so a writer that does not take the lock (an older fj,fj auth loginorfj auth logoutin another terminal) could be overwritten, or a signed-out session brought back, by an answer that arrived late. This closes that.answer_action(pure) compares the refresh token this exchange sent with the record as it is after the network wait: still the same, store the answer; a different one, the chain moved on, keep what is stored and carry on with the stored bearer; no record, the session ended meanwhile, fail with the sign-in hint instead of writing it back.settle_refresh_answerapplies that decision with the writers passed in, andrefresh_sessioncalls it with the real ones, so a test drives the whole decision and sees whether the store writer is invoked.Tests (
fjord::oidc::tests, 5 new): the stored-token, moved-on and signed-out decisions;a_late_answer_repeating_the_spent_token_leaves_the_rotated_token_stored(the rotating answer settles first and is written, the answer that repeats the spent token settles last, the writer is not invoked for it, the stored token stays the rotated one and the caller gets the stored bearer); a signed-out session is not written; a moved-on chain with no bearer left is an error, not a write.cargo test --bin fj fjord::oidcpasses (57).cargo fmt --checkis clean; clippy shows nothing in the touched file (it flagschunks_exactinsrc/cli/pr_line_comment.rs, which is not part of this change).Failure paths
Fix scope: invariant is that a refresh answer can only move the stored chain forward. Paths checked: explicit
fj auth refresh, the instance-list retry and the shared client's 401 retry (all three go throughrefresh_session).Known edge cases (follow-up, none filed): self-review's Lows, deliberate.
fj auth loginandfj auth logoutdo not take the refresh lock, so a login or logout that lands in the instant between them can still be overwritten or undone. Closing it means making login and logout take the same lock, a larger change to the auth flow than this fix.bearer_replaced_by_peer) already had. Writers are ordered refresh first, bearer second, so the window is a crash or a failed write between them.Verified locally:
cargo fmt --check,cargo test --bin fj fjord::oidc. Self-review: two rounds, no blocking findings.Automated Codex review (read-only) of head
8bd3399f82.No new blocking findings against STANDARDS.md. The three refresh entry points use the updated settlement path.
Low (edge case),
src/fjord/oidc.rs:1155: Invariant: a late answer must not overwrite a newer session or undo logout. The reread and write remain separate, so an unlocked login or logout between them can still be overwritten. Fix: make login and logout acquire the refresh lock. Already accepted in “Known edge cases (follow-up).”Low (edge case),
src/fjord/oidc.rs:1181: Invariant: an adopted bearer must belong to the stored refresh session. After login writes a replacement refresh record, a crash or bearer-write failure can leave the old bearer, which this branch returns successfully. Fix: validate the pair’s identity and completeness before adoption, including the existing peer shortcut. Already accepted in “Known edge cases (follow-up).”BLOCKING: 0
Reviewed head
8bd3399f82at 2026-10-31T21:43:29Z by codex exec (read-only sandbox).Keep a late refresh answer from replacing a newer stored refresh token (CLS-1061)to Keep a late refresh answer from replacing a newer stored refresh token; pr edit never moves the base (CLS-1061, CLS-1063)Automated Codex review (read-only) of head
16ed343b72.No new blocking findings found.
src/fjord/oidc.rs:1152,src/fjord/oidc.rs:1181,src/api/pull_core.rs:284. Edge case, accepted deferrals: The PR body already identifies the remaining concurrency invariants: refresh settlement must preserve intervening login/logout changes, an adopted bearer must match its refresh record, and base restoration must preserve an intentional concurrent retarget. The separate reads and writes still permit those narrow races or partial-write windows. Impacts include resurrecting a signed-out session, adopting a mismatched bearer, or undoing a maintainer’s retarget. Follow-up fixes are shared locking across auth writers, coherent credential-pair validation, and conditional base updates when supported. No new evidence warrants making these blocking.Reviewed against
STANDARDS.md, including shared edit callers and all three refresh entry points. Tests were inspected, not run, under the read-only constraint.BLOCKING: 0
Reviewed head
16ed343b72at 2026-10-31T22:11:37Z by codex exec (read-only sandbox).Automated Codex review (read-only) of head
884b5b5197.No new blocking problems found against STANDARDS.md. Tests were inspected, not run.
src/fjord/oidc.rs:1155,1177andsrc/api/pull_core.rs:285. The PR body already documents the remaining atomicity and identity invariants: a concurrent login/logout can race refresh persistence, an incomplete credential pair can supply the wrong bearer, and a concurrent intentional retarget can be undone. These affect all callers through the shared helpers. Follow-up fixes are shared credential locking, validation of the credential pair, and conditional base restoration. No new evidence warrants escalating these accepted cases.BLOCKING: 0
Reviewed head
884b5b5197at 2026-10-31T22:18:36Z by codex exec (read-only sandbox).