feat(auth): harden OAuth state generation and callback validation (closes #1562)

generateOAuthState() now throws when crypto.getRandomValues is unavailable
instead of silently degrading to a non-cryptographic Math.random() fallback —
OAuth state is a CSRF token and must be unpredictable.

The web /oauth/callback handler now rejects a returned `state` param that does
not parse to the expected 64-char-hex authId shape (a forgery indicator),
surfacing a clear error toast instead of proceeding with an undefined session.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S3fTN8H3R8YV4yUGvZjYnX
This commit is contained in:
cliffhall 2026-07-01 18:23:38 -04:00
commit 2b84281346

View file

@ -1472,10 +1472,15 @@ function App() {
// session key the pre-redirect page saved the fetch log under, so the
// rebuilt client can restore those `auth` entries. Read it before the
// URL is cleared below.
//
// CSRF hardening: a `state` that is present but does not parse to our
// expected 64-char-hex authId shape is a forgery indicator — the value
// did not originate from `generateOAuthState`. Reject the callback with a
// clear error instead of silently proceeding with an undefined sessionId.
const stateParam = new URLSearchParams(window.location.search).get("state");
const sessionId = stateParam
? (parseOAuthState(stateParam)?.authId ?? undefined)
: undefined;
const parsedState = stateParam ? parseOAuthState(stateParam) : null;
const stateRejected = stateParam !== null && parsedState === null;
const sessionId = parsedState?.authId ?? undefined;
const pendingId =
window.sessionStorage.getItem(OAUTH_PENDING_SERVER_KEY) ?? undefined;
window.sessionStorage.removeItem(OAUTH_PENDING_SERVER_KEY);
@ -1484,6 +1489,16 @@ function App() {
// the (now single-use) authorization code through the exchange again.
window.history.replaceState({}, "", "/");
if (stateRejected) {
notifications.show({
title: "OAuth callback rejected",
message:
"OAuth callback carried an unrecognized state parameter; rejecting to prevent a cross-site request. Please try connecting again.",
color: "red",
});
return;
}
if (!params.successful) {
notifications.show({
title: "OAuth authorization failed",