From 19455e184b96187896b45a22eea965908ed5bfcc Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Mon, 30 Nov 2026 21:17:57 +0100 Subject: [PATCH] fix(oauth): declarar el origen del redirect_uri en los redirects externos SvelteKit 3 rechaza redirect() a otro origen sin { external }: al pulsar Permitir/Denegar en /oauth/authorize daba 500 redirect_external_not_allowed. Co-Authored-By: Claude Opus 5.5 --- src/routes/oauth/authorize/+page.server.ts | 21 +++-- .../oauth/authorize/page.server.test.ts | 89 +++++++++++++++++++ 2 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/routes/oauth/authorize/page.server.test.ts diff --git a/src/routes/oauth/authorize/+page.server.ts b/src/routes/oauth/authorize/+page.server.ts index c192c99..da83691 100644 --- a/src/routes/oauth/authorize/+page.server.ts +++ b/src/routes/oauth/authorize/+page.server.ts @@ -49,6 +49,14 @@ function errorRedirect( }); } +/** + * 303 to the client's `redirect_uri`. SvelteKit 3 refuses external redirects unless their origin + * is declared; this one is safe because the URI was matched exactly against the registered ones. + */ +function redirectToClient(redirectUri: string, location: string): never { + throw redirect(303, location, { external: [new URL(redirectUri).origin] }); +} + export async function load({ url, setHeaders }) { if (!oauthService.isEnabled()) throw error(404, 'Not found'); // the consent screen must not be framed (clickjacking on "Allow") @@ -57,7 +65,7 @@ export async function load({ url, setHeaders }) { const validation = await oauthService.validateAuthorizeRequest(readParams(url.searchParams)); if (!validation.ok) { if (validation.kind === 'page') return { invalid: validation.description }; - throw redirect(303, errorRedirect(validation, oauthIssuer(url))); + redirectToClient(validation.redirectUri, errorRedirect(validation, oauthIssuer(url))); } return { @@ -77,13 +85,13 @@ export const actions = { const validation = await oauthService.validateAuthorizeRequest(readParams(form)); if (!validation.ok) { if (validation.kind === 'page') throw error(400, validation.description); - throw redirect(303, errorRedirect(validation, oauthIssuer(url))); + redirectToClient(validation.redirectUri, errorRedirect(validation, oauthIssuer(url))); } const issuer = oauthIssuer(url); if (form.get('decision') !== 'allow') { - throw redirect( - 303, + redirectToClient( + validation.redirectUri, callbackUrl(validation.redirectUri, issuer, validation.state, { error: 'access_denied', error_description: 'The user denied the request', @@ -98,6 +106,9 @@ export const actions = { codeChallenge: validation.codeChallenge, resource: validation.resource, }); - throw redirect(303, callbackUrl(validation.redirectUri, issuer, validation.state, { code })); + redirectToClient( + validation.redirectUri, + callbackUrl(validation.redirectUri, issuer, validation.state, { code }), + ); }, }; diff --git a/src/routes/oauth/authorize/page.server.test.ts b/src/routes/oauth/authorize/page.server.test.ts new file mode 100644 index 0000000..3879190 --- /dev/null +++ b/src/routes/oauth/authorize/page.server.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it, vi } from 'vitest'; + +const REDIRECT = 'https://claude.ai/api/mcp/auth_callback'; +const validation = vi.hoisted(() => ({ value: null as any })); + +vi.mock('$app/env/private', () => ({ ORIGIN: 'https://gitops.example.com' })); +vi.mock('#modules/auth', () => ({ + oauthService: { + isEnabled: () => true, + validateAuthorizeRequest: async () => validation.value, + issueCode: () => 'signed-code', + }, +})); + +// the real `redirect` of @sveltejs/kit: it is what refuses undeclared external origins +import { actions, load } from './+page.server'; + +const ok = { + ok: true, + client: { clientId: 'goc_1', clientName: 'Claude' }, + redirectUri: REDIRECT, + codeChallenge: 'c', + resource: 'https://mcp.example.com/mcp', + state: 'st', +}; + +async function redirected(promise: Promise) { + try { + await promise; + } catch (error: any) { + return { status: error.status, location: new URL(error.location) }; + } + throw new Error('expected a redirect'); +} + +function decide(decision: string) { + return actions.decision({ + request: new Request('https://gitops.example.com/oauth/authorize?/decision', { + method: 'POST', + body: new URLSearchParams({ decision }), + }), + locals: { user: { id: 'user-1' } }, + url: new URL('https://gitops.example.com/oauth/authorize'), + } as any); +} + +describe('/oauth/authorize redirects to the client', () => { + it('allow: 303 with code, state and iss', async () => { + validation.value = ok; + const { status, location } = await redirected(decide('allow')); + expect(status).toBe(303); + expect(location.origin + location.pathname).toBe(REDIRECT); + expect(Object.fromEntries(location.searchParams)).toEqual({ + code: 'signed-code', + state: 'st', + iss: 'https://gitops.example.com', + }); + }); + + it('deny: 303 with access_denied', async () => { + validation.value = ok; + const { status, location } = await redirected(decide('deny')); + expect(status).toBe(303); + expect(location.searchParams.get('error')).toBe('access_denied'); + expect(location.searchParams.get('state')).toBe('st'); + expect(location.searchParams.get('iss')).toBe('https://gitops.example.com'); + }); + + it('load with a redirectable error: 303 with the error', async () => { + validation.value = { + ok: false, + kind: 'redirect', + redirectUri: REDIRECT, + error: 'invalid_request', + description: 'bad', + state: 'st', + }; + const { status, location } = await redirected( + load({ + url: new URL('https://gitops.example.com/oauth/authorize'), + setHeaders: () => {}, + } as any), + ); + expect(status).toBe(303); + expect(location.origin).toBe('https://claude.ai'); + expect(location.searchParams.get('error')).toBe('invalid_request'); + expect(location.searchParams.get('iss')).toBe('https://gitops.example.com'); + }); +});