Skip to content

Commit 24080b1

Browse files
authored
feat: enable csrf token header (coder#11283)
* feat: enable csrf token header * Exempt external auth requets * ensure dev server bypasses CSRF * external auth is just get requests * Add some more routes * Extra assurance nothing breaks
1 parent fbda21a commit 24080b1

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

coderd/httpmw/csrf.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,23 @@ func CSRF(secureCookie bool) func(next http.Handler) http.Handler {
1919
mw.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2020
http.Error(w, "Something is wrong with your CSRF token. Please refresh the page. If this error persists, try clearing your cookies.", http.StatusBadRequest)
2121
}))
22-
2322
// Exempt all requests that do not require CSRF protection.
2423
// All GET requests are exempt by default.
2524
mw.ExemptPath("/api/v2/csp/reports")
2625

27-
// Top level agent routes.
28-
mw.ExemptRegexp(regexp.MustCompile("api/v2/workspaceagents/[^/]*$"))
2926
// Agent authenticated routes
3027
mw.ExemptRegexp(regexp.MustCompile("api/v2/workspaceagents/me/*"))
28+
mw.ExemptRegexp(regexp.MustCompile("api/v2/workspaceagents/*"))
29+
// Workspace Proxy routes
30+
mw.ExemptRegexp(regexp.MustCompile("api/v2/workspaceproxies/me/*"))
3131
// Derp routes
3232
mw.ExemptRegexp(regexp.MustCompile("derp/*"))
33+
// Scim
34+
mw.ExemptRegexp(regexp.MustCompile("api/v2/scim/*"))
35+
// Provisioner daemon routes
36+
mw.ExemptRegexp(regexp.MustCompile("/organizations/[^/]+/provisionerdaemons/*"))
3337

3438
mw.ExemptFunc(func(r *http.Request) bool {
35-
// Enable CSRF in November 2022 by deleting this "return true" line.
36-
// CSRF is not enforced to ensure backwards compatibility with older
37-
// cli versions.
38-
//nolint:revive
39-
return true
40-
4139
// CSRF only affects requests that automatically attach credentials via a cookie.
4240
// If no cookie is present, then there is no risk of CSRF.
4341
//nolint:govet
@@ -59,6 +57,13 @@ func CSRF(secureCookie bool) func(next http.Handler) http.Handler {
5957
return true
6058
}
6159

60+
if r.Header.Get(codersdk.ProvisionerDaemonPSK) != "" {
61+
// If present, the provisioner daemon also is providing an api key
62+
// that will make them exempt from CSRF. But this is still useful
63+
// for enumerating the external auths.
64+
return true
65+
}
66+
6267
// If the X-CSRF-TOKEN header is set, we can exempt the func if it's valid.
6368
// This is the CSRF check.
6469
sent := r.Header.Get("X-CSRF-TOKEN")

site/vite.config.ts

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export default defineConfig({
3838
},
3939
server: {
4040
port: process.env.PORT ? Number(process.env.PORT) : 8080,
41+
headers: {
42+
// This header corresponds to "src/api/api.ts"'s hardcoded FE token.
43+
// This is the secret side of the CSRF double cookie submit method.
44+
// This should be sent on **every** response from the webserver.
45+
//
46+
// This is required because in production, the Golang webserver generates
47+
// this "Set-Cookie" header. The Vite webserver needs to replicate this
48+
// behavior. Instead of implementing CSRF though, we just use static
49+
// values for simplicity.
50+
"Set-Cookie":
51+
"csrf_token=JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4=; Path=/; HttpOnly; SameSite=Lax",
52+
},
4153
proxy: {
4254
"/api": {
4355
ws: true,

0 commit comments

Comments
 (0)