Skip to content

Commit 258e9ad

Browse files
Merge pull request rails#44470 from p8/docs/csp-header
Improve Content Security Policy documentation [ci-skip]
2 parents 0170745 + b588ceb commit 258e9ad

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

actionpack/lib/action_dispatch/http/content_security_policy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
require "active_support/core_ext/object/deep_dup"
44

55
module ActionDispatch # :nodoc:
6-
# Allows configuring a
6+
# Configures the HTTP
77
# {Content-Security-Policy}[https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy]
8-
# to help protect against XSS and injection attacks.
8+
# response header to help protect against XSS and injection attacks.
99
#
1010
# Example global policy:
1111
#

guides/source/security.md

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,17 +1075,14 @@ Here is a list of common headers:
10751075
* **Access-Control-Allow-Origin:** Used to control which sites are allowed to bypass same origin policies and send cross-origin requests.
10761076
* **Strict-Transport-Security:** [Used to control if the browser is allowed to only access a site over a secure connection](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security)
10771077

1078-
### Content Security Policy
1078+
### Content-Security-Policy Header
10791079

10801080
To help protect against XSS and injection attacks, it is recommended to define a
1081-
[Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
1082-
for your application. Rails provides a DSL that allows you to configure a
1083-
Content Security Policy. You can configure a global default policy and then
1084-
override it on a per-resource basis and even use lambdas to inject per-request
1085-
values into the header such as account subdomains in a multi-tenant
1086-
application.
1081+
[Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
1082+
response header for your application. Rails provides a DSL that allows you to
1083+
configure the header.
10871084

1088-
Example global policy:
1085+
Define the security policy in the appropriate initializer:
10891086

10901087
```ruby
10911088
# config/initializers/content_security_policy.rb
@@ -1096,57 +1093,65 @@ Rails.application.config.content_security_policy do |policy|
10961093
policy.object_src :none
10971094
policy.script_src :self, :https
10981095
policy.style_src :self, :https
1099-
11001096
# Specify URI for violation reports
11011097
policy.report_uri "/csp-violation-report-endpoint"
11021098
end
11031099
```
11041100

1105-
Example controller overrides:
1101+
The globally configured policy can be overridden on a per-resource basis:
11061102

11071103
```ruby
1108-
# Override policy inline
11091104
class PostsController < ApplicationController
11101105
content_security_policy do |policy|
11111106
policy.upgrade_insecure_requests true
1107+
policy.base_uri "https://www.example.com"
11121108
end
11131109
end
1110+
```
11141111

1115-
# Using literal values
1116-
class PostsController < ApplicationController
1117-
content_security_policy do |policy|
1118-
policy.base_uri "https://www.example.com"
1119-
end
1112+
Or it can be disabled:
1113+
1114+
```ruby
1115+
class LegacyPagesController < ApplicationController
1116+
content_security_policy false, only: :index
11201117
end
1118+
```
1119+
1120+
Use lambdas to inject per-request values, such as account subdomains in a
1121+
multi-tenant application:
11211122

1122-
# Using mixed static and dynamic values
1123+
```ruby
11231124
class PostsController < ApplicationController
11241125
content_security_policy do |policy|
11251126
policy.base_uri :self, -> { "https://#{current_user.domain}.example.com" }
11261127
end
11271128
end
1128-
1129-
# Disabling the global CSP
1130-
class LegacyPagesController < ApplicationController
1131-
content_security_policy false, only: :index
1132-
end
11331129
```
11341130

11351131
#### Reporting Violations
11361132

1137-
Use the `content_security_policy_report_only`
1138-
configuration attribute to set
1133+
Enable the
1134+
[report-uri](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/report-uri)
1135+
directive to report violations to the specified URI:
1136+
1137+
```ruby
1138+
Rails.application.config.content_security_policy do |policy|
1139+
policy.report_uri "/csp-violation-report-endpoint"
1140+
end
1141+
```
1142+
1143+
When migrating legacy content, you might want to report violations without
1144+
enforcing the policy. Set the
11391145
[Content-Security-Policy-Report-Only](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only)
1140-
in order to report only content violations for migrating
1141-
legacy content
1146+
response header to only report violations:
11421147

11431148
```ruby
1144-
# config/initializers/content_security_policy.rb
11451149
Rails.application.config.content_security_policy_report_only = true
11461150
```
11471151

1152+
Or override it in a controller:
1153+
11481154
```ruby
1149-
# Controller override
11501155
class PostsController < ApplicationController
11511156
content_security_policy_report_only only: :index
11521157
end

0 commit comments

Comments
 (0)