@fastify/middie vulnerable to path-scoped middleware bypass via request path matching mismatches
Description
Impact
@fastify/middie matches path-scoped middleware against its own reading of the request path, which can differ from the path Fastify's router (find-my-way) uses to dispatch. When the two disagree, the router routes a request to a handler that a middleware mount covers while middie skips that middleware, so an unauthenticated client reaches a handler that an authentication, authorization or rate-limiting middleware was meant to protect. No special configuration is required.
Three request forms trigger the mismatch. An empty parameter or wildcard value matches in find-my-way but not in middie's path-to-regexp mount, which needs at least one character, so /tenant//admin reaches a /tenant/:id/admin mount with id empty, /user/ reaches /user/:id, and /files/ reaches /files/*path; multiple parameters in one segment and plugin prefixes behave the same way. A request target that starts with *, such as *admin, is routed to /admin by find-my-way, which skips the first character of the path, while middie matches the target as sent. And a literal % in a mount path is kept encoded as %25 by find-my-way but decoded once more by middie, so /100%25 reaches a /100% mount that middie skips. All releases up to 9.3.7 are affected.
Patches
Upgrade to @fastify/middie 9.3.8 or later. middie now matches middleware against the path find-my-way routes: empty parameter and wildcard values are allowed, request targets that do not start with / other than the asterisk-form are rejected, and a literal % in a mount path is matched as the router keeps it encoded.
Workarounds
Enforce authentication in a Fastify hook (such as preHandler) that runs after the router has resolved the request, and base the decision on the matched route (request.routeOptions.url) rather than request.url. This covers all three vectors.
References
- delvedor/find-my-way#471, which stops find-my-way from routing a path that does not start with
/. - GHSA-hx87-8wv7-pjv8, a middleware bypass of the same kind via absolute-form request targets.