Commit a0a73005 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'check-content-type-and-be-less-permissive-in-site-validation' into 'master'

Address on-demand DAST site validation feedback

See merge request gitlab-org/gitlab!46906
parents aa5bde98 84aca59b
...@@ -41,7 +41,7 @@ module DastSiteValidations ...@@ -41,7 +41,7 @@ module DastSiteValidations
case dast_site_validation.validation_strategy case dast_site_validation.validation_strategy
when 'text_file' when 'text_file'
response.body.include?(token) response.content_type == 'text/plain' && response.body == token
when 'header' when 'header'
response.headers[DastSiteValidation::HEADER] == token response.headers[DastSiteValidation::HEADER] == token
else else
......
...@@ -5,6 +5,7 @@ require 'spec_helper' ...@@ -5,6 +5,7 @@ require 'spec_helper'
RSpec.describe DastSiteValidations::ValidateService do RSpec.describe DastSiteValidations::ValidateService do
let(:dast_site_validation) { create(:dast_site_validation) } let(:dast_site_validation) { create(:dast_site_validation) }
let(:token) { dast_site_validation.dast_site_token.token } let(:token) { dast_site_validation.dast_site_token.token }
let(:headers) { { 'Content-Type' => 'text/plain; charset=utf-8' } }
subject do subject do
described_class.new( described_class.new(
...@@ -36,7 +37,7 @@ RSpec.describe DastSiteValidations::ValidateService do ...@@ -36,7 +37,7 @@ RSpec.describe DastSiteValidations::ValidateService do
before do before do
stub_licensed_features(security_on_demand_scans: true) stub_licensed_features(security_on_demand_scans: true)
stub_feature_flags(security_on_demand_scans_site_validation: true) stub_feature_flags(security_on_demand_scans_site_validation: true)
stub_request(:get, dast_site_validation.validation_url).to_return(body: token) stub_request(:get, dast_site_validation.validation_url).to_return(body: token, headers: headers)
end end
it 'validates the url before making an http request' do it 'validates the url before making an http request' do
...@@ -110,7 +111,7 @@ RSpec.describe DastSiteValidations::ValidateService do ...@@ -110,7 +111,7 @@ RSpec.describe DastSiteValidations::ValidateService do
context 'when the token is not found' do context 'when the token is not found' do
let(:token) do let(:token) do
SecureRandom.hex '<div>' + dast_site_validation.dast_site_token.token + '</div>'
end end
it 'raises an exception' do it 'raises an exception' do
...@@ -123,10 +124,18 @@ RSpec.describe DastSiteValidations::ValidateService do ...@@ -123,10 +124,18 @@ RSpec.describe DastSiteValidations::ValidateService do
let(:dast_site_validation) { create(:dast_site_validation, validation_strategy: :text_file) } let(:dast_site_validation) { create(:dast_site_validation, validation_strategy: :text_file) }
before do before do
stub_request(:get, dast_site_validation.validation_url).to_return(body: token) stub_request(:get, dast_site_validation.validation_url).to_return(body: token, headers: headers)
end end
it_behaves_like 'a validation' it_behaves_like 'a validation'
context 'when content type is incorrect' do
let(:headers) { { 'Content-Type' => 'text/html; charset=UTF-8' } }
it 'raises an exception' do
expect { subject }.to raise_error(DastSiteValidations::ValidateService::TokenNotFound)
end
end
end end
context 'when validation_strategy=header' do context 'when validation_strategy=header' do
......
...@@ -21,14 +21,13 @@ RSpec.describe DastSiteValidationWorker do ...@@ -21,14 +21,13 @@ RSpec.describe DastSiteValidationWorker do
end end
context 'when the feature is enabled' do context 'when the feature is enabled' do
let(:response_body) do let(:response_body) { dast_site_validation.dast_site_token.token }
dast_site_validation.dast_site_token.token let(:headers) { { 'Content-Type' => 'text/plain; charset=utf-8' } }
end
before do before do
stub_licensed_features(security_on_demand_scans: true) stub_licensed_features(security_on_demand_scans: true)
stub_feature_flags(security_on_demand_scans_site_validation: true) stub_feature_flags(security_on_demand_scans_site_validation: true)
stub_request(:get, dast_site_validation.validation_url).to_return(body: response_body) stub_request(:get, dast_site_validation.validation_url).to_return(body: response_body, headers: headers)
end end
context 'when the request body contains the token' do context 'when the request body contains the token' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment