This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git
The following commit(s) were added to refs/heads/2.2.X by this push:
new 6784fd901 Set a maximum size for teh HTTP header
6784fd901 is described below
commit 6784fd901dafdecc07fcc7b56921423df972c498
Author: emmanuel lecharny <[email protected]>
AuthorDate: Fri Nov 13 05:39:00 2026 +0100
Set a maximum size for teh HTTP header
---
.../org/apache/mina/http/HttpClientDecoder.java | 48 ++++++++++++++++++++
.../org/apache/mina/http/HttpServerDecoder.java | 51 +++++++++++++++++++++-
.../java/org/apache/mina/http/api/HttpStatus.java | 5 +++
.../apache/mina/http/HttpServerDecoderTest.java | 34 +++++++++++++++
4 files changed, 137 insertions(+), 1 deletion(-)
diff --git
a/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
b/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
index 21b29af06..4950fe422 100644
--- a/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
+++ b/mina-http/src/main/java/org/apache/mina/http/HttpClientDecoder.java
@@ -43,6 +43,23 @@ import org.slf4j.LoggerFactory;
public class HttpClientDecoder implements ProtocolDecoder {
private static final Logger LOGGER =
LoggerFactory.getLogger(HttpClientCodec.class);
+ /**
+ * The default maximum size in bytes of an accumulated HTTP response head
+ * (status line plus headers). A hostile or broken server that keeps
+ * sending header bytes without ever terminating the head (CRLFCRLF)
+ * would otherwise make the decoder accumulate an ever-growing buffer,
+ * exhausting the heap. It can be changed globally with the
+ * <tt>org.apache.mina.http.maxResponseHeadSize</tt> system property, or
+ * per decoder with the {@link #HttpClientDecoder(int)} constructor.
+ * A value of <tt>0</tt> or less disables the limit (previous, unbounded
+ * behavior).
+ */
+ public static final int DEFAULT_MAX_RESPONSE_HEAD_SIZE =
Integer.getInteger(
+ "org.apache.mina.http.maxResponseHeadSize", 16 * 1024);
+
+ /** The maximum size in bytes of an accumulated HTTP response head */
+ private final int maxResponseHeadSize;
+
/** Key for decoder current state */
private static final String DECODER_STATE_ATT = "http.ds";
@@ -82,6 +99,26 @@ public class HttpClientDecoder implements ProtocolDecoder {
/** Regex to split cookie header following RFC6265 Section 5.4 */
public static final Pattern COOKIE_SEPARATOR_PATTERN =
Pattern.compile(";");
+ /**
+ * Creates a new HttpClientDecoder instance, limiting the accumulated HTTP
+ * response head to {@link #DEFAULT_MAX_RESPONSE_HEAD_SIZE} bytes.
+ */
+ public HttpClientDecoder() {
+ this(DEFAULT_MAX_RESPONSE_HEAD_SIZE);
+ }
+
+ /**
+ * Creates a new HttpClientDecoder instance
+ *
+ * @param maxResponseHeadSize the maximum size in bytes of an accumulated
+ * HTTP response head (status line plus headers); responses whose head
+ * exceeds this size fail with an {@link HttpException}. A value of
+ * <tt>0</tt> or less disables the limit.
+ */
+ public HttpClientDecoder(int maxResponseHeadSize) {
+ this.maxResponseHeadSize = maxResponseHeadSize;
+ }
+
/**
* {@inheritDoc}
*/
@@ -114,6 +151,17 @@ public class HttpClientDecoder implements ProtocolDecoder {
DefaultHttpResponse rp = parseHttpReponseHead(msg.buf());
if (rp == null) {
+ // Refuse to accumulate more than maxResponseHeadSize
bytes for a
+ // single response head: a server withholding the CRLFCRLF
+ // terminator must not be able to grow the partial-head
buffer
+ // without bound.
+ if ((maxResponseHeadSize > 0) && (msg.remaining() >
maxResponseHeadSize)) {
+ session.removeAttribute(PARTIAL_HEAD_ATT);
+ session.removeAttribute(DECODER_STATE_ATT);
+ throw new
HttpException(HttpStatus.CLIENT_ERROR_REQUEST_ENTITY_TOO_LARGE,
+ "HTTP response head exceeds " +
maxResponseHeadSize + " bytes");
+ }
+
// we copy the incoming BB because it's going to be
recycled by the inner IoProcessor for next reads
ByteBuffer partial = ByteBuffer.allocate(msg.remaining());
partial.put(msg.buf());
diff --git
a/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
b/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
index 3dbc04ef5..03f38199c 100644
--- a/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
+++ b/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
@@ -43,6 +43,24 @@ import org.slf4j.LoggerFactory;
public class HttpServerDecoder implements ProtocolDecoder {
private static final Logger LOGGER =
LoggerFactory.getLogger(HttpServerCodec.class);
+ /**
+ * The default maximum size in bytes of an accumulated HTTP request head
+ * (request line plus headers). A peer that keeps sending header bytes
+ * without ever terminating the head (CRLFCRLF) would otherwise make the
+ * decoder accumulate and re-parse an ever-growing buffer, exhausting the
+ * heap with a single unauthenticated connection. The 16 KB default is in
+ * line with mainstream HTTP servers; it can be changed globally with the
+ * <tt>org.apache.mina.http.maxRequestHeadSize</tt> system property, or
+ * per decoder with the {@link #HttpServerDecoder(int)} constructor.
+ * A value of <tt>0</tt> or less disables the limit (previous, unbounded
+ * behavior).
+ */
+ public static final int DEFAULT_MAX_REQUEST_HEAD_SIZE = Integer.getInteger(
+ "org.apache.mina.http.maxRequestHeadSize", 16 * 1024);
+
+ /** The maximum size in bytes of an accumulated HTTP request head */
+ private final int maxRequestHeadSize;
+
/** Key for decoder current state */
private static final String DECODER_STATE_ATT = "http.ds";
@@ -79,6 +97,26 @@ public class HttpServerDecoder implements ProtocolDecoder {
/** Regex to split cookie header following RFC6265 Section 5.4 */
public static final Pattern COOKIE_SEPARATOR_PATTERN =
Pattern.compile(";");
+ /**
+ * Creates a new HttpServerDecoder instance, limiting the accumulated HTTP
+ * request head to {@link #DEFAULT_MAX_REQUEST_HEAD_SIZE} bytes.
+ */
+ public HttpServerDecoder() {
+ this(DEFAULT_MAX_REQUEST_HEAD_SIZE);
+ }
+
+ /**
+ * Creates a new HttpServerDecoder instance
+ *
+ * @param maxRequestHeadSize the maximum size in bytes of an accumulated
+ * HTTP request head (request line plus headers); requests whose head
+ * exceeds this size fail with an {@link HttpException} (431). A value of
+ * <tt>0</tt> or less disables the limit.
+ */
+ public HttpServerDecoder(int maxRequestHeadSize) {
+ this.maxRequestHeadSize = maxRequestHeadSize;
+ }
+
/**
* {@inheritDoc}
*/
@@ -111,8 +149,19 @@ public class HttpServerDecoder implements ProtocolDecoder {
HttpRequestImpl httpRequest = parseHttpRequestHead(msg.buf());
session.removeAttribute(DECODER_STATE_ATT);
-
if (httpRequest == null) {
+ // Refuse to accumulate more than maxRequestHeadSize bytes
for a
+ // single request head: a peer withholding the CRLFCRLF
terminator
+ // must not be able to grow the partial-head buffer (and
the
+ // re-parse work on every read) without bound.
+ if ((maxRequestHeadSize > 0) && (msg.remaining() >
maxRequestHeadSize)) {
+ session.removeAttribute(PARTIAL_HEAD_ATT);
+ session.removeAttribute(DECODER_STATE_ATT);
+
+ throw new
HttpException(HttpStatus.CLIENT_ERROR_REQUEST_HEADER_FIELDS_TOO_LARGE,
+ "HTTP request head exceeds " +
maxRequestHeadSize + " bytes");
+ }
+
// we copy the incoming BB because it's going to be
recycled by the inner IoProcessor for next reads
ByteBuffer partial = ByteBuffer.allocate(msg.remaining());
partial.put(msg.buf());
diff --git a/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
b/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
index aee07c47e..9b499ce27 100644
--- a/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
+++ b/mina-http/src/main/java/org/apache/mina/http/api/HttpStatus.java
@@ -198,6 +198,11 @@ public enum HttpStatus {
*/
CLIENT_ERROR_UPGRADE_REQUIRED(426, "HTTP/1.1 426 Upgrade Required"),
+ /**
+ * 431 - Request Header Fields Too Large
+ */
+ CLIENT_ERROR_REQUEST_HEADER_FIELDS_TOO_LARGE(431, "HTTP/1.1 431 Request
Header Fields Too Large"),
+
// 5xx - Server Error
/**
* 500 - Internal Server Error
diff --git
a/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
b/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
index 27be47b2b..ba266c058 100644
--- a/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
+++ b/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
@@ -22,6 +22,7 @@ package org.apache.mina.http;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
@@ -317,6 +318,39 @@ public class HttpServerDecoderTest {
assertEquals("localhost", request.getHeader("host"));
assertTrue(out.getQueue().poll() instanceof HttpEndOfContent);
}
+
+ @Test
+ public void unterminatedRequestHeadIsRejectedOnceTooLarge() throws
Exception {
+ ProtocolDecoderQueue out = new ProtocolDecoderQueue();
+ IoSession localSession = new DummySession();
+ ProtocolDecoder localDecoder = new HttpServerDecoder(128);
+
+ // A request head that never contains the CRLFCRLF terminator: the
+ // decoder must refuse to accumulate it beyond the configured limit
+ // instead of growing (and re-parsing) it forever.
+ IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
+ buffer.putString("GET / HTTP/1.1\r\nHost: dummy\r\nX-Filler: ",
encoder);
+
+ for (int i = 0; i < 8; i++) {
+ buffer.putString("aaaaaaaaaaaaaaaa", encoder);
+ }
+
+ buffer.rewind();
+
+ try {
+ while (buffer.hasRemaining()) {
+ localDecoder.decode(localSession, buffer, out);
+ }
+
+ fail("The over-sized request head should have been rejected");
+ } catch (HttpException e) {
+ assertEquals(431, e.getStatusCode());
+ }
+
+ // The partial-head accumulation state must have been cleaned up
+ assertEquals(null, localSession.getAttribute(DECODER_STATE_ATT));
+ assertEquals(null, localSession.getAttribute("http.ph"));
+ }
@Test
public void dosOnRequestWithAdditionalData() throws Exception {