Skip to content

Commit 656f1ec

Browse files
ndom91ThangHuuVu
andauthored
fix(sveltekit): remmove redundant csrf check (#10963)
* fix: rm unnecessary csrfCheck in sveltekit * fix: rm unnecessary csrf check in webauthn signIn() * fix: drop more unnecessary skipCSRFCheck calls --------- Co-authored-by: Thang Vu <[email protected]>
1 parent 577b7f9 commit 656f1ec

File tree

4 files changed

+5
-21
lines changed

4 files changed

+5
-21
lines changed

packages/frameworks-sveltekit/src/lib/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { RequestEvent } from "@sveltejs/kit"
33
import { parse } from "set-cookie-parser"
44
import { env } from "$env/dynamic/private"
55

6-
import { Auth, createActionURL, raw, skipCSRFCheck } from "@auth/core"
6+
import { Auth, createActionURL, raw } from "@auth/core"
77
import type { SvelteKitAuthConfig } from "./types"
88
import { setEnvDefaults } from "./env"
99

@@ -65,7 +65,7 @@ export async function signIn(
6565
headers.set("Content-Type", "application/x-www-form-urlencoded")
6666
const body = new URLSearchParams({ ...rest, callbackUrl })
6767
const req = new Request(url, { method: "POST", headers, body })
68-
const res = await Auth(req, { ...config, raw, skipCSRFCheck })
68+
const res = await Auth(req, { ...config, raw })
6969

7070
for (const c of res?.cookies ?? []) {
7171
event.cookies.set(c.name, c.value, { path: "/", ...c.options })
@@ -103,7 +103,7 @@ export async function signOut(
103103
const body = new URLSearchParams({ callbackUrl })
104104
const req = new Request(url, { method: "POST", headers, body })
105105

106-
const res = await Auth(req, { ...config, raw, skipCSRFCheck })
106+
const res = await Auth(req, { ...config, raw })
107107

108108
for (const c of res?.cookies ?? [])
109109
event.cookies.set(c.name, c.value, { path: "/", ...c.options })

packages/frameworks-sveltekit/src/lib/client.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export type SignInAuthorizationParams =
3636
/**
3737
* Client-side method to initiate a signin flow
3838
* or send the user to the signin page listing all possible providers.
39-
* Automatically adds the CSRF token to the request.
4039
*
4140
* [Documentation](https://authjs.dev/reference/sveltekit/client#signin)
4241
*/
@@ -65,10 +64,6 @@ export async function signIn<
6564

6665
const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}`
6766

68-
// TODO: Remove this since SvelteKit offers the CSRF protection via origin check
69-
const csrfTokenResponse = await fetch(`${basePath}/auth/csrf`)
70-
const { csrfToken } = await csrfTokenResponse.json()
71-
7267
const res = await fetch(_signInUrl, {
7368
method: "post",
7469
headers: {
@@ -78,7 +73,6 @@ export async function signIn<
7873
// @ts-ignore
7974
body: new URLSearchParams({
8075
...options,
81-
csrfToken,
8276
callbackUrl,
8377
}),
8478
})
@@ -98,24 +92,19 @@ export async function signIn<
9892

9993
/**
10094
* Signs the user out, by removing the session cookie.
101-
* Automatically adds the CSRF token to the request.
10295
*
10396
* [Documentation](https://authjs.dev/reference/sveltekit/client#signout)
10497
*/
10598
export async function signOut(options?: SignOutParams) {
10699
const { callbackUrl = window.location.href } = options ?? {}
107100
const basePath = base ?? ""
108-
// TODO: Remove this since SvelteKit offers the CSRF protection via origin check
109-
const csrfTokenResponse = await fetch(`${basePath}/auth/csrf`)
110-
const { csrfToken } = await csrfTokenResponse.json()
111101
const res = await fetch(`${basePath}/auth/signout`, {
112102
method: "post",
113103
headers: {
114104
"Content-Type": "application/x-www-form-urlencoded",
115105
"X-Auth-Return-Redirect": "1",
116106
},
117107
body: new URLSearchParams({
118-
csrfToken,
119108
callbackUrl,
120109
}),
121110
})

packages/frameworks-sveltekit/src/lib/env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core"
1+
import { setEnvDefaults as coreSetEnvDefaults, skipCSRFCheck } from "@auth/core"
22
import { dev, building } from "$app/environment"
33
import { base } from "$app/paths"
44
import type { SvelteKitAuthConfig } from "./types"
@@ -9,6 +9,7 @@ export function setEnvDefaults(
99
) {
1010
config.trustHost ??= dev
1111
config.basePath = `${base}/auth`
12+
config.skipCSRFCheck = skipCSRFCheck
1213
if (building) return
1314
coreSetEnvDefaults(envObject, config)
1415
}

packages/frameworks-sveltekit/src/lib/webauthn.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ async function webAuthnOptions(providerId: string, options?: SignInOptions) {
4343
/**
4444
* Client-side method to initiate a webauthn signin flow
4545
* or send the user to the signin page listing all possible providers.
46-
* Automatically adds the CSRF token to the request.
4746
*
4847
* [Documentation](https://authjs.dev/reference/sveltekit/client#signin)
4948
*/
@@ -85,10 +84,6 @@ export async function signIn<
8584
webAuthnBody.action = action
8685
}
8786

88-
// TODO: Remove this since Sveltekit offers the CSRF protection via origin check
89-
const csrfTokenResponse = await fetch(`${basePath}/auth/csrf`)
90-
const { csrfToken } = await csrfTokenResponse.json()
91-
9287
const res = await fetch(_signInUrl, {
9388
method: "post",
9489
headers: {
@@ -98,7 +93,6 @@ export async function signIn<
9893
// @ts-ignore
9994
body: new URLSearchParams({
10095
...options,
101-
csrfToken,
10296
callbackUrl,
10397
...webAuthnBody,
10498
}),

0 commit comments

Comments
 (0)