Skip to content

[BUG] Forbidden - Invalid Origin in localhost #190

@shamilovstas

Description

@shamilovstas

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions