Repository navigation
fix(csrf): honor Secure(false) option for request URL scheme detection - #215
Closed
AdamMagued wants to merge 1 commit into
Closed
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
Author
|
Closing in favor of earlier community PR #212 to avoid duplicate review effort. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Technical Explanation & Root Cause Analysis
In
gorilla/csrf, when evaluating requests with non-idempotent HTTP methods (such as POST), the middleware constructs a shallow copy ofr.URLto determine the origin and compare against incomingOriginheaders viasameOrigin(a, b):Because server requests in Go's
net/httpdo not populater.URL.Scheme, the scheme defaults to"https"unless explicit plaintext context is provided viaPlaintextHTTPRequest(isPlaintext).When users configure the middleware for local development over plain HTTP using
csrf.Secure(false),isPlaintextremains false. As a result, incoming requests withOrigin: http://localhost:8080(andHost: localhost:8080) are compared againstrequestURL.Scheme = "https", causingsameOrigin(&requestURL, parsedOrigin)to fail due to scheme mismatch and incorrectly rejecting the request withErrBadOrigin(HTTP 403 Forbidden).Solution
Update the request URL scheme determination to also check if the
Secure(false)option is set on the middleware (!cs.opts.Secure):When
csrf.Secure(false)is enabled, the scheme defaults to"http", allowing same-origin checks against plain HTTP origins to succeed without requiring manual additions toTrustedOrigins.Test Verification
Added
TestInsecureOriginSameOriginincsrf_test.go:csrf.Protectwithcsrf.Secure(false).Host: localhost:8080andOrigin: http://localhost:8080.ErrBadOrigin(returns HTTP 200 OK).Fixes #188