Keep a late refresh answer from replacing a newer stored refresh token; pr edit never moves the base (CLS-1061, CLS-1063) #302

Merged
stephen merged 5 commits from stephen/cls-1061-refresh-cas into main 2026-10-31 22:43:14 +00:00
Owner

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 edit was seen to retarget a PR's base).

CLS-1063: fj pr edit and the PR's base

Seen by the web lane: after fj pr edit 1345 -R rasterstate/fjord-platform --body-file <file>, the PR's base had moved from develop to main. In fjord-platform and paragon only promotions go to main, so a silent retarget can send work reviewed for develop straight to production.

I could not reproduce it from this code. EditPull already skips None fields, and fj --debug pr edit for a body-only edit sends exactly {"body": "..."}. I created a PR on a non-default base in the private stephen/fj-cli-test repo and ran body, title, label and --attach edits 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 attach and 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 (base given) goes straight through.
  • fj pr edit --base <branch> is a new explicit flag; base is sent only when it is given.
  • run_edit is split out of edit so a test can drive the command against a mocked API.

Tests: fjord::cli::pr::tests::edit_requests asserts the request bodies on the wire. A body-only edit of a PR on develop sends only {"body": ...} (and no base), a title-only edit sends only the title, --base sends 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 main retargets and --base <old> restores it. cargo test passes (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_session holds 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 login or fj auth logout in 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_answer applies that decision with the writers passed in, and refresh_session calls 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::oidc passes (57). cargo fmt --check is clean; clippy shows nothing in the touched file (it flags chunks_exact in src/cli/pr_line_comment.rs, which is not part of this change).

Failure paths

  • Retries and idempotency: an echoed or redelivered answer never moves the stored chain backwards.
  • Identity across attempts: the key is the refresh token that was sent, compared with the stored record after the wait.
  • Failures are not successes: a signed-out session is an error, not a quiet success.
  • Partial failure: the record is still written first and the bearer second, as before.

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 through refresh_session).

Known edge cases (follow-up, none filed): self-review's Lows, deliberate.

  • The re-read and the write are separate operations, and fj auth login and fj auth logout do 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.
  • The branch that adopts a peer's bearer reads the bearer after the refresh record and does not check that the pair is complete or for the same identity, the same exposure the existing peer shortcut (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.

  • CLS-1063's guard restores the base it read before the edit, so a maintainer who retargets the PR in the instant between that read and the PATCH answer would be put back. The window is the length of one request and the alternative is leaving a possibly corrupted base alone; a conditional update needs a server revision Forgejo does not offer.
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 edit` was seen to retarget a PR's base). ## CLS-1063: `fj pr edit` and the PR's base Seen by the web lane: after `fj pr edit 1345 -R rasterstate/fjord-platform --body-file <file>`, the PR's base had moved from `develop` to `main`. In fjord-platform and paragon only promotions go to `main`, so a silent retarget can send work reviewed for `develop` straight to production. I could not reproduce it from this code. `EditPull` already skips `None` fields, and `fj --debug pr edit` for a body-only edit sends exactly `{"body": "..."}`. I created a PR on a non-default base in the private `stephen/fj-cli-test` repo and ran body, title, label and `--attach` edits 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 attach` and 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 (`base` given) goes straight through. - `fj pr edit --base <branch>` is a new explicit flag; `base` is sent only when it is given. - `run_edit` is split out of `edit` so a test can drive the command against a mocked API. Tests: `fjord::cli::pr::tests::edit_requests` asserts the request bodies on the wire. A body-only edit of a PR on `develop` sends only `{"body": ...}` (and no `base`), a title-only edit sends only the title, `--base` sends 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 main` retargets and `--base <old>` restores it. `cargo test` passes (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_session` holds 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 login` or `fj auth logout` in 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_answer` applies that decision with the writers passed in, and `refresh_session` calls 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::oidc` passes (57). `cargo fmt --check` is clean; clippy shows nothing in the touched file (it flags `chunks_exact` in `src/cli/pr_line_comment.rs`, which is not part of this change). Failure paths - Retries and idempotency: an echoed or redelivered answer never moves the stored chain backwards. - Identity across attempts: the key is the refresh token that was sent, compared with the stored record after the wait. - Failures are not successes: a signed-out session is an error, not a quiet success. - Partial failure: the record is still written first and the bearer second, as before. 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 through `refresh_session`). Known edge cases (follow-up, none filed): self-review's Lows, deliberate. - The re-read and the write are separate operations, and `fj auth login` and `fj auth logout` do 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. - The branch that adopts a peer's bearer reads the bearer after the refresh record and does not check that the pair is complete or for the same identity, the same exposure the existing peer shortcut (`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. - CLS-1063's guard restores the base it read before the edit, so a maintainer who retargets the PR in the instant between that read and the PATCH answer would be put back. The window is the length of one request and the alternative is leaving a possibly corrupted base alone; a conditional update needs a server revision Forgejo does not offer.
Settle a refresh answer through one function so a test drives the whole decision (CLS-1061)
All checks were successful
ci / check (pull_request) Successful in 20m5s
ci / coverage (pull_request) Successful in 5m2s
ci / live-e2e (pull_request) Successful in 5m29s
8bd3399f82
gerri approved these changes 2026-10-31 21:43:30 +00:00
Dismissed
gerri left a comment

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 8bd3399f82 at 2026-10-31T21:43:29Z by codex exec (read-only sandbox).

Automated Codex review (read-only) of head 8bd3399f825d8172663ffc33da53c409e7536e2a. 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 8bd3399f825d8172663ffc33da53c409e7536e2a at 2026-10-31T21:43:29Z by codex exec (read-only sandbox).
pr edit: keep the base in the shared edit, reconcile a lost answer, test the callers (CLS-1063)
Some checks failed
ci / check (pull_request) Failing after 5m16s
ci / coverage (pull_request) Has been skipped
ci / live-e2e (pull_request) Has been skipped
16ed343b72
stephen changed title from 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) 2026-10-31 22:09:14 +00:00
gerri approved these changes 2026-10-31 22:11:38 +00:00
Dismissed
gerri left a comment

Automated Codex review (read-only) of head 16ed343b72.

No new blocking findings found.

  • Low: 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 16ed343b72 at 2026-10-31T22:11:37Z by codex exec (read-only sandbox).

Automated Codex review (read-only) of head 16ed343b72ce43bc1d657b64a46b3f368a7541ba. No new blocking findings found. - **Low**: `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 16ed343b72ce43bc1d657b64a46b3f368a7541ba at 2026-10-31T22:11:37Z by codex exec (read-only sandbox).
pr edit: pass the repository context by reference (clippy)
All checks were successful
ci / check (pull_request) Successful in 20m30s
ci / coverage (pull_request) Successful in 2m43s
ci / live-e2e (pull_request) Successful in 3m31s
884b5b5197
gerri approved these changes 2026-10-31 22:18:37 +00:00
gerri left a comment

Automated Codex review (read-only) of head 884b5b5197.

No new blocking problems found against STANDARDS.md. Tests were inspected, not run.

  • Low (edge case, accepted deferrals): src/fjord/oidc.rs:1155,1177 and src/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 884b5b5197 at 2026-10-31T22:18:36Z by codex exec (read-only sandbox).

Automated Codex review (read-only) of head 884b5b51973e479c5b7b616376651829d5de2b1c. No new blocking problems found against STANDARDS.md. Tests were inspected, not run. - **Low** (edge case, accepted deferrals): `src/fjord/oidc.rs:1155,1177` and `src/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 884b5b51973e479c5b7b616376651829d5de2b1c at 2026-10-31T22:18:36Z by codex exec (read-only sandbox).
Sign in to join this conversation.
No description provided.