T09 · Insecure Skill Coding Practices
- Location
references/middleware.md:110- Finding
Bearer Token Presence Check Allows Authentication Bypass
- Content
View full analysis
Vulnerability Details
File Location:
references/middleware.md, lines 110–122
Vulnerability Type: Broken authentication caused by missing token verification
Risk Level: HighVulnerable Code
javascript export const config = { matcher: ['/api/:path*', '/admin/:path*'], }; export function middleware(context) { const token = context.request.headers.get('Authorization'); if (!token || !token.startsWith('Bearer ')) { return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401, headers: { 'Content-Type': 'application/json' }, }); } return context.next(); }Technical Analysis
This copy-ready example is presented as an authentication guard, but it only verifies that the
Authorizationheader starts withBearer. It does not validate the token's signature, value, expiration, issuer, audience, algorithm, session state, or authorization claims.Consequently, any attacker can satisfy the guard with an arbitrary header such as:
http Authorization: Bearer attacker-controlled-valueThe middleware then invokes
context.next()and treats the request as authenticated. Because the matcher covers both/api/*and/admin/*, applications copying this pattern may unintentionally expose all routes under those prefixes.Attack Path
- An application adopts the documented middleware as an authentication guard.
- A protected API or administrative endpoint is placed under
/api/*or/admin/*. - An unauthenticated attacker sends a request with an arbitrary nonempty bearer value.
- The prefix check succeeds even though the token has no valid signature or identity.
- The middleware forwards the request to the protected route.
- The attacker receives the same endpoint access that the application intended to grant only to authenticated users.
Impact Assessment
An attacker can bypass the documented authenticat ...[truncated 377 chars]
- Remediation
View remediation
Remediation Suggestions
- Parse the bearer credential strictly and reject missing, empty, malformed, or duplicated authorization headers.
- Verify the token cryptographically using a trusted JWT or session library.
- Restrict acceptable signing algorithms and validate the signature using a trusted key.
- Enforce expiration, not-before time, issuer, audience, and token-purpose claims.
- Check required roles or scopes separately for administrative and data-changing routes.
- Support revocation or server-side session validation where immediate invalidation is required.
- Return
401 Unauthorizedfor invalid authentication and403 Forbiddenfor valid identities lacking permission. - Do not present a header-presence or prefix check as a complete authentication implementation.
- Add tests proving that arbitrary, expired, incorrectly signed, wrong-audience, and insufficient-scope tokens are rejected.
