2018-11-30 22:51:45 +08:00
|
|
|
# frozen_string_literal: true
|
2018-10-23 01:22:23 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ContentSecurityPolicy do
|
2019-09-20 20:32:43 +08:00
|
|
|
after do
|
|
|
|
DiscoursePluginRegistry.reset!
|
|
|
|
end
|
|
|
|
|
2018-10-23 01:22:23 +08:00
|
|
|
describe 'report-uri' do
|
|
|
|
it 'is enabled by SiteSetting' do
|
|
|
|
SiteSetting.content_security_policy_collect_reports = true
|
2018-11-30 22:51:45 +08:00
|
|
|
report_uri = parse(policy)['report-uri'].first
|
|
|
|
expect(report_uri).to eq('http://test.localhost/csp_reports')
|
2018-10-23 01:22:23 +08:00
|
|
|
|
|
|
|
SiteSetting.content_security_policy_collect_reports = false
|
2018-11-30 22:51:45 +08:00
|
|
|
report_uri = parse(policy)['report-uri']
|
2018-10-23 01:22:23 +08:00
|
|
|
expect(report_uri).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-10 04:04:50 +08:00
|
|
|
describe 'base-uri' do
|
|
|
|
it 'is set to none' do
|
|
|
|
base_uri = parse(policy)['base-uri']
|
|
|
|
expect(base_uri).to eq(["'none'"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'object-src' do
|
|
|
|
it 'is set to none' do
|
|
|
|
object_srcs = parse(policy)['object-src']
|
|
|
|
expect(object_srcs).to eq(["'none'"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-10 08:53:10 +08:00
|
|
|
describe 'upgrade-insecure-requests' do
|
|
|
|
it 'is not included when force_https is off' do
|
|
|
|
SiteSetting.force_https = false
|
|
|
|
expect(parse(policy)['upgrade-insecure-requests']).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is included when force_https is on' do
|
|
|
|
SiteSetting.force_https = true
|
|
|
|
expect(parse(policy)['upgrade-insecure-requests']).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-17 05:25:21 +08:00
|
|
|
describe 'worker-src' do
|
2020-08-14 21:11:28 +08:00
|
|
|
it 'has expected values' do
|
2018-11-30 22:51:45 +08:00
|
|
|
worker_srcs = parse(policy)['worker-src']
|
2018-11-17 05:25:21 +08:00
|
|
|
expect(worker_srcs).to eq(%w[
|
2020-08-14 21:11:28 +08:00
|
|
|
'self'
|
2020-08-14 19:30:39 +08:00
|
|
|
http://test.localhost/assets/
|
|
|
|
http://test.localhost/brotli_asset/
|
|
|
|
http://test.localhost/javascripts/
|
|
|
|
http://test.localhost/plugins/
|
2018-11-17 05:25:21 +08:00
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'script-src' do
|
|
|
|
it 'always has self, logster, sidekiq, and assets' do
|
2018-11-30 22:51:45 +08:00
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include(*%w[
|
2018-11-16 01:14:16 +08:00
|
|
|
http://test.localhost/logs/
|
|
|
|
http://test.localhost/sidekiq/
|
|
|
|
http://test.localhost/mini-profiler-resources/
|
|
|
|
http://test.localhost/assets/
|
|
|
|
http://test.localhost/brotli_asset/
|
|
|
|
http://test.localhost/extra-locales/
|
|
|
|
http://test.localhost/highlight-js/
|
|
|
|
http://test.localhost/javascripts/
|
2018-11-17 05:25:21 +08:00
|
|
|
http://test.localhost/plugins/
|
2018-11-16 01:14:16 +08:00
|
|
|
http://test.localhost/theme-javascripts/
|
2018-11-27 05:49:57 +08:00
|
|
|
http://test.localhost/svg-sprite/
|
2018-11-16 01:14:16 +08:00
|
|
|
])
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
|
2020-04-02 23:16:38 +08:00
|
|
|
it 'includes "report-sample" when report collection is enabled' do
|
|
|
|
SiteSetting.content_security_policy_collect_reports = true
|
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include("'report-sample'")
|
|
|
|
end
|
|
|
|
|
2020-11-07 04:15:36 +08:00
|
|
|
context 'for Google Analytics' do
|
|
|
|
before do
|
|
|
|
SiteSetting.ga_universal_tracking_code = 'UA-12345678-9'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allowlists Google Analytics v3 when integrated' do
|
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include('https://www.google-analytics.com/analytics.js')
|
|
|
|
expect(script_srcs).not_to include('https://www.googletagmanager.com/gtag/js')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allowlists Google Analytics v4 when integrated' do
|
|
|
|
SiteSetting.ga_version = 'v4_gtag'
|
|
|
|
|
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include('https://www.google-analytics.com/analytics.js')
|
|
|
|
expect(script_srcs).to include('https://www.googletagmanager.com/gtag/js')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allowlists Google Tag Manager when integrated' do
|
2018-10-23 01:22:23 +08:00
|
|
|
SiteSetting.gtm_container_id = 'GTM-ABCDEF'
|
|
|
|
|
2018-11-30 22:51:45 +08:00
|
|
|
script_srcs = parse(policy)['script-src']
|
2018-12-14 21:17:31 +08:00
|
|
|
expect(script_srcs).to include('https://www.googletagmanager.com/gtm.js')
|
2021-03-26 23:19:31 +08:00
|
|
|
expect(script_srcs.to_s).to include('nonce-')
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
it 'allowlists CDN assets when integrated' do
|
2018-11-16 01:14:16 +08:00
|
|
|
set_cdn_url('https://cdn.com')
|
|
|
|
|
2018-11-30 22:51:45 +08:00
|
|
|
script_srcs = parse(policy)['script-src']
|
2018-11-16 01:14:16 +08:00
|
|
|
expect(script_srcs).to include(*%w[
|
|
|
|
https://cdn.com/assets/
|
|
|
|
https://cdn.com/brotli_asset/
|
|
|
|
https://cdn.com/highlight-js/
|
|
|
|
https://cdn.com/javascripts/
|
2018-11-17 05:25:21 +08:00
|
|
|
https://cdn.com/plugins/
|
2018-11-16 01:14:16 +08:00
|
|
|
https://cdn.com/theme-javascripts/
|
|
|
|
http://test.localhost/extra-locales/
|
|
|
|
])
|
|
|
|
|
|
|
|
global_setting(:s3_cdn_url, 'https://s3-cdn.com')
|
2018-10-23 01:22:23 +08:00
|
|
|
|
2018-11-30 22:51:45 +08:00
|
|
|
script_srcs = parse(policy)['script-src']
|
2018-11-16 01:14:16 +08:00
|
|
|
expect(script_srcs).to include(*%w[
|
|
|
|
https://s3-cdn.com/assets/
|
|
|
|
https://s3-cdn.com/brotli_asset/
|
|
|
|
https://cdn.com/highlight-js/
|
|
|
|
https://cdn.com/javascripts/
|
2018-11-17 05:25:21 +08:00
|
|
|
https://cdn.com/plugins/
|
2018-11-16 01:14:16 +08:00
|
|
|
https://cdn.com/theme-javascripts/
|
|
|
|
http://test.localhost/extra-locales/
|
|
|
|
])
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
2020-03-18 06:23:46 +08:00
|
|
|
|
|
|
|
it 'adds subfolder to CDN assets' do
|
|
|
|
set_cdn_url('https://cdn.com')
|
|
|
|
set_subfolder('/forum')
|
|
|
|
|
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include(*%w[
|
|
|
|
https://cdn.com/forum/assets/
|
|
|
|
https://cdn.com/forum/brotli_asset/
|
|
|
|
https://cdn.com/forum/highlight-js/
|
|
|
|
https://cdn.com/forum/javascripts/
|
|
|
|
https://cdn.com/forum/plugins/
|
|
|
|
https://cdn.com/forum/theme-javascripts/
|
|
|
|
http://test.localhost/forum/extra-locales/
|
|
|
|
])
|
|
|
|
|
|
|
|
global_setting(:s3_cdn_url, 'https://s3-cdn.com')
|
|
|
|
|
|
|
|
script_srcs = parse(policy)['script-src']
|
|
|
|
expect(script_srcs).to include(*%w[
|
|
|
|
https://s3-cdn.com/assets/
|
|
|
|
https://s3-cdn.com/brotli_asset/
|
|
|
|
https://cdn.com/forum/highlight-js/
|
|
|
|
https://cdn.com/forum/javascripts/
|
|
|
|
https://cdn.com/forum/plugins/
|
|
|
|
https://cdn.com/forum/theme-javascripts/
|
|
|
|
http://test.localhost/forum/extra-locales/
|
|
|
|
])
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
2018-10-23 01:22:23 +08:00
|
|
|
|
2021-06-08 21:32:31 +08:00
|
|
|
describe 'manifest-src' do
|
|
|
|
it 'is set to self' do
|
|
|
|
expect(parse(policy)['manifest-src']).to eq(["'self'"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-23 03:00:25 +08:00
|
|
|
describe 'frame-ancestors' do
|
|
|
|
context 'with content_security_policy_frame_ancestors enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.content_security_policy_frame_ancestors = true
|
|
|
|
Fabricate(:embeddable_host, host: 'https://a.org')
|
|
|
|
Fabricate(:embeddable_host, host: 'https://b.org')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'always has self' do
|
|
|
|
frame_ancestors = parse(policy)['frame-ancestors']
|
|
|
|
expect(frame_ancestors).to include("'self'")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes all EmbeddableHost' do
|
|
|
|
EmbeddableHost
|
|
|
|
frame_ancestors = parse(policy)['frame-ancestors']
|
|
|
|
expect(frame_ancestors).to include("https://a.org")
|
|
|
|
expect(frame_ancestors).to include("https://b.org")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with content_security_policy_frame_ancestors disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.content_security_policy_frame_ancestors = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set frame-ancestors' do
|
|
|
|
frame_ancestors = parse(policy)['frame-ancestors']
|
|
|
|
expect(frame_ancestors).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 02:59:15 +08:00
|
|
|
context 'with a plugin' do
|
|
|
|
let(:plugin_class) do
|
|
|
|
Class.new(Plugin::Instance) do
|
|
|
|
attr_accessor :enabled
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
2018-12-04 11:48:13 +08:00
|
|
|
end
|
2021-06-08 02:59:15 +08:00
|
|
|
end
|
|
|
|
|
2021-06-08 21:32:31 +08:00
|
|
|
it 'can extend script-src, object-src, manifest-src' do
|
2021-06-08 02:59:15 +08:00
|
|
|
plugin = plugin_class.new(nil, "#{Rails.root}/spec/fixtures/plugins/csp_extension/plugin.rb")
|
|
|
|
|
|
|
|
plugin.activate!
|
|
|
|
Discourse.plugins << plugin
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2021-06-08 02:59:15 +08:00
|
|
|
plugin.enabled = true
|
|
|
|
expect(parse(policy)['script-src']).to include('https://from-plugin.com')
|
|
|
|
expect(parse(policy)['object-src']).to include('https://test-stripping.com')
|
|
|
|
expect(parse(policy)['object-src']).to_not include("'none'")
|
2021-06-08 21:32:31 +08:00
|
|
|
expect(parse(policy)['manifest-src']).to include("'self'")
|
|
|
|
expect(parse(policy)['manifest-src']).to include('https://manifest-src.com')
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2021-06-08 02:59:15 +08:00
|
|
|
plugin.enabled = false
|
|
|
|
expect(parse(policy)['script-src']).to_not include('https://from-plugin.com')
|
2021-06-08 21:32:31 +08:00
|
|
|
expect(parse(policy)['manifest-src']).to_not include('https://manifest-src.com')
|
2021-06-08 02:59:15 +08:00
|
|
|
|
|
|
|
Discourse.plugins.delete plugin
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2021-06-08 02:59:15 +08:00
|
|
|
it 'can extend frame_ancestors' do
|
|
|
|
SiteSetting.content_security_policy_frame_ancestors = true
|
|
|
|
plugin = plugin_class.new(nil, "#{Rails.root}/spec/fixtures/plugins/csp_extension/plugin.rb")
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2021-06-08 02:59:15 +08:00
|
|
|
plugin.activate!
|
|
|
|
Discourse.plugins << plugin
|
|
|
|
|
|
|
|
plugin.enabled = true
|
|
|
|
expect(parse(policy)['frame-ancestors']).to include("'self'")
|
|
|
|
expect(parse(policy)['frame-ancestors']).to include('https://frame-ancestors-plugin.ext')
|
|
|
|
|
|
|
|
plugin.enabled = false
|
|
|
|
expect(parse(policy)['frame-ancestors']).to_not include('https://frame-ancestors-plugin.ext')
|
|
|
|
|
|
|
|
Discourse.plugins.delete plugin
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
|
2019-12-30 20:17:12 +08:00
|
|
|
it 'only includes unsafe-inline for qunit paths' do
|
|
|
|
expect(parse(policy(path_info: "/qunit"))['script-src']).to include("'unsafe-eval'")
|
|
|
|
expect(parse(policy(path_info: "/wizard/qunit"))['script-src']).to include("'unsafe-eval'")
|
|
|
|
expect(parse(policy(path_info: "/"))['script-src']).to_not include("'unsafe-eval'")
|
|
|
|
end
|
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
context "with a theme" do
|
|
|
|
let!(:theme) {
|
|
|
|
Fabricate(:theme).tap do |t|
|
|
|
|
settings = <<~YML
|
|
|
|
extend_content_security_policy:
|
|
|
|
type: list
|
|
|
|
default: 'script-src: from-theme.com'
|
|
|
|
YML
|
|
|
|
t.set_field(target: :settings, name: :yaml, value: settings)
|
|
|
|
t.save!
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
def theme_policy
|
2021-06-18 10:16:26 +08:00
|
|
|
policy(theme.id)
|
2019-02-11 20:32:04 +08:00
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
it 'can be extended by themes' do
|
|
|
|
policy # call this first to make sure further actions clear the cache
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
expect(parse(policy)['script-src']).not_to include('from-theme.com')
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to include('from-theme.com')
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
theme.update_setting(:extend_content_security_policy, "script-src: https://from-theme.net|worker-src: from-theme.com")
|
|
|
|
theme.save!
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to_not include('from-theme.com')
|
|
|
|
expect(parse(theme_policy)['script-src']).to include('https://from-theme.net')
|
|
|
|
expect(parse(theme_policy)['worker-src']).to include('from-theme.com')
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2019-02-11 20:32:04 +08:00
|
|
|
theme.destroy!
|
|
|
|
|
|
|
|
expect(parse(theme_policy)['script-src']).to_not include('https://from-theme.net')
|
|
|
|
expect(parse(theme_policy)['worker-src']).to_not include('from-theme.com')
|
|
|
|
end
|
2020-03-11 21:30:45 +08:00
|
|
|
|
2020-03-14 00:45:55 +08:00
|
|
|
it 'can be extended by theme modifiers' do
|
2020-03-11 21:30:45 +08:00
|
|
|
policy # call this first to make sure further actions clear the cache
|
|
|
|
|
|
|
|
theme.theme_modifier_set.csp_extensions = ["script-src: https://from-theme-flag.script", "worker-src: from-theme-flag.worker"]
|
|
|
|
theme.save!
|
|
|
|
|
2021-06-18 10:16:26 +08:00
|
|
|
child_theme = Fabricate(:theme, component: true)
|
|
|
|
theme.add_relative_theme!(:child, child_theme)
|
|
|
|
child_theme.theme_modifier_set.csp_extensions = ["script-src: https://child-theme-flag.script", "worker-src: child-theme-flag.worker"]
|
|
|
|
child_theme.save!
|
|
|
|
|
2020-03-11 21:30:45 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to include('https://from-theme-flag.script')
|
2021-06-18 10:16:26 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to include('https://child-theme-flag.script')
|
2020-03-11 21:30:45 +08:00
|
|
|
expect(parse(theme_policy)['worker-src']).to include('from-theme-flag.worker')
|
2021-06-18 10:16:26 +08:00
|
|
|
expect(parse(theme_policy)['worker-src']).to include('child-theme-flag.worker')
|
2020-03-11 21:30:45 +08:00
|
|
|
|
|
|
|
theme.destroy!
|
2021-06-18 10:16:26 +08:00
|
|
|
child_theme.destroy!
|
2020-03-11 21:30:45 +08:00
|
|
|
|
|
|
|
expect(parse(theme_policy)['script-src']).to_not include('https://from-theme-flag.script')
|
|
|
|
expect(parse(theme_policy)['worker-src']).to_not include('from-theme-flag.worker')
|
2021-06-18 10:16:26 +08:00
|
|
|
expect(parse(theme_policy)['worker-src']).to_not include('from-theme-flag.worker')
|
|
|
|
expect(parse(theme_policy)['worker-src']).to_not include('child-theme-flag.worker')
|
2020-03-11 21:30:45 +08:00
|
|
|
end
|
2020-04-24 16:47:01 +08:00
|
|
|
|
|
|
|
it 'is extended automatically when themes reference external scripts' do
|
|
|
|
policy # call this first to make sure further actions clear the cache
|
|
|
|
|
2020-04-27 22:56:29 +08:00
|
|
|
theme.set_field(target: :common, name: "header", value: <<~SCRIPT)
|
2020-05-05 11:46:57 +08:00
|
|
|
<script src='https://example.com/myscript.js'></script>
|
2021-01-09 21:52:53 +08:00
|
|
|
<script src='https://example.com/myscript2.js?with=query'></script>
|
2020-05-05 11:46:57 +08:00
|
|
|
<script src='//example2.com/protocol-less-script.js'></script>
|
|
|
|
<script src='domain-only.com'></script>
|
2020-04-27 22:56:29 +08:00
|
|
|
<script>console.log('inline script')</script>
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
theme.set_field(target: :desktop, name: "header", value: "")
|
2020-04-24 16:47:01 +08:00
|
|
|
theme.save!
|
|
|
|
|
|
|
|
expect(parse(theme_policy)['script-src']).to include('https://example.com/myscript.js')
|
2021-01-09 21:52:53 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to include('https://example.com/myscript2.js')
|
|
|
|
expect(parse(theme_policy)['script-src']).not_to include('?')
|
2020-04-27 22:56:29 +08:00
|
|
|
expect(parse(theme_policy)['script-src']).to include('example2.com/protocol-less-script.js')
|
|
|
|
expect(parse(theme_policy)['script-src']).not_to include('domain-only.com')
|
|
|
|
expect(parse(theme_policy)['script-src']).not_to include(a_string_matching /^\/theme-javascripts/)
|
2020-04-24 16:47:01 +08:00
|
|
|
|
|
|
|
theme.destroy!
|
|
|
|
|
|
|
|
expect(parse(theme_policy)['script-src']).to_not include('https://example.com/myscript.js')
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be extended by site setting' do
|
|
|
|
SiteSetting.content_security_policy_script_src = 'from-site-setting.com|from-site-setting.net'
|
|
|
|
|
|
|
|
expect(parse(policy)['script-src']).to include('from-site-setting.com', 'from-site-setting.net')
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse(csp_string)
|
|
|
|
csp_string.split(';').map do |policy|
|
|
|
|
directive, *sources = policy.split
|
|
|
|
[directive, sources]
|
|
|
|
end.to_h
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
|
2021-06-18 10:16:26 +08:00
|
|
|
def policy(theme_id = nil, path_info: "/")
|
|
|
|
ContentSecurityPolicy.policy(theme_id, path_info: path_info)
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|