skip to content

Why do runtime CSS-in-JS styles run into a style-src policy without 'unsafe-inline', and what changes when the styles are extracted at build time?

level: seniorimportance: nice to knowfreq 4%

answer

  1. what the policy calls inline
  2. style elements written by script or server
  3. contents differ on every render
  4. a linked file from your own origin
  5. style attributes count too

basics

~20 s

A runtime library writes its rules into style elements, which style-src treats as inline style blocks; without 'unsafe-inline' they need a matching nonce or hash. Extraction emits linked .css files from your origin, which 'self' already allows, so nothing inline remains.

solid answer

~50 s

A `style-src` directive decides which stylesheets the browser applies, and `<style>` elements count as **inline style blocks**. A policy without `'unsafe-inline'` refuses them unless they carry a nonce or match a hash the policy lists. A runtime library produces exactly those blocks: in the browser it creates `<style>` elements as components mount, and on the server it emits collected rules as `<style>` elements in the HTML. Their content changes with every page and state, so hashes cannot be precomputed; the workable route is a per-response nonce that the library stamps on every element it creates. **Build-time extraction** removes the problem: the CSS ships as linked `.css` files from the app's own origin, allowed by `'self'`, with no inline blocks and no nonce plumbing. What remains to check is dynamic values written as `style` attributes in server HTML, which the policy also treats as inline.

go deeper

for a junior

Remember that a content security policy can refuse inline style elements, and that runtime styling libraries produce exactly those.

for a middle

Explain which elements the library creates on the server and in the browser, why the policy sees them as inline, and why a nonce rather than a hash is the workable allowance.

for a senior

Show how extraction lets the policy stay strict, and audit what remains inline afterwards: style attributes in server HTML, third-party widgets and development-only injection.

for a principal

Treat a strict style policy as an architectural constraint on the styling choice, weighing nonce plumbing for every runtime-styled app against standardising on extracted stylesheets.

## What style-src judges A **Content Security Policy** (CSP) is a response header that tells the browser which sources it may load and apply. Its `style-src` directive covers stylesheets; when it is absent, the browser falls back to `default-src`. MDN's reference for `style-src` lists what a restrictive policy such as `style-src 'self'` blocks: - `<link rel="stylesheet">` from origins the policy does not allow; - **inline `<style>` elements**; - **inline `style` attributes** in markup, and styles set from script through `setAttribute('style', …)` or `cssText`. It also notes what is *not* blocked: setting individual properties on an element's `style` object from script. Inline styles are allowed again by `'unsafe-inline'`, by a **nonce source** matching a `nonce` attribute, or by a **hash source** matching a block's exact text. MDN adds that the spec ties CSSOM string-parsing methods such as `insertRule()` to `'unsafe-eval'`, but no browser currently blocks them. ## Why a runtime library meets the policy at all A **runtime CSS-in-JS library** generates CSS while components render and has to put it somewhere the browser applies it. On a concert-ticketing site under a strict policy, that means: 1. **In the browser**, the library creates `<style>` elements and fills them with rules as event cards, the seat map and the checkout form mount. 2. **On the server**, the library collects the rules a render produced and writes them into the HTML as `<style>` elements, per page or per streamed chunk. 3. **Both are inline style blocks** in the policy's terms, so without `'unsafe-inline'` the browser refuses them and the components render unstyled; the console reports the refusals. The obvious relaxation, adding `'unsafe-inline'`, gives up one of the main protections the policy exists for, and MDN calls disallowing inline styles and scripts one of the biggest security wins CSP provides. ## Why a hash does not help, and a nonce does | Allow mechanism | Fits runtime-injected styles? | Why | |---|---|---| | `'unsafe-inline'` | Technically | Re-opens every inline style, defeating the policy | | Hash source | No | The block's text depends on which components and props rendered, so it cannot be hashed in advance | | Nonce source | Yes | A fresh random value per response, stamped on every `<style>` the library creates | Hash sources do suit genuinely static inline blocks, such as a fixed critical-CSS snippet in a page template, because their text never changes between responses. Generated CSS-in-JS output is the opposite case. The nonce route works, but it is plumbing: the server must generate a nonce per response, put it in the header, and hand it to the styling library so that both server-emitted and client-created elements carry it. Runtime libraries expose a way to receive it; styled-components 6.4.0, for example, added first-class nonce configuration. Threading that value through server rendering and the bundler is deployment practice in its own right. ## What build-time extraction changes A **zero-runtime** library compiles styles to static `.css` files during the build. At runtime: - the page links those files with `<link rel="stylesheet">` from the app's own origin, which `style-src 'self'` already allows; - there are no `<style>` elements to approve, no nonce to pass to a styling library and no style collection on the server; - the policy can stay strict without any styling-specific exception; - if the stylesheet is served from a separate CDN origin, that origin goes into `style-src` as a host source, because `'self'` covers only the page's own origin. ## What still needs attention after extraction Extraction removes injected style blocks, not every inline style: - **Custom properties set as `style` attributes.** If a dynamic value, such as a seat colour, is written into server-rendered HTML as `style="--seat-colour: …"`, it is an inline style attribute and the policy refuses it unless inline styles are relaxed for attributes. Values applied by script through the element's `style` object are not blocked, so how your framework applies inline styles decides which case you are in. - **Third-party widgets** that inject their own `<style>` elements meet the same policy. - **Development servers** can differ from production: Vite's docs, for example, say an imported `.css` file is injected through a `<style>` tag with hot-reload support, while production builds extract CSS into files loaded by `<link>`. Test the policy against a production build.

  • Why not simply allow the runtime library's styles with a hash source?
    A hash matches one exact block of text, whitespace included. A runtime library's blocks depend on which components rendered, with which props, on which page, so their text changes constantly and cannot be listed in a header ahead of time. A per-response nonce approves the element regardless of its content.
  • Does moving dynamic values to inline custom properties keep a strict policy clean?
    Only partly. A `style` attribute written into server HTML counts as an inline style, so a policy without `'unsafe-inline'` refuses it. Properties set by script on an element's `style` object are not blocked. Check how your framework applies inline styles, and prefer predefined variants where the values form a small fixed set.

  • Styles generated by JavaScript bypass style-src because only scripts are checked
  • A hash source is the cleanest way to allow runtime-injected style elements
  • Adding 'unsafe-inline' to style-src costs nothing in security
  • Build-time extraction still needs a nonce on its linked stylesheet
  • Inline style attributes are never affected by a style-src policy