-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Adding a MaximumRequestSizeHandler #1916
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6809685
Adding a MaximumRequestSizeHandler
russ 82b768a
Setting the spec max_size to something very small to fix the spec.
russ 2c42573
Making max_size an Int64
russ 9a9934a
Removing the redundant settings in spec helper.
russ 52b2000
Linting.
russ 786a303
Adding docs to the head of the handler file.
russ 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 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,54 @@ | ||
require "../spec_helper" | ||
require "http/server" | ||
|
||
include ContextHelper | ||
|
||
describe Lucky::MaximumRequestSizeHandler do | ||
context "when the handler is disabled" do | ||
it "simply serves the request" do | ||
context = build_small_request_context("/path") | ||
Lucky::MaximumRequestSizeHandler.temp_config(enabled: false) do | ||
run_request_size_handler(context) | ||
end | ||
context.response.status.should eq(HTTP::Status::OK) | ||
end | ||
end | ||
|
||
context "when the handler is enabled" do | ||
it "with a small request, serve the request" do | ||
context = build_small_request_context("/path") | ||
Lucky::MaximumRequestSizeHandler.temp_config(enabled: true) do | ||
run_request_size_handler(context) | ||
end | ||
context.response.status.should eq(HTTP::Status::OK) | ||
end | ||
|
||
it "with a large request, deny the request" do | ||
context = build_large_request_context("/path") | ||
Lucky::MaximumRequestSizeHandler.temp_config( | ||
enabled: true, | ||
max_size: 10, | ||
) do | ||
run_request_size_handler(context) | ||
end | ||
context.response.status.should eq(HTTP::Status::PAYLOAD_TOO_LARGE) | ||
end | ||
end | ||
end | ||
|
||
private def run_request_size_handler(context) | ||
handler = Lucky::MaximumRequestSizeHandler.new | ||
handler.next = ->(_ctx : HTTP::Server::Context) {} | ||
handler.call(context) | ||
end | ||
|
||
private def build_small_request_context(path : String) : HTTP::Server::Context | ||
build_context(path: path) | ||
end | ||
|
||
private def build_large_request_context(path : String) : HTTP::Server::Context | ||
build_context(path: path).tap do |context| | ||
context.request.headers["Content-Length"] = "1000000" | ||
context.request.body = "a" * 1000000 | ||
end | ||
end |
This file contains 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,52 @@ | ||
# Allows a maximum request size to be set for incoming requests. | ||
# | ||
# Configure the max_size to the maximum size in bytes that you | ||
# want to allow. | ||
# | ||
# ``` | ||
# Lucky::MaximumRequestSizeHandler.configure do |settings| | ||
# settings.enabled = true | ||
# settings.max_size = 1_048_576 # 1MB | ||
# end | ||
# ``` | ||
|
||
class Lucky::MaximumRequestSizeHandler | ||
include HTTP::Handler | ||
|
||
Habitat.create do | ||
setting enabled : Bool = false | ||
setting max_size : Int64 = 1_048_576_i64 # 1MB | ||
end | ||
|
||
def call(context) | ||
return call_next(context) unless settings.enabled | ||
|
||
body_size = 0 | ||
body = IO::Memory.new | ||
|
||
begin | ||
buffer = Bytes.new(8192) # 8KB buffer | ||
while read_bytes = context.request.body.try(&.read(buffer)) | ||
body_size += read_bytes | ||
body.write(buffer[0, read_bytes]) | ||
|
||
if body_size > settings.max_size | ||
context.response.status = HTTP::Status::PAYLOAD_TOO_LARGE | ||
context.response.print("Request entity too large") | ||
return context | ||
end | ||
|
||
break if read_bytes < buffer.size # End of body | ||
end | ||
rescue IO::Error | ||
context.response.status = HTTP::Status::BAD_REQUEST | ||
context.response.print("Error reading request body") | ||
return context | ||
end | ||
|
||
# Reset the request body for downstream handlers | ||
context.request.body = IO::Memory.new(body.to_s) | ||
|
||
call_next(context) | ||
end | ||
end |
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.
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.
seems this failed?