2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::ContentSecurityPolicy::ConfigLoader do
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:policy) { ActionDispatch::ContentSecurityPolicy.new }
|
|
|
|
let(:csp_config) do
|
|
|
|
{
|
|
|
|
enabled: true,
|
|
|
|
report_only: false,
|
|
|
|
directives: {
|
|
|
|
base_uri: 'http://example.com',
|
|
|
|
child_src: "'self' https://child.example.com",
|
2021-11-18 22:05:49 +05:30
|
|
|
connect_src: "'self' ws://example.com",
|
2019-10-12 21:52:04 +05:30
|
|
|
default_src: "'self' https://other.example.com",
|
|
|
|
script_src: "'self' https://script.exammple.com ",
|
|
|
|
worker_src: "data: https://worker.example.com",
|
|
|
|
report_uri: "http://example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe '.default_enabled' do
|
|
|
|
let(:enabled) { described_class.default_enabled }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'is enabled' do
|
|
|
|
expect(enabled).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in production' do
|
|
|
|
before do
|
|
|
|
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is disabled' do
|
|
|
|
expect(enabled).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.default_directives' do
|
|
|
|
let(:directives) { described_class.default_directives }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'returns default directives' do
|
2021-06-08 01:23:25 +05:30
|
|
|
directive_names = (described_class::DIRECTIVES - ['report_uri'])
|
|
|
|
directive_names.each do |directive|
|
|
|
|
expect(directives.has_key?(directive)).to be_truthy
|
|
|
|
expect(directives[directive]).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(directives.has_key?('report_uri')).to be_truthy
|
|
|
|
expect(directives['report_uri']).to be_nil
|
|
|
|
expect(directives['child_src']).to eq(directives['frame_src'])
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'adds all websocket origins to support Safari' do
|
|
|
|
it 'with insecure domain' do
|
|
|
|
stub_config_setting(host: 'example.com', https: false)
|
|
|
|
expect(directives['connect_src']).to eq("'self' ws://example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with secure domain' do
|
|
|
|
stub_config_setting(host: 'example.com', https: true)
|
|
|
|
expect(directives['connect_src']).to eq("'self' wss://example.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with custom port' do
|
|
|
|
stub_config_setting(host: 'example.com', port: '1234')
|
|
|
|
expect(directives['connect_src']).to eq("'self' ws://example.com:1234")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with custom port and secure domain' do
|
|
|
|
stub_config_setting(host: 'example.com', https: true, port: '1234')
|
|
|
|
expect(directives['connect_src']).to eq("'self' wss://example.com:1234")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when CDN host is defined' do
|
2021-06-08 01:23:25 +05:30
|
|
|
before do
|
2021-10-27 15:23:28 +05:30
|
|
|
stub_config_setting(cdn_host: 'https://example.com')
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'adds CDN host to CSP' do
|
|
|
|
expect(directives['script_src']).to eq("'strict-dynamic' 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha/ https://www.recaptcha.net https://apis.google.com https://example.com")
|
|
|
|
expect(directives['style_src']).to eq("'self' 'unsafe-inline' https://example.com")
|
|
|
|
expect(directives['font_src']).to eq("'self' https://example.com")
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when sentry is configured' do
|
2021-06-08 01:23:25 +05:30
|
|
|
before do
|
2021-10-27 15:23:28 +05:30
|
|
|
stub_sentry_settings
|
2021-11-18 22:05:49 +05:30
|
|
|
stub_config_setting(host: 'example.com')
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'adds sentry path to CSP without user' do
|
2021-11-18 22:05:49 +05:30
|
|
|
expect(directives['connect_src']).to eq("'self' ws://example.com dummy://example.com/43")
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
context 'when CUSTOMER_PORTAL_URL is set' do
|
|
|
|
before do
|
|
|
|
stub_env('CUSTOMER_PORTAL_URL', 'https://customers.example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in production' do
|
|
|
|
before do
|
|
|
|
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('production'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add CUSTOMER_PORTAL_URL to CSP' do
|
|
|
|
expect(directives['frame_src']).to eq("'self' https://www.google.com/recaptcha/ https://www.recaptcha.net/ https://content.googleapis.com https://content-compute.googleapis.com https://content-cloudbilling.googleapis.com https://content-cloudresourcemanager.googleapis.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in development' do
|
|
|
|
before do
|
|
|
|
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds CUSTOMER_PORTAL_URL to CSP' do
|
|
|
|
expect(directives['frame_src']).to eq("'self' https://www.google.com/recaptcha/ https://www.recaptcha.net/ https://content.googleapis.com https://content-compute.googleapis.com https://content-cloudbilling.googleapis.com https://content-cloudresourcemanager.googleapis.com https://customers.example.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
describe '#load' do
|
2019-10-12 21:52:04 +05:30
|
|
|
subject { described_class.new(csp_config[:directives]) }
|
|
|
|
|
|
|
|
def expected_config(directive)
|
|
|
|
csp_config[:directives][directive].split(' ').map(&:strip)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the policy properly' do
|
|
|
|
subject.load(policy)
|
|
|
|
|
|
|
|
expect(policy.directives['base-uri']).to eq([csp_config[:directives][:base_uri]])
|
|
|
|
expect(policy.directives['default-src']).to eq(expected_config(:default_src))
|
2021-11-18 22:05:49 +05:30
|
|
|
expect(policy.directives['connect-src']).to eq(expected_config(:connect_src))
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(policy.directives['child-src']).to eq(expected_config(:child_src))
|
|
|
|
expect(policy.directives['worker-src']).to eq(expected_config(:worker_src))
|
|
|
|
expect(policy.directives['report-uri']).to eq(expected_config(:report_uri))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores malformed policy statements' do
|
|
|
|
csp_config[:directives][:base_uri] = 123
|
|
|
|
|
|
|
|
subject.load(policy)
|
|
|
|
|
|
|
|
expect(policy.directives['base-uri']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|