Skip to content

fix: relax csrf to exclude path based apps #11430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 8, 2024
Merged

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jan 4, 2024

Fixes #11406

I am working on a unit test to verify and make sure this does not break in future. Our current tests use the header auth method, which bypasses CSRF. My manual testing was just a fileserver which only used GET requests.

@@ -315,3 +319,62 @@ func TestSwagger(t *testing.T) {
require.Equal(t, "<pre>\n</pre>\n", string(body))
})
}

func TestCSRFExempt(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on a unit test to verify

Is it this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. All of our existing unit tests bypass CSRF since the codersdk uses the HEADER method to auth, not the cookie.

coderd/coderd.go Outdated
// CSRF only needs to apply to /api routes. It does not apply to GET requests
// anyway, which is most other routes. We want to exempt any external auth or
// application type routes.
httpmw.CSRF(options.SecureAuthCookie),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a bunch of ExemptRegexp() calls inside this middleware that may need to be reviewed after this change. It might be nicer to have those passed explicitly as arguments to httpmw.CSRF instead of being hidden inside the middleware.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, this will pretty quickly fail on someone when they add a new route if it breaks. The error is a CSRF error, which brings you to here. I prefer to have this all in 1 place, and this should never be configurable by a customer except maybe to disable it entirely.

I think the current exempts are overkill. I intentionally went a bit overkill because I was afraid of breaking anything. Some

The reason there are exempts like this is when I turned this back on, I was trying to error on the side of caution. I think we should have a PR to remove an exempt 1 by 1 when we determine they can never cause an issue.

An example is the /provisionerdaemons ones. They are currently only GET requests, and I do not think the actual daemons are using cookies to authenticate. However I error-ed on the side of caution since these routes should never use CSRF anyway.

I can make follow up PRs to drop these with rational for each one

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Emyrk Emyrk enabled auto-merge (squash) January 5, 2024 16:29
@Emyrk
Copy link
Member Author

Emyrk commented Jan 8, 2024

@johnstcn CSRF state is set via go template on page render.

So the CSRF middleware has to be present for page loads.

I just added this instead.

if !strings.HasPrefix(r.URL.Path, "/api") {
    return true
}

There is only an "exempt" syntax, rather than an "opt in" unfortunately. I think this is the easiest fix.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. LGTM once the linter is happy and once there are some more test cases in TestCSRFExempt

Comment on lines 21 to 27
}{
{
Name: "Root",
Path: "/",
Exempt: true,
},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add some more test cases here

@Emyrk
Copy link
Member Author

Emyrk commented Jan 8, 2024

... once the linter is happy ...

It is never happy 😄

@Emyrk Emyrk merged commit fb29af6 into main Jan 8, 2024
@Emyrk Emyrk deleted the stevenmasley/relax_csrf branch January 8, 2024 22:33
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSRF enforcement in v2.6.0 breaks RStudio IDE and other POST requests
3 participants