GeezerVison is showing github.com — Auto-port 4.1: Avoid classloader leak via the shared TransferEncoding… · netty/netty@cbc3d67
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/http test/java/io/netty/handler/codec/http
codec/src/main/java/io/netty/handler/codec
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +3-6Lines changed: 3 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | import io.netty.util.ByteProcessor; | 28 | 28 | import io.netty.util.internal.StringUtil; | 29 | 29 | import io.netty.util.internal.SystemPropertyUtil; | 30 | | -import io.netty.util.internal.ThrowableUtil; | 31 | 30 |
| 32 | 31 | import java.util.List; | 33 | 32 | import java.util.concurrent.atomic.AtomicBoolean; | @@ -173,11 +172,9 @@ public void run() { | 173 | 172 | } | 174 | 173 | }; | 175 | 174 | private static final TransferEncodingNotAllowedException TRANSFER_ENCODING_NOT_ALLOWED = | 176 | | - ThrowableUtil.unknownStackTrace( | 177 | | - new TransferEncodingNotAllowedException( | 178 | | - "The Transfer-Encoding header is only allowed in HTTP/1.1 or newer"), | 179 | | - HttpObjectDecoder.class, | 180 | | - "readHeaders(ByteBuf)"); | | 175 | + TransferEncodingNotAllowedException.newStatic( | | 176 | + "The Transfer-Encoding header is only allowed in HTTP/1.1 or newer", | | 177 | + HttpObjectDecoder.class, "readHeaders(ByteBuf)"); | 181 | 178 |
| 182 | 179 | private final int maxChunkSize; | 183 | 180 | private final boolean chunkedSupported; | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/TransferEncodingNotAllowedException.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | package io.netty.handler.codec.http; | 17 | 17 |
| 18 | 18 | import io.netty.handler.codec.DecoderException; | | 19 | +import io.netty.util.internal.PlatformDependent; | | 20 | +import io.netty.util.internal.SuppressJava6Requirement; | | 21 | +import io.netty.util.internal.ThrowableUtil; | 19 | 22 |
| 20 | 23 | /** | 21 | 24 | * Thrown by {@link HttpObjectDecoder} when an HTTP message uses a protocol version older than {@code HTTP/1.1} | @@ -29,4 +32,21 @@ public final class TransferEncodingNotAllowedException extends DecoderException | 29 | 32 | public TransferEncodingNotAllowedException(String message) { | 30 | 33 | super(message); | 31 | 34 | } | | 35 | + | | 36 | + @SuppressJava6Requirement(reason = "uses Java 7+ DecoderException.<init>(String, Throwable, boolean, boolean)" + | | 37 | + " but is guarded by version checks") | | 38 | + private TransferEncodingNotAllowedException(String message, boolean shared) { | | 39 | + // A shared instance must not populate a (native) backtrace: doing so would pin the Classloader of whatever | | 40 | + // thread happened to trigger its creation (see https://github.com/netty/netty/issues/17128). | | 41 | + super(message, null, false, false); | | 42 | + assert shared; | | 43 | + } | | 44 | + | | 45 | + static TransferEncodingNotAllowedException newStatic(String message, Class<?> clazz, String method) { | | 46 | + if (PlatformDependent.javaVersion() >= 7) { | | 47 | + return new TransferEncodingNotAllowedException(message, true); | | 48 | + } | | 49 | + // Java 6 has no way to disable the stack trace, so only replace the visible one. | | 50 | + return ThrowableUtil.unknownStackTrace(new TransferEncodingNotAllowedException(message), clazz, method); | | 51 | + } | 32 | 52 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | import static io.netty.handler.codec.http.HttpHeaderNames.HOST; | 37 | 37 | import static io.netty.handler.codec.http.HttpHeadersTestUtils.of; | 38 | 38 | import static org.assertj.core.api.Assertions.assertThat; | | 39 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; | 39 | 40 | import static org.junit.jupiter.api.Assertions.assertEquals; | 40 | 41 | import static org.junit.jupiter.api.Assertions.assertFalse; | 41 | 42 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; | @@ -863,6 +864,26 @@ public void testContentLengthHeaderAndChunkedHttp10() { | 863 | 864 | assertFalse(channel.finish()); | 864 | 865 | } | 865 | 866 |
| | 867 | + @Test | | 868 | + public void testTransferEncodingNotAllowedDoesNotFillInStackTrace() { | | 869 | + String requestStr = "POST / HTTP/1.0\r\n" + | | 870 | + "Host: example.com\r\n" + | | 871 | + "Transfer-Encoding: chunked\r\n\r\n" + | | 872 | + "0\r\n\r\n"; | | 873 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder()); | | 874 | + assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII))); | | 875 | + HttpRequest request = channel.readInbound(); | | 876 | + Throwable cause = request.decoderResult().cause(); | | 877 | + assertInstanceOf(TransferEncodingNotAllowedException.class, cause); | | 878 | + | | 879 | + // The exception is shared, so it must not populate a (native) backtrace: doing so would pin the Classloader | | 880 | + // of whatever thread happened to trigger its creation (see https://github.com/netty/netty/issues/17128). | | 881 | + StackTraceElement[] before = cause.getStackTrace(); | | 882 | + cause.fillInStackTrace(); | | 883 | + assertArrayEquals(before, cause.getStackTrace()); | | 884 | + assertFalse(channel.finish()); | | 885 | + } | | 886 | + | 866 | 887 | @Test | 867 | 888 | void mustRejectImproperlyTerminatedChunkExtensions() throws Exception { | 868 | 889 | // See full explanation: https://w4ke.info/2025/06/18/funky-chunks.html | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/CodecException.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package io.netty.handler.codec; | 17 | 17 |
| | 18 | +import io.netty.util.internal.SuppressJava6Requirement; | | 19 | + | 18 | 20 | /** | 19 | 21 | * An {@link Exception} which is thrown by a codec. | 20 | 22 | */ | @@ -48,4 +50,17 @@ public CodecException(String message) { | 48 | 50 | public CodecException(Throwable cause) { | 49 | 51 | super(cause); | 50 | 52 | } | | 53 | + | | 54 | + /** | | 55 | + * Creates a new instance with the given suppression and stack trace settings, see | | 56 | + * {@link RuntimeException#RuntimeException(String, Throwable, boolean, boolean)}. | | 57 | + * <p> | | 58 | + * This constructor requires Java 7+ and must only be invoked after checking | | 59 | + * {@code PlatformDependent.javaVersion() >= 7}. | | 60 | + */ | | 61 | + @SuppressJava6Requirement(reason = "uses Java 7+ RuntimeException.<init>(String, Throwable, boolean, boolean)" + | | 62 | + " but callers are guarded by version checks") | | 63 | + protected CodecException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | | 64 | + super(message, cause, enableSuppression, writableStackTrace); | | 65 | + } | 51 | 66 | } |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/DecoderException.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | */ | 16 | 16 | package io.netty.handler.codec; | 17 | 17 |
| | 18 | +import io.netty.util.internal.SuppressJava6Requirement; | | 19 | + | 18 | 20 | /** | 19 | 21 | * An {@link CodecException} which is thrown by a decoder. | 20 | 22 | */ | @@ -48,4 +50,17 @@ public DecoderException(String message) { | 48 | 50 | public DecoderException(Throwable cause) { | 49 | 51 | super(cause); | 50 | 52 | } | | 53 | + | | 54 | + /** | | 55 | + * Creates a new instance with the given suppression and stack trace settings, see | | 56 | + * {@link RuntimeException#RuntimeException(String, Throwable, boolean, boolean)}. | | 57 | + * <p> | | 58 | + * This constructor requires Java 7+ and must only be invoked after checking | | 59 | + * {@code PlatformDependent.javaVersion() >= 7}. | | 60 | + */ | | 61 | + @SuppressJava6Requirement(reason = "uses Java 7+ RuntimeException.<init>(String, Throwable, boolean, boolean)" + | | 62 | + " but callers are guarded by version checks") | | 63 | + protected DecoderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | | 64 | + super(message, cause, enableSuppression, writableStackTrace); | | 65 | + } | 51 | 66 | } |
|