Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/routes/oauth/authorize/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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',
Expand All @@ -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 }),
);
},
};
89 changes: 89 additions & 0 deletions src/routes/oauth/authorize/page.server.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>) {
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');
});
});
Loading