From 9962090295405c890a448840fa3d354da04e0f9b Mon Sep 17 00:00:00 2001 From: Max Farrell Date: Thu, 5 Nov 2026 20:24:33 -0600 Subject: [PATCH] Fix OAuth Connect needing two clicks and then failing The consent page's CSP had form-action 'self'. form-action also governs where a form submission may be redirected, so browsers dropped the 302 from Connect/Cancel to the client's registered redirect URI: the first click silently spent the one-time consent ('does nothing') and a second click failed as already used ('Connection request could not be verified'). Allow exactly the validated redirect URI's origin (or scheme for native apps), and assert it in the OAuth smoke test. Verified in a real browser: one click now reaches the client callback with code, state and iss. Co-Authored-By: Claude Opus 5.5 --- scripts/oauth-smoke.mjs | 7 +++++++ src/routes/admin/oauth/authorize/+server.ts | 20 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/oauth-smoke.mjs b/scripts/oauth-smoke.mjs index 72c6f04..62469ab 100644 --- a/scripts/oauth-smoke.mjs +++ b/scripts/oauth-smoke.mjs @@ -92,6 +92,13 @@ export async function testOAuth({ response.headers.get('content-security-policy') ?? '', /frame-ancestors 'none'/, ); + // form-action also governs the redirect after Connect/Cancel: without the client's origin the + // browser drops the 302 back to the client, so the first click "does nothing". + assert.match( + response.headers.get('content-security-policy') ?? '', + /form-action 'self' http:\/\/127\.0\.0\.1:34791;/, + 'consent form may redirect to the registered client origin, and only there', + ); // A browser sends "Origin: null" on this page's same-origin POST under 'no-referrer', which the // CSRF check rejects, so the Connect button would fail for real users. assert.equal( diff --git a/src/routes/admin/oauth/authorize/+server.ts b/src/routes/admin/oauth/authorize/+server.ts index 40d799a..d675fe1 100644 --- a/src/routes/admin/oauth/authorize/+server.ts +++ b/src/routes/admin/oauth/authorize/+server.ts @@ -19,7 +19,22 @@ const escape = (value: string) => char ]!, ); -function page(body: string, status = 200, extra?: Headers) { +// CSP form-action also governs where a form submission may be REDIRECTED. Approve and Deny answer +// with a 302 to the client's registered redirect URI, so with plain 'self' the browser silently +// drops that redirect: the first Connect click spends the one-time consent and "does nothing", and a +// second click then fails as already used. Allow exactly the validated redirect URI's origin. +function formActionSource(redirectUri: string): string | null { + try { + const u = new URL(redirectUri); + if (u.protocol === 'https:' || u.protocol === 'http:') return u.origin; + // Native-app custom schemes (e.g. cursor:) have no origin; a scheme-source covers them. + return /^[a-z][a-z0-9+.-]*:$/.test(u.protocol) ? u.protocol : null; + } catch { + return null; + } +} + +function page(body: string, status = 200, extra?: Headers, formAction?: string | null) { const headers = new Headers(extra); headers.set('Content-Type', 'text/html; charset=utf-8'); headers.set('Cache-Control', 'no-store'); @@ -29,7 +44,7 @@ function page(body: string, status = 200, extra?: Headers) { headers.set('Referrer-Policy', 'same-origin'); headers.set( 'Content-Security-Policy', - "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'", + `default-src 'none'; style-src 'unsafe-inline'; form-action 'self'${formAction ? ' ' + formAction : ''}; frame-ancestors 'none'; base-uri 'none'`, ); return new Response( `Connect to ${escape(site.adminName)}
${body}
`, @@ -90,6 +105,7 @@ export const GET: RequestHandler = async ({ request, url, locals }) => { `

${escape(site.adminName)}

Connect ${escape(description.clientName)}?

This application is requesting access to ${escape(new URL(getOAuthResource(url.origin)).host)}.

${description.clientDomain ? `

Client identity domain: ${escape(description.clientDomain)}

` : '

The application name is supplied by the application and is not a verified identity.

'}

After approval, your browser will return to ${escape(description.redirectHost)}.

${escape(description.redirectUri)}

${description.redirectIsLoopback ? '

This callback connects to an application on your computer. Another local process could be listening at this address; continue only if you started this connection.

' : ''}

Permissions

Read site content and administrative data
Required for this connection. Sensitive form answers remain excluded.

${wantsWrite ? '' : ''}${wantsOffline ? '' : ''}

You can revoke access under Connected apps.

`, 200, transaction.headers, + formActionSource(description.redirectUri), ); } catch (error) { return authorizationFailure(error);