From 0d596e99a99870375206eae9c9fdaaa71b728cf4 Mon Sep 17 00:00:00 2001 From: Shawzyy <72698415+shawzyy457@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:58:20 +0000 Subject: [PATCH] jetty: apply maxRequestSize to chunked request bodies fixes #4053 JettyContext.body() wrapped the stream in LimitedInputStream only when the declared length reached maxRequestSize, so a chunked body (length -1) was read with no limit. Netty and Undertow already reject it with 413. --- .../io/jooby/internal/jetty/JettyContext.java | 3 +- .../test/java/io/jooby/i4053/Issue4053.java | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/src/test/java/io/jooby/i4053/Issue4053.java diff --git a/modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java b/modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java index 8b29f15b55..297c805ca8 100644 --- a/modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java +++ b/modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java @@ -150,7 +150,8 @@ public Body body() { } InputStream in = Content.Source.asInputStream(request); long len = request.getLength(); - if (len >= maxRequestSize) { + // A chunked body reports -1, so it gets the limit too. + if (len < 0 || len >= maxRequestSize) { in = new LimitedInputStream(in, maxRequestSize); } return Body.of(this, in, len); diff --git a/tests/src/test/java/io/jooby/i4053/Issue4053.java b/tests/src/test/java/io/jooby/i4053/Issue4053.java new file mode 100644 index 0000000000..50f5d6dc44 --- /dev/null +++ b/tests/src/test/java/io/jooby/i4053/Issue4053.java @@ -0,0 +1,65 @@ +/* + * Jooby https://jooby.io + * Apache License Version 2.0 https://jooby.io/LICENSE.txt + * Copyright 2014 Edgar Espina + */ +package io.jooby.i4053; + +import static io.jooby.test.TestUtil._16kb; +import static io.jooby.test.TestUtil._19kb; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import io.jooby.ServerOptions; +import io.jooby.junit.ServerTest; +import io.jooby.junit.ServerTestRunner; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okio.BufferedSink; + +public class Issue4053 { + + @ServerTest + public void maxRequestSizeAppliesToChunkedBody(ServerTestRunner runner) { + runner + .options(new ServerOptions().setMaxRequestSize(ServerOptions._16KB)) + .define(app -> app.post("/4053", ctx -> ctx.body().value(""))) + .ready( + client -> { + client.post( + "/4053", + chunked(_19kb), + rsp -> { + assertEquals(413, rsp.code()); + }); + client.post( + "/4053", + chunked(_16kb), + rsp -> { + assertEquals(200, rsp.code()); + assertEquals(_16kb, rsp.body().string()); + }); + }); + } + + // OkHttp sends a body of unknown length with Transfer-Encoding: chunked. + private static RequestBody chunked(String value) { + return new RequestBody() { + @Override + public MediaType contentType() { + return MediaType.get("text/plain"); + } + + @Override + public long contentLength() { + return -1; + } + + @Override + public void writeTo(BufferedSink sink) throws IOException { + sink.writeUtf8(value); + } + }; + } +}