Skip to content

Commit 1fb4985

Browse files
committed
Update docs for proper nonce use
/cc @dqdinh and #242
1 parent 83a564a commit 1fb4985

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,6 @@ Code | Result
195195

196196
#### Nonce
197197

198-
script/style-nonce can be used to whitelist inline content. To do this, call the `SecureHeaders.content_security_policy_nonce` then set the nonce attributes on the various tags.
199-
200-
Setting a nonce will also set 'unsafe-inline' for browsers that don't support nonces for backwards compatibility. 'unsafe-inline' is ignored if a nonce is present in a directive in compliant browsers.
201-
202-
```erb
203-
<script nonce="<%= content_security_policy_nonce %>">
204-
console.log("whitelisted, will execute")
205-
</script>
206-
207-
<script nonce="lol">
208-
console.log("won't execute, not whitelisted")
209-
</script>
210-
211-
<script>
212-
console.log("won't execute, not whitelisted")
213-
</script>
214-
```
215-
216198
You can use a view helper to automatically add nonces to script tags:
217199

218200
```erb
@@ -240,6 +222,24 @@ body {
240222
</style>
241223
```
242224

225+
script/style-nonce can be used to whitelist inline content. To do this, call the `content_security_policy_script_nonce` or `content_security_policy_style_nonce` then set the nonce attributes on the various tags.
226+
227+
Setting a nonce will also set 'unsafe-inline' for browsers that don't support nonces for backwards compatibility. 'unsafe-inline' is ignored if a nonce is present in a directive in compliant browsers.
228+
229+
```erb
230+
<script nonce="<%= content_security_policy_script_nonce %>">
231+
console.log("whitelisted, will execute")
232+
</script>
233+
234+
<script nonce="lol">
235+
console.log("won't execute, not whitelisted")
236+
</script>
237+
238+
<script>
239+
console.log("won't execute, not whitelisted")
240+
</script>
241+
```
242+
243243
#### Hash
244244

245245
The hash feature has been removed, for now.

0 commit comments

Comments
 (0)