Summary
A logic flaw in @koa/router causes middleware to be silently dropped from the execution chain when a router prefix includes path parameters.
When critical middleware such as authentication, authorization, rate limiting, or input sanitization is skipped, attackers may access protected routes or send unsafe input without the intended safeguards being applied.
Affected context
This vulnerability appears in Node.js applications using @koa/router versions 14.0.0 up to (but not including) 15.0.0 where:
- Routers are configured with a prefix that contains one or more path parameters (for example,
/:tenant/apior/v:version/users) - Security-sensitive middleware (authentication, authorization, rate limiting, validation, sanitization, logging, etc.) is attached to the router or routes under that prefix
- Application developers assume that all requests matching those routes will pass through the configured middleware chain
Under these conditions, some middleware functions may be silently skipped, creating an inconsistent and unintuitive protection boundary.
Exploit preconditions
- Attack surface: HTTP(S) endpoints served via Koa that use @koa/router with parameterized prefixes
- Required access: Ability to send HTTP requests to affected routes; no prior authentication is necessary if the skipped middleware was intended to enforce it
- Trigger point: Requests to routes under a router that uses a prefix with path parameters, where middleware is expected to run but is silently dropped
- Why exploitation is possible: The router's internal handling of prefix path parameters causes certain middleware to be omitted from the execution chain without error, so routes that appear protected in code can be reached without the corresponding checks being executed
Why this happens
The root cause is a routing and middleware composition bug in @koa/router where prefixes containing path parameters interact incorrectly with the middleware chain assembly.
Instead of reliably composing all registered middleware in the order defined, the router logic can silently omit middleware when a prefixed route includes parameters in the prefix. This leads to inconsistent enforcement of access control and request processing rules.
Conceptually, this maps to CWE-284: Improper Access Control, because the application code indicates that certain resources should be protected by middleware-based checks, yet the underlying routing framework fails to apply those controls as intended, resulting in a practical bypass of authentication, authorization, and other security-related middleware.
Potential impact
- Attackers could access endpoints that developers intended to protect with authentication middleware
- Attackers may bypass fine-grained authorization checks tied to roles, tenants, or scopes
- Rate-limiting middleware may not execute, allowing abuse such as brute-force attempts or resource exhaustion
- Input validation or sanitization middleware may be skipped, increasing the risk of injection or data integrity issues
- Security logging or auditing middleware may not run, reducing visibility into malicious or unexpected activity
General mitigation guidance
Effective mitigation focuses on ensuring that middleware execution does not depend on fragile or ambiguous router prefix configurations:
- Avoid relying on prefixes that contain dynamic path parameters for security-critical middleware; instead, attach such middleware directly to specific routes or sub-routers without parameterized prefixes
- Structure routers so that authentication and authorization middleware are defined at clearly separated layers (for example, a top-level security router) where route paths are explicit and stable
- Introduce defensive checks inside downstream handlers (e.g., verifying that authentication context is present and valid) rather than assuming middleware always ran
- Apply least-privilege principles: treat unauthenticated or unauthorised requests as the default and require explicit confirmation before performing sensitive actions
- Use centralized, reusable middleware configurations for critical protections, and avoid complex or heavily parameterized routing structures for those paths
How teams detect issues like this
Detection generally involves both code-centric and runtime-centric approaches:
- Static/code review: Look for @koa/router usage where
router.prefix()or equivalent mechanisms include path parameters (such as/:id,/:tenant,/v:version) and where security-relevant middleware is attached at the router level - Pattern-based checks: Define internal guidelines or linters that flag parameterized prefixes on routers that are expected to enforce authentication, authorization, or rate limiting
- Automated testing: Add integration tests that assert critical middleware effects (such as requiring authentication, enforcing tenant boundaries, or applying rate limits) for routes under parameterized prefixes
- Runtime monitoring: Inspect access logs and authorization metrics for anomalies, such as protected routes being accessed without corresponding authentication events, which can indicate skipped middleware

