Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading