8000 csrf: sanitize token after reading from cookie by tsimmons · Pull Request #6969 · gogs/gogs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

csrf: sanitize token after reading from cookie #6969

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 4 commits into from
May 26, 2022
Merged

Conversation

tsimmons
Copy link

Prevent XSS from _csrf cookie by sanitizing user cookie

To prevent a Cross-Site Scripting (XSS) vulnerability, we should sanitize the user-supplied cookie header. Strip all but a-z, A-Z and 0-9 from the _csrf cookie.

Link to the issue

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

Copy link
Member
@unknwon unknwon left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

After a closer look, I found a better way of handling it, see fc15ac4

After sanitization:

CleanShot 2022-05-26 at 17 52 28@2x

Let me know what you think!


It is true that the general sanitization isn't as clean as the regex one in terms of stripping, but since what characters are used to generate a CSRF token isn't in the control of the application ("Gogs"), I think it would be more future-proof as long as no XSS is possible.

@unknwon unknwon linked an issue May 26, 2022 that may be closed by this pull request
1 task
@unknwon unknwon changed the title Prevent XSS from _csrf cookie by sanitizing, stripping invalid chars csrf: sanitize token before being rendered into HTML May 26, 2022
@codecov
Copy link
codecov bot commented May 26, 2022

Codecov Report

Merging #6969 (bc85458) into main (bdff033) will not change coverage.
The diff coverage is n/a.

@@          Coverage Diff          @@
##            main   #6969   +/-   ##
=====================================
  Coverage   8.89%   8.89%           
=====================================
  Files         99      99           
  Lines      13262   13262           
=====================================
  Hits        1179    1179           
  Misses     11917   11917           
  Partials     166     166           

@tsimmons
Copy link
Author
tsimmons commented May 26, 2022

Looking at your screen shot, it seems the markup.Sanitize is not stripping all brackets (or sanitizing them) ...

<div>

should not be part of the variable name, no angle brackets should break through. In addition, it looks like you would permit other markup that should not be allowed for the _csrf variable looking at NewSanitizer() in internal/markup/sanitizer.go. I'd have to vote no on this proposal for compliance.

@tsimmons
Copy link
Author

Looking at the source for go-macaron csrf, only alphanumeric characters are allowed for the token, if you can create a new Sanitizer that strips all but a-z, A-Z, 0-9 that would be better.

See go-macaron csrf code.

@unknwon
Copy link
Member
unknwon commented May 26, 2022

OK, makes sense! Pushed another version in bc85458

@unknwon unknwon changed the title csrf: sanitize token before being rendered into HTML csrf: sanitize token after reading from cookie May 26, 2022
@tsimmons
Copy link
Author

Looks good!

@unknwon unknwon merged commit d54e153 into gogs:main May 26, 2022
unknwon added a commit that referenced this pull request May 31, 2022
Co-authored-by: Joe Chen <jc@unknwon.io>
dna2github pushed a commit to dna2fork/gogs that referenced this pull request May 1, 2023
Co-authored-by: Joe Chen <jc@unknwon.io>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2023
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.

Cookie CSRF
2 participants
0