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( `
${escape(site.adminName)}
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)}
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.
' : ''}