Skip to content

fix(jwe-decrypt): reject tokens that fail to decrypt#13404

Open
shreemaan-abhishek wants to merge 2 commits into
apache:masterfrom
shreemaan-abhishek:fix/jwe-decrypt-decryption-error
Open

fix(jwe-decrypt): reject tokens that fail to decrypt#13404
shreemaan-abhishek wants to merge 2 commits into
apache:masterfrom
shreemaan-abhishek:fix/jwe-decrypt-decryption-error

Conversation

@shreemaan-abhishek
Copy link
Copy Markdown
Contributor

Description

jwe-decrypt's jwe_decrypt_with_obj() returned only the decrypted text, so the error from the AES-256-GCM decryption was always discarded. rewrite() therefore never reached the failed to decrypt JWE token branch, and a well-formed JWE whose ciphertext could not actually be decrypted was treated as a success and forwarded upstream.

This PR propagates the decryption error and returns 400 when decryption does not produce a plaintext, so only tokens that genuinely decrypt are accepted.

A test case was added to t/plugin/jwe-decrypt.t covering a well-formed token whose ciphertext fails to decrypt.

This is backward compatible: tokens that decrypt successfully are unaffected; only tokens that never decrypted, which previously slipped through, are now correctly rejected.

Which issue(s) this PR fixes:

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

jwe_decrypt_with_obj() returned only the decrypted value, so the error
from the AES-256-GCM decryption was always discarded. rewrite() then
never entered the "failed to decrypt JWE token" branch: a well-formed
JWE whose ciphertext could not be decrypted was accepted and forwarded
upstream instead of being rejected.

Propagate the decryption error and return 400 when decryption does not
yield a plaintext. Add a test covering a well-formed token whose
ciphertext fails to decrypt.
@shreemaan-abhishek shreemaan-abhishek marked this pull request as ready for review May 21, 2026 06:21
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels May 21, 2026
The JWE token used by the header-parsing tests was generated before the
IV handling was corrected, so it no longer decrypts under the current
code. These tests previously passed only because a failed decryption did
not stop the request. Now that a failed decryption is rejected, the
token is regenerated so it decrypts successfully.
Copy link
Copy Markdown

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

This PR fixes a correctness issue in the jwe-decrypt auth plugin where AES-256-GCM decryption errors were dropped, allowing structurally valid but undecryptable JWE tokens to pass through and be forwarded upstream. It propagates the decryption error and rejects requests when decryption fails, with a regression test ensuring undecryptable ciphertext is denied.

Changes:

  • Propagate the AES-256-GCM decrypt error from jwe_decrypt_with_obj() to its caller.
  • Update rewrite() to reject requests when decryption yields no plaintext (returning HTTP 400).
  • Add a test case covering a well-formed JWE that fails integrity/authentication during decryption.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
apisix/plugins/jwe-decrypt.lua Return decrypt errors from the AES-GCM decrypt call and fail closed in rewrite() when plaintext is not produced.
t/plugin/jwe-decrypt.t Update token fixtures and add a regression test ensuring undecryptable (but well-formed) JWE tokens are rejected with 400.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants