Summary
A path-matching inconsistency in @fastify/middie can cause path-scoped middleware to be skipped even though Fastify routes a request to the protected handler. In affected configurations, an unauthenticated client may reach that handler without checks such as authentication, authorization, or rate limiting.
Affected context
The issue affects applications using @fastify/middie when Fastify routing is configured with caseSensitive: false and a protected middleware mount path contains the letter k. Fastify's router applies Unicode-aware lowercasing, while middie's path-matching regular expression does not handle the same non-ASCII character equivalently, creating a mismatch between routing and middleware selection.
Exploit preconditions
- Attack surface: HTTP requests to routes protected by path-scoped middleware
- Required access: An unauthenticated client able to send requests to the application
- Configuration required: Fastify routing uses
caseSensitive: false, and the protected mount path contains the letterk - Trigger point: Path matching for a request handled by Fastify's router and middie's middleware matcher
- Why exploitation is possible: The router and middleware matcher normalize and compare the path differently, so routing can select a protected handler while the middleware does not match and run
Why this happens
This is a path canonicalization and authorization enforcement inconsistency. Fastify's router uses Unicode-aware lowercasing when case-insensitive routing is enabled, while middie's regular-expression matching for mounted middleware does not fold non-ASCII characters the same way. This behavior relates to CWE-178: Improper Handling of Case Sensitivity and can result in CWE-863: Incorrect Authorization when security middleware is skipped for a handler that relies on it.
Potential impact
- Unauthenticated users may reach handlers intended to be protected
- Authorization checks provided by path-scoped middleware could be bypassed
- Rate limits or other middleware-based controls may not run
- Sensitive data or operations may become accessible, depending on the handler
General mitigation guidance
Reduce reliance on path-scoped middleware as the sole authorization boundary. Apply authorization checks in routing hooks that run for the selected handler, such as a post-routing pre-handler hook, and use consistent path normalization across routing and middleware. Keeping case-sensitive routing enabled is another pattern that avoids this particular mismatch when compatible with application requirements. Apply least privilege to handlers and services so that a missed middleware check has a limited effect.
How teams detect issues like this
Review applications that combine case-insensitive routing with path-scoped security middleware, especially where protected mount paths contain k. Test that routing and middleware selection agree for equivalent Unicode and ASCII path representations. Static analysis and code review can flag authorization implemented only in middleware mounts, as well as mismatched normalization logic. Policy checks can require authorization hooks on sensitive routes and regression tests for path-matching behavior.

