Summary
A logic flaw in NestJS applications using @nestjs/platform-fastify can allow attackers to bypass authentication and authorization middleware when certain Fastify path-normalization options are enabled.
By crafting requests that are normalized differently by Fastify than they are matched by NestJS routing and guards, an attacker may gain access to protected routes that should require valid credentials or specific privileges.
Affected context
This vulnerability affects NestJS applications that:
- Use the Fastify adapter (
@nestjs/platform-fastify) for HTTP handling - Enable Fastify path-normalization options (such as those that alter or canonicalize URLs)
- Rely on NestJS guards, interceptors, or middleware for authentication/authorization checks on specific route patterns
The issue emerges when the HTTP server's normalized path used for routing differs from the raw or expected path on which security checks were conceptually based, creating inconsistencies between what is authenticated and what is actually served.
Exploit preconditions
- Attack surface: Public or internal HTTP endpoints exposed by a NestJS application using Fastify.
- Required access: Network access to the affected service; no prior authentication may be required for exploitation.
- Trigger point: Submission of specially crafted HTTP request paths that are transformed by Fastify's normalization logic into routes protected by guards or middleware.
- Why exploitation is possible: The authorization logic assumes a particular path structure, while the underlying HTTP server normalizes or rewrites that path before routing. This mismatch can cause NestJS to treat a request as matching a protected route without the corresponding guard or middleware being reliably applied, or to route traffic around intended protection.
Why this happens
The root cause is an inconsistent trust in normalized request paths between the HTTP adapter (Fastify) and the NestJS routing and guard system.
When path-normalization options are enabled, Fastify may canonicalize or alter incoming URLs (e.g., collapsing segments, handling trailing slashes, or resolving ambiguous path sequences) before routing. If NestJS guards, interceptors, or middleware are configured assuming a different path representation, security decisions may be made on one view of the path while routing decisions are made on another. This aligns with CWE-863: Incorrect Authorization, where authorization logic is incomplete or based on mismatched assumptions, allowing access to resources without proper checks.
Potential impact
- Attackers may access API endpoints that are intended to require authentication.
- Privilege escalation where low-privileged users can reach endpoints reserved for administrators or internal systems.
- Exposure of sensitive data from protected resources (e.g., user profiles, configuration, or logs).
- Unauthorized execution of state-changing operations such as account modifications, configuration changes, or business-critical transactions.
- Potential pivot into broader system compromise depending on the sensitivity and capabilities of the exposed endpoints.
General mitigation guidance
Effective mitigation focuses on aligning security checks with actual routing behavior and reducing reliance on ambiguous path handling:
- Prefer consistent, conservative path handling configuration: avoid enabling path-normalization options that can significantly alter requested URLs unless they are clearly understood and tested with security controls.
- Ensure authentication and authorization guards are attached at module or global levels rather than relying solely on finely scoped path patterns that may be affected by normalization.
- Treat all incoming paths as untrusted and validate them consistently before applying security decisions; avoid constructing security logic based on assumptions of raw URL structure.
- Implement least-privilege principles for routes and controllers: deny by default and explicitly allow where required, so unexpected routing paths fail closed rather than open.
- Use clear, unambiguous route definitions and avoid overlapping or ambiguous paths that can be interpreted differently after normalization.
How teams detect issues like this
Detection should target path-handling inconsistencies and missing guard coverage:
- Static analysis and code review:
- Review NestJS controllers and route definitions for endpoints that are expected to be protected but do not have guards or middleware consistently applied.
- Look for usage of Fastify path-normalization or URL rewriting options and cross-check that corresponding routes are tested with those settings enabled.
- Flag patterns where security decisions (e.g., guards, interceptors) depend on specific URL shapes that may be altered by normalization.
- Configuration review:
- Audit Fastify adapter configuration for path-normalization features and document their intended behavior.
- Ensure that security policies describe how paths are normalized and routed under different deployment conditions (proxies, load balancers, etc.).
- Runtime and log-based detection:
- Monitor access logs for unexpected access patterns, such as protected endpoints being reached via unusual or malformed URLs.
- Correlate authorization failures and successes across similar endpoints to identify suspicious discrepancies.
- Automated tooling:
- Incorporate API security testing tools or fuzzers that vary URL encodings, segments, and slashes to identify cases where protected endpoints become reachable without proper authentication.

