-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/issue99 request body size limit filter #114
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
MartinStenhagen
wants to merge
11
commits into
main
Choose a base branch
from
feature/issue99-request-body-size-limit-filter
base: main
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
11 commits
Select commit
Hold shift + click to select a range
6ef1bc5
Added public static final int SC_PAYLOAD_TOO_LARGE = 413; and Map.ent…
MartinStenhagen bd15ba4
Merge branch 'main' into feature/issue99-request-body-size-limit-filter
MartinStenhagen 43f74fb
doFilter override and (empty for now) methods.
MartinStenhagen 44751ad
mayHaveBody method added
MartinStenhagen 2bbbe83
getHeaderAsLong-method added
MartinStenhagen 2089bb9
reject-method added
MartinStenhagen f3768e3
added check statusCode == HttpResponseBuilder.SC_PAYLOAD_TOO_LARGE
MartinStenhagen 3de21ac
Updated AppConfig and ConnectionHandler to use the new Filter.
MartinStenhagen 1be5e20
Updated application.yml and created tests
MartinStenhagen 715913e
Fixed maxBytes <= 0 to maxBytes < 0.
MartinStenhagen fd96cdb
After coderabbit feedback:
MartinStenhagen 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
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
97 changes: 97 additions & 0 deletions
97
src/main/java/org/example/filter/MaxRequestBodySizeFilter.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,97 @@ | ||
| package org.example.filter; | ||
|
|
||
|
|
||
| import org.example.http.HttpResponseBuilder; | ||
| import org.example.httpparser.HttpRequest; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A filter that rejects requests with bodies larger than a configured byte limit. | ||
| * It primarily uses the Content-Length header (when present), and can also validate | ||
| * a parsed request body if available. | ||
| */ | ||
| public class MaxRequestBodySizeFilter implements Filter { | ||
|
|
||
| private final long maxBytes; | ||
|
|
||
| public MaxRequestBodySizeFilter(long maxBytes) { | ||
| if (maxBytes < 0) { | ||
| throw new IllegalArgumentException("maxBytes must be >= 0"); | ||
| } | ||
| this.maxBytes = maxBytes; | ||
| } | ||
| @Override | ||
| public void init() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(HttpRequest request, HttpResponseBuilder response, FilterChain chain) { | ||
|
|
||
| if (!mayHaveBody(request.getMethod())) { | ||
| chain.doFilter(request, response); | ||
| return; | ||
| } | ||
|
|
||
| Long contentLength = getHeaderAsLong(request.getHeaders(), "Content-Length"); | ||
|
|
||
| if (contentLength != null && contentLength > maxBytes) { | ||
| reject(response, contentLength); | ||
| return; | ||
| } | ||
|
|
||
| // fallback: if a body has already been parsed, validate it as well | ||
| String body = request.getBody(); | ||
| if (body != null && !body.isEmpty()) { | ||
| int bodySizeInBytes = body.getBytes(StandardCharsets.UTF_8).length; | ||
| if (bodySizeInBytes > maxBytes) { | ||
| reject(response, (long) bodySizeInBytes); | ||
| return; | ||
| } | ||
| } | ||
| chain.doFilter(request, response); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
|
|
||
| } | ||
|
|
||
| private boolean mayHaveBody(String method) { | ||
| if (method == null) { | ||
| return false; | ||
| } | ||
| String normalizedMethod = method.trim().toUpperCase(); | ||
| return normalizedMethod.equals("POST") || normalizedMethod.equals("PUT") || normalizedMethod.equals("PATCH"); | ||
| } | ||
|
|
||
| private Long getHeaderAsLong(Map<String, String> headers, String headerName) { | ||
| if (headers == null || headerName == null) { | ||
| return null; | ||
| } | ||
| String rawHeaderValue = null; | ||
| for (Map.Entry<String, String> headerEntry : headers.entrySet()) { | ||
| String currentHeaderName = headerEntry.getKey(); | ||
| if (currentHeaderName.equalsIgnoreCase(headerName)) { | ||
| rawHeaderValue = headerEntry.getValue(); | ||
| break; | ||
| } | ||
| } | ||
| if (rawHeaderValue == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| long parsedContentLength = Long.parseLong(rawHeaderValue.trim()); | ||
| return parsedContentLength < 0 ? null : parsedContentLength; | ||
| } catch (NumberFormatException ex) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private void reject(HttpResponseBuilder response, Long contentLength) { | ||
| response.setStatusCode(HttpResponseBuilder.SC_PAYLOAD_TOO_LARGE); | ||
| response.setBody("Payload too large: " + contentLength + " bytes (max " + maxBytes + ")"); | ||
| } | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -10,3 +10,7 @@ ipFilter: | |
| mode: "BLOCKLIST" | ||
| blockedIps: [ ] | ||
| allowedIps: [ ] | ||
|
|
||
| maxRequestBody: | ||
| enabled: true | ||
| maxBytes: 1048576 | ||
128 changes: 128 additions & 0 deletions
128
src/test/java/org/example/filter/MaxRequestBodySizeTest.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,128 @@ | ||
| package org.example.filter; | ||
|
|
||
| import org.example.http.HttpResponseBuilder; | ||
| import org.example.httpparser.HttpRequest; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class MaxRequestBodySizeFilterTest { | ||
|
|
||
| private static FilterChain spyChain(AtomicBoolean called) { | ||
| return (req, resp) -> called.set(true); | ||
| } | ||
|
|
||
| @Test | ||
| void getRequest_shouldPassThrough() { | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(10); | ||
|
|
||
| HttpRequest request = new HttpRequest("GET", "/", "HTTP/1.1", Map.of(), null); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertTrue(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_OK, response.getStatusCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void postWithTooLargeContentLength_shouldRejectWith413() { | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(10); | ||
|
|
||
| HttpRequest request = new HttpRequest( | ||
| "POST", "/upload", "HTTP/1.1", | ||
| Map.of("Content-Length", "11"), | ||
| null | ||
| ); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertFalse(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_PAYLOAD_TOO_LARGE, response.getStatusCode()); | ||
| assertTrue(response.getBody().startsWith("Payload too large:"), "body should explain rejection"); | ||
| } | ||
|
|
||
| @Test | ||
| void postWithContentLengthEqualToMax_shouldPassThrough() { | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(10); | ||
|
|
||
| HttpRequest request = new HttpRequest( | ||
| "POST", "/upload", "HTTP/1.1", | ||
| Map.of("Content-Length", "10"), | ||
| null | ||
| ); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertTrue(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_OK, response.getStatusCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidContentLength_shouldBeIgnoredAndPassThrough() { | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(10); | ||
|
|
||
| HttpRequest request = new HttpRequest( | ||
| "POST", "/upload", "HTTP/1.1", | ||
| Map.of("Content-Length", "abc"), | ||
| null | ||
| ); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertTrue(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_OK, response.getStatusCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void contentLengthHeaderName_shouldBeCaseInsensitive() { | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(10); | ||
|
|
||
| HttpRequest request = new HttpRequest( | ||
| "POST", "/upload", "HTTP/1.1", | ||
| Map.of("content-length", "11"), // lowercase key | ||
| null | ||
| ); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertFalse(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_PAYLOAD_TOO_LARGE, response.getStatusCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void bodyFallback_shouldCountUtf8Bytes() { | ||
| // "€" is 3 bytes in UTF-8 | ||
| MaxRequestBodySizeFilter filter = new MaxRequestBodySizeFilter(2); | ||
|
|
||
| HttpRequest request = new HttpRequest( | ||
| "POST", "/upload", "HTTP/1.1", | ||
| Map.of(), // no Content-Length | ||
| "€" | ||
| ); | ||
| HttpResponseBuilder response = new HttpResponseBuilder(); | ||
| AtomicBoolean chainCalled = new AtomicBoolean(false); | ||
|
|
||
| filter.doFilter(request, response, spyChain(chainCalled)); | ||
|
|
||
| assertFalse(chainCalled.get()); | ||
| assertEquals(HttpResponseBuilder.SC_PAYLOAD_TOO_LARGE, response.getStatusCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void negativeMaxBytes_shouldThrow() { | ||
| assertThrows(IllegalArgumentException.class, () -> new MaxRequestBodySizeFilter(-1)); | ||
| } | ||
| } |
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.
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.
Filter integration currently bypasses parsed-body enforcement.
MaxRequestBodySizeFilteris added here, but in this execution pathHttpRequestis constructed with an empty body on Line 71. That means the filter’s fallback byte-length check never runs, so enforcement is effectively limited to the declaredContent-Lengthheader only.🤖 Prompt for AI Agents