Skip to main content
Menu
Advisories

JWS and JWE serializers decode attacker-controlled JSON without bounds before verification (CPU and memory denial of service)

GHSA-2XQ2-PP89-X7FRPublished Updated GitHub

Description

Impact

Every JWS and JWE serializer gives the token, or its base64url-decoded protected headers, to json_decode() before any algorithm, header checker or key is consulted. json_decode() only limits how deep a document is nested, not how many values it holds. The sender of a token therefore chooses what its rejection costs, without holding any key or valid token:

  • CPU. In associative mode every JSON object becomes a PHP array, and PHP's hash tables have no per-process seed. Member names that collide (decimal multiples of 131072, or equal-length concatenations of Ez, FY and G8) make the decoding quadratic. A compact JWS or JWE of 1.3 MB whose protected header holds 65,536 such names took 8.6 s to reject, against 0.03 s for other names of the same length, and the cost roughly quadruples each time the token doubles. In the JSON General serialization, the same cost can be spread over the protected headers of many signatures.
  • Memory. Nested arrays such as [[[…]]] decode to about a hundred times their size wherever they are in the token, including in members that no serializer reads. A JSON General JWS of 1 MB took 108 MB. Under PHP's default memory_limit of 128M, a token of 1.25 MB ends the request in a fatal error that the application cannot catch.

Nothing is disclosed and no token is accepted: this is a denial of service, and a few concurrent requests can keep every worker of a pool busy.

Affected configurations

Applications that unserialize or load JWS or JWE from untrusted senders, whatever the serialization (compact, JSON flattened, JSON general), the algorithms or the header checkers: JWSSerializerManager, JWESerializerManager, JWSLoader, JWELoader, NestedTokenLoader and the Symfony bundle services built on them.

The token has to be several hundred kilobytes long, so the exposure depends on what the HTTP stack lets through:

  • tokens received in a request body (form-posted client_assertion, request objects, form_post responses, back-channel logout tokens, JSON-serialized tokens) are exposed up to post_max_size, 8 MB by default;
  • tokens received in an HTTP header (bearer tokens, DPoP proofs) are exposed when the server accepts large headers. nginx and Apache stop a header line at 8 KB by default, which keeps such a token harmless. Caddy, on which FrankenPHP is built, accepts about 1 MB by default: a compact JWS of 1 MB in an Authorization header took 5.4 s to reject.

The unbounded decoding is as old as the library: every release is affected.

Patches

Fixed in 3.4.13, 4.0.9, 4.1.11 and 4.2.6. 3.4.13 is the last release of the 3.4 branch, which has reached its end of life.

Before decoding a JSON document, the serializers now count the brackets, braces and commas found outside its string literals, which bounds the number of values and object members it can hold. All the documents of one token, meaning the token itself in the JSON serializations and each of its protected headers, share a budget of 4,096. A token beyond that budget is rejected before json_decode() runs, with the same InvalidArgumentException('Unsupported input.') as any input that cannot be parsed, so the loaders report it like any other token they cannot load. The tokens above are now rejected in about 0.02 s, using at most a few megabytes of memory. The most expensive token left within the budget (4,000 colliding names, 74 KB) costs 0.03 s.

Compatibility. Only tokens beyond the budget behave differently: they are rejected, even when they are correctly signed or encrypted. The content of strings is not counted, so large payloads, unencoded (b64: false) payloads and long x5c chains are not affected. A protected header usually holds a few dozen values and a signature or recipient entry five to ten, so a JSON General JWS can still carry up to 2,047 signatures and a JWE several hundred recipients. JsonConverter::decode() itself is unchanged: key sets loaded by JKUFactory or X5UFactory, and the claims an application decodes after verification, are not limited.

Workarounds

Until you upgrade, limit the size of the tokens you accept before giving them to the library. Either check strlen($token) (a few tens of kilobytes is ample for most tokens), or cap the size of request bodies and headers on the endpoints that receive tokens: client_max_body_size in nginx, LimitRequestBody and LimitRequestFieldSize in Apache, max_header_size in Caddy.

PHP itself has no protection against hash collisions in json_decode(). An application that JSON-decodes untrusted request bodies itself has the same exposure, whatever the library does, and the same size limits protect it.

References

  • A. Klink and J. Wälde, "Efficient Denial of Service Attacks on Web Application Platforms", 28C3, December 2011 (hash collisions in PHP arrays)
  • RFC 7515, section 7.2, and RFC 7516, section 7.2 (JWS and JWE JSON Serialization)

Credits

Reported by Anthropic's OSS Scanner (ANT-2026-M729NBUL).

Affected versions

References

GitHub · web-token/jwt-framework