debian-mirror-gitlab/spec/support/shared_examples/csp.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.6 KiB
Ruby
Raw Permalink Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
RSpec.shared_examples 'setting CSP' do |rule_name|
let_it_be(:default_csp_values) { "'self' https://some-cdn.test" }
shared_context 'csp config' do |csp_rule|
before do
csp = ActionDispatch::ContentSecurityPolicy.new do |p|
p.send(csp_rule, default_csp_values) if csp_rule
end
expect_next_instance_of(extended_controller_class) do |controller|
expect(controller).to receive(:current_content_security_policy).at_least(:once).and_return(csp)
end
end
end
2022-08-13 15:12:31 +05:30
context 'csp config and feature toggle', :do_not_stub_snowplow_by_default do
context 'when no CSP config' do
include_context 'csp config', nil
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it 'does not add CSP directives' do
is_expected.to be_blank
end
2020-04-08 14:13:33 +05:30
end
2022-08-13 15:12:31 +05:30
describe "when a CSP config exists for #{rule_name}" do
include_context 'csp config', rule_name.parameterize.underscore.to_sym
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
context 'when feature is enabled' do
it "appends to #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("#{rule_name} #{default_csp_values}")
is_expected.to include(allowlisted_url)
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
2022-08-13 15:12:31 +05:30
context 'when feature is disabled' do
include_context 'disable feature'
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it "keeps original #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("#{rule_name} #{default_csp_values}")
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
end
2022-08-13 15:12:31 +05:30
describe "when a CSP config exists for default-src but not #{rule_name}" do
include_context 'csp config', :default_src
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
context 'when feature is enabled' do
it "uses default-src values in #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("default-src #{default_csp_values}")
is_expected.to include(allowlisted_url)
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
2022-08-13 15:12:31 +05:30
context 'when feature is disabled' do
include_context 'disable feature'
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it "does not add #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("default-src #{default_csp_values}")
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
end
2022-08-13 15:12:31 +05:30
describe "when a CSP config exists for font-src but not #{rule_name}" do
include_context 'csp config', :font_src
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
context 'when feature is enabled' do
it "uses default-src values in #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("font-src #{default_csp_values}")
is_expected.not_to include("#{rule_name} #{default_csp_values}")
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
2022-08-13 15:12:31 +05:30
context 'when feature is disabled' do
include_context 'disable feature'
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
it "does not add #{rule_name}" do
2023-03-04 22:38:38 +05:30
is_expected.to include("font-src #{default_csp_values}")
2022-08-13 15:12:31 +05:30
end
2020-04-08 14:13:33 +05:30
end
end
end
end