Summary
A path-matching discrepancy in @fastify/express can cause a request with an empty dynamic path segment to reach a Fastify route while skipping Express middleware mounted on the matching pattern. If that middleware provides authentication or authorization, an unauthenticated request may reach a handler that it is meant to protect.
Affected context
The issue affects applications using @fastify/express versions 4.0.5 through 4.0.9, where Express middleware is mounted on a dynamic path pattern that overlaps with a Fastify route. The middleware matcher requires a non-empty dynamic segment, while Fastify's router can match an empty parameter or wildcard value. Practical impact depends on the handler and what the empty value selects; no special configuration is required for the mismatch to occur.
Exploit preconditions
- Attack surface: An HTTP endpoint served by an affected application
- Required access: A request can be made to the application; authentication may not be required if the skipped middleware is the control intended to require it
- Trigger point: Routing a request whose path contains an empty dynamic segment matching both the middleware mount and a Fastify route
- Why exploitation is possible: The middleware matcher and Fastify router interpret the empty segment differently, allowing the route handler to run without the scoped middleware
Why this happens
The underlying bug is inconsistent path interpretation between the @fastify/express middleware matcher and Fastify's router. The matcher excludes empty dynamic values that the router accepts, so middleware enforcement can be bypassed for a route sharing the dynamic pattern. This is an authentication-bypass risk (CWE-287) arising from inconsistent security-relevant interpretation of input, also associated with CWE-436.
Potential impact
- An unauthenticated user may reach a handler protected by scoped middleware
- An attacker could access or alter data depending on the handler's behavior and permissions
- Security controls implemented only in the skipped middleware may not apply to the affected request
General mitigation guidance
Defensive approaches include aligning route and middleware path-matching semantics so security controls apply consistently to every matching request. Prefer framework-supported authentication and authorization mechanisms that are enforced at the route or handler boundary, and avoid relying solely on path-scoped middleware where routing behavior can diverge. Treat path parameters as untrusted, validate them explicitly, and apply least privilege to handlers and their downstream operations.
How teams detect issues like this
- Review route and middleware registrations for overlapping dynamic patterns
- Test empty path parameters and wildcard values to verify that authentication and authorization run before protected handlers
- Use static analysis or code review checks to flag security controls attached only through path-scoped middleware
- Enforce policy checks in CI so route changes cannot introduce paths that bypass required controls

