test(auth): jwt validation edge cases#161
Open
Jayking40 wants to merge 1 commit intoCalloraOrg:mainfrom
Open
Conversation
|
@Jayking40 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
test(auth): JWT validation edge cases
Summary
Hardens the requireAuth middleware with proper JWT verification and adds comprehensive tests covering JWT failure modes: expired tokens, malformed payloads, unexpected algorithms, missing claims, and wrong signing secrets.
Previously, requireAuth extracted the Bearer token string and used it directly as the user ID without any cryptographic verification. This PR upgrades it to perform full
jwt.verify()validation while maintaining backward compatibility with thex-user-idheader path used by existing routes.Changes
Middleware hardening (requireAuth)
JWT_SECRETenvironment variablealg: none)userIdclaim in the token payloadTOKEN_EXPIRED,INVALID_TOKEN,MISSING_CLAIMS,MISSING_TOKENx-user-idheader fallback for backward compatibilityExtended JWT test helpers
alg: nonetoken for attack simulationAdded 20 integration tests covering:
alg: none)Testing
PASS tests/integration/requireAuth.test.ts requireAuth – happy path ✓ passes through with a valid Bearer JWT and sets authenticatedUser ✓ accepts x-user-id header when no Bearer token is present requireAuth – missing credentials ✓ returns 401 when no Authorization or x-user-id header is sent ✓ returns 401 when Authorization header is present but not Bearer scheme ✓ returns 401 when Bearer prefix has no token value ✓ returns 401 with MISSING_TOKEN for Bearer followed by only whitespace requireAuth – expired tokens ✓ returns 401 with TOKEN_EXPIRED code for an expired JWT requireAuth – malformed tokens ✓ rejects a completely invalid string ✓ rejects a token with only two dot-separated segments and garbage ✓ rejects a token with valid base64 header but corrupted payload ✓ does not leak token content in the error response requireAuth – algorithm restrictions ✓ rejects a token signed with HS384 when only HS256 is allowed ✓ rejects a token signed with HS512 when only HS256 is allowed ✓ rejects a crafted "alg: none" token requireAuth – wrong signing secret ✓ rejects a token signed with an incorrect secret requireAuth – missing or invalid claims ✓ rejects a token that has no userId claim ✓ rejects a token where userId is an empty string ✓ rejects a token where userId is a number instead of a string ✓ rejects a token with only standard JWT claims and no userId requireAuth – server misconfiguration ✓ returns 401 when JWT_SECRET env var is not set
Tests: 20 passed, 20 total
Full suite: all 193 tests pass. The 2 pre-existing suite failures (app.test.ts, health.test.ts) are caused by a missing Prisma generated client and are unrelated.
Security notes
none-algorithm and RSA-confusion attacksuserIdclaim to be acceptedJWT_SECRETenvironment variable must be configured for Bearer token auth to workCloses #109