GeezerVison is showing github.com — Avoid classloader leak via the shared TransferEncodingNotAllowedExcep… · netty/netty@2547186
File treeExpand file treeCollapse file tree Open diff view settings codec-base/src/main/java/io/netty/handler/codec main/java/io/netty/handler/codec/http test/java/io/netty/handler/codec/http
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-base/src/main/java/io/netty/handler/codec/CodecException.java +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,4 +48,12 @@ public CodecException(String message) { | 48 | 48 | public CodecException(Throwable cause) { | 49 | 49 | super(cause); | 50 | 50 | } | | 51 | + | | 52 | + /** | | 53 | + * Creates a new instance with the given suppression and stack trace settings, see | | 54 | + * {@link RuntimeException#RuntimeException(String, Throwable, boolean, boolean)}. | | 55 | + */ | | 56 | + protected CodecException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | | 57 | + super(message, cause, enableSuppression, writableStackTrace); | | 58 | + } | 51 | 59 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-base/src/main/java/io/netty/handler/codec/DecoderException.java +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,4 +48,12 @@ public DecoderException(String message) { | 48 | 48 | public DecoderException(Throwable cause) { | 49 | 49 | super(cause); | 50 | 50 | } | | 51 | + | | 52 | + /** | | 53 | + * Creates a new instance with the given suppression and stack trace settings, see | | 54 | + * {@link RuntimeException#RuntimeException(String, Throwable, boolean, boolean)}. | | 55 | + */ | | 56 | + protected DecoderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | | 57 | + super(message, cause, enableSuppression, writableStackTrace); | | 58 | + } | 51 | 59 | } |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +2-6Lines changed: 2 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,8 @@ 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"); | 181 | 177 |
| 182 | 178 | private final int maxChunkSize; | 183 | 179 | 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 +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,4 +29,15 @@ public final class TransferEncodingNotAllowedException extends DecoderException | 29 | 29 | public TransferEncodingNotAllowedException(String message) { | 30 | 30 | super(message); | 31 | 31 | } | | 32 | + | | 33 | + private TransferEncodingNotAllowedException(String message, boolean shared) { | | 34 | + // A shared instance must not populate a (native) backtrace: doing so would pin the Classloader of whatever | | 35 | + // thread happened to trigger its creation (see https://github.com/netty/netty/issues/17128). | | 36 | + super(message, null, false, false); | | 37 | + assert shared; | | 38 | + } | | 39 | + | | 40 | + static TransferEncodingNotAllowedException newStatic(String message) { | | 41 | + return new TransferEncodingNotAllowedException(message, true); | | 42 | + } | 32 | 43 | } |
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 | |
|