fix(auth): close the fail-open issuer check and the remaining clear races (#2144)

Copilot review rounds 8 and 9.

- A cached metadata document with no `issuer` was letting every issuer-bound
  token go to its single revocation endpoint. Absence establishes nothing, so
  that is now a mismatch: an issuer-bound grant must match, and only a legacy
  unkeyed grant — which nothing binds to a different authorization server —
  proceeds without the comparison. This was a credential-disclosure risk, not
  just a correctness one.
- The ctx-less grant read was skipped whenever it carried an issuer stamp, on
  the assumption that slot had already been enumerated. If `listIssuers` fails
  there is no enumerated slot behind the stamp, so the one readable grant went
  unrevoked while `clear` deleted it. Both reads now share one issuer+token
  key, and the stamped issuer is retained rather than discarded.
- `AuthTab`'s repeat guard read `clearState`, which is not updated until React
  re-renders, so two `s` events in one input turn both started a clear against
  the same store entry. It is a ref now, set before the call.
- A rejected clear left `ownClearRef` set — `handleClearOAuth` never reached
  its `oauthRevision` bump — so the next unrelated revision was swallowed and
  the failure banner stranded. Cleared on that path.
- The web `clearServerOAuthAndDisconnect` had the staleness the TUI path was
  already fixed for: `isActive`/`inspectorClient` are captured before a
  five-second await, so a switch during it disconnected the session the user
  had just moved to and ran the session-wide cleanup against it. Revalidated
  against `sessionRef` after each await.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177mPHAdECD18nLLCTwR5rh
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-28 04:38:38 -04:00
commit 5170cc484b

View file

@ -138,6 +138,55 @@ describe("AuthTab", () => {
expect(lastFrame() ?? "").toContain("OAuth state cleared");
});
// A state-based guard is not effective until React re-renders, so two `s`
// events in the SAME input turn both read the last-rendered "idle" and start
// concurrent clears against one store entry. No tick between the writes.
it("ignores a repeat delivered in the same input turn", async () => {
const onClearOAuth = vi.fn(() => new Promise<void>(() => {}));
const { stdin } = render(
<AuthTab
{...baseProps}
onClearOAuth={onClearOAuth}
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
focused
/>,
);
await tick();
stdin.write("s");
stdin.write("s");
await tick();
expect(onClearOAuth).toHaveBeenCalledTimes(1);
});
// A rejection releases the lock, so the user can retry.
it("allows a retry after a rejected clear", async () => {
const onClearOAuth = vi
.fn<() => Promise<void>>()
.mockRejectedValueOnce(new Error("nope"))
.mockResolvedValue(undefined);
const { stdin } = render(
<AuthTab
{...baseProps}
onClearOAuth={onClearOAuth}
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
focused
/>,
);
await tick();
stdin.write("s");
await tick();
stdin.write("s");
await tick();
expect(onClearOAuth).toHaveBeenCalledTimes(2);
});
// A rejection is NOT a revocation failure — those come back as outcomes and
// are reported through the message line. This is the local clear or the
// disconnect itself failing, so reporting success would be a plain lie.