fix(encryption): read alg/enc exclusively from protected header in JWEDecrypter#658
Open
rossaddison wants to merge 1 commit into
Open
fix(encryption): read alg/enc exclusively from protected header in JWEDecrypter#658rossaddison wants to merge 1 commit into
rossaddison wants to merge 1 commit into
Conversation
…EDecrypter RFC 7516 §4.1.1 (alg) and §4.1.2 (enc) require both parameters to be integrity-protected. The previous implementation passed the merged $completeHeader (sharedProtectedHeader + sharedHeader + recipientHeader) to getKeyEncryptionAlgorithm() and getContentEncryptionAlgorithm(). Because array_merge() exhibits last-wins behaviour, an attacker could override alg or enc by placing a different value in an unprotected header field, creating a TOCTOU split between HeaderCheckerManager validation and actual decryption. Fix: extract alg/enc exclusively from getSharedProtectedHeader(). The merged $completeHeader is still forwarded to decryptCEK() because ECDH parameters (epk, apu, apv) may legitimately reside in per-recipient unprotected headers. Also adds is_string() guards in both getter methods so a malformed non-string header value cannot reach the AlgorithmManager. Note: JWSVerifier::getAlgorithm() already reads alg from getProtectedHeader() only and is not affected. Fixes web-token#114
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.
Summary
JWEDecrypter::decryptRecipientKey()builds$completeHeaderby merging the shared protected header, the shared unprotected header, and the per-recipient unprotected header viaarray_merge(). Becausearray_merge()exhibits last-wins behaviour for duplicate string keys, an attacker can override the integrity-protectedalgorencparameters by placing different values in an unprotected header field.This creates a TOCTOU split:
HeaderCheckerManagervalidatesalg/encfrom the protected headergetKeyEncryptionAlgorithm()/getContentEncryptionAlgorithm()previously resolved the algorithm from the merged header where unprotected values winRelated advisory: #114
What this PR does NOT change
JWSVerifier::getAlgorithm()already readsalgexclusively fromgetProtectedHeader()and is not affected.Fix
Pass
$jwe->getSharedProtectedHeader()— the integrity-protected portion — to bothgetKeyEncryptionAlgorithm()andgetContentEncryptionAlgorithm()instead of the merged$completeHeader.The merged
$completeHeaderis still forwarded todecryptCEK()unchanged, because ECDH-ES parameters (epk,apu,apv) may legitimately reside in per-recipient unprotected headers per RFC 7516 §4.6.is_string()guards are added in both getter methods so a malformed non-string header value cannot reach theAlgorithmManager.RFC references
algMUST be integrity-protectedencMUST be integrity-protectedTesting
Please run the existing JWE test suite. A targeted regression test demonstrating the override scenario would be a welcome addition from the maintainers.