Skip to content

Pin JWT signing algorithm to HS256 across all JWT validation sites - #57

Merged
intel352 merged 3 commits into
mainfrom
copilot/pin-jwt-signing-algorithm-hs256
Feb 23, 2026
Merged

intel352 merged 3 commits into
mainfrom
copilot/pin-jwt-signing-algorithm-hs256

Conversation

Copilot AI commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

JWT key validation functions accepted any *jwt.SigningMethodHMAC variant (HS256, HS384, HS512), leaving an algorithm confusion vector where tokens signed with a weaker or unintended HMAC variant would be accepted.

Changes

  • api/middleware.go: After the HMAC type assertion, explicitly reject any algorithm other than HS256:

    if t.Method.Alg() != jwt.SigningMethodHS256.Alg() {
        return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
    }
  • api/auth_handler.go: Applied the same HS256 algorithm pin to the Refresh endpoint's jwt.Parse key function.

  • module/jwt_auth.go: Applied the same HS256 algorithm pin to three jwt.Parse key functions: Authenticate, extractClaims, and handleRefresh.

  • module/api_v1_handler.go: Applied the same HS256 algorithm pin to the extractClaims method's jwt.Parse key function.

  • api/auth_handler_test.go: Extended TestRequireAuth with sub-cases asserting that tokens signed with HS384 and HS512 are rejected with HTTP 401.

Original prompt

Pin JWT Signing Algorithm to HS256 in api/middleware.go

Problem

api/middleware.go validates JWT tokens by checking that the signing method is of type *jwt.SigningMethodHMAC, but does not verify the specific algorithm (HS256 vs HS384 vs HS512). This is a mild algorithm confusion vector — a token signed with a weaker or different HMAC variant would be accepted.

Fix Direction

  1. Open api/middleware.go and find the jwt.Parse key function (around line 91–96).
  2. After confirming the method is *jwt.SigningMethodHMAC, add an explicit algorithm check:
    if token.Method.Alg() != jwt.SigningMethodHS256.Alg() {
        return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
    }
  3. Optionally make the expected algorithm configurable (e.g. read from the same config struct that holds the JWT secret), defaulting to HS256.
  4. Update any tests that use a different HMAC algorithm to use HS256, or add a test that verifies tokens signed with HS384/HS512 are rejected.

Acceptance Criteria

  • Tokens signed with HS384 or HS512 are rejected with an error
  • Tokens signed with HS256 continue to be accepted
  • The expected algorithm is either hardcoded to HS256 or configurable
  • go build ./... passes
  • go test ./... passes

Context

See docs/DEFERRED_ISSUES.md — section "Security", item "JWT Algorithm Not Explicitly Pinned".


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants