GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix maxAllocation for brotli-encoded content in HttpContentDecompressor by skyguard1 · Pull Request #17037 · netty/netty

Fix maxAllocation for brotli-encoded content in HttpContentDecompressor #17037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 3 commits into netty:4.2 from skyguard1:fix_brotli_http_content_decompressor_max_allocation
Jul 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix maxAllocation for brotli-encoded content in HttpContentDecompressor #17037

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
36ebd41
Fix maxAllocation for brotli-encoded content in HttpContentDecompressor
skyguard1 Jul 2, 2026
e3726ba
Update by review
skyguard1 Jul 2, 2026
6a7ca97
Merge branch '4.2' into fix_brotli_http_content_decompressor_max_allo…
normanmaurer Jul 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

16 changes: 16 additions & 0 deletions 16 codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliDecoder.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public BrotliDecoder(int inputBufferSize, int outputBufferSize) {
this.outputBufferSize = ObjectUtil.checkPositive(outputBufferSize, "outputBufferSize");
}

/**
* Creates a new {@link BrotliDecoder} that use the {@code maxAllocation}
* semantics: the supplied value bounds the size of the emitted decompressed chunks.
* The input buffer size stays at the decoder's default.
*
* @param maxAllocation maximum size, in bytes, of each decompressed output
* buffer forwarded downstream; if {@code 0}, the
* decoder's default output cap is used.
*/
public static BrotliDecoder newDecoderWithMaxAllocation(int maxAllocation) {
ObjectUtil.checkPositiveOrZero(maxAllocation, "maxAllocation");
return maxAllocation > 0 ?
new BrotliDecoder(DEFAULT_INPUT_BUFFER_SIZE, maxAllocation) :
new BrotliDecoder();
}

private void forwardOutput(ChannelHandlerContext ctx) {
ByteBuffer nativeBuffer = decoder.pull(outputBufferSize);
// nativeBuffer actually wraps brotli's internal buffer so we need to copy its content
Expand Down
2 changes: 1 addition & 1 deletion 2 codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Excep
.channelId(channel.id())
.hasDisconnect(channel.metadata().hasDisconnect())
.config(channel.config())
.handlers(new BrotliDecoder(maxAllocation))
.handlers(BrotliDecoder.newDecoderWithMaxAllocation(maxAllocation))
.build();
}

Expand Down
73 changes: 73 additions & 0 deletions 73 codec-http/src/test/java/io/netty/handler/codec/http/HttpContentDecompressorTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
import io.netty.handler.flow.FlowControlHandler;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.test.DisabledForSlowLeakDetection;
import com.aayushatharva.brotli4j.encoder.BrotliOutputStream;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -198,6 +203,74 @@ public void testZipBomb(String encoding) {
assertEquals((long) chunkSize * numberOfChunks, incomingHandler.total);
}

@Test
public void testBrotliDecodingHonorsMaxAllocationAsOutputCap() throws Exception {
Assumptions.assumeTrue(Brotli.isAvailable(),
"brotli4j native library not available on this platform");

// 128KB of moderately compressible bytes so the decompressed size
// definitely exceeds BrotliDecoder's 64KB default output cap and any
// small maxAllocation we might set below.
byte[] payload = new byte[128 * 1024];
for (int i = 0; i < payload.length; i++) {
payload[i] = (byte) (i % 251);
}
ByteArrayOutputStream compressedOut = new ByteArrayOutputStream();
BrotliOutputStream brotliOs = new BrotliOutputStream(compressedOut);
brotliOs.write(payload);
brotliOs.close();
byte[] compressed = compressedOut.toByteArray();

// Deliberately use a tiny maxAllocation. Before the fix this configured
// BrotliDecoder.inputBufferSize = 64, effectively breaking the decoder
// for realistic payloads; after the fix it configures outputBufferSize.
EmbeddedChannel channel = new EmbeddedChannel(new HttpContentDecompressor(64));
try {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.BR);
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
assertTrue(channel.writeInbound(response));
assertTrue(channel.writeInbound(new DefaultHttpContent(Unpooled.wrappedBuffer(compressed))));
assertTrue(channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT));

// Drain and concatenate every decompressed HttpContent chunk.
byte[] decompressed = new byte[0];
int contentChunks = 0;
Object msg;
while ((msg = channel.readInbound()) != null) {
try {
if (msg instanceof HttpContent) {
ByteBuf buf = ((HttpContent) msg).content();
if (buf.readableBytes() > 0) {
contentChunks++;
int len = decompressed.length;
decompressed = Arrays.copyOf(decompressed, len + buf.readableBytes());
buf.readBytes(decompressed, len, buf.readableBytes());
}
}
} finally {
ReferenceCountUtil.release(msg);
}
}

assertThat(decompressed)
.as("decompressed bytes must match original payload")
.isEqualTo(payload);

// Regression signal: with maxAllocation=64 correctly routed to
// BrotliDecoder.outputBufferSize, a 128KB payload must be forwarded
// in many small chunks. Under the previous (buggy) wiring the same
// maxAllocation would be applied to inputBufferSize while
// outputBufferSize stayed at BrotliDecoder's 64KB default, so only
// a handful of large chunks would be emitted.
assertThat(contentChunks)
.as("expected many small chunks when maxAllocation=64 caps output size")
.isGreaterThan(100);
} finally {
channel.finishAndReleaseAll();
}
}

private static final class ZipBombIncomingHandler extends ChannelInboundHandlerAdapter {
final int memoryLimit;
long total;
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.