Skip to content

πŸ› Enforce HS256-only JWT parsing β€” fix for TAG-Security review#4356

Merged
clubanderson merged 1 commit intomainfrom
fix/jwt-algorithm-validation
Apr 3, 2026
Merged

πŸ› Enforce HS256-only JWT parsing β€” fix for TAG-Security review#4356
clubanderson merged 1 commit intomainfrom
fix/jwt-algorithm-validation

Conversation

@clubanderson
Copy link
Copy Markdown
Collaborator

Summary

Consolidates all JWT parsing into a shared middleware.ParseJWT() function that uses jwt.WithValidMethods([]string{"HS256"}) β€” the golang-jwt/v5 library's recommended approach for preventing algorithm confusion attacks.

Before: jwt.ParseWithClaims() called directly at 4 locations without algorithm restriction.
After: Single ParseJWT() with HS256-only enforcement + defense-in-depth HMAC method check in keyfunc.

This is a prerequisite for the CNCF TAG-Security self-assessment (cncf/toc#2106) where we claim HS256-only enforcement.

Test plan

  • All existing JWT tests pass (TestJWTAuth β€” valid, missing, invalid sig, expired, query param)
  • Go build passes

All JWT parsing now goes through middleware.ParseJWT() which uses a shared
jwt.Parser configured with jwt.WithValidMethods([]string{"HS256"}).

Previously, ParseWithClaims was called without algorithm restriction β€” the
library's default accepts any signing method. This could theoretically
allow algorithm confusion attacks (e.g., HS384, RS256-with-HMAC-key).

Defense-in-depth: the keyfunc also explicitly checks token.Method is
*jwt.SigningMethodHMAC before returning the secret.

Four call sites consolidated:
- JWTAuth middleware (HTTP API)
- ValidateJWT (WebSocket/exec)
- RefreshToken handler
- Logout handler

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
@clubanderson
Copy link
Copy Markdown
Collaborator Author

/lgtm
/approve

Copilot AI review requested due to automatic review settings April 3, 2026 00:06
@kubestellar-prow
Copy link
Copy Markdown
Contributor

@clubanderson: you cannot LGTM your own PR.

Details

In response to this:

/lgtm
/approve

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Apr 3, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: clubanderson

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

1 similar comment
@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: clubanderson

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@netlify
Copy link
Copy Markdown

netlify bot commented Apr 3, 2026

βœ… Deploy Preview for kubestellarconsole canceled.

Name Link
πŸ”¨ Latest commit 3d42b25
πŸ” Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/69cf04772d22780008281c10

@kubestellar-prow kubestellar-prow bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

πŸ‘‹ Hey @clubanderson β€” thanks for opening this PR!

πŸ€– This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

@kubestellar-prow kubestellar-prow bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Consolidates JWT parsing/validation into a single middleware helper that enforces HS256-only parsing to prevent JWT algorithm confusion attacks, aligning implementation with the stated TAG-Security posture.

Changes:

  • Introduces middleware.ParseJWT() backed by a shared jwt.Parser configured with WithValidMethods([]string{"HS256"}) plus a defense-in-depth HMAC method check.
  • Updates all production call sites that previously used jwt.ParseWithClaims() directly to use middleware.ParseJWT() instead.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/api/middleware/auth.go Adds HS256-only shared parser and ParseJWT(); updates middleware validation paths to call it.
pkg/api/handlers/auth.go Updates logout/refresh handlers to parse tokens via middleware.ParseJWT() for consistent algorithm enforcement.

var jwtParser = jwt.NewParser(jwt.WithValidMethods([]string{"HS256"}))

// ParseJWT parses and validates a JWT token using the shared HS256-only parser.
// All JWT validation in the codebase should use this function (or the JWTAuth
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring claims "All JWT validation in the codebase" should use ParseJWT/JWTAuth, but there is at least one remaining direct jwt.ParseWithClaims usage in tests (pkg/api/handlers/auth_test.go). Either update that test to call ParseJWT too, or narrow this comment to explicitly refer to production/authentication paths to avoid a misleading guarantee.

Suggested change
// All JWT validation in the codebase should use this function (or the JWTAuth
// Production authentication paths should use this function (or the JWTAuth

Copilot uses AI. Check for mistakes.
@clubanderson
Copy link
Copy Markdown
Collaborator Author

πŸ”„ Auto-Applying Copilot Code Review

Copilot code review found 1 code suggestion(s) and 0 general comment(s).

@copilot Please apply all of the following code review suggestions:

  • pkg/api/middleware/auth.go (line 39): // Production authentication paths should use this function (or the JWTAuth

Push all fixes in a single commit. Run cd web && npm run build && npm run lint before committing.


Auto-generated by copilot-review-apply workflow.

@clubanderson clubanderson merged commit 079e2d2 into main Apr 3, 2026
26 of 27 checks passed
@kubestellar-prow kubestellar-prow bot deleted the fix/jwt-algorithm-validation branch April 3, 2026 00:18
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

Post-merge build verification passed βœ…

Both Go and frontend builds compiled successfully against merge commit 079e2d24ed1427440c166cb740568fa7c102c175.

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

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has signed the DCO. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants