Skip to content
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

[BUG] Allowing methods that are not uppercase should be possible but isn't #253

Open
1 task done
jub0bs opened this issue Dec 18, 2023 · 0 comments
Open
1 task done
Labels

Comments

@jub0bs
Copy link

jub0bs commented Dec 18, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Although method names are case-sensitive, Gorilla's CORS middleware takes the non-standard approach of normalising method names by uppercasing them. Such unwarranted case normalisation causes problems for clients that send requests whose method is not uppercase—and not some case-insensitive match for one of DELETE, GET, HEAD, OPTIONS, POST, or PUT, names for which the Fetch standard carves out an exception.

Expected Behavior

Gorilla should not normalise the case of allowed method names.

Steps To Reproduce

Here is a (failing) test case that illustrates the problem:

func TestHandlePreflightLowercaseAllowedMethod(t *testing.T) {
	const (
		origin = "https://foo.com"
		method = "patch"
	)
	req, _ := http.NewRequest(http.MethodOptions, "http://example.com/foo", nil)
	req.Header.Add("Origin", origin)
	req.Header.Add("Access-Control-Request-Method", method)

	rr := httptest.NewRecorder()

	testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

	cors := CORS(
		AllowedOrigins([]string{origin}),
		AllowedMethods([]string{method}),
	)
	cors(testHandler).ServeHTTP(rr, req)
	resp := rr.Result()

	if got, want := resp.StatusCode, http.StatusOK; got != want {
		t.Errorf("bad status: got %v want %v", got, want)
	}

	header := resp.Header.Get(corsAllowMethodsHeader)
	if got, want := header, method; got != want {
		t.Errorf("bad header: expected %q method header, got %q", want, got)
	}
}

Current result:

$ go test -run ^TestHandlePreflightLowercaseAllowedMethod$ github.com/gorilla/handlers
--- FAIL: TestHandlePreflightLowercaseAllowedMethod (0.00s)
    cors_test.go:210: bad status: got 405 want 200
    cors_test.go:215: bad header: expected "patch" method header, got ""
FAIL
FAIL    github.com/gorilla/handlers     0.790s
FAIL

Anything else?

More about this topic in one of my recent blog posts.

@jub0bs jub0bs added the bug label Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant