Skip to content

Commit

Permalink
feat: add support for additional request limits
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmoehl committed Nov 12, 2024
1 parent 8ba4070 commit f867d63
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
34 changes: 33 additions & 1 deletion jobs/gorouter/spec
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,44 @@ properties:
description: "Enforce strict validation of a route service signature"
default: false
router.max_header_kb:
description: |
Deprecated, use router.max_request_header_kb instead which is equivalent to this option.
default: 1024 # 1Mb
router.max_request_header_kb:
description: |
This value controls the maximum number of bytes (in KB) the gorouter will read
parsing the request header's keys and values, including the request
line. It does not limit the size of the request body. Requests with
larger headers will result in a 431 status code. Must be between 1 and 1024kb.
default: 1024 # 1Mb
Note: This value is called max_header_kb for compatibilty reasons but only
affects the request headers and not the response headers. See max_response_header_kb
for that.
Note: This value takes precedence over router.max_header_kb but has no default to not break
existing setups. If you previously configured router.max_header_kb it is recommended to
switch to this property instead.
example: 1024 # 1Mb
router.max_response_header_kb:
description: |
This value controls the maximum number of bytes (in KB) the gorouter will read
parsing the response header's keys and values, including the request
line. It does not limit the size of the response body. Responses with
larger headers will result in a 502 status code. A limit of zero or less will
result in the default GoLang limit being used.
default: 0
router.max_request_headers:
description: |
This value controls the maximum number of headers gorouter will accept in a
single request. Only the header keys are counted and values separated by commas
are not considered additional headers. Setting this to zero or less disables the
limit.
default: 0
router.max_response_headers:
description: |
This value controls the maximum number of headers gorouter will accept in a
single response. Only the header keys are counted and values separated by commas
are not considered additional headers. Setting this to zero or less disables the
limit.
default: 0
router.extra_headers_to_log:
description: "An array of headers that access log events will be annotated with. This only applies to headers on requests."
default: []
Expand Down
25 changes: 17 additions & 8 deletions jobs/gorouter/templates/gorouter.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ def parse_ip (ip, var_name)
end
end

def validate_max_header_kb (kb)
if kb < 1 or kb > 1024
raise "Invalid router.max_header_kb value. Must be between 1 and 1,024 kb"
end
kb * 1024
end

def validate_balancing_algorithm (algorithm)
valid_balancing_algorithms = ['round-robin', 'least-connection']
unless valid_balancing_algorithms.include?(algorithm)
Expand Down Expand Up @@ -82,6 +75,19 @@ def status_tls
return tls
end

def max_request_header_bytes
# See property description for details.
if_p("router.max_request_header_kb") do |kb|
return kb * 1024
end

kb = p("router.max_header_kb")
if kb < 1 or kb > 1024
raise "Invalid router.max_header_kb value. Must be between 1 and 1,024 kb"
end

return kb * 1024
end

params = {
'zone' => spec.az,
Expand Down Expand Up @@ -145,7 +151,10 @@ params = {
'route_services_internal_server_port' => p('router.route_services_internal_server_port'),
'route_services_hairpinning_allowlist' => p('router.route_services_internal_lookup_allowlist'),
'extra_headers_to_log' => p('router.extra_headers_to_log'),
'max_header_bytes' => validate_max_header_kb(p('router.max_header_kb')),
'max_request_header_bytes' => max_request_header_bytes,
'max_response_header_bytes' => p('router.max_response_header_kb') * 1024,
'max_request_headers' => p('router.max_request_headers'),
'max_response_headers' => p('router.max_response_headers'),
'token_fetcher_max_retries' => 3,
'token_fetcher_retry_interval' => '5s',
'token_fetcher_expiration_buffer_time' => 30,
Expand Down
27 changes: 24 additions & 3 deletions spec/gorouter_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,30 @@
end
end

describe 'max_header_kb' do
it 'should set max_header_kb' do
expect(parsed_yaml['max_header_bytes']).to eq(1_048_576)
describe 'max_(request_)header_kb' do
context 'as a default' do
it 'should set max_request_header_bytes' do
expect(parsed_yaml['max_request_header_bytes']).to eq(1_048_576)
end
end

context 'when only max_header_kb is set' do
before do
deployment_manifest_fragment['router']['max_header_kb'] = 10
end
it 'should set max_request_header_bytes according to it' do
expect(parsed_yaml['max_request_header_bytes']).to eq(10_240)
end
end

context 'when max_header_kb and max_request_header_kb are set' do
before do
deployment_manifest_fragment['router']['max_header_kb'] = 10
deployment_manifest_fragment['router']['max_request_header_kb'] = 20
end
it 'should set max_request_header_bytes according to max_request_header_kb' do
expect(parsed_yaml['max_request_header_bytes']).to eq(20_480)
end
end
end

Expand Down

0 comments on commit f867d63

Please sign in to comment.