Commit c234f26
authored
In Rails, `nonce: true` on `javascript_include_tag` and `javascript_tag`
resolve to `content_security_policy_nonce` at runtime, and `nonce:
false` omits the attribute. Other helpers like `tag.script` and
`content_tag` pass it through as a literal value.
Previously, `javascript_include_tag` and `javascript_tag` also just
passed along and transformed the `nonce` value as a literal value.
This pulls request adds `resolve_nonce_attribute()` to the parser to
handle this transformation for `javascript_include_tag` and
`javascript_tag`.
So a document like:
```erb
<%= javascript_tag nonce: true do %>
alert('Hello')
<% end %>
```
Now properly parses as:
```diff
@ DocumentNode (location: (1:0)-(4:0))
└── children: (2 items)
├── @ HTMLElementNode (location: (1:0)-(3:9))
│ ├── open_tag:
│ │ └── @ ERBOpenTagNode (location: (1:0)-(1:36))
│ │ ├── tag_opening: "<%=" (location: (1:0)-(1:3))
│ │ ├── content: " javascript_tag nonce: true do " (location: (1:3)-(1:34))
│ │ ├── tag_closing: "%>" (location: (1:34)-(1:36))
│ │ ├── tag_name: "script" (location: (1:4)-(1:18))
│ │ └── children: (1 item)
│ │ └── @ HTMLAttributeNode (location: (1:19)-(1:30))
│ │ ├── name:
│ │ │ └── @ HTMLAttributeNameNode (location: (1:19)-(1:24))
│ │ │ └── children: (1 item)
│ │ │ └── @ LiteralNode (location: (1:19)-(1:24))
│ │ │ └── content: "nonce"
│ │ │
│ │ ├── equals: ": " (location: (1:24)-(1:26))
│ │ └── value:
│ │ └── @ HTMLAttributeValueNode (location: (1:26)-(1:30))
│ │ ├── open_quote: ∅
│ │ ├── children: (1 item)
- │ │ │ └── @ LiteralNode (location: (1:26)-(1:30))
- │ │ │ └── content: "true"
+ │ │ │ └── @ RubyLiteralNode (location: (1:26)-(1:30))
+ │ │ │ └── content: "content_security_policy_nonce"
│ │ │
│ │ ├── close_quote: ∅
│ │ └── quoted: false
│ │
│ ├── tag_name: "script" (location: (1:4)-(1:18))
│ ├── body: (1 item)
│ │ └── @ LiteralNode (location: (1:36)-(3:0))
│ │ └── content: "\n alert('Hello')\n"
│ │
│ ├── close_tag:
│ │ └── @ ERBEndNode (location: (3:0)-(3:9))
│ │ ├── tag_opening: "<%" (location: (3:0)-(3:2))
│ │ ├── content: " end " (location: (3:2)-(3:7))
│ │ └── tag_closing: "%>" (location: (3:7)-(3:9))
│ │
│ ├── is_void: false
│ └── element_source: "ActionView::Helpers::JavaScriptHelper#javascript_tag"
│
└── @ HTMLTextNode (location: (3:9)-(4:0))
└── content: "\n"
```
So that it can get properly transformed from/to:
```erb
<script nonce="<%= content_security_policy_nonce %>">
alert('Hello')
</script>
```
Additionally, it updates the `html-require-script-nonce` linter rule to
flag `tag.script` and `content_tag :script` using `nonce: true` or
`nonce: false`, since those produce literal attribute values that will
not match the CSP header. The rule now flags the following:
```erb
<%= tag.script nonce: true do %>
alert('Hello')
<% end %>
```
with:
```txt
`nonce: true` on `tag.script` outputs a literal `nonce="true"` attribute, which will not match the Content Security Policy header and the browser will block the script. Only `javascript_tag` and `javascript_include_tag` resolve `nonce: true` to the per-request `content_security_policy_nonce`. Use `javascript_tag` with `nonce: true` instead.
```
Follow up on #1384
1 parent dc5a00a commit c234f26
File tree
19 files changed
+564
-19
lines changed- javascript/packages
- linter
- src/rules
- test/rules
- rewriter/test
- src
- analyze/action_view
- include/analyze/action_view
- test
- analyze/action_view
- asset_tag_helper
- javascript_helper
- tag_helper
- snapshots/analyze/action_view
- asset_tag_helper/java_script_include_tag_test
- java_script_helper/java_script_tag_test
- tag_helper
- content_tag_test
- tag_test
19 files changed
+564
-19
lines changedLines changed: 38 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
7 | 12 | | |
8 | 13 | | |
9 | 14 | | |
| |||
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
27 | 62 | | |
| 63 | + | |
| 64 | + | |
28 | 65 | | |
29 | 66 | | |
30 | 67 | | |
| |||
Lines changed: 63 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
104 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
105 | 109 | | |
106 | 110 | | |
107 | 111 | | |
108 | 112 | | |
109 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
110 | 170 | | |
111 | 171 | | |
112 | | - | |
| 172 | + | |
113 | 173 | | |
114 | 174 | | |
115 | 175 | | |
Lines changed: 61 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
209 | 221 | | |
210 | 222 | | |
211 | 223 | | |
| |||
232 | 244 | | |
233 | 245 | | |
234 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
235 | 259 | | |
236 | 260 | | |
237 | 261 | | |
| |||
430 | 454 | | |
431 | 455 | | |
432 | 456 | | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
433 | 489 | | |
434 | 490 | | |
435 | 491 | | |
| |||
454 | 510 | | |
455 | 511 | | |
456 | 512 | | |
457 | | - | |
| 513 | + | |
458 | 514 | | |
459 | | - | |
| 515 | + | |
460 | 516 | | |
461 | 517 | | |
462 | 518 | | |
463 | | - | |
| 519 | + | |
464 | 520 | | |
465 | | - | |
| 521 | + | |
466 | 522 | | |
467 | 523 | | |
468 | 524 | | |
| |||
504 | 560 | | |
505 | 561 | | |
506 | 562 | | |
507 | | - | |
| 563 | + | |
508 | 564 | | |
509 | 565 | | |
510 | 566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
257 | 309 | | |
258 | 310 | | |
259 | 311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
272 | 277 | | |
273 | 278 | | |
274 | 279 | | |
| |||
604 | 609 | | |
605 | 610 | | |
606 | 611 | | |
| 612 | + | |
| 613 | + | |
607 | 614 | | |
608 | 615 | | |
609 | 616 | | |
| |||
698 | 705 | | |
699 | 706 | | |
700 | 707 | | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
701 | 714 | | |
702 | 715 | | |
703 | 716 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
116 | 122 | | |
117 | 123 | | |
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
138 | 146 | | |
139 | 147 | | |
0 commit comments