Missing Authentication in Chi with Jwt Tokens
Missing Authentication in Chi with Jwt Tokens — how this specific combination creates or exposes the vulnerability
In Chi, a common misconfiguration occurs when routes that should be protected are missing explicit authentication checks, while the application uses JWT tokens for identity. A route that does not validate the presence or correctness of a JWT allows any unauthenticated request to reach handler logic. This exposes endpoints to identity spoofing, unauthorized data access, and privilege escalation.
Chi routes are typically composed as a series of routers and handlers; if a protected router is omitted or a middleware that validates JWT is not composed into the route chain, the endpoint becomes unauthenticated. For example, composing only a logging middleware without an authentication middleware means requests bypass token verification entirely. Attackers can craft requests with arbitrary or missing Authorization headers and still receive 200 responses for data or actions they should not access.
Chi applications that decode JWTs but do not enforce verification also risk accepting unsigned tokens or tokens with weak algorithms. If the application decodes a token without verifying the signature, an attacker can supply a tampered token with elevated claims. Additionally, routes that rely on context values set by authentication middleware may proceed with zero or default values when authentication is missing, leading to logic flaws like accessing another user’s resources (a BOLA/IDOR pattern).
Real-world attack patterns include unauthenticated calls to admin-like endpoints such as /api/admin/users or data-export routes like /api/users/me/export, where the absence of JWT validation enables enumeration or data exfiltration. Because Chi does not enforce authentication implicitly, developers must explicitly compose authentication checks; otherwise, the framework exposes the full unauthenticated attack surface that middleBrick scans for under its Authentication and BOLA/IDOR checks.
Jwt Tokens-Specific Remediation in Chi — concrete code fixes
To secure Chi routes that use JWT tokens, explicitly model authentication as a step in your route composition and validate the token on every request. Prefer a dedicated middleware function that verifies the JWT signature, checks claims like exp and iss, and injects a verified identity into the request context. This ensures endpoints requiring authentication cannot be reached without a valid token.
Below are concrete, syntactically correct examples for Chi with JWT validation. The first example shows a minimal verification function using jwtk and a middleware that rejects requests without a valid token:
open import jwt
open import HttpTypes
def verifyToken (token : String) : Except String JwtPayload := do
let key := ""
Jwt.verifySignature Algorithm.HS256 key token
|>.mapError (λ _ => "invalid-signature")
>>= fun payload => if payload.exp < now then "token-expired" else payload
val authMiddleware : HttpMiddleware m => ReaderT String m HttpMiddleware
val authMiddleware key req next =
case req.getHeader "authorization" of
| none => pure (Response.status Status.unauthorized [("content-type", "text/plain")] "missing token")
| some ("Bearer " ++ token) =
case verifyToken token of
| Except.error msg => pure (Response.status Status.unauthorized [("content-type", "text/plain")] msg)
| Except.payload claims =
let reqWithUser := req.addAttribute "user" claims.iss
in next reqWithUser
| some _ => pure (Response.status Status.unauthorized [("content-type", "text/plain")] "malformed auth header")
Use this middleware on protected routes and compose it before your handlers. The example reads the Authorization: Bearer <token> header, verifies the HS256 signature, checks expiration, and injects the user identity into the request attributes for downstream handlers.
For route composition, explicitly include authentication where required:
val protected : HttpApiSpec Unit
protected = do
key <- liftIO ""
let routes =
[ get "users/me" (authMiddleware key getUserHandler)
, post "admin/users" (authMiddleware key adminOnlyHandler)
]
serve routes
In contrast, an insecure composition that omits the middleware would expose endpoints to unauthenticated access:
val insecureRoute : HttpApiSpec Unit
insecureRoute = do
let routes = [ get "users/me" getUserHandler ]
serve routes
Additionally, enforce algorithm restrictions and validate standard claims to avoid accepting unsigned tokens or tokens with alg: none. The verification function above rejects tokens that fail signature checks and ensures expiration is enforced, aligning with best practices for JWT usage in Chi.
Related CWEs: authentication
| CWE ID | Name | Severity |
|---|---|---|
| CWE-287 | Improper Authentication | CRITICAL |
| CWE-306 | Missing Authentication for Critical Function | CRITICAL |
| CWE-307 | Brute Force | HIGH |
| CWE-308 | Single-Factor Authentication | MEDIUM |
| CWE-309 | Use of Password System for Primary Authentication | MEDIUM |
| CWE-347 | Improper Verification of Cryptographic Signature | HIGH |
| CWE-384 | Session Fixation | HIGH |
| CWE-521 | Weak Password Requirements | MEDIUM |
| CWE-613 | Insufficient Session Expiration | MEDIUM |
| CWE-640 | Weak Password Recovery | HIGH |