← All posts

Field notes / Security

JWT Decompression Attacks and Mitigations

TLDR

If your app decompresses user controlled JWT payloads without limits, a tiny token can expand into a huge buffer and starve memory or CPU. Edge limits might block delivery in one deployment. That is nice. It is not a fix. Add strict size caps and timeouts in code.

Background

A compression bomb is a small input that explodes in size when decompressed. Think few kilobytes in, hundreds of megabytes out. When applications accept compressed JWT payloads and call decompression with no limits, attackers can turn that into denial of service.

Even if an edge proxy or gateway blocks oversized headers or bodies, your app should still defend itself. Deployments change. Staging paths exist. Someone will hit an origin directly. Defense in depth means your code must stand on its own.

Quick proof of concept generator

Local testing only. Do not target systems you do not own.

# jwt_compression_bomb.py
import gzip
import base64
import json

def make_compressed_payload(repeat_count=100000):
    repetitive_data = "A" * repeat_count
    payload = {"data": repetitive_data, "iat": 1234567890}
    json_data = json.dumps(payload).encode()
    compressed = gzip.compress(json_data)
    b64_encoded = base64.b64encode(compressed).decode()
    print(f"Original size: {len(json_data)} bytes")
    print(f"Compressed size: {len(compressed)} bytes")
    print(f"Compression ratio: {len(json_data) / max(1, len(compressed)):.1f}x")
    return b64_encoded

if __name__ == "__main__":
    token_part = make_compressed_payload(100000)
    print("Base64 compressed token part:")
    print(token_part[:200] + "...")

Why edge limits are not enough

How to fix the application layer

1) Pre check compressed size

Base64 decode and reject above a small limit. Fail fast.

2) Enforce decompression limits and timeouts

Never read from gzip without a size cap. Add a timeout.

// Go example
lr := io.LimitReader(gzipReader, maxBytes+1)
n, err := io.Copy(outBuf, lr)
if n > maxBytes {
    return fmt.Errorf("token too large")
}

3) Fail safe with clear errors

{"error":"invalid token: compressed payload too large"}

Security checklist

Testing guidance

Final notes

Infrastructure that blocks giant headers is a nice bouncer at the door. Your code still needs locks on the safe.

Add limits. Ship tests. Sleep better.

← Back to Blog

← Back to the blog