Skip to content

Commit 7f4ab16

Browse files
authored
fix: disable CSRF checks in dev (#14335)
See #14309 (comment) closes #14309
1 parent be0f8f6 commit 7f4ab16

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

.changeset/modern-icons-film.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: disable CSRF checks in dev

packages/kit/src/exports/public.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ export interface KitConfig {
437437
*
438438
* If the array contains `'*'`, all origins will be trusted. This is generally not recommended!
439439
*
440-
* **Warning**: Only add origins you completely trust, as this bypasses CSRF protection for those origins.
440+
* > [!NOTE] Only add origins you completely trust, as this bypasses CSRF protection for those origins.
441+
*
442+
* CSRF checks only apply in production, not in local development.
441443
* @default []
442444
* @example ['https://checkout.stripe.com', 'https://accounts.google.com']
443445
*/

packages/kit/src/runtime/server/respond.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,34 @@ export async function internal_respond(request, options, manifest, state) {
7373
const is_data_request = has_data_suffix(url.pathname);
7474
const remote_id = get_remote_id(url);
7575

76-
const request_origin = request.headers.get('origin');
76+
if (!DEV) {
77+
const request_origin = request.headers.get('origin');
7778

78-
if (remote_id) {
79-
if (request.method !== 'GET' && request_origin !== url.origin) {
80-
const message = 'Cross-site remote requests are forbidden';
81-
return json({ message }, { status: 403 });
82-
}
83-
} else if (options.csrf_check_origin) {
84-
const forbidden =
85-
is_form_content_type(request) &&
86-
(request.method === 'POST' ||
87-
request.method === 'PUT' ||
88-
request.method === 'PATCH' ||
89-
request.method === 'DELETE') &&
90-
request_origin !== url.origin &&
91-
(!request_origin || !options.csrf_trusted_origins.includes(request_origin));
92-
93-
if (forbidden) {
94-
const message = `Cross-site ${request.method} form submissions are forbidden`;
95-
const opts = { status: 403 };
96-
97-
if (request.headers.get('accept') === 'application/json') {
98-
return json({ message }, opts);
79+
if (remote_id) {
80+
if (request.method !== 'GET' && request_origin !== url.origin) {
81+
const message = 'Cross-site remote requests are forbidden';
82+
return json({ message }, { status: 403 });
9983
}
84+
} else if (options.csrf_check_origin) {
85+
const forbidden =
86+
is_form_content_type(request) &&
87+
(request.method === 'POST' ||
88+
request.method === 'PUT' ||
89+
request.method === 'PATCH' ||
90+
request.method === 'DELETE') &&
91+
request_origin !== url.origin &&
92+
(!request_origin || !options.csrf_trusted_origins.includes(request_origin));
93+
94+
if (forbidden) {
95+
const message = `Cross-site ${request.method} form submissions are forbidden`;
96+
const opts = { status: 403 };
97+
98+
if (request.headers.get('accept') === 'application/json') {
99+
return json({ message }, opts);
100+
}
100101

101-
return text(message, opts);
102+
return text(message, opts);
103+
}
102104
}
103105
}
104106

packages/kit/test/apps/basics/test/server.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ test.describe('Cookies', () => {
6161
});
6262

6363
test.describe('CSRF', () => {
64+
if (process.env.DEV) {
65+
return;
66+
}
67+
6468
test('Blocks requests with incorrect origin', async ({ baseURL }) => {
6569
const content_types = [
6670
'application/x-www-form-urlencoded',

packages/kit/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ declare module '@sveltejs/kit' {
413413
*
414414
* If the array contains `'*'`, all origins will be trusted. This is generally not recommended!
415415
*
416-
* **Warning**: Only add origins you completely trust, as this bypasses CSRF protection for those origins.
416+
* > [!NOTE] Only add origins you completely trust, as this bypasses CSRF protection for those origins.
417+
*
418+
* CSRF checks only apply in production, not in local development.
417419
* @default []
418420
* @example ['https://checkout.stripe.com', 'https://accounts.google.com']
419421
*/

0 commit comments

Comments
 (0)