aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorKohei Suzuki <eagletmt@gmail.com>2018-02-18 21:36:59 +0900
committerKohei Suzuki <eagletmt@gmail.com>2018-02-18 23:45:57 +0900
commit53d863d4bbfe279e00433ef3672b040e2e6ef267 (patch)
tree6f059261f4f1a437f07ac038fe5857fdf8e7be76 /actionpack
parent099a28bbecb5b6fdabcae261d22c424f67a21601 (diff)
downloadrails-53d863d4bbfe279e00433ef3672b040e2e6ef267.tar.gz
rails-53d863d4bbfe279e00433ef3672b040e2e6ef267.tar.bz2
rails-53d863d4bbfe279e00433ef3672b040e2e6ef267.zip
Skip generating empty CSP header when no policy is configured
`Rails.application.config.content_security_policy` is configured with no policies by default. In this case, Content-Security-Policy header should not be generated instead of generating the header with no directives. Firefox also warns "Content Security Policy: Couldn't process unknown directive ''".
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/http/content_security_policy.rb12
-rw-r--r--actionpack/test/dispatch/content_security_policy_test.rb22
2 files changed, 30 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch/http/content_security_policy.rb b/actionpack/lib/action_dispatch/http/content_security_policy.rb
index 4883e23d24..160c345361 100644
--- a/actionpack/lib/action_dispatch/http/content_security_policy.rb
+++ b/actionpack/lib/action_dispatch/http/content_security_policy.rb
@@ -21,7 +21,10 @@ module ActionDispatch #:nodoc:
return response if policy_present?(headers)
if policy = request.content_security_policy
- headers[header_name(request)] = policy.build(request.controller_instance)
+ built_policy = policy.build(request.controller_instance)
+ if built_policy
+ headers[header_name(request)] = built_policy
+ end
end
response
@@ -172,7 +175,12 @@ module ActionDispatch #:nodoc:
end
def build(context = nil)
- build_directives(context).compact.join("; ") + ";"
+ built_directives = build_directives(context).compact
+ if built_directives.empty?
+ nil
+ else
+ built_directives.join("; ") + ";"
+ end
end
private
diff --git a/actionpack/test/dispatch/content_security_policy_test.rb b/actionpack/test/dispatch/content_security_policy_test.rb
index 7c4a65a633..cfec81eeae 100644
--- a/actionpack/test/dispatch/content_security_policy_test.rb
+++ b/actionpack/test/dispatch/content_security_policy_test.rb
@@ -8,7 +8,7 @@ class ContentSecurityPolicyTest < ActiveSupport::TestCase
end
def test_build
- assert_equal ";", @policy.build
+ assert_nil @policy.build
@policy.script_src :self
assert_equal "script-src 'self';", @policy.build
@@ -271,6 +271,10 @@ class ContentSecurityPolicyIntegrationTest < ActionDispatch::IntegrationTest
head :ok
end
+ def empty_policy
+ head :ok
+ end
+
private
def condition?
params[:condition] == "true"
@@ -284,12 +288,14 @@ class ContentSecurityPolicyIntegrationTest < ActionDispatch::IntegrationTest
get "/inline", to: "policy#inline"
get "/conditional", to: "policy#conditional"
get "/report-only", to: "policy#report_only"
+ get "/empty-policy", to: "policy#empty_policy"
end
end
POLICY = ActionDispatch::ContentSecurityPolicy.new do |p|
p.default_src :self
end
+ EMPTY_POLICY = ActionDispatch::ContentSecurityPolicy.new
class PolicyConfigMiddleware
def initialize(app)
@@ -297,7 +303,12 @@ class ContentSecurityPolicyIntegrationTest < ActionDispatch::IntegrationTest
end
def call(env)
- env["action_dispatch.content_security_policy"] = POLICY
+ env["action_dispatch.content_security_policy"] =
+ if env["PATH_INFO"] == "/empty-policy"
+ EMPTY_POLICY
+ else
+ POLICY
+ end
env["action_dispatch.content_security_policy_report_only"] = false
env["action_dispatch.show_exceptions"] = false
@@ -337,6 +348,13 @@ class ContentSecurityPolicyIntegrationTest < ActionDispatch::IntegrationTest
assert_policy "default-src 'self'; report-uri /violations;", report_only: true
end
+ def test_empty_policy
+ get "/empty-policy"
+ assert_response :success
+ assert_not response.headers.key?("Content-Security-Policy")
+ assert_not response.headers.key?("Content-Security-Policy-Report-Only")
+ end
+
private
def env_config