fix(jwe-decrypt): reject malformed tokens instead of returning 500 - #13844
Open
AlinsRan wants to merge 1 commit into
Open
fix(jwe-decrypt): reject malformed tokens instead of returning 500#13844AlinsRan wants to merge 1 commit into
AlinsRan wants to merge 1 commit into
Conversation
A crafted JWE token makes the plugin throw a Lua error, so the request fails with 500 instead of the 400 the plugin already returns for invalid tokens: - a header segment that decodes to JSON `null` yields the truthy `cjson.null` userdata, and a scalar yields a number, so reading `kid` from it throws - an iv, ciphertext or tag that is not valid base64url decodes to nil, so `aes:new()` returns nil and calling `decrypt()` on it throws The consumer secret has the same problem: when data encryption is enabled the schema check cannot validate it, so a secret that is not valid base64url is accepted and `get_secret()` returns nil at request time. And when the check does run, `#base64.decode_base64url(secret)` throws on such a secret rather than reporting a schema error. Validate the decoded header, the token segments and the secret, and check the `aes:new()` return value, so all of these end up in the existing 400 response.
AlinsRan
force-pushed
the
fix/jwe-decrypt-malformed-token
branch
from
August 19, 2026 06:19
4b721d3 to
af4180a
Compare
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
A crafted JWE token makes
jwe-decryptthrow a Lua error, so the request ends with a 500 instead of the 400 the plugin already returns for invalid tokens:core.json.decode()and only checked for truthiness. A header ofnulldecodes to the truthycjson.nulluserdata and a JSON scalar decodes to a number, sojwe_obj.header_obj.kidthrows:iv,ciphertextortagthat is not valid base64url decodes to nil.aes:new()then returnsnil, "iv is needed", and callingdecrypt()on it throws:Both are reachable with a single request against any route using the plugin, e.g.
The consumer secret has the same shape of problem.
check_schema()skips the length check when data encryption is enabled — which is the default — so asecretthat is not valid base64url is accepted, andget_secret()returns nil at request time, again ending in a 500. When the check does run (data_encryption.enable_encrypt_fields: false),#base64.decode_base64url(conf.secret)throws on such a secret instead of reporting a schema error.This PR validates the decoded header, the token segments and the secret, and checks the
aes:new()return value, so all of these paths end in the 400 the plugin already returns.Checklist
Tests: 5 new blocks in
t/plugin/jwe-decrypt.tcover thenullheader, a scalar header, invalid base64url token segments, a consumer secret that cannot be decoded at request time, and the schema error for a non-base64url secret. All five fail on master and pass with this change. No documentation change is needed: the plugin already documents 400 for an invalid token, and this only stops the malformed cases from escaping as 500.