Skip to content

Commit 8200cf3

Browse files
authored
Merge pull request #4175 from DataDog/appsec-handle-non-int-status-code
Fix handling of non integer response status code in AppSec
2 parents e91980e + b95a011 commit 8200cf3

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

lib/datadog/appsec/response.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def block_response(env, options)
8787
body << content(content_type)
8888

8989
Response.new(
90-
status: options['status_code'] || 403,
90+
status: options['status_code']&.to_i || 403,
9191
headers: { 'Content-Type' => content_type },
9292
body: body,
9393
)
@@ -97,15 +97,14 @@ def redirect_response(env, options)
9797
if options['location'] && !options['location'].empty?
9898
content_type = content_type(env)
9999

100-
status = options['status_code'] >= 300 && options['status_code'] < 400 ? options['status_code'] : 303
101-
102100
headers = {
103101
'Content-Type' => content_type,
104102
'Location' => options['location']
105103
}
106104

105+
status_code = options['status_code'].to_i
107106
Response.new(
108-
status: status,
107+
status: (status_code >= 300 && status_code < 400 ? status_code : 303),
109108
headers: headers,
110109
body: [],
111110
)

spec/datadog/appsec/response_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
let(:type) { 'html' }
24-
let(:status_code) { 100 }
24+
let(:status_code) { '100' }
2525

2626
context 'status_code' do
2727
subject(:status) { described_class.negotiate(env, actions).status }
@@ -92,15 +92,15 @@
9292
end
9393

9494
let(:location) { 'foo' }
95-
let(:status_code) { 303 }
95+
let(:status_code) { '303' }
9696

9797
context 'status_code' do
9898
subject(:status) { described_class.negotiate(env, actions).status }
9999

100100
it { is_expected.to eq 303 }
101101

102102
context 'when status code do not starts with 3' do
103-
let(:status_code) { 202 }
103+
let(:status_code) { '202' }
104104

105105
it { is_expected.to eq 303 }
106106
end

0 commit comments

Comments
 (0)