Rejected JWE tokens write decryption keys into exception stack traces (4.2.1 to 4.2.5)
Description
Impact
Since 4.2.1, the exception that JWELoader throws for a rejected token has, as its previous exception, the failure met with the last key it tried, and JWEDecrypter hands every per-key failure to the onError callable (7e93ca0d, #699). Some of those failures are raised in calls that receive key material as a plain string: JWEDecrypter::checkCekSize() and decryptPayload(), decryptContent() of the AES-GCM and AES-CBC-HMAC algorithms (and of the experimental AES-CCM ones), unwrap() of spomky-labs/aes-key-wrap (A\*KW, PBES2, ECDH-ES+A\*KW, ECDH-SS+A\*KW) and ConcatKDF::generate().
When PHP records the arguments of stack frames (zend.exception_ignore_args = Off: the built-in default, in effect whenever no php.ini is loaded, as in the official Docker images, and the value of php.ini-development), the exception chain holds:
- the application's
dirkey or AES key wrapping key, for a token made of arbitrary bytes that anyone can build without any key; - the PBKDF2 output for a salt and an iteration count chosen by the sender (PBES2), which turns each password guess into a single PBKDF2 iteration offline;
- the content encryption key of a genuine token that was captured and altered (one flipped tag bit, another
enc), and for ECDH-ES the shared secret Z or the derived key wrapping key.
The library sends nothing back to the sender. The exposure depends on where the application records the exception:
- text forms (
(string) $e,getTraceAsString()of the previous exception, PHP's ownUncaught …log line) show the first 15 bytes of each argument (zend.exception_string_param_max_lendefaults to 15). For a 128-bit key (dirwith A128GCM, A128KW), the missing byte is found with at most 256 trial decryptions of one token. Withdisplay_errorson and no exception handler, the same line is printed in the response; - tools that copy
getTrace()arguments, such as Symfony'sFlattenException(debug error page, profiler) and error trackers that record frame arguments, receive the whole key, whatever its size.
Private RSA, EC and OKP keys are not exposed: they travel as JWK objects, and ext/sodium does not record the arguments of the X25519 operation. With zend.exception_ignore_args = On (the value of php.ini-production), nothing is recorded.
Affected configurations
Applications that decrypt JWEs from untrusted senders with web-token/jwt-library or web-token/jwt-framework 4.2.1 to 4.2.5 (and web-token/jwt-experimental 4.2.1 to 4.2.5 for the AES-CCM algorithms), record exceptions with the arguments of their frames, and let people other than their operators read those records.
Versions before 4.2.1 are not affected: their decrypter discarded every per-key failure and the loader threw a new exception without previous. The unreleased 4.3.x and 5.0.x branches carry the same code and receive the fix before 4.3.0.
Patches
Fixed in 4.2.6. The parameters that receive key material on the decryption path are marked #[\SensitiveParameter], so that PHP records a SensitiveParameterValue object instead of the value: the content encryption key in JWEDecrypter and in decryptContent() of the AES-GCM, AES-CBC-HMAC and experimental AES-CCM algorithms, the shared secret of ConcatKDF::generate() and the decrypted block of RSA1_5. spomky-labs/aes-key-wrap does not mark its own parameters: its failure is now rethrown as a new exception without previous, so the frame that holds the key encryption key is not kept.
The same release keeps the decrypted content of a nested token out of the traces. When the inner JWS of a nested token is refused, for instance once the issuer has rotated its signing key, the decrypted inner token and its claims were arguments of JWSLoader::loadAndVerifyWithKeySet() and of its override in the bundle, of the serializers, and of the verify() method of the algorithms that throw. These parameters are now marked too, and JsonConverter::decode() chains a copy of the exception of json_decode(), whose trace held the document, instead of the original. The sender cannot have a genuine token refused, so this part is hardening; it has been present since 1.1.0.
Compatibility. The classes, messages, codes and previous exceptions of the exception chain are unchanged. An AES key wrap failure is still an InvalidArgumentException with the message of spomky-labs/aes-key-wrap, now created by the library. The previous exception of JsonConverter::decode() is still a JsonException with the same message and code. Only the recorded arguments change: debug traces no longer show the keys, tokens and signing inputs listed above. The attribute is not inherited, so a third-party content encryption algorithm, or a class that overrides decryptContent(), verify() or hash(), has to mark its own parameter.
Workarounds
Set zend.exception_ignore_args = On and zend.exception_string_param_max_len = 0, as php.ini-production does. Do not send exception traces with their arguments to logs or error trackers that other people can read. If such records may have been read, rotate the dir, AES key wrapping and PBES2 keys after upgrading.
References
- PHP RFC: Redacting parameters in back traces (the
SensitiveParameterattribute, PHP 8.2) - PHP directives
zend.exception_ignore_argsandzend.exception_string_param_max_len - 7e93ca0d (#699), which chains the cause of a failure since 4.2.1
Credits
Reported by Anthropic's OSS Scanner (ANT-2026-EVZLYH1X, ANT-2026-GVIDATLB).