2026-11-25 14 min read

Fourteen lines above the patch: Traefik's Brotli memory bug

One HTTP/2 connection, 250 requests, and not a single byte of the responses read. Traefik was running in a container with 1 GiB of memory, and before my 30-second hold was over, the kernel's OOM killer had ended it with exit status 137.

The cause was one line of Go. It sat fourteen lines above a patch that had fixed the exact same problem in a sibling function the week before, and the comment explaining the bug was already in the file. It was just under the wrong function.

The short version

Traefik can compress responses on the fly. For every response it compresses, it keeps a compressor in memory, and it only releases that compressor once the client has received the whole body. A client that asks for many compressed responses and then stops reading keeps all of those compressors alive.

That part is ordinary proxy behaviour. The bug was the price per response: Traefik built its Brotli compressor with the library defaults, about 12 MiB each. One HTTP/2 connection can carry 250 streams, so a single unauthenticated client could pin around 3 GiB of Go heap. In a container capped at 1 GiB, the kernel's OOM killer took Traefik down.

The advisory is GHSA-3rh5-223h-cpwr,[1] rated High. It is the Brotli counterpart of the zstd advisory,[2] which Traefik scored 8.7 under CVSS 4.0: same attack, same impact, a different encoder.

  1. 01
    Nov 2: zstd encoder bounded
    PR #14167 caps the zstd window at 128 KiB. Shipped in v3.7.18 on Nov 10.
  2. 02
    Nov 12: zstd advisory published
    GHSA-x5pf-cqh3-2gv8, about 18 MiB of encoder state per in-flight response.
  3. 03
    Nov 18: Brotli path reported
    Reported to Traefik through GitHub private vulnerability reporting.
  4. 04
    Nov 20: Brotli writer bounded
    PR #14324 sets Quality 5 and LGWin 18.
  5. 05
    Nov 23-24: v3.7.20, v3.8.0, advisory
    Fix released; GHSA-3rh5-223h-cpwr published the next day.
Timeline, 2026

Fourteen lines

On November 12, 2026, Traefik published an advisory for its Compress middleware.[2] The zstd encoder was being created with library defaults, which kept around 18 MiB of encoder state per in-flight response. A client that opened concurrent requests and read slowly could force Traefik to run out of memory. The patch in v3.7.18 bounded the zstd window to 128 KiB and brought each encoder under 1 MiB.[3]

Six days later I opened pkg/middlewares/compress/compress.go to read that commit. This is what the file looked like after it:[4]

pkg/middlewares/compress/compress.go (v3.7.19)
func (c *compress) newBrotliHandler(middlewareName string) (http.Handler, error) {
	cfg := Config{MinSize: c.minSize, MiddlewareName: middlewareName}
	if len(c.includes) > 0 {
		cfg.IncludedContentTypes = c.includes
	} else {
		cfg.ExcludedContentTypes = c.excludes
	}
 
	newBrotliWriter := func(rw http.ResponseWriter) (CompressionWriter, string, error) {
		return brotli.NewWriter(rw), brotliName, nil
	}
	return NewCompressionHandler(cfg, newBrotliWriter, c.next)
}
 
func (c *compress) newZstdHandler(middlewareName string) (http.Handler, error) {
	cfg := Config{MinSize: c.minSize, MiddlewareName: middlewareName}
	if len(c.includes) > 0 {
		cfg.IncludedContentTypes = c.includes
	} else {
		cfg.ExcludedContentTypes = c.excludes
	}
 
	newZstdWriter := func(rw http.ResponseWriter) (CompressionWriter, string, error) {
		// Each in-flight response holds one encoder until it is fully written.
		// The default options (8 MiB window) use about 18 MiB per encoder, the gzhttp options below use less than 1 MiB.
		writer, err := zstd.NewWriter(rw,
			zstd.WithEncoderLevel(zstd.SpeedFastest),
			zstd.WithEncoderConcurrency(1),
			zstd.WithLowerEncoderMem(true),
			zstd.WithWindowSize(128<<10),
		)

The zstd writer gets four options and a comment that describes this bug almost word for word: each in-flight response holds one encoder until it is fully written. Fourteen lines up, newBrotliHandler calls brotli.NewWriter(rw) with nothing.

I reported it to Traefik on November 18. The fix shipped in v3.7.20 and v3.8.0 on November 23, and the advisory went out the next day as GHSA-3rh5-223h-cpwr.[1] The rest of this post is about why that one unconfigured call held megabytes per stream, how HTTP/2 flow control locks it open, and the difference between Go heap and Linux RSS that nearly fooled my container test.

Why the encoder stays pinned until the client reads

Traefik's Compress middleware enables three encodings by default: gzip, br, and zstd. A plain compress: {} on a router is enough for any unauthenticated client sending Accept-Encoding: br to land in newBrotliHandler.

The lifecycle inside CompressionHandler (pkg/middlewares/compress/compression_handler.go) looks harmless at a glance: ServeHTTP pulls a writer from a sync.Pool (or creates one), wraps the http.ResponseWriter, and calls c.next.ServeHTTP. The problem is when the writer goes back to the pool:

compressionWriter, err := c.getCompressionWriter(rw)
if err != nil {
	// ...
	return
}
defer c.putCompressionWriter(compressionWriter)
 
responseWriter := &responseWriter{
	rw:                rw,
	compressionWriter: compressionWriter,
	// ...
}
defer responseWriter.close()
 
c.next.ServeHTTP(responseWriter, r)

Both close() and putCompressionWriter() are deferred until next.ServeHTTP returns. While the compressed response is still being written to the socket, that encoder sits live on the Go heap, pinned by the active handler goroutine.

  1. 01
    Client sends Accept-Encoding: br
    Default compress: {} routes the request into newBrotliHandler.
  2. 02
    Traefik allocates a brotli.Writer
    CompressionHandler enters next.ServeHTTP; defer putCompressionWriter cannot run yet.
  3. 03
    Client announces HTTP/2 window 0
    Once buffered output exceeds minSize (1024 B), ServeHTTP blocks trying to write. The writer stays alive.
How one client pins 250 encoders in memory

What brotli.NewWriter actually allocates

Traefik links github.com/andybalholm/brotli (v1.2.7 in v3.7.19). Calling brotli.NewWriter(rw) passes DefaultCompression (Quality: 6) and leaves LGWin at 0, which the encoder resolves to defaultWindow = 22, a 4 MiB sliding window.

Two allocations inside the library drive the footprint:

  • The ring buffer (ringbuffer.go, quality.go). computeRbBits sets the ring buffer size to 1 + max(lgwin, lgblock). With lgwin = 22 and lgblock = 16, that is 2^23 bytes (8 MiB) plus a 64 KiB tail buffer. The slice allocates lazily as bytes flow through (ringBufferInitBuffer), so the writer grows as the response gets larger.
  • The match hasher (quality.go, h5.go). At Quality: 6 with no size hint, chooseHasher picks h5 with bucket_bits = 14 (16,384 buckets) and block_bits = 5 (32 slots per bucket). The buckets slice alone is 16,384 * 32 * 4 bytes, or 2 MiB, before counting command and temporary storage buffers.

Measuring isolated brotli.Writer instances in Go after writing text without closing them shows how the footprint scales with response size (and how the patch bounds it):

Input size | Default (q6, lgwin 22)     | Patched (q5, lgwin 18)
-----------+----------------------------+----------------------------
64 KiB     | 10.87 MiB heap (20,646 B)  |  2.37 MiB heap (20,782 B)
1 MiB      | 12.41 MiB heap (306,243 B) |  4.03 MiB heap (329,271 B)
8 MiB      | 39.16 MiB heap (2,409 KB)  |  4.03 MiB heap (2,631 KB)

Notice the 8 MiB row: at default options, a single stalled writer climbs to nearly 40 MiB of Go heap. The fix PR describes the same shape: "more than 12 MiB, growing with the response size".[5]

Why my first HTTP/1.1 test missed it

My first reproducer sent 1 MiB responses over HTTP/1.1 with a client that stopped calling recv(). Nothing stayed pinned.

That negative result makes sense once you trace the buffers. A 1 MiB text body compresses down to roughly 300 KiB. On loopback, the kernel socket send and receive buffers swallowed all 300 KiB immediately, ServeHTTP returned, and defer putCompressionWriter put the writer back into sync.Pool. To stall HTTP/1.1 without touching socket tuning, the compressed output has to overflow the kernel buffers (an 8 MiB response worked: 100 stalled HTTP/1.1 connections held 1,284 MiB of Go heap and 811 MiB of RSS).

HTTP/2 skips kernel buffer guesswork completely. Over a single TLS connection, the client sends SETTINGS_INITIAL_WINDOW_SIZE = 0, opens 250 concurrent streams (maxConcurrentStreams defaults to 250 in Traefik's entrypoint config), requests a 1 MiB compressible asset with Accept-Encoding: br, and never sends WINDOW_UPDATE. Once Traefik buffers past minSize (1024 bytes by default) and writes the first compressed frame, HTTP/2 flow control stalls every stream inside ServeHTTP.

Nothing times those streams out by default either. RespondingTimeouts.SetDefaults sets ReadTimeout (60s) and IdleTimeout (180s), but leaves WriteTimeout at 0 (no timeout). In my loopback run against the official traefik_v3.7.19 binary, all 250 streams stayed alive past 200 seconds:

Version: 3.7.19 (1 MiB text response, 1 HTTP/2 connection, pprof after 3 GCs)
gzip 100 streams held  12s | heap    7 ->  129 MiB ( +1.21 MiB each) | RSS  93 ->  169 MiB
zstd 100 streams held  12s | heap    7 ->  105 MiB ( +0.98 MiB each) | RSS  92 ->  180 MiB
br   100 streams held  12s | heap    7 -> 1285 MiB (+12.78 MiB each) | RSS  94 ->  581 MiB
br   250 streams held 200s | heap    7 -> 3118 MiB (+12.44 MiB each) | RSS  91 -> 1179 MiB
gzip1.21 MiB
zstd0.98 MiB
br12.78 MiB
Go heap added per stalled HTTP/2 stream, Traefik v3.7.19, 1 MiB response

gzip and zstd sit around 1 MiB per stalled stream, right where v3.7.18 left them. br costs ten to thirteen times that, and on one connection it drives Go heap above 3 GiB.

Play with the numbers yourself. Pick an encoding, then drag the stream count down to see how few stalled streams it takes:

how much memory can one client pin?
Go heap pinned3.1 GiB
Estimate from measured per-stream heap at a 1 MiB response. Heap is not RSS; see the 64 KiB section.
Back-of-envelope: stalled streams × encoder heap per stream

Heap is not RSS: the 64 KiB surprise

When you test memory exhaustion in Go, HeapAlloc from pprof can make a bug look more lethal than the Linux kernel actually sees.

I ran the 250-stream HTTP/2 reproducer inside a Podman container capped at 1 GiB (--memory 1g --memory-swap 1g). This is the 1 MiB run, verbatim:

reproduce.sh, v3.7.19, memory.max = 1 GiB
Version:      3.7.19== memory.max of this cgroup: 1073741824 bytesgzip 250 streams on 1 connection held 30s | heap    7 ->  311 MiB ( +1.21 MiB each) | RSS   87 ->  275 MiBzstd 250 streams on 1 connection held 30s | heap    7 ->  251 MiB ( +0.98 MiB each) | RSS   87 ->  309 MiBbr   250 streams on 1 connection: Traefik no longer answers (URLError)/w/reproduce.sh: line 20:   545 Killed                  "$@" "$T" --configFile="$LAB/static.yml" > "$LAB/traefik.out" 2>&1     Traefik process is gone (exit status 137)oom_kill 1
Captured output of the container run, replayed line by line

Then I repeated it with a 64 KiB response. Side by side:

Container memory.max = 1073741824 bytes (1 GiB), 250 streams, 30s hold:
 
1 MiB response:
  gzip | heap 311 MiB | RSS 275 MiB | survived
  zstd | heap 251 MiB | RSS 309 MiB | survived
  br   | killed by kernel (exit 137, oom_kill 1)
 
64 KiB response:
  gzip | heap 310 MiB | RSS 276 MiB | survived
  zstd | heap 251 MiB | RSS 304 MiB | survived
  br   | heap 2760 MiB | RSS 696 MiB | survived (oom_kill 0)

Even though the 64 KiB run reported 2,760 MiB on the Go heap, Linux VmRSS only reached 696 MiB, so the cgroup OOM killer never fired. Because the Go runtime reserves large backing slices without faulting every page until bytes actually touch them, a small 64 KiB response leaves most of that 8 MiB ring buffer unbacked in physical RAM. One connection against a 64 KiB asset did not kill a 1 GiB container; one connection against a 1 MiB asset did.

What v3.7.20 and v3.8.0 changed

In PR #14324,[5] Traefik bounded newBrotliHandler with explicit WriterOptions. The whole fix is one line:

PR #14324
 newBrotliWriter := func(rw http.ResponseWriter) (CompressionWriter, string, error) {
-	return brotli.NewWriter(rw), brotliName, nil
+	return brotli.NewWriterOptions(rw, brotli.WriterOptions{Quality: 5, LGWin: 18}), brotliName, nil
 }

Two parameters changed:

  • LGWin: 18 shrinks the sliding window from 4 MiB (2^22) to 256 KiB (2^18), which drops the ring buffer from 8 MiB to 512 KiB (plus tail).
  • Quality: 5 drops h5's block_bits from 5 to 4, halving the match bucket table from 2 MiB to 1 MiB.

Here is the same attack against both release binaries, recorded on my VPS. Each Traefik gets 1 GiB of address space above its idle size, and the client opens 100 Brotli streams on one HTTP/2 connection and reads nothing:

Real run, 31 seconds: v3.7.19 dies with fatal error: out of memory, v3.7.20 holds the same 100 streams

The vulnerable binary dies trying to allocate a 12582912-byte block, 12 MiB on the dot. Every Go out-of-memory crash I recorded failed on that same allocation size. The patched binary levels off around 1.8 GiB of address space and 425 MiB of RSS. That is still 100 streams' worth of memory, which is why the writeTimeout advice below still matters after you upgrade.

In the isolated writer benchmark above, the patched writer stays at 4.03 MiB for both 1 MiB and 8 MiB of input instead of climbing to 39.16 MiB, at a cost of 7.5% to 9.2% larger compressed output. The new regression test TestBrotliWindowSize checks the WBITS header bits (RFC 7932) directly on compressed output so future refactors cannot silently fall back to LGWin: 22.

What to check on your own Traefik instances

  • Upgrade to v3.7.20 or v3.8.0. If you run an EOL v3.0 to v3.6 line, there is no backport coming.
  • If you cannot upgrade right away, remove br from encodings. Leaving compress: {} empty turns br on automatically. Setting encodings: ["gzip", "zstd"] (on v3.7.18+, where zstd is already bounded) closes this path without disabling compression.
  • Do not leave writeTimeout at 0. Even after the patch, 250 stalled Brotli streams at 4 MiB each still add up to about 1 GiB of Go heap on a single HTTP/2 connection. Setting entryPoints.<name>.transport.respondingTimeouts.writeTimeout (and adding inFlightReq on routes serving large dynamic responses) puts a ceiling on how long one slow client can hold backend and middleware state.

There is also a simple habit in this bug for anyone reading security advisories: when a patch hardens one implementation, read its siblings in the same file. Here the fixed function and the vulnerable one were fourteen lines apart.

references

  1. [1]

    Traefik, "Unauthenticated memory exhaustion: the Compress middleware's Brotli writer holds megabytes per response in flight, unbounded (the Brotli counterpart of GHSA-x5pf-cqh3-2gv8)", GHSA-3rh5-223h-cpwr, severity High, 24 November 2026.

  2. [2]

    Traefik, "Unauthenticated memory-exhaustion DoS: the Compress middleware pins ~18 MiB of zstd encoder state per in-flight response", GHSA-x5pf-cqh3-2gv8, 12 November 2026.

  3. [3]

    Julien Salleyron, "Limit zstd encoder memory in the Compress middleware", traefik/traefik PR #14167, 2 November 2026.

  4. [4]

    Traefik v3.7.19, pkg/middlewares/compress/compress.go, newBrotliHandler.

  5. [5]

    Romain Tribotte, "Limit Brotli writer memory in the Compress middleware", traefik/traefik PR #14324, 20 November 2026.

$