Fix JWT header validation for tokens with kid field but missing typ field#19
Open
jroark-envoy wants to merge 1 commit intokataras:mainfrom
Open
Fix JWT header validation for tokens with kid field but missing typ field#19jroark-envoy wants to merge 1 commit intokataras:mainfrom
jroark-envoy wants to merge 1 commit intokataras:mainfrom
Conversation
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.
Fix JWT header validation for tokens with kid field but missing typ field
Fixes #13
This PR resolves the JWT validation issue reported in #13 where tokens with additional header fields (like
kid) fail validation when the optionaltypfield is missing.Problem Description
The current
compareHeaderfunction intoken.goincorrectly assumes that any JWT header without thetypfield will be exactly 15 bytes long. This assumption breaks when headers contain additional fields like:kid(Key ID) - commonly used by services like Cloudflare Zero Trustiss(Issuer)jku(JWK Set URL)Example failing header:
{"kid":"test-key-id","alg":"ES384"}This is 34 bytes, not 15, so it bypasses the "no typ field" check and fails validation.
Root Cause
In
token.golines 216-224:The hardcoded
n == 15check only handles the specific case of{"alg":"XXX"}but not headers with additional fields.Solution
This fix:
typto be missing or "JWT"Changes Made
encoding/jsonimportcompareHeaderfunction with JSON parsing fallbackTesting
The fix has been validated with:
typfieldPerformance Impact
Related Issues
typfieldtypfield optional