This PR adds comprehensive negative test cases for the requireAuth middleware in src/middleware/authorization.ts to ensure proper rejection of malformed, expired, and maliciously-crafted JWT tokens.
Added the following test cases to the existing test suite:
-
Missing Claims Tests
rejects a token missing the required 'sub' claim- Verifies tokens withoutsubare rejected with "missing required claims" messagerejects a token missing the required 'email' claim- Verifies tokens withoutemailare rejectedrejects a token missing the required 'role' claim- Verifies tokens withoutroleare rejected with "unrecognised role" message
-
Header Edge Case Tests
rejects missing Authorization header- Verifies 401 on missing Authorization headerrejects malformed Bearer prefix (no space)- Verifies rejection when "Bearer" prefix lacks space separatorrejects empty token after Bearer- Verifies rejection of empty token string after "Bearer "rejects malformed Bearer prefix (wrong prefix)- Verifies rejection of non-Bearer authentication schemes (e.g., Basic)
-
Malformed Token Tests
rejects a token with a tampered signature- Verifies tokens with modified signatures are rejectedrejects a token with malformed base64 payload- Verifies tokens with invalid base64 encoding are rejected
Added JSDoc security notes to the requireAuth function documenting:
- Only HS256 algorithm is accepted
- Required claims (
sub,email,role) - Role validation against platform allowlist
- Rejection of
alg: noneand algorithm confusion attempts
The test additions ensure that:
-
JWT Expiry: Expired tokens are rejected with an appropriate error message, preventing replay attacks on stale tokens.
-
Signature Validation: Tokens signed with a different secret or with tampered signatures fail HMAC verification, preventing forgery.
-
Algorithm Confusion Protection: The tests explicitly verify that:
alg: nonetokens are rejected (prevents unsecured JWT bypass)- RS256, HS384, HS512 algorithms are rejected (prevents algorithm confusion attacks)
- Tokens without an algorithm header are rejected
-
Claim Validation: Missing or malformed claims result in rejection before the request reaches protected endpoints, ensuring request integrity.
All 31 tests pass successfully, covering the full spectrum of JWT verification failure modes for the requireAuth middleware.
closes #473