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 HttpContentDecompress… · netty/netty@0332676

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src/main/java/io/netty/handler/codec/compression
    • BrotliDecoder.java
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpContentDecompressor.java
    • test/java/io/netty/handler/codec/http
      • HttpContentDecompressorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliDecoder.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliDecoder.java
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ public BrotliDecoder(int inputBufferSize, int outputBufferSize) {
8080
this.outputBufferSize = ObjectUtil.checkPositive(outputBufferSize, "outputBufferSize");
8181
}
8282

83+
/**
84+
* Creates a new {@link BrotliDecoder} that use the {@code maxAllocation}
85+
* semantics: the supplied value bounds the size of the emitted decompressed chunks.
86+
* The input buffer size stays at the decoder's default.
87+
*
88+
* @param maxAllocation maximum size, in bytes, of each decompressed output
89+
* buffer forwarded downstream; if {@code 0}, the
90+
* decoder's default output cap is used.
91+
*/
92+
public static BrotliDecoder newDecoderWithMaxAllocation(int maxAllocation) {
93+
ObjectUtil.checkPositiveOrZero(maxAllocation, "maxAllocation");
94+
return maxAllocation > 0 ?
95+
new BrotliDecoder(DEFAULT_INPUT_BUFFER_SIZE, maxAllocation) :
96+
new BrotliDecoder();
97+
}
98+
8399
private void forwardOutput(ChannelHandlerContext ctx) {
84100
ByteBuffer nativeBuffer = decoder.pull(outputBufferSize);
85101
// nativeBuffer actually wraps brotli's internal buffer so we need to copy its content
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecompressor.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Excep
117117
.channelId(channel.id())
118118
.hasDisconnect(channel.metadata().hasDisconnect())
119119
.config(channel.config())
120-
.handlers(new BrotliDecoder(maxAllocation))
120+
.handlers(BrotliDecoder.newDecoderWithMaxAllocation(maxAllocation))
121121
.build();
122122
}
123123

Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpContentDecompressorTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpContentDecompressorTest.java
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@
2727
import io.netty.handler.flow.FlowControlHandler;
2828
import io.netty.util.ReferenceCountUtil;
2929
import io.netty.util.test.DisabledForSlowLeakDetection;
30+
import com.aayushatharva.brotli4j.encoder.BrotliOutputStream;
31+
import org.junit.jupiter.api.Assumptions;
3032
import org.junit.jupiter.api.Test;
3133
import org.junit.jupiter.params.ParameterizedTest;
3234
import org.junit.jupiter.params.provider.MethodSource;
3335

36+
import java.io.ByteArrayOutputStream;
3437
import java.util.ArrayList;
38+
import java.util.Arrays;
3539
import java.util.List;
3640
import java.util.concurrent.atomic.AtomicInteger;
3741

42+
import static org.assertj.core.api.Assertions.assertThat;
3843
import static org.junit.jupiter.api.Assertions.assertEquals;
3944
import static org.junit.jupiter.api.Assertions.assertFalse;
4045
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -198,6 +203,74 @@ public void testZipBomb(String encoding) {
198203
assertEquals((long) chunkSize * numberOfChunks, incomingHandler.total);
199204
}
200205

206+
@Test
207+
public void testBrotliDecodingHonorsMaxAllocationAsOutputCap() throws Exception {
208+
Assumptions.assumeTrue(Brotli.isAvailable(),
209+
"brotli4j native library not available on this platform");
210+
211+
// 128KB of moderately compressible bytes so the decompressed size
212+
// definitely exceeds BrotliDecoder's 64KB default output cap and any
213+
// small maxAllocation we might set below.
214+
byte[] payload = new byte[128 * 1024];
215+
for (int i = 0; i < payload.length; i++) {
216+
payload[i] = (byte) (i % 251);
217+
}
218+
ByteArrayOutputStream compressedOut = new ByteArrayOutputStream();
219+
BrotliOutputStream brotliOs = new BrotliOutputStream(compressedOut);
220+
brotliOs.write(payload);
221+
brotliOs.close();
222+
byte[] compressed = compressedOut.toByteArray();
223+
224+
// Deliberately use a tiny maxAllocation. Before the fix this configured
225+
// BrotliDecoder.inputBufferSize = 64, effectively breaking the decoder
226+
// for realistic payloads; after the fix it configures outputBufferSize.
227+
EmbeddedChannel channel = new EmbeddedChannel(new HttpContentDecompressor(64));
228+
try {
229+
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
230+
response.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.BR);
231+
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
232+
assertTrue(channel.writeInbound(response));
233+
assertTrue(channel.writeInbound(new DefaultHttpContent(Unpooled.wrappedBuffer(compressed))));
234+
assertTrue(channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT));
235+
236+
// Drain and concatenate every decompressed HttpContent chunk.
237+
byte[] decompressed = new byte[0];
238+
int contentChunks = 0;
239+
Object msg;
240+
while ((msg = channel.readInbound()) != null) {
241+
try {
242+
if (msg instanceof HttpContent) {
243+
ByteBuf buf = ((HttpContent) msg).content();
244+
if (buf.readableBytes() > 0) {
245+
contentChunks++;
246+
int len = decompressed.length;
247+
decompressed = Arrays.copyOf(decompressed, len + buf.readableBytes());
248+
buf.readBytes(decompressed, len, buf.readableBytes());
249+
}
250+
}
251+
} finally {
252+
ReferenceCountUtil.release(msg);
253+
}
254+
}
255+
256+
assertThat(decompressed)
257+
.as("decompressed bytes must match original payload")
258+
.isEqualTo(payload);
259+
260+
// Regression signal: with maxAllocation=64 correctly routed to
261+
// BrotliDecoder.outputBufferSize, a 128KB payload must be forwarded
262+
// in many small chunks. Under the previous (buggy) wiring the same
263+
// maxAllocation would be applied to inputBufferSize while
264+
// outputBufferSize stayed at BrotliDecoder's 64KB default, so only
265+
// a handful of large chunks would be emitted.
266+
assertThat(contentChunks)
267+
.as("expected many small chunks when maxAllocation=64 caps output size")
268+
.isGreaterThan(100);
269+
} finally {
270+
channel.finishAndReleaseAll();
271+
}
272+
}
273+
201274
private static final class ZipBombIncomingHandler extends ChannelInboundHandlerAdapter {
202275
final int memoryLimit;
203276
long total;

0 commit comments

Comments
0 (0)
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.