Skip to content

Commit

Permalink
Revert to Rack compliant behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
vpellan committed Jul 23, 2024
1 parent fa699c8 commit 2701a76
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
9 changes: 1 addition & 8 deletions lib/datadog/appsec/contrib/rack/gateway/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ def method

def headers
result = request.env.each_with_object({}) do |(k, v), h|
# When multiple headers with the same name are present, they are concatenated with a comma
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
# Because headers are case insensitive, HTTP_FOO and HTTP_Foo is the same, and should be merged
next unless k.start_with?('HTTP_')

key = k.delete_prefix('HTTP_').tap(&:downcase!).tap { |s| s.tr!('_', '-') }
current_val = h[key]
h[key] = current_val.nil? ? v : "#{current_val}, #{v}"
h[k.delete_prefix('HTTP_').tap(&:downcase!).tap { |s| s.tr!('_', '-') }] = v if k.start_with?('HTTP_')
end

result['content-type'] = request.content_type if request.content_type
Expand Down
6 changes: 2 additions & 4 deletions spec/datadog/appsec/contrib/rack/gateway/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '10.10.10.10', 'CONTENT_TYPE' => 'text/html',
'HTTP_COOKIE' => 'foo=bar', 'HTTP_USER_AGENT' => 'WebKit',
'HTTP_' => 'empty header', 'HTTP_123' => 'numbered header',
'HTTP_123_FOO' => 'alphanumerical header', 'HTTP_FOO_123' => 'reverse alphanumerical header',
'HTTP_foo' => 'lowercase header', 'HTTP_Foo' => 'mixed case header'
'HTTP_123_FOO' => 'alphanumerical header', 'HTTP_FOO_123' => 'reverse alphanumerical header'
}
)
)
Expand All @@ -59,8 +58,7 @@
'' => 'empty header',
'123' => 'numbered header',
'123-foo' => 'alphanumerical header',
'foo-123' => 'reverse alphanumerical header',
'foo' => 'lowercase header, mixed case header'
'foo-123' => 'reverse alphanumerical header'
}
expect(request.headers).to eq(expected_headers)
end
Expand Down

0 comments on commit 2701a76

Please sign in to comment.