Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

nonce

A one-time token that marks a script or style as trusted under Content Security Policy. Put it on every script you serve, inline or external, and pair it with 'strict-dynamic'.

Overview

The nonce attribute marks an element as trusted by the page's Content Security Policy (CSP). The server generates a fresh random value for each response, puts it in the policy (script-src 'nonce-R4nd0m') and on every script it means to run. An attacker who injects markup cannot guess the value, so injected scripts are blocked.

Nonces work for external scripts as well as inline ones. The recommended "strict CSP" puts a nonce on every <script> and adds 'strict-dynamic', instead of listing allowed hosts.

Applies to: <script> (inline and src), <style> and <link rel="stylesheet"> (via style-src), and <link rel="preload"> / modulepreload. It is a global attribute, but only these elements use it.

The demo page sets a policy in a <meta> tag. The script with the nonce runs, the one without is blocked (see the console), and VB's own module, which also carries the nonce, loads normally. The demo uses a fixed nonce because it is a static file; that is exactly what you must not do in production.

Values

ValueMeaning
A random tokenAt least 128 bits from a cryptographically secure generator, base64-encoded. New for every response.
Absent or emptyThe element has no nonce and is allowed only by some other source in the policy (a hash, a host, 'self').

In the policy the value is written 'nonce-VALUE', in single quotes.

Examples

A strict CSP

This is the policy recommended by web.dev's strict CSP guide. Modern browsers use only the nonce and 'strict-dynamic'; 'unsafe-inline' and https: are fallbacks that those browsers ignore once a nonce or 'strict-dynamic' is present.

Generating the nonce per response

What 'strict-dynamic' changes

  • Scripts that a trusted script creates (document.createElement('script'), import()) are trusted too, without their own nonce.
  • Host allowlists, 'self' and 'unsafe-inline' in script-src are ignored. Only nonces and hashes admit scripts from the markup.
  • Parser-inserted scripts are not trusted by propagation: document.write('<script ...>') and scripts added via innerHTML need their own nonce (and innerHTML scripts never run anyway).

In Vanilla Breeze

  • Scripts: put the nonce on the VB <script type="module"> tag. Module imports inherit the nonce, and with 'strict-dynamic' the scripts VB loads later (pack bundles, the autoloader's component chunks, Mermaid for <diagram-wc>, Turnstile for bot protection) are trusted as well. The demo above runs VB under this policy.
  • Styles: VB does not copy a nonce onto the stylesheets it adds at runtime. Some components and utilities insert <style> elements (the interestfor polyfill, <chart-wc>, <geo-map>), theme and pack CSS arrive as <link> elements, and a few write style="" attributes into markup (the <theme-picker> swatches, data-mock frame placeholders). A style-src that allows only nonces breaks these. Leave style-src unset, or use style-src 'self' 'unsafe-inline' plus your CDN host. Styles set through element.style are never affected.
  • Your own markup: inline event handlers such as onclick="" are blocked by any nonce-based policy. VB's declarative attributes (commandfor, data-toggle, data-copy and the rest) need no inline script.

Security

  • A reused nonce is no nonce. Generate one per response. Caching a page with its nonce baked in (at a CDN or in a service worker) serves the same value to everyone, and an attacker who reads it once can use it. Either do not cache nonced HTML, or inject a fresh nonce at the edge for each response (for example with Cloudflare's HTMLRewriter).
  • Never echo user input into a nonced script. The nonce trusts the whole element, including whatever an attacker managed to put inside it.
  • Nonce hiding needs a header. When the policy arrives in a Content-Security-Policy header, the browser empties the nonce attribute after parsing (getAttribute('nonce') returns "", while the element.nonce property keeps the value), so injected CSS selectors cannot read it. With a <meta> policy, as in the demo, the attribute stays readable.
  • Prefer the header to <meta>. A meta policy applies only to content after it, and cannot use frame-ancestors, report-uri or sandbox.

Browser support

CSP nonces are supported in all current browsers. 'strict-dynamic': Chrome 52, Firefox 52, Safari 15.4 (MDN browser-compat-data, November 2026).

Gotchas

  • Static hosting. A pre-built page cannot carry a per-request nonce by itself. Use hashes of your inline scripts ('sha256-...') with 'strict-dynamic', or add the nonce in an edge function.
  • Inline event handlers and javascript: URLs are never allowed by a nonce. Move them into nonced scripts or use declarative attributes.
  • Third-party snippets that use document.write to add scripts break under 'strict-dynamic'.
  • Debug in report-only mode first: send Content-Security-Policy-Report-Only with the same policy and watch the console before enforcing it.

Related

Which to choose

  • Server-rendered pages — nonce on every script plus 'strict-dynamic'.
  • Static pages — hashes for inline scripts plus 'strict-dynamic', or nonces injected at the edge.
  • Third-party files — a nonce decides whether the tag may run; integrity decides whether the file is the one you expect. Use both.
↑