-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Protection against CSRF added #22077
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package common | ||
|
@@ -78,5 +78,43 @@ func Middlewares() []func(http.Handler) http.Handler { | |
next.ServeHTTP(resp, req) | ||
}) | ||
}) | ||
|
||
// Add CSRF handler. | ||
handlers = append(handlers, csrfHandler()) | ||
|
||
return handlers | ||
} | ||
|
||
// csfrHandler blocks recognized CSRF attempts. | ||
// WARNING: for this proctection to work, web browser compatible with | ||
// Fetch Metadata Request Headers (https://w3c.github.io/webappsec-fetch-metadata) | ||
// must be used. | ||
func csrfHandler() func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { | ||
|
||
// Put header names we use for CSRF recognition into Vary response header. | ||
if setting.CORSConfig.Enabled { | ||
resp.Header().Set("Vary", "Origin, Sec-Fetch-Site") | ||
} else { | ||
resp.Header().Set("Vary", "Sec-Fetch-Site") | ||
} | ||
|
||
// Allow requests not recognized as CSRF. | ||
secFetchSite := strings.ToLower(req.Header.Get("Sec-Fetch-Site")) | ||
if req.Method == "GET" || // GET must not be used for changing state (CSRF resistant). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it also cover HEAD ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added HEAD and OPTIONS also. |
||
secFetchSite == "" || // Accept requests from clients without Fetch Metadata Request Headers support. | ||
secFetchSite == "same-origin" || // Accept requests from own origin. | ||
secFetchSite == "none" || // Accept requests initiated by user (i.e. using bookmark). | ||
((secFetchSite == "same-site" || secFetchSite == "cross-site") && // Accept cross site requests allowed by CORS. | ||
setting.CORSConfig.Enabled && setting.Cors.OriginAllowed(req)) { | ||
next.ServeHTTP(resp, req) | ||
return | ||
} | ||
|
||
// Forbid and log other requests as CSRF. | ||
log.Error("CSRF rejected: METHOD=\"%s\", Origin=\"%s\", Sec-Fetch-Site=\"%s\"", req.Method, req.Header.Get("Origin"), secFetchSite) | ||
http.Error(resp, http.StatusText(http.StatusForbidden), http.StatusForbidden) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, updating the copyright year is kinda useless nowadays, as using git is sufficient to establish who/when the work was done (which is why there is "the gitea authors" and not the detail). That's the opinion of the Linux Foundation ( https://www.linuxfoundation.org/blog/blog/copyright-notices-in-open-source-software-projects , the author is the Vice President of Compliance and Legal ). I also got the same answer from our legal department, even if I haven't been able to convince them to publish something outside our intranet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.