Summary
A middleware prefix handling flaw in @fastify/express allows routes inside prefixed plugin scopes to be matched without their intended path-scoped middleware when non-string mount paths are used.
When applications rely on scoped middleware for authentication, authorization, rate limiting, or auditing, attackers can send requests to prefixed routes that bypass these controls and gain unintended access.
Affected context
This vulnerability arises in Node.js applications using Fastify with the @fastify/express plugin, specifically when:
- Plugins are registered with a prefix (e.g.,
/api/v1), and - Middleware is mounted using non-string path arguments (arrays of paths or regular expressions), and
- Those middleware functions are expected to enforce security or auditing at specific route paths.
In this context, prefixed routes still match and execute route handlers, but the associated middleware is not invoked because their mount paths are not correctly rewritten with the plugin prefix.
Exploit preconditions
- Attack surface: HTTP endpoints exposed by Fastify applications using @fastify/express with prefixed plugins.
- Required access: Ability to send HTTP requests to prefixed routes (typically remote, unauthenticated network access).
- Trigger point: Requests directed at routes inside a plugin scope that rely on path-scoped middleware registered with array or RegExp mount paths.
- Why exploitation is possible: The plugin prefix is only applied to string mount paths; arrays and regular expressions remain unprefixed, so middleware path matching fails while the route itself still matches and executes, leaving protected functionality reachable without the intended checks.
Why this happens
The core issue is inconsistent handling of route prefixes when applying middleware mount paths within the @fastify/express integration.
Only string-based path arguments receive the plugin prefix, while non-string paths—such as arrays and regular expressions—are left unchanged. This breaks the assumed invariant that all paths inside a prefixed scope share the same prefix.
Under CWE-285 (Improper Authorization), this manifests as security-critical middleware (auth, rate limiting, auditing) being logically detached from the routes they are intended to protect, allowing unauthorized requests to reach those routes without undergoing the configured checks.
Potential impact
- Unauthorized access to routes that were intended to require authentication.
- Bypass of authorization checks and role or permission enforcement.
- Circumvention of rate limiting and abuse-prevention controls.
- Missing audit logging or compliance trails for sensitive operations.
- Increased exposure of internal APIs or administrative functionality.
General mitigation guidance
Effective mitigation focuses on aligning middleware scoping logic with route prefix behavior and reducing implicit assumptions about path matching.
Common defensive patterns include:
- Using consistent, string-based mount paths in prefixed plugin scopes so that prefix rewriting is predictable and uniform.
- Avoiding reliance on complex path arrays or regular expressions for critical security middleware unless their interaction with prefixes is explicitly validated.
- Centralizing authentication and authorization logic at a higher, non-prefixed level (e.g., global hooks or root-level middleware) so that security does not depend on per-scope rewriting.
- Designing middleware registration to default to protecting all routes within a trust boundary, rather than relying on fine-grained path filters that can desynchronize from routing behavior.
- Applying least-privilege principles in route design, so sensitive operations are isolated and guarded by multiple layers of checks rather than a single path-based middleware.
How teams detect issues like this
Teams can detect this class of issue by combining static inspection, configuration review, and runtime verification:
- Static analysis or code search for middleware registration using arrays or regular expressions in @fastify/express plugins that also use prefixes, especially where those middleware functions perform authentication, authorization, or rate limiting.
- Code review checklists that explicitly ask whether middleware mount paths are correctly aligned with route prefixes, and whether any non-string paths might be skipped.
- Automated policy checks in CI that flag prefixed plugin registrations where security-sensitive middleware is not applied globally or where path filters are inconsistent with declared route paths.
- Integration tests that exercise protected routes via their full, prefixed paths and assert that the expected middleware behavior (auth challenge, denial, logging, throttling) occurs.

