# Linter Rule: Require nonce attribute on inline scripts **Rule:** `html-require-script-nonce` ## Description Require a `nonce` attribute on inline ` ``` ```erb ``` Rails helpers with `nonce: true`: ```erb <%= javascript_tag nonce: true do %> alert("Hello, world!") <% end %> ``` Unlike `javascript_tag`, `tag.script` does not resolve `nonce: true` to the request's CSP nonce. Pass the nonce value explicitly: ```erb <%= tag.script nonce: request.content_security_policy_nonce do %> alert("Hello, world!") <% end %> ``` External scripts (not flagged): ```erb ``` ```erb <%= javascript_include_tag "application" %> ``` Non-JavaScript script types (not flagged): ```erb ``` ```erb ``` ### 🚫 Bad HTML script tags without a nonce: ```erb ``` ```erb ``` Inline Rails helpers without a nonce: ```erb <%= javascript_tag do %> alert("Hello, world!") <% end %> ``` ```erb <%= tag.script do %> alert("Hello, world!") <% end %> ``` Rails tag helpers with a literal boolean nonce: ```erb <%= tag.script nonce: true do %> alert("Hello, world!") <% end %> ``` ## Framework-specific recommendations The offense for a missing `nonce` names `request.content_security_policy_nonce` only when the project opts into Action View in its `.herb.yml`: ```yaml framework: actionview ``` Under any other framework, and when no framework is configured, the rule recommends a dynamically generated nonce without naming a Rails API. ## References - [Inspiration: ERB Lint `RequireScriptNonce` rule](https://github.com/Shopify/erb_lint/blob/main/lib/erb_lint/linters/require_script_nonce.rb) - [MDN: Content Security Policy (CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) - [Rails: `content_security_policy_nonce`](https://api.rubyonrails.org/classes/ActionDispatch/ContentSecurityPolicy/Request.html)