fix(jwt-auth): reject malformed JWT signature instead of erroring#13518
Open
shreemaan-abhishek wants to merge 2 commits into
Open
fix(jwt-auth): reject malformed JWT signature instead of erroring#13518shreemaan-abhishek wants to merge 2 commits into
shreemaan-abhishek wants to merge 2 commits into
Conversation
verify_signature indexed the per-algorithm verifier and passed the decoded signature without guarding either. A token carrying an unsupported alg, a non-base64url signature, or a signature of the wrong length made the verifier raise a Lua error (e.g. "Signature must be 64 bytes." or a length-of-nil error), which propagated as a 500 response instead of a clean 401 for any route protected by jwt-auth. Guard the algorithm lookup and the base64url decode, and run the verifier under pcall so a malformed token is rejected as an invalid signature rather than crashing the request.
- localize tostring to satisfy the lj-releng global check (CI lint) - keep the verifier's own (verified, err) return values through the pcall wrapper instead of dropping the secondary error detail - merge the two malformed-signature cases into one self-contained test to remove the order dependency between them
Contributor
Author
|
Addressed the lint failure and review feedback in the follow-up commit:
Verified locally: |
membphis
approved these changes
Jun 11, 2026
AlinsRan
approved these changes
Jun 12, 2026
nic-6443
approved these changes
Jun 12, 2026
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.
Description
jwt-auth's custom JWT parser (apisix/plugins/jwt-auth/parser.lua) replacedresty.jwtverification with per-algorithm verifiers.verify_signaturelooked up the verifier and passed the decoded signature without guarding either path:The per-algorithm verifiers
asserton signature length and key validity, andbase64_decodereturnsnilfor an invalid base64url signature. So a token with a wrong-length signature (assert(#signature == 64, "Signature must be 64 bytes.")) or a non-base64url signature (#nil-> "attempt to get length of a nil value") makes the verifier raise a Lua error. Since the caller invokesverify_signaturewithout a guard, the error propagates and the request returns 500 instead of 401 for any route protected byjwt-auth. An unauthenticated client that knows a valid consumer key can repeatedly trigger these internal errors and error-log noise.This makes
verify_signaturedefensive: guard the algorithm lookup and the base64url decode, and run the verifier underpcallso a malformed token is cleanly rejected as an invalid signature instead of crashing the request.Which issue(s) this PR fixes:
N/A (reported via a private security scan)
Checklist