Skip to content

Commit c8dd405

Browse files
committed
ensure enforce_request_limits returns json errors
1 parent 1caf733 commit c8dd405

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

lib/hooks/app/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def self.create(config:, endpoints:, log:)
9494
plugin.on_request(rack_env)
9595
end
9696

97-
enforce_request_limits(config)
97+
enforce_request_limits(config, request_context)
9898
request.body.rewind
9999
raw_body = request.body.read
100100

lib/hooks/app/helpers.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ def uuid
1717
# Enforce request size and timeout limits
1818
#
1919
# @param config [Hash] The configuration hash, must include :request_limit
20+
# @param request_context [Hash] Context for the request, e.g. request ID (optional)
2021
# @raise [StandardError] Halts with error if request body is too large
2122
# @return [void]
2223
# @note Timeout enforcement should be handled at the server level (e.g., Puma)
23-
def enforce_request_limits(config)
24+
def enforce_request_limits(config, request_context = {})
2425
# Optimized content length check - check most common sources first
2526
content_length = request.content_length if respond_to?(:request) && request.respond_to?(:content_length)
2627

@@ -34,7 +35,8 @@ def enforce_request_limits(config)
3435
content_length = content_length&.to_i
3536

3637
if content_length && content_length > config[:request_limit]
37-
error!("request body too large", 413)
38+
request_id = request_context&.dig(:request_id)
39+
error!({ error: "request_body_too_large", message: "request body too large", request_id: }, 413)
3840
end
3941

4042
# Note: Timeout enforcement would typically be handled at the server level (Puma, etc.)

spec/unit/lib/hooks/app/helpers_spec.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "tempfile"
4+
require "json"
45
require_relative "../../../spec_helper"
56

67
describe Hooks::App::Helpers do
@@ -23,7 +24,7 @@ def request
2324
end
2425

2526
def error!(message, code)
26-
raise StandardError, "#{code}: #{message}"
27+
raise StandardError, "#{code}: #{message.to_json}"
2728
end
2829
end
2930
end
@@ -57,8 +58,20 @@ def error!(message, code)
5758

5859
it "raises error when content length exceeds limit" do
5960
helper.headers["Content-Length"] = "1500"
60-
61-
expect { helper.enforce_request_limits(config) }.to raise_error(StandardError, /413.*too large/)
61+
request_context = { request_id: "test-request-id" }
62+
63+
error = nil
64+
begin
65+
helper.enforce_request_limits(config, request_context)
66+
rescue StandardError => e
67+
error = e
68+
end
69+
70+
expect(error).to be_a(StandardError)
71+
expect(error.message).to start_with("413: ")
72+
body = error.message.sub("413: ", "")
73+
parsed = JSON.parse(body)
74+
expect(parsed).to eq({ "error" => "request_body_too_large", "message" => "request body too large", "request_id" => "test-request-id" })
6275
end
6376
end
6477

0 commit comments

Comments
 (0)