Skip to content

Commit cae82f7

Browse files
committed
Check the request's own URL and keep the check during prerender, as SvelteKit did
Review of b1bc0be: SvelteKit's event.url drops suffixes such as /__data.json, so /api/auth/oauth2/token/__data.json was exempt too; the check now reads request.url, and tests cover that path and a percent-encoded spelling. It now runs whenever SvelteKit's did (built servers, prerender included). The requests SvelteKit answers before any hook (a trailing-slash 308, /_app/env.js, __route.js lookups) run no handler and change nothing; the comment records them. The built-server test now asserts Better Auth's OAuth error for the token request instead of only the absence of SvelteKit's refusal. pnpm check, pnpm lint, pnpm test pass.
1 parent b1bc0be commit cae82f7

4 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎src/hooks.server.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,16 @@ test("native OAuth clients' token requests reach Better Auth, while other cross-
535535
headers: form_headers,
536536
body: 'grant_type=authorization_code&code=made-up&client_id=made-up&code_verifier=made-up'
537537
});
538-
assert.notEqual(token.status, 403, token.body);
539-
assert.doesNotMatch(token.body, /Cross-site/);
538+
assert.ok([400, 401].includes(token.status), `${token.status} ${token.body}`);
539+
assert.equal(typeof JSON.parse(token.body).error, 'string', token.body);
540540

541541
const revoke = await send('/api/auth/oauth2/revoke', {
542542
host: loopback_host,
543543
method: 'POST',
544544
headers: form_headers,
545545
body: 'token=made-up&client_id=made-up'
546546
});
547+
assert.ok([200, 400, 401].includes(revoke.status), `${revoke.status} ${revoke.body}`);
547548
assert.doesNotMatch(revoke.body, /Cross-site/);
548549

549550
for (const headers of [form_headers, { ...form_headers, origin: 'https://evil.example' }]) {

‎src/hooks.server.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export const handle: Handle = async ({ event, resolve }) => {
1212
event.locals.user = null;
1313
event.locals.is_local_development = false;
1414

15-
// SvelteKit's own cross-site form check, run first as SvelteKit ran it (and, like it, only in
16-
// built servers), except on the endpoints native OAuth clients post to.
17-
if (!dev && !building) {
18-
const refusal = refuse_cross_site_form(event.request, event.url);
15+
// SvelteKit's own cross-site form check, run first, in built servers as SvelteKit ran it,
16+
// except on the endpoints native OAuth clients post to (src/lib/server/cross_site_forms.ts).
17+
if (!dev) {
18+
const refusal = refuse_cross_site_form(event.request);
1919
if (refusal) return refusal;
2020
}
2121

‎src/lib/server/cross_site_forms.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function form_post(path: string, headers: Record<string, string> = {}, method =
1212
headers: { 'content-type': 'application/x-www-form-urlencoded', ...headers },
1313
body: 'grant_type=authorization_code'
1414
});
15-
return refuse_cross_site_form(request, url);
15+
return refuse_cross_site_form(request);
1616
}
1717

1818
test('a native client may post forms to the token and revocation endpoints with no Origin', () => {
@@ -24,6 +24,8 @@ test('a native client may post forms to the token and revocation endpoints with
2424
test('every other path still refuses a form post from another site or with no Origin, as SvelteKit did', async () => {
2525
for (const [path, headers] of [
2626
['/api/auth/oauth2/token/', {}],
27+
['/api/auth/oauth2/token/__data.json', {}],
28+
['/api/auth/oauth2/%74oken', {}],
2729
['/api/auth/oauth2/consent', {}],
2830
['/api/auth/sign-out', { origin: 'https://evil.example' }],
2931
['/consent', { origin: 'null' }]
@@ -56,6 +58,5 @@ test('same-site form posts, other content types, and GETs go on', () => {
5658
assert.equal(form_post('/api/auth/sign-out', { origin: ORIGIN }), null);
5759
assert.equal(form_post('/consent', { 'content-type': 'application/json' }), null);
5860

59-
const url = new URL('/consent', ORIGIN);
60-
assert.equal(refuse_cross_site_form(new Request(url), url), null);
61+
assert.equal(refuse_cross_site_form(new Request(new URL('/consent', ORIGIN))), null);
6162
});

‎src/lib/server/cross_site_forms.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
// instead, on every path but those two. They need no CSRF protection: they read no cookie, and a
77
// request only works with what the caller already holds (an authorization code with its PKCE
88
// verifier, a refresh token, or the token to revoke), never with a signed-in browser's session.
9+
//
10+
// It reads the request's own URL, not SvelteKit's `event.url`, which drops suffixes such as
11+
// `/__data.json`, so only those exact paths are exempt. SvelteKit answers a few requests before
12+
// any hook runs, and those now skip the check: a trailing-slash redirect (a 308, whose repeated
13+
// request is checked), `/_app/env.js` (public settings), and `__route.js` route lookups. None of
14+
// them runs a handler or changes anything.
915

1016
// RFC 6749's token endpoint and RFC 7009's revocation endpoint, as Better Auth's OAuth provider
1117
// serves them (see /.well-known/oauth-authorization-server).
@@ -30,7 +36,9 @@ function is_form_content_type(request: Request): boolean {
3036
* SvelteKit's refusal of a cross-site form submission, with its status and message, or null when
3137
* the request may go on. A form post to the token or revocation endpoint always goes on.
3238
*/
33-
export function refuse_cross_site_form(request: Request, url: URL): Response | null {
39+
export function refuse_cross_site_form(request: Request): Response | null {
40+
const url = new URL(request.url);
41+
3442
if (NATIVE_CLIENT_FORM_PATHS.has(url.pathname)) return null;
3543
if (!CHECKED_METHODS.has(request.method) || !is_form_content_type(request)) return null;
3644
if (request.headers.get('origin') === url.origin) return null;

0 commit comments

Comments
 (0)