Repository navigation
fix(http): correct chunked request body size counting #7074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bladehan1
wants to merge
4
commits into
tronprotocol:release_v4.8.3
Choose a base branch
from
bladehan1:fix/http-chunked-body-size-limit
base: release_v4.8.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f3c6b43
fix(api): correct chunked request body size counting
bladehan1 568e264
chore(http): merge release_v4.8.3 updates
bladehan1 6b95269
test(http): cover request size counter boundaries
bladehan1 9be048a
refactor(http): simplify request size handler
bladehan1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
framework/src/main/java/org/tron/common/application/RequestSizeLimitHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package org.tron.common.application; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.eclipse.jetty.http.BadMessageException; | ||
| import org.eclipse.jetty.server.HttpInput; | ||
| import org.eclipse.jetty.server.Request; | ||
| import org.eclipse.jetty.server.handler.HandlerWrapper; | ||
|
|
||
| /** | ||
| * Enforces request-body limits using Jetty's received-byte counter. | ||
| * | ||
| * <p>This request-only handler installs its own input interceptor to avoid counting the same | ||
| * unread content repeatedly. It does not install a response-limiting output interceptor. | ||
| */ | ||
| public class RequestSizeLimitHandler extends HandlerWrapper { | ||
|
|
||
| private final long requestLimit; | ||
|
|
||
| public RequestSizeLimitHandler(long requestLimit) { | ||
| this.requestLimit = requestLimit; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(String target, Request baseRequest, HttpServletRequest request, | ||
| HttpServletResponse response) throws IOException, ServletException { | ||
| if (requestLimit >= 0) { | ||
| long contentLength = baseRequest.getContentLengthLong(); | ||
| checkRequestLimit(contentLength); | ||
|
|
||
| if (contentLength < 0) { | ||
| HttpInput input = baseRequest.getHttpInput(); | ||
| input.addInterceptor(content -> { | ||
| if (content == null) { | ||
| return null; | ||
| } | ||
| // addContent updates this counter before interception, under HttpInput's input lock. | ||
| checkRequestLimit(input.getContentReceived()); | ||
| return content; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| super.handle(target, baseRequest, request, response); | ||
| } | ||
|
|
||
| private void checkRequestLimit(long size) { | ||
| if (requestLimit >= 0 && size > requestLimit) { | ||
| throw new BadMessageException(413, | ||
| "Request body is too large: " + size + ">" + requestLimit); | ||
| } | ||
| } | ||
| } | ||
283 changes: 283 additions & 0 deletions
283
framework/src/test/java/org/tron/common/jetty/RequestSizeLimitHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| package org.tron.common.jetty; | ||
|
|
||
| import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.Socket; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.eclipse.jetty.http.BadMessageException; | ||
| import org.eclipse.jetty.server.HttpInput; | ||
| import org.eclipse.jetty.server.Request; | ||
| import org.eclipse.jetty.server.Server; | ||
| import org.eclipse.jetty.server.ServerConnector; | ||
| import org.eclipse.jetty.server.handler.AbstractHandler; | ||
| import org.eclipse.jetty.server.handler.HandlerWrapper; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.tron.common.application.RequestSizeLimitHandler; | ||
| import org.tron.json.JSONObject; | ||
|
|
||
| public class RequestSizeLimitHandlerTest { | ||
|
|
||
| private static final int LIMIT = 64 * 1024; | ||
| private final Map<String, HttpInput> inputs = new ConcurrentHashMap<>(); | ||
| private final Map<String, List<Long>> arrivals = new ConcurrentHashMap<>(); | ||
| private Server server; | ||
| private int port; | ||
| private volatile CountDownLatch firstReads; | ||
|
|
||
| @Before | ||
| public void setup() throws Exception { | ||
| server = new Server(); | ||
| ServerConnector connector = new ServerConnector(server); | ||
| connector.setHost("127.0.0.1"); | ||
| connector.setPort(0); | ||
| server.addConnector(connector); | ||
| RequestSizeLimitHandler limitHandler = new RequestSizeLimitHandler(LIMIT); | ||
| limitHandler.setHandler(new AbstractHandler() { | ||
| @Override | ||
| public void handle(String target, Request baseRequest, HttpServletRequest request, | ||
| HttpServletResponse response) throws IOException { | ||
| HttpInput input = baseRequest.getHttpInput(); | ||
| inputs.put(target, input); | ||
| int count = 0; | ||
| int error = 0; | ||
| long firstReceived = 0; | ||
| try { | ||
| while (input.read() != -1) { | ||
| count++; | ||
| int checkpoint = firstReads == null ? 1 | ||
| : 13 + Integer.parseInt(target.substring("/concurrent".length())); | ||
| if (count == checkpoint) { | ||
| firstReceived = input.getContentReceived(); | ||
| if (firstReads != null) { | ||
| firstReads.countDown(); | ||
| } | ||
| } | ||
| } | ||
| } catch (BadMessageException e) { | ||
| error = e.getCode(); | ||
| } | ||
| byte[] body = ("{\"bytes\":" + count + ",\"received\":" + input.getContentReceived() | ||
| + ",\"firstReceived\":" + firstReceived + ",\"error\":" + error + "}") | ||
| .getBytes(StandardCharsets.UTF_8); | ||
| // Diagnostic endpoint: assertions inspect the caught exception code and read count. | ||
| response.setStatus(200); | ||
| response.setContentType("application/json"); | ||
| response.setContentLength(body.length); | ||
| response.getOutputStream().write(body); | ||
| baseRequest.setHandled(true); | ||
| } | ||
| }); | ||
| HandlerWrapper observer = new HandlerWrapper() { | ||
| @Override | ||
| public void handle(String target, Request baseRequest, HttpServletRequest request, | ||
| HttpServletResponse response) throws IOException, ServletException { | ||
| HttpInput input = baseRequest.getHttpInput(); | ||
| List<Long> counts = Collections.synchronizedList(new ArrayList<>()); | ||
| arrivals.put(target, counts); | ||
| input.addInterceptor(content -> { | ||
| if (content != null && content.hasContent()) { | ||
| long received = input.getContentReceived(); | ||
| if (counts.isEmpty() || counts.get(counts.size() - 1) != received) { | ||
| counts.add(received); | ||
| } | ||
| } | ||
| return content; | ||
| }); | ||
| super.handle(target, baseRequest, request, response); | ||
| } | ||
| }; | ||
| observer.setHandler(limitHandler); | ||
| server.setHandler(observer); | ||
| server.start(); | ||
| port = connector.getLocalPort(); | ||
| } | ||
|
|
||
| @After | ||
| public void teardown() throws Exception { | ||
| server.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOneByteReadsAcceptExactLimit() throws Exception { | ||
| try (Socket socket = connect()) { | ||
| writeHeaders(socket, "/exact", true, LIMIT); | ||
| writeBody(socket, true, LIMIT); | ||
| JSONObject result = readResponse(socket); | ||
| Assert.assertEquals(0, result.getIntValue("error")); | ||
| Assert.assertEquals(LIMIT, result.getIntValue("bytes")); | ||
| Assert.assertEquals(LIMIT, result.getLongValue("received")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testOneByteReadsRejectBeforeReturningBytesBeyondLimit() throws Exception { | ||
| try (Socket socket = connect()) { | ||
| writeHeaders(socket, "/over", true, LIMIT + 4096); | ||
| writeBody(socket, true, LIMIT + 4096); | ||
| JSONObject result = readResponse(socket); | ||
| Assert.assertEquals(413, result.getIntValue("error")); | ||
| Assert.assertTrue(result.toJSONString(), result.getIntValue("bytes") <= LIMIT); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testConcurrentRequestsHaveIndependentCumulativeCounters() throws Exception { | ||
| int requests = 4; | ||
| firstReads = new CountDownLatch(requests); | ||
| ExecutorService executor = Executors.newFixedThreadPool(requests, | ||
| new ThreadFactoryBuilder().setNameFormat("request-size-counter-client-%d").build()); | ||
| try { | ||
| List<Future<JSONObject>> results = new ArrayList<>(); | ||
| for (int i = 0; i < requests; i++) { | ||
| final int index = i; | ||
| results.add(executor.submit(() -> { | ||
| boolean chunked = index % 2 == 0; | ||
| int total = 1024 + index * 257; | ||
| int first = 13 + index; | ||
| try (Socket socket = connect()) { | ||
| writeHeaders(socket, "/concurrent" + index, chunked, total); | ||
| writePart(socket, chunked, first); | ||
| // All four bodies are partially read before any client sends its remainder. | ||
| Assert.assertTrue(firstReads.await(10, TimeUnit.SECONDS)); | ||
| writePart(socket, chunked, 31); | ||
| writePart(socket, chunked, total - first - 31); | ||
| finishBody(socket, chunked); | ||
| return readResponse(socket); | ||
| } | ||
| })); | ||
| } | ||
| for (int i = 0; i < requests; i++) { | ||
| JSONObject result = results.get(i).get(15, TimeUnit.SECONDS); | ||
| int total = 1024 + i * 257; | ||
| Assert.assertEquals(0, result.getIntValue("error")); | ||
| Assert.assertEquals(total, result.getIntValue("bytes")); | ||
| Assert.assertEquals(total, result.getLongValue("received")); | ||
| Assert.assertEquals(13 + i, result.getLongValue("firstReceived")); | ||
| List<Long> counts = arrivals.get("/concurrent" + i); | ||
| Assert.assertTrue(counts.toString(), counts.contains((long) (13 + i))); | ||
| Assert.assertEquals(total, counts.get(counts.size() - 1).longValue()); | ||
| for (int j = 1; j < counts.size(); j++) { | ||
| Assert.assertTrue(counts.toString(), counts.get(j) > counts.get(j - 1)); | ||
| } | ||
| for (int j = 0; j < i; j++) { | ||
| Assert.assertNotSame(inputs.get("/concurrent" + i), inputs.get("/concurrent" + j)); | ||
| } | ||
| } | ||
| } finally { | ||
| executor.shutdown(); | ||
| if (!executor.awaitTermination(10, TimeUnit.SECONDS)) { | ||
| executor.shutdownNow(); | ||
| Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testKeepAliveRecyclesCounterBetweenFramings() throws Exception { | ||
| try (Socket socket = connect()) { | ||
| for (int i = 0; i < 4; i++) { | ||
| boolean chunked = i % 2 == 0; | ||
| int total = 1024 - i * 257; | ||
| writeHeaders(socket, "/reuse" + i, chunked, total); | ||
| writeBody(socket, chunked, total); | ||
| JSONObject result = readResponse(socket); | ||
| Assert.assertEquals(0, result.getIntValue("error")); | ||
| Assert.assertEquals(total, result.getIntValue("bytes")); | ||
| Assert.assertEquals(total, result.getLongValue("received")); | ||
| Assert.assertSame(inputs.get("/reuse0"), inputs.get("/reuse" + i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Socket connect() throws Exception { | ||
| Socket socket = new Socket("127.0.0.1", port); | ||
| socket.setSoTimeout(15000); | ||
| return socket; | ||
| } | ||
|
|
||
| private void writeHeaders(Socket socket, String path, boolean chunked, int total) | ||
| throws Exception { | ||
| String framing = chunked ? "Transfer-Encoding: chunked" : "Content-Length: " + total; | ||
| socket.getOutputStream().write(("POST " + path + " HTTP/1.1\r\nHost: localhost\r\n" | ||
| + framing + "\r\n\r\n").getBytes(StandardCharsets.US_ASCII)); | ||
| socket.getOutputStream().flush(); | ||
| } | ||
|
|
||
| private void writeBody(Socket socket, boolean chunked, int total) throws Exception { | ||
| writePart(socket, chunked, total); | ||
| finishBody(socket, chunked); | ||
| } | ||
|
|
||
| private void writePart(Socket socket, boolean chunked, int size) throws Exception { | ||
| OutputStream output = socket.getOutputStream(); | ||
| if (chunked) { | ||
| output.write((Integer.toHexString(size) + "\r\n").getBytes(StandardCharsets.US_ASCII)); | ||
| } | ||
| byte[] body = new byte[size]; | ||
| Arrays.fill(body, (byte) 'a'); | ||
| output.write(body); | ||
| if (chunked) { | ||
| output.write("\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
| } | ||
| output.flush(); | ||
| } | ||
|
|
||
| private void finishBody(Socket socket, boolean chunked) throws Exception { | ||
| if (chunked) { | ||
| socket.getOutputStream().write("0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
| socket.getOutputStream().flush(); | ||
| } | ||
| } | ||
|
|
||
| private JSONObject readResponse(Socket socket) throws Exception { | ||
| InputStream input = socket.getInputStream(); | ||
| Assert.assertTrue(readLine(input).startsWith("HTTP/1.1 200")); | ||
| int length = -1; | ||
| String line; | ||
| while (!(line = readLine(input)).isEmpty()) { | ||
| if (line.startsWith("Content-Length:")) { | ||
| length = Integer.parseInt(line.substring("Content-Length:".length()).trim()); | ||
| } | ||
| } | ||
| Assert.assertTrue("response must have a content length", length >= 0); | ||
| byte[] body = new byte[length]; | ||
| for (int i = 0; i < length; i++) { | ||
| int value = input.read(); | ||
| Assert.assertTrue("early response EOF", value >= 0); | ||
| body[i] = (byte) value; | ||
| } | ||
| return JSONObject.parseObject(new String(body, StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| private String readLine(InputStream input) throws Exception { | ||
| ByteArrayOutputStream line = new ByteArrayOutputStream(); | ||
| int value; | ||
| while ((value = input.read()) != '\n') { | ||
| Assert.assertTrue("early header EOF", value >= 0); | ||
| if (value != '\r') { | ||
| line.write(value); | ||
| } | ||
| } | ||
| return new String(line.toByteArray(), StandardCharsets.US_ASCII); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[NIT] replace the long warpper
ifwith below earlier return:if (requestLimit < 0) { return}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SeriousCoding789 Keeping the conditional block here. A negative limit disables size checking, but the request must still be dispatched downstream; a bare
returnwould skip that dispatch. The current structure keeps one shared delegation point throughHandlerWrapper.handle().