security: pin JWT signing algorithm to HS256 to prevent confusion attacks#99
Merged
security: pin JWT signing algorithm to HS256 to prevent confusion attacks#99
Conversation
#95) Verified that all JWT validation paths in JWTAuthModule already enforce HS256 via both type assertion (*jwt.SigningMethodHMAC) and explicit algorithm check (token.Method.Alg() != jwt.SigningMethodHS256.Alg()). Added tests to module/jwt_auth_test.go that explicitly confirm tokens signed with HS384 or HS512 are rejected by: - Authenticate() — the AuthProvider interface method - handleRefresh via Handle() — the /auth/refresh endpoint - extractUserFromRequest via Handle() — all protected endpoints The api package (middleware.go, auth_handler.go) already had equivalent algorithm rejection tests in auth_handler_test.go. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive test coverage to verify that JWT algorithm confusion attacks are prevented in the JWTAuthModule. The PR confirms that all JWT validation paths already enforce HS256 algorithm pinning through both type assertions and explicit algorithm checks, and adds tests to ensure this security control continues to work correctly.
Changes:
- Added 4 new test functions covering all three JWT parsing paths in
JWTAuthModule - Tests verify that HS384 and HS512 tokens are rejected while HS256 tokens are accepted
- Complements existing algorithm confusion tests in the
apipackage
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.
Summary
*jwt.SigningMethodHMAC) and an explicit algorithm check (token.Method.Alg() != jwt.SigningMethodHS256.Alg())module/jwt_auth_test.gocovering all three JWT parsing paths inJWTAuthModule:Authenticate(), the/auth/refreshendpoint, and protected endpoints viaextractUserFromRequest()apipackage (middleware.go,auth_handler.go) already had equivalent algorithm rejection testsAffected files
module/jwt_auth_test.go— 4 new test functions (8 sub-tests) confirming HS384/HS512 tokens are rejectedTest plan
TestJWTAuth_Authenticate_RejectsNonHS256—Authenticate()rejects HS384 and HS512TestJWTAuth_Authenticate_AcceptsHS256—Authenticate()accepts valid HS256 tokensTestJWTAuth_HandleRefresh_RejectsNonHS256—/auth/refreshrejects HS384 and HS512TestJWTAuth_ExtractUser_RejectsNonHS256— protected endpoints reject HS384 and HS512TestJWTAuth_*tests continue to passgo fmtandgolangci-lintpassCloses #95
🤖 Generated with Claude Code