From 72711d8bcd3e6e4d277bb4adfee56c374fe89d8c Mon Sep 17 00:00:00 2001 From: Naveed Khan Date: Thu, 19 Nov 2026 14:34:56 +0530 Subject: [PATCH] reject non-ascii digits in content-length and chunk size Long#parseLong accepts any Unicode decimal digit, and with radix 16 also fullwidth Latin letters, so a connection that decodes message heads with a charset such as UTF-8 parses a Content-Length of U+0661 U+0660 (Arabic-Indic "10") as 10 and a fullwidth 'A' chunk size as 10. RFC 9112 restricts these to 1*DIGIT and 1*HEXDIG. Reject non-ASCII values before parsing in MessageSupport#getContentLength and both chunk decoders. --- .../hc/core5/http/impl/io/ChunkedInputStream.java | 3 ++- .../apache/hc/core5/http/impl/nio/ChunkDecoder.java | 3 ++- .../apache/hc/core5/http/message/MessageSupport.java | 3 ++- .../hc/core5/http/impl/io/TestChunkCoding.java | 11 +++++++++++ .../hc/core5/http/impl/nio/TestChunkDecoder.java | 12 ++++++++++++ .../hc/core5/http/message/TestMessageSupport.java | 9 +++++++++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java index 21ad612b5..d049b9057 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java @@ -41,6 +41,7 @@ import org.apache.hc.core5.http.message.MessageSupport; import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.CharArrayBuffer; +import org.apache.hc.core5.util.TextUtils; /** * Implements chunked transfer coding. The content is received in small chunks. @@ -270,7 +271,7 @@ private long getChunkSize() throws IOException { separator = lineBuffer.length(); } final String s = this.lineBuffer.substringTrimmed(0, separator); - if (MessageSupport.isSignedOrEmpty(s)) { + if (MessageSupport.isSignedOrEmpty(s) || !TextUtils.isAllASCII(s)) { throw new MalformedChunkCodingException("Bad chunk header: " + s); } try { diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkDecoder.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkDecoder.java index 935235cc7..abc405d91 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkDecoder.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkDecoder.java @@ -46,6 +46,7 @@ import org.apache.hc.core5.http.nio.SessionInputBuffer; import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.CharArrayBuffer; +import org.apache.hc.core5.util.TextUtils; /** * Implements chunked transfer decoding. The content is received in small chunks. @@ -129,7 +130,7 @@ private void readChunkHead() throws IOException { separator = this.lineBuf.length(); } final String s = this.lineBuf.substringTrimmed(0, separator); - if (MessageSupport.isSignedOrEmpty(s)) { + if (MessageSupport.isSignedOrEmpty(s) || !TextUtils.isAllASCII(s)) { throw new MalformedChunkCodingException("Bad chunk header: " + s); } try { diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/MessageSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/MessageSupport.java index 3d0779fc0..bbdf34a9c 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/MessageSupport.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/MessageSupport.java @@ -58,6 +58,7 @@ import org.apache.hc.core5.http.ProtocolException; import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.CharArrayBuffer; +import org.apache.hc.core5.util.TextUtils; import org.apache.hc.core5.util.Tokenizer; /** @@ -750,7 +751,7 @@ public static long getContentLength(final HttpMessage message) throws HttpExcept final Header contentLengthHeader = message.getFirstHeader(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader != null) { final String s = contentLengthHeader.getValue(); - if (isSignedOrEmpty(s)) { + if (isSignedOrEmpty(s) || !TextUtils.isAllASCII(s)) { throw new ProtocolException("Invalid content length: " + s); } try { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestChunkCoding.java b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestChunkCoding.java index f3d695e1b..cab4481fd 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestChunkCoding.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestChunkCoding.java @@ -261,6 +261,17 @@ void testCorruptChunkedInputStreamOverflowSize() throws IOException { } } + @Test + void testCorruptChunkedInputStreamNonAsciiDigitSize() throws IOException { + // U+FF21 FULLWIDTH LATIN CAPITAL LETTER A is accepted by Long#parseLong as hex digit 10 + final String s = "A\r\n0123456789\r\n0\r\n\r\n"; + final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder()); + final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); + try (ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream)) { + Assertions.assertThatThrownBy(in::read).isInstanceOf(MalformedChunkCodingException.class); + } + } + @Test void testChunkedInputStreamSurroundingWhitespaceSize() throws IOException { final String s = " 5 \r\n01234\r\n0\r\n\r\n"; diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/TestChunkDecoder.java b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/TestChunkDecoder.java index aef068020..1b4f22dc8 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/TestChunkDecoder.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/TestChunkDecoder.java @@ -216,6 +216,18 @@ void testChunkSizeOverflowRejected() { .isInstanceOf(MalformedChunkCodingException.class); } + @Test + void testChunkSizeNonAsciiDigitRejected() { + // U+0665 ARABIC-INDIC DIGIT FIVE is accepted by Long#parseLong as digit 5 + final ReadableByteChannel channel = new ReadableByteChannelMock( + new String[] {"٥\r\nhello\r\n0\r\n\r\n"}, StandardCharsets.UTF_8); + final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.UTF_8); + final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, new BasicHttpTransportMetrics()); + final ByteBuffer dst = ByteBuffer.allocate(1024); + Assertions.assertThatThrownBy(() -> decoder.read(dst)) + .isInstanceOf(MalformedChunkCodingException.class); + } + @Test void testChunkSizeSurroundingWhitespaceAccepted() throws Exception { decode(" 5 \r\nhello\r\n0\r\n\r\n"); diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestMessageSupport.java b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestMessageSupport.java index aa97a5043..da7db49c3 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestMessageSupport.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestMessageSupport.java @@ -573,6 +573,15 @@ void testMessageWithPlusSignedContentLength() { .isInstanceOf(ProtocolException.class); } + @Test + void testMessageWithNonAsciiDigitContentLength() { + // U+0661 U+0660 ARABIC-INDIC DIGITS ONE ZERO are accepted by Long#parseLong as 10 + final HttpMessage message = new BasicHttpResponse(200); + message.addHeader("Content-Length", "١٠"); + Assertions.assertThatThrownBy(() -> MessageSupport.getContentLength(message)) + .isInstanceOf(ProtocolException.class); + } + @Test void testMessageWithEmptyContentLength() { final HttpMessage message = new BasicHttpResponse(200);