Skip to content
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

Implement XSRFIgnoreMethods #207

Merged
merged 4 commits into from
Jul 25, 2024
Merged

Implement XSRFIgnoreMethods #207

merged 4 commits into from
Jul 25, 2024

Conversation

oalexander6
Copy link
Contributor

XSRFIgnoreMethods

Background

I discovered while using this library that the current XSRF protections do not allow for a GET request sent by the browser on protected routes, due to requiring the presence of the XSRF token in a header. This is a problem for applications that are not SPAs and send GET requests to the server to retrieve a new HTML page. In these situations, the common pattern is to skip XSRF checks on GET requests.

Solution

To resolve this, I implemented a configuration option called XSRFIgnoreMethods, allowing the user to specify a comma-separated list of HTTP methods for which XSRF checks should be skipped when extracting and validating the JWT. I am open to any feedback, modifications, or discussions about this.

@oalexander6 oalexander6 requested a review from umputun as a code owner July 20, 2024 22:19
@umputun
Copy link
Member

umputun commented Jul 20, 2024

@oalexander6
Copy link
Contributor Author

No, in this situation I still definitely want XSRF protections on POST, PUT, DELETE, etc. but specifically not for GET requests. The DisableXSRF option disables it for all methods.

@oalexander6
Copy link
Contributor Author

This PR would allow for ignoring just certain HTTP methods, by setting XSRFIgnoreMethods: []string{"GET", "HEAD"} for example, to just skip the XSRF checks for "GET" and "HEAD" only

token/jwt.go Outdated
JWTHeaderKey string
XSRFCookieName string
XSRFHeaderKey string
XSRFIgnoreMethods string
Copy link
Member

Choose a reason for hiding this comment

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

I prefer not to use a comma-separated string in this context and then apply strings.contains on the string subsequently. It would be more organized to utilize a slice of methods in this scenario and perform an exact match on the request method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I originally did that but saw the the default values for the related options were constants and wanted to stick to that, which couldn't be done with a slice. I will switch it back to an array of strings.

@umputun
Copy link
Member

umputun commented Jul 20, 2024

This PR would allow for ignoring just certain HTTP methods, by setting XSRFIgnoreMethods: []string{"GET", "HEAD"} for example, to just skip the XSRF checks for "GET" and "HEAD" only

Unless I missed something, this code does not expect a []string with all the ignored methods but rather a single string (see my comment in the review above).

@umputun
Copy link
Member

umputun commented Jul 20, 2024

one more thing - this change should be also documented in readme. Maybe a section about "Options to ignore XSRF protections" or smth like what showing how to do it and providing a basic explanation why this may be needed

@oalexander6
Copy link
Contributor Author

one more thing - this change should be also documented in readme. Maybe a section about "Options to ignore XSRF protections" or smth like what showing how to do it and providing a basic explanation why this may be needed

Will do

@oalexander6
Copy link
Contributor Author

The requested changes have been made. Thanks for the feedback.

@coveralls
Copy link

coveralls commented Jul 21, 2024

Pull Request Test Coverage Report for Build 10101670690

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 83.371%

Totals Coverage Status
Change from base Build 9764450722: 0.02%
Covered Lines: 2582
Relevant Lines: 3097

💛 - Coveralls

Copy link
Member

@umputun umputun left a comment

Choose a reason for hiding this comment

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

a minor thing with implicit types for Contains func

token/jwt.go Outdated Show resolved Hide resolved
Copy link
Member

@umputun umputun left a comment

Choose a reason for hiding this comment

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

LGTM, thx

@umputun umputun merged commit db26833 into go-pkgz:master Jul 25, 2024
5 checks passed
@paskal
Copy link
Collaborator

paskal commented Jul 29, 2024

@oalexander6 could you please copy the very same changes to v2 directory please?

@oalexander6
Copy link
Contributor Author

@oalexander6 could you please copy the very same changes to v2 directory please?

#210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants