Summary
A path-matching mismatch in @fastify/express can cause path-scoped Express middleware to be skipped when used inside a prefixed Fastify plugin. If that middleware provides authentication or authorization, an unauthenticated request may reach a protected route without those checks.
Affected context
The issue affects applications using @fastify/express versions 4.0.0 through 4.0.9 where path-scoped Express middleware is registered inside a Fastify plugin with a prefix. Certain combinations of a trailing slash in the plugin prefix or a missing leading slash in the middleware mount path can make Express and Fastify interpret the route differently. String, array, and regular-expression mount paths are affected.
Exploit preconditions
- Attack surface: HTTP routes served by a Fastify application using @fastify/express
- Required access: No prior access is required when the affected route is publicly reachable
- Trigger point: A normal request to a route in a prefixed plugin where path-scoped middleware is expected to run
- Why exploitation is possible: A path-joining mismatch can prevent Express middleware from matching, while Fastify still dispatches the route
Why this happens
The plugin constructs a path-scoped middleware mount path by concatenating it with the Fastify plugin prefix. Under affected path combinations, Express and Fastify interpret the resulting paths inconsistently, allowing the router to dispatch a request after the middleware has failed to match. This behavior can lead to CWE-863: Incorrect Authorization when the skipped middleware enforces access control, and reflects a broader interpretation conflict described by CWE-436.
Potential impact
- Unauthenticated users may reach routes intended to require authentication
- Authorization checks implemented in the skipped middleware could be bypassed
- Sensitive data or operations exposed by affected routes could be accessed or invoked
General mitigation guidance
Defensive approaches focus on keeping middleware and router path interpretation aligned:
- Use path-joining and normalization behavior that consistently handles leading and trailing slashes
- Prefer secure-by-default routing and middleware registration patterns that fail closed when a match is uncertain
- Apply authentication and authorization at a layer that cannot be bypassed by middleware path mismatches
- Minimize reliance on user-controlled or otherwise untrusted values when constructing route and mount paths
- Limit route privileges and access to sensitive operations according to least-privilege principles
How teams detect issues like this
- Review middleware registration in prefixed plugins for path concatenation and slash-normalization assumptions
- Test that every protected route invokes its expected authentication and authorization middleware across path variants
- Use static analysis or code review checks to flag middleware paths assembled through ad hoc string concatenation
- Enforce policies requiring explicit access-control coverage for sensitive routes
- Add integration tests that verify requests are denied when authentication or authorization middleware should apply

