-
Notifications
You must be signed in to change notification settings - Fork 162
Open
Labels
Description
Is there an existing issue for this?
- I have searched the existing issues
Current Behavior
When running in localhost, every csrf check returns "Invalid Origin" error. go-chi
router is used.
router.Use(CsrfMiddleware(mustGetCsrfKey()))
func CsrfMiddleware(key []byte) func(http.Handler) http.Handler {
var opts []csrf.Option
env := os.Getenv("APP_ENV")
if env == "local" || env == "dev" {
opts = append(opts, csrf.Secure(false))
}
csrfFn := csrf.Protect(key, opts...)
return csrfFn
}
I made sure that the APP_ENV variable is correctly set and csrf.Protect
is called with csrf.Secure(false)
option.
After debugging I noticed the error happens do to the check if !sameOrigin(&requestURL, parsedOrigin) ...
in gorilla/csrf/csrf.go
. The request url gets rewritten in the same csrf.go
file in this block:
requestURL := *r.URL // shallow clone
requestURL.Scheme = "https"
if isPlaintext {
requestURL.Scheme = "http"
}
if requestURL.Host == "" {
requestURL.Host = r.Host
}
Expected Behavior
csrf.Secure(false) option should prevent rewriting url scheme from "http" to "https"
Steps To Reproduce
No response
Anything else?
No response
matthewrankin, Darkness4, mejo- and leodip