server: make the router method-aware, dropping in-handler method guards - #4919
Open
youdie006 wants to merge 1 commit into
Open
server: make the router method-aware, dropping in-handler method guards#4919youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
router.Mux's registration methods now take an optional list of HTTP methods; the router matches them and answers any other method with a uniform 405 via a shared MethodNotAllowedHandler, so handlers no longer guard the method themselves. Pass the allowed method at each Mount call-site (device, token, introspection) and remove the per-handler r.Method checks. CORS routes still allow OPTIONS through for preflight, and the 405 handler keeps the configured response headers. Wrong-method requests now get a uniform 405 instead of each endpoint's former 400. Refs dexidp#4907. Signed-off-by: youdie006 <youdie006@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes the server’s router.Mux method-aware so HTTP method matching happens at route registration time (via gorilla/mux), allowing per-handler r.Method guards to be removed and consolidating wrong-method responses to a uniform 405 Method Not Allowed.
Changes:
- Extend
router.Mux’sHandle/HandleFunc/HandleCORSAPIs with an optionalmethods ...stringvariadic parameter. - Wire method restrictions into the server’s gorilla/mux routes and add a shared
MethodNotAllowedHandlerthat renders the standard error page while still applying configured headers. - Update device, token, and introspection mounts to declare explicit methods and remove in-handler method checks; adjust tests for the new 405 behavior.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| server/server.go | Makes route registration method-aware and installs a uniform 405 handler wrapped with configured headers. |
| server/router/router.go | Updates the Mux interface to accept optional HTTP methods for route-level matching. |
| server/introspection/introspection.go | Mounts introspection as POST-only and removes the internal method guard. |
| server/introspection/introspection_test.go | Updates tests to reflect router-level method enforcement. |
| server/grants/grants.go | Mounts /token as POST-only and removes the internal method guard. |
| server/errors_test.go | Updates the device callback wrong-method test to expect 405. |
| server/device/device.go | Mounts device endpoints with explicit methods and removes per-handler guards. |
| server/device_authorize_test.go | Updates /device/code wrong-method test to expect router-level 405 behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+43
to
47
| // The router restricts /device/code to POST, so a GET is rejected | ||
| // with the shared 405 handler before reaching the handler. That | ||
| // handler renders the HTML error page, which sets no content type. | ||
| testName: "Method not allowed (GET)", | ||
| clientID: "test", |
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.
What
Implements #4907 as its own PR (per @nabokihms's note that this cross-cutting change is best done standalone): make
router.Muxmethod-aware so per-handlerr.Methodguards can be dropped in favor of route-level matching and a uniform 405.Changes
server/router/router.go:Handle/HandleFunc/HandleCORSgained an optional variadicmethods ...string. When methods are given the route only matches those; passing none leaves it open to all (so existing unrestricted mounts compile and behave unchanged).HandlePrefixis left as-is (subtree static mounts don't method-match).server/server.go: therouteMuxadapter forwards the methods; thehandle/handleFunc/handleWithCORSclosures chain gorilla'sroute.Methods(...). A sharedMethodNotAllowedHandler— wired to the existingErrMsgMethodNotAllowed/renderErrorHTML page and wrapped inhandlerWithHeadersso configured headers still apply — answers wrong-method requests with a uniform 405.handleWithCORSalso allowsOPTIONSthrough so CORS preflight still reaches the middleware./device(GET),/device/auth/verify_code(POST),/device/code(POST),/device/callback(GET),/token(POST),/token/introspect(POST).Behavior
Correct-method requests are unchanged. Wrong-method requests now get a uniform
405instead of each endpoint's former400(device/token previously returned 400 JSON) — the intended consolidation. When CORS isn't configured, anOPTIONS/GETto/tokennow yields 405 rather than 400.Tests
Updated the three wrong-method tests to the new router-level 405 (
TestHandleDeviceCodeGET,TestDeviceCallbackMethodErrorPUT, and the introspection helper's dropped GET sub-case).go build ./...,go vet ./server/...,go test ./server/..., andgofmtall pass; I also caught and fixed aTestHeadersregression (the 405 handler neededhandlerWithHeadersso HSTS still applies to wrong-method responses).Refs #4907.
AI disclosure: this PR was drafted with Claude Code (AI-assisted). Verified with the full
./server/...test suite (including the updated wrong-method tests); DCO sign-off included.