-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Configurable to be on/off. Off by default. * Configurable to set the maximum request size. Default is 1MB. Ref #1143
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
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,51 @@ | ||
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) do | ||
run_request_size_handler(context) | ||
end | ||
context.response.status.should eq(HTTP::Status::PAYLOAD_TOO_LARGE) | ||
Check failure on line 31 in spec/lucky/maximum_request_size_handler_spec.cr GitHub Actions / specs (shard.yml, 1.10.0, false)
Check failure on line 31 in spec/lucky/maximum_request_size_handler_spec.cr GitHub Actions / specs (shard.yml, latest, false)
Check failure on line 31 in spec/lucky/maximum_request_size_handler_spec.cr GitHub Actions / specs (shard.edge.yml, latest, true)
|
||
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
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,40 @@ | ||
class Lucky::MaximumRequestSizeHandler | ||
include HTTP::Handler | ||
|
||
Habitat.create do | ||
setting enabled : Bool = false | ||
setting max_size : Int32 = 1_048_576 # 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 |