@@ -1075,17 +1075,14 @@ Here is a list of common headers:
1075
1075
* ** Access-Control-Allow-Origin:** Used to control which sites are allowed to bypass same origin policies and send cross-origin requests.
1076
1076
* ** 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 )
1077
1077
1078
- ### Content Security Policy
1078
+ ### Content- Security- Policy Header
1079
1079
1080
1080
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.
1087
1084
1088
- Example global policy:
1085
+ Define the security policy in the appropriate initializer :
1089
1086
1090
1087
``` ruby
1091
1088
# config/initializers/content_security_policy.rb
@@ -1096,57 +1093,65 @@ Rails.application.config.content_security_policy do |policy|
1096
1093
policy.object_src :none
1097
1094
policy.script_src :self , :https
1098
1095
policy.style_src :self , :https
1099
-
1100
1096
# Specify URI for violation reports
1101
1097
policy.report_uri " /csp-violation-report-endpoint"
1102
1098
end
1103
1099
```
1104
1100
1105
- Example controller overrides :
1101
+ The globally configured policy can be overridden on a per-resource basis :
1106
1102
1107
1103
``` ruby
1108
- # Override policy inline
1109
1104
class PostsController < ApplicationController
1110
1105
content_security_policy do |policy |
1111
1106
policy.upgrade_insecure_requests true
1107
+ policy.base_uri " https://www.example.com"
1112
1108
end
1113
1109
end
1110
+ ```
1114
1111
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
1120
1117
end
1118
+ ```
1119
+
1120
+ Use lambdas to inject per-request values, such as account subdomains in a
1121
+ multi-tenant application:
1121
1122
1122
- # Using mixed static and dynamic values
1123
+ ``` ruby
1123
1124
class PostsController < ApplicationController
1124
1125
content_security_policy do |policy |
1125
1126
policy.base_uri :self , -> { " https://#{ current_user.domain } .example.com" }
1126
1127
end
1127
1128
end
1128
-
1129
- # Disabling the global CSP
1130
- class LegacyPagesController < ApplicationController
1131
- content_security_policy false , only: :index
1132
- end
1133
1129
```
1134
1130
1135
1131
#### Reporting Violations
1136
1132
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
1139
1145
[ 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:
1142
1147
1143
1148
``` ruby
1144
- # config/initializers/content_security_policy.rb
1145
1149
Rails .application.config.content_security_policy_report_only = true
1146
1150
```
1147
1151
1152
+ Or override it in a controller:
1153
+
1148
1154
``` ruby
1149
- # Controller override
1150
1155
class PostsController < ApplicationController
1151
1156
content_security_policy_report_only only: :index
1152
1157
end
0 commit comments