return proper status code 401/403 for various token auth situations - #347
Merged
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The changes are cohesive, improve security correctness and status-code semantics, and are backed by comprehensive, targeted tests across token, JWKS, and HTTP middleware paths.
Pull request overview
This PR corrects authentication vs authorization handling across the server by returning 401 Unauthorized for token/authentication failures and 403 Forbidden for authenticated-but-not-authorized cases (e.g., insufficient capabilities / low trust), while also hardening token parsing to avoid trusting unverified claims and preventing panics on malformed claims.
Changes:
- Adjust middleware and response handling to consistently classify failures as 401 vs 403 (and 400 for missing/invalid route vars like
mac). - Ensure JWT capability checks occur only after cryptographic verification; add safer claim type handling.
- Add extensive unit tests covering malformed/expired/invalid-signature tokens, capability enforcement, JWKS verification, and HTTP status classification.
File summaries
| File | Description |
|---|---|
| security/token.go | Defers capability checks until after signature/claim validation; avoids panic on non-string partner-id. |
| security/token_test.go | Adds regression tests for invalid signature/expired token classification and malformed partner-id handling. |
| security/jwks_manager.go | Hardens capabilities parsing; adds NewJwksManagerFromJWKS for testability. |
| security/jwks_manager_test.go | Adds end-to-end JWKS-backed tests (valid, missing capability, malformed capability, invalid signature, expired). |
| http/webconfig_server.go | Returns 400 for missing/invalid mac; returns 401 for authentication failures and 403 for capability/low-trust failures. |
| http/webconfig_server_test.go | Adds broad middleware status-code coverage for both TokenManager and JWKS-enabled paths. |
| http/response.go | Ensures 401 responses follow the same “header-only” behavior as 403/304/204. |
| http/multipart_test.go | Updates expected status for unauthenticated access and adds wrong-mac token 401 regression. |
| http/http_client.go | Treats 401 like other expected client error statuses in response handling. |
| common/error.go | Introduces a shared ErrNoCapabilities sentinel for consistent errors.Is checks. |
Review details
- Files reviewed: 10/10 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
No description provided.