Repository navigation
fix(router): set the 405's Allow header at the end of the chain #1297
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package celeris | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // celeris#889 (from #852's round-2 review): the 405's Allow header is set by | ||
| // the end of the 405 chain, not before the global middleware, so a | ||
| // middleware's own answer to a request whose method no route takes (an auth | ||
| // 401) does not tell an unauthenticated client which methods the path takes. | ||
|
|
||
| func TestAllowOnlyOnThe405Answer889(t *testing.T) { | ||
| auth := func(c *Context) error { | ||
| if c.Header("authorization") == "" { | ||
| return c.Blob(401, "text/plain", []byte("unauthorized")) | ||
| } | ||
| return c.Next() | ||
| } | ||
| for _, custom := range []bool{false, true} { | ||
| name := "builtin" | ||
| if custom { | ||
| name = "custom-handler" | ||
| } | ||
| t.Run(name, func(t *testing.T) { | ||
| s := New(Config{}) | ||
| s.Use(auth) | ||
| s.GET("/admin/users", func(c *Context) error { return c.String(200, "ok") }) | ||
| s.POST("/admin/users", func(c *Context) error { return c.String(201, "ok") }) | ||
| if custom { | ||
| s.MethodNotAllowed(func(c *Context) error { return c.String(405, "custom 405") }) | ||
| } | ||
|
|
||
| // Unauthenticated: the middleware answers, with no Allow, on a | ||
| // path with routes and on one without. | ||
| for _, rq := range [][2]string{{"DELETE", "/admin/users"}, {"GET", "/nope"}} { | ||
| for arm, rw := range serve852(t, s, rq[0], rq[1]) { | ||
| desc := arm + " " + rq[0] + " " + rq[1] | ||
| r := one852(t, desc, rw) | ||
| if r.status != 401 { | ||
| t.Errorf("%s: status %d, want 401", desc, r.status) | ||
| } | ||
| if a := r.header("allow"); len(a) != 0 { | ||
| t.Errorf("%s: Allow %v on the middleware's own 401: it names the methods the path takes to an unauthenticated client", desc, a) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Admitted: the 405 itself carries the one Allow header listing | ||
| // the methods the path takes, builtin or custom handler alike. | ||
| s2 := New(Config{}) | ||
| s2.Use(func(c *Context) error { return c.Next() }) | ||
| s2.GET("/admin/users", func(c *Context) error { return c.String(200, "ok") }) | ||
| s2.POST("/admin/users", func(c *Context) error { return c.String(201, "ok") }) | ||
| if custom { | ||
| s2.MethodNotAllowed(func(c *Context) error { return c.String(405, "custom 405") }) | ||
| } | ||
| for arm, rw := range serve852(t, s2, "DELETE", "/admin/users") { | ||
| r := one852(t, arm, rw) | ||
| if r.status != 405 { | ||
| t.Fatalf("%s: status %d, want 405", arm, r.status) | ||
| } | ||
| a := r.header("allow") | ||
| if len(a) != 1 || !strings.Contains(a[0], "GET") || !strings.Contains(a[0], "POST") || strings.Contains(a[0], "DELETE") { | ||
| t.Errorf("%s: Allow %v on the 405, want one header listing GET and POST and not DELETE", arm, a) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestAllowOnA405WithTheChainStartedByStart889: the chains Start builds | ||
| // (buildUnmatchedChains) end in the same Allow-setting step, and the context's | ||
| // per-request value does not leak into the next request that reuses it. | ||
| func TestAllowOnA405WithTheChainStartedByStart889(t *testing.T) { | ||
| s := New(Config{}) | ||
| s.Use(func(c *Context) error { | ||
| if c.Method() == "DELETE" && c.Header("authorization") == "" { | ||
| return c.Blob(401, "text/plain", []byte("unauthorized")) | ||
| } | ||
| return c.Next() | ||
| }) | ||
| s.GET("/x", func(c *Context) error { return c.String(200, "ok") }) | ||
| ra := &routerAdapter{server: s} | ||
| ra.buildUnmatchedChains() | ||
| run := func(method, path string, hdr ...string) recordedResponse852 { | ||
| t.Helper() | ||
| st, _ := newTestStream(method, path) | ||
| for i := 0; i+1 < len(hdr); i += 2 { | ||
| st.Headers = append(st.Headers, [2]string{hdr[i], hdr[i+1]}) | ||
| } | ||
| rw := &recordingRW852{} | ||
| st.ResponseWriter = rw | ||
| if err := ra.HandleStream(t.Context(), st); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| st.Release() | ||
| return one852(t, method+" "+path, rw) | ||
| } | ||
| if r := run("DELETE", "/x"); r.status != 401 || len(r.header("allow")) != 0 { | ||
| t.Errorf("unauthenticated DELETE /x: %d allow=%v, want 401 and no Allow", r.status, r.header("allow")) | ||
| } | ||
| if r := run("POST", "/x"); r.status != 405 || len(r.header("allow")) != 1 { | ||
| t.Errorf("POST /x: %d allow=%v, want 405 with one Allow", r.status, r.header("allow")) | ||
| } | ||
| if r := run("GET", "/nope"); r.status != 404 || len(r.header("allow")) != 0 { | ||
| t.Errorf("GET /nope: %d allow=%v, want 404 and no Allow", r.status, r.header("allow")) | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Script executed:
Repository: goceleris/celeris
Length of output: 7021
Script executed:
Repository: goceleris/celeris
Length of output: 28397
Script executed:
Repository: goceleris/celeris
Length of output: 9578
Script executed:
Repository: goceleris/celeris
Length of output: 7702
Preserve
Allowwhen middleware returns a 405 error.For
POST /xwith onlyGET /xregistered, a middlewareHTTPErrorwithCode: 405skipswithAllow. The default error response can therefore omitAllow, which a 405 response requires. The fallback cannot repair the response afterhandleErrorwrites it.Add the pending value when the default error handler writes a 405, without replacing an explicit
Allow. Add a regression test for this path.Suggested fix
var he *HTTPError if errors.As(err, &he) { + if he.Code == 405 && c.unmatchedAllow != "" && + !slices.ContainsFunc(c.respHeaders, func(h [2]string) bool { return h[0] == "allow" }) { + hdrs = append(hdrs, [2]string{"allow", c.unmatchedAllow}) + } c.statusCode = he.Code