Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/datadog/appsec/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ def block_response(interrupt_params, http_accept_header)

def redirect_response(interrupt_params)
status_code = interrupt_params['status_code'].to_i
location = interrupt_params.fetch('location')

if (security_response_id = interrupt_params.fetch('security_response_id'))
location.gsub!(SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id)
end

Response.new(
status: ((status_code >= 300 && status_code < 400) ? status_code : 303),
headers: {'Location' => interrupt_params.fetch('location')},
headers: {'Location' => location},
body: [],
)
end
Expand Down
26 changes: 21 additions & 5 deletions spec/datadog/appsec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@
let(:interrupt_params) do
{
'location' => location,
'status_code' => status_code
'status_code' => status_code,
'security_response_id' => security_response_id
}
end

let(:location) { 'foo' }
let(:location) { 'example.com' }
let(:status_code) { '303' }
let(:security_response_id) { '73bb7b99-52f6-43ea-998c-6cbc6b80f520' }

context 'status_code' do
subject(:status) { described_class.from_interrupt_params(interrupt_params, http_accept_header).status }
Expand All @@ -117,9 +119,23 @@
context 'headers' do
subject(:headers) { described_class.from_interrupt_params(interrupt_params, http_accept_header).headers }

context 'Location' do
it 'uses the one from the configuration' do
expect(headers['Location']).to eq('foo')
it 'sets Location header' do
expect(headers['Location']).to eq("example.com")
end

context 'when location contains security response id placeholder' do
let(:location) { 'example.com?security_response_id=[security_response_id]' }

it 'sets Location header with substituted security response id placeholder' do
expect(headers['Location']).to eq("example.com?security_response_id=#{security_response_id}")
end

context 'when security_response_id is missing in action params' do
let(:security_response_id) { nil }

it 'sets Location header without removing security response id placeholder' do
expect(headers['Location']).to eq('example.com?security_response_id=[security_response_id]')
end
end
end
end
Expand Down