Repository navigation
fix(core): let a GET middleware exclusion cover HEAD requests - #17950
Merged
kamilmysliwiec merged 2 commits intoSep 30, 2026
Merged
Conversation
`MiddlewareConsumer.exclude({ path, method: RequestMethod.GET })` skipped
the middleware for GET requests only. A HEAD request to the same path,
which the router serves from the GET handler, still ran it, although
the middleware module already lets a HEAD request match a GET
middleware route when deciding whether the middleware runs.
Treat a HEAD request as excluded when the route is excluded for GET.
A HEAD-only exclusion still does not cover GET requests.
4 of 12 tasks
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
GET /healthskips the middleware, butHEAD /healthstill runs it, although the router serves HEAD from the@Get()handler. Uptime monitors and load balancers often probe with HEAD, so the health check gets the middleware's 401 or side effects. Express and Fastify behave the same way.isMiddlewareRouteExcluded()passes the request method straight toisRouteExcluded(), which needs an exact method match. When deciding whether a middleware runs,MiddlewareModule.registerHandler()already lets a HEAD request match aRequestMethod.GETroute (pinned by the "HEAD auto-forwarding to GET" cases inintegration/hello-world/e2e/middleware-fastify.spec.ts), so a middleware bound to GET runs for HEAD while an exclusion for GET does not cover it.The new spec against the current
utils.ts:Issue Number: N/A
What is the new behavior?
A HEAD request is excluded when the route is excluded for HEAD or for GET, the same HEAD-for-GET rule that
registerHandler()uses.isRouteExcluded()itself is unchanged, since the global prefix exclusion also uses it. A HEAD-only exclusion still does not cover GET requests; a second new case pins that, so the rule cannot be widened into skipping the middleware for GET. The rule is by path and method, not by handler: a route that also has an explicit@Head()handler and is excluded for GET now skips the middleware on HEAD as well.A throwaway app on Express and Fastify, with
forRoutes('*')and withforRoutes({ path: '*', method: RequestMethod.GET }), now skips the middleware forHEAD /healthand still runs it for other paths.Does this PR introduce a breaking change?
Other information
npx vitest run packages/core: 1401 passed.