On a healthcare app, doctors couldn’t see the Cloudflare Turnstile checkbox on the sign-in page. They tried again, and Rack::Attack locked them out (5 attempts per email in 20 minutes). The same day, on another Rails app, the Chartkick charts of a dashboard stayed on “Loading…” when opened through a link, and showed up after a refresh.

In both cases, an inline <script> with a CSP nonce didn’t run after a Turbo Drive visit.

What Turbo does with inline scripts

Turbo Drive swaps the <body> without reloading the document, which therefore keeps the Content Security Policy from the first load. Turbo re-runs the new page’s inline scripts by cloning them, and gives them the nonce of the page it just received. If that nonce differs from the original policy’s, the browser blocks the script and only says so in the console.

First load: the CSP header allows nonce A. Turbo visit to the sign-in page: the inline script gets nonce B from the new page, the document's policy still expects A, the browser refuses the script and the Turnstile checkbox never appears.
The policy stays the one from the first load, the nonce follows the page received.

First case: a nonce tied to the session

The nonce was derived from the session id. A visitor landing on the home page has no id yet and gets a random nonce; the next page has one, so a different nonce. The id also changes on sign-in and sign-out. Clicking “Sign in” from the home page was enough to get the Turnstile script refused, while a phone that opened the sign-in page directly had no problem.

The logs showed a second bug. An iPhone loads the page at 20:28:51 and submits the autofilled form five seconds later, before Turnstile has produced its token. The rejection comes back in 96 ms and counts against the Rack::Attack limits.

Second case: a misattributed cause

Chartkick creates each chart with an inline script. In the pull request, I blamed a bug in Turbo 8.0.23, the latest published release, which replaces the nonce of cloned scripts with an empty string (hotwired/turbo#1502, fixed in the repo by #1503, not shipped yet). But that bug only affects scripts in the <head>: the charts live in the <body>, and this app also derives its nonce from the session. The Stimulus controller fixed the symptom, but I never pinned down the cause.

Stimulus controllers and a per-browser nonce

In each app, the offending script became a Stimulus controller: turnstile in the first one, which renders the widget, and chartkick in the second, which takes the chart data as attributes, builds the chart on connect() and destroys it on disconnect().

On the sign-in page, the submit button stays disabled until the Turnstile token arrives, and every rejection is logged with its reason. A doctor who still gets locked out sees a “Too many attempts” page that says how many minutes to wait, instead of a plain-text 429.

The first app still had other inline scripts, in the consultation form, the dashboard charts and the layouts’ <head>. The nonce now comes from an encrypted cookie set by the first response, one per browser:

config.content_security_policy_nonce_generator = lambda do |request|
  jar = request.cookie_jar.encrypted
  jar[:csp_nonce] || SecureRandom.base64(16).tap do |nonce|
    jar[:csp_nonce] = {value: nonce, httponly: true, same_site: :lax}
  end
end

My first attempt marked the csp-nonce tag with data-turbo-track="reload" to reload the page whenever the nonce changed. CI failed: the reload wiped the message shown after sign-in and the errors of a form rendered with a 422. I dropped it.

What’s still open

The cookie doesn’t protect <head> scripts from the Turbo 8.0.23 bug, which wipes their nonce whatever its value. I no longer write inline scripts in views: behavior that has to survive a visit goes into a Stimulus controller.