A platform for community discussion. Free, open, simple.
FEATURE: Add CSP frame-ancestors support (#12404)
Rafael dos Santos Silvafb4486d5f1a8
diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rbindex fd4702ba3a7..fc5ae4756e7 100644--- a/app/controllers/embed_controller.rb+++ b/app/controllers/embed_controller.rb@@ -12,7 +12,7 @@ class EmbedController < ApplicationControllerlayout 'embed'rescue_from Discourse::InvalidAccess do- response.headers['X-Frame-Options'] = "ALLOWALL"+ response.headers.delete('X-Frame-Options')if current_user.try(:admin?)@setup_url = "#{Discourse.base_url}/admin/customize/embedding"@show_reason = true@@ -24,7 +24,7 @@ class EmbedController < ApplicationControllerdef topicsdiscourse_expires_in 1.minute- response.headers['X-Frame-Options'] = "ALLOWALL"+ response.headers.delete('X-Frame-Options')unless SiteSetting.embed_topics_list?render 'embed_topics_error', status: 400return@@ -157,7 +157,7 @@ class EmbedController < ApplicationControllerendend- response.headers['X-Frame-Options'] = "ALLOWALL"+ response.headers.delete('X-Frame-Options')rescue URI::Errorraise Discourse::InvalidAccess.new('invalid referer host')enddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex fbed6eee272..cb3a7ca3e1e 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1602,6 +1602,7 @@ en:content_security_policy: "Enable Content-Security-Policy"content_security_policy_report_only: "Enable Content-Security-Policy-Report-Only"content_security_policy_collect_reports: "Enable CSP violation report collection at /csp_reports"+ content_security_policy_frame_ancestors: "Restrict who can embed this site in iframes via CSP. Control allowed hosts on <a href='%{base_path}/admin/customize/embedding'>Embedding</a>"content_security_policy_script_src: "Additional allowlisted script sources. The current host and CDN are included by default. See <a href='https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243' target='_blank'>Mitigate XSS Attacks with Content Security Policy.</a>"invalidate_inactive_admin_email_after_days: "Admin accounts that have not visited the site in this number of days will need to re-validate their email address before logging in. Set to 0 to disable."top_menu: "Determine which items appear in the homepage navigation, and in what order. Example latest|new|unread|categories|top|read|posted|bookmarks"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex c3c2801e1ff..33bd1327e4b 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1593,6 +1593,8 @@ security:default: falsecontent_security_policy_collect_reports:default: false+ content_security_policy_frame_ancestors:+ default: falsecontent_security_policy_script_src:type: simple_listdefault: ""diff --git a/lib/content_security_policy/default.rb b/lib/content_security_policy/default.rbindex 1d5315709eb..6826de68e82 100644--- a/lib/content_security_policy/default.rb+++ b/lib/content_security_policy/default.rb@@ -13,6 +13,7 @@ class ContentSecurityPolicydirectives[:script_src] = script_srcdirectives[:worker_src] = worker_srcdirectives[:report_uri] = report_uri if SiteSetting.content_security_policy_collect_reports+ directives[:frame_ancestors] = frame_ancestors if restrict_embed?endend@@ -73,5 +74,17 @@ class ContentSecurityPolicydef report_uri"#{base_url}/csp_reports"end++ def frame_ancestors+ [+ "'self'",+ *EmbeddableHost.pluck(:host).map { |host| "https://#{host}" }+ ]+ end++ def restrict_embed?+ SiteSetting.content_security_policy_frame_ancestors &&+ !SiteSetting.embed_any_origin+ endendenddiff --git a/spec/lib/content_security_policy_spec.rb b/spec/lib/content_security_policy_spec.rbindex f4a73c6bbc9..3f7b6ac12e3 100644--- a/spec/lib/content_security_policy_spec.rb+++ b/spec/lib/content_security_policy_spec.rb@@ -154,6 +154,39 @@ describe ContentSecurityPolicy doendend+ 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+it 'can be extended by plugins' doplugin = Class.new(Plugin::Instance) doattr_accessor :enableddiff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rbindex 62e42b1d819..9d476ed3a1c 100644--- a/spec/requests/embed_controller_spec.rb+++ b/spec/requests/embed_controller_spec.rb@@ -94,7 +94,7 @@ describe EmbedController do'REFERER' => 'https://example.com/evil-trout'}expect(response.status).to eq(200)- expect(response.headers['X-Frame-Options']).to eq("ALLOWALL")+ expect(response.headers['X-Frame-Options']).to be_nilexpect(response.body).to match("data-embed-id=\"de-1234\"")expect(response.body).to match("data-topic-id=\"#{topic.id}\"")expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")@@ -157,7 +157,7 @@ describe EmbedController docontext "success" doafter doexpect(response.status).to eq(200)- expect(response.headers['X-Frame-Options']).to eq("ALLOWALL")+ expect(response.headers['X-Frame-Options']).to be_nilendit "tells the topic retriever to work when no previous embed is found" do@@ -249,5 +249,21 @@ describe EmbedController doexpect(response.body).to match(I18n.t('embed.error'))endend++ context "CSP frame-ancestors enabled" do+ before do+ SiteSetting.content_security_policy_frame_ancestors = true+ end++ it "includes all the hosts" do+ get '/embed/comments',+ params: { embed_url: embed_url },+ headers: { 'REFERER' => "http://eviltrout.com/wat/1-2-3.html" }++ expect(response.headers['Content-Security-Policy']).to match(/frame-ancestors.*https:\/\/discourse\.org/)+ expect(response.headers['Content-Security-Policy']).to match(/frame-ancestors.*https:\/\/example\.com/)+ end+ end+endend