blog · · engineering

A style-src nonce does not read your CSS

Mermaid fences now draw in outl's desktop and mobile apps. The hard part was not the renderer, it was that a nonce on style-src does not check what a declaration says, it checks which DOM API wrote it. The identical text paints through el.style.cssText and is ignored through setAttribute or innerHTML, because an attribute has nowhere to carry a nonce. So the sanitizer outl hands Pele does not filter style attributes, it moves every one of them onto the CSSOM, and the filtering of script, foreignObject and javascript: hrefs rides along on a parse that was already paid for. The same rule cost the feature its zoom: Pele writes the control bar with setAttribute and then reads it back, and a repair pass can fix what a library writes but not what it reads. Zoom shipped off.

A
10 min read

stroke:black on an SVG <g> paints when it is assigned through el.style.cssText and does nothing when it is assigned through setAttribute("style", …).

Same element, same declaration, same document, same frame. The difference is a nonce on style-src, and the thing that nonce checks is not the CSS.

That asymmetry decided the shape of almost everything in the change that made a ```mermaid fence draw in outl’s desktop and mobile apps. Not the renderer choice, not the fence priority, not the settings key. Those took a day. The question of which DOM API a third-party renderer happens to reach for took the rest, and it is not a property any library documents.

a nonce is not a thing an attribute can carry

A nonce lives on an element: <style nonce="…">. A style attribute is not an element. There is nowhere to put one.

CSP’s answer to that is not to exempt attributes from the check. It is to fail them. And the second half is what makes it sudden: once any nonce or hash appears in style-src, the 'unsafe-inline' keyword sitting next to it stops counting. There is a way back in, 'unsafe-hashes' plus one hash per distinct declaration, which for a generated diagram means hashing every declaration mermaid decides to emit. That is not a door.

So the whole mechanism is: an attribute cannot prove who wrote it, and the policy’s response is to disbelieve all of them.

The configured policy in crates/outl-mobile/src-tauri/tauri.conf.json reads style-src 'self' 'unsafe-inline' today, and under that line a style attribute is fine. The comment at the top of sanitize.ts names what takes the line away:

/**
 * Turn Pele's SVG string into a live node whose colours survive a
 * style-src nonce.
 *
 * Tauri hashes the inline boot style in the mobile shell and drops
 * `'unsafe-inline'`. A `style` attribute inserted with `innerHTML`
 * or `setAttribute` is then ignored, so the diagram loses its
 * strokes. Assigning the same text through `CSSStyleDeclaration`
 * is not gated by that directive.
 */

Nothing outl authors is at risk here. Every colour the app paints comes out of outl_theme::Palette through a stylesheet, which is what invariant 13 in the root CLAUDE.md exists to enforce. The one thing in the product that arrives as a document full of style="…" attributes, inserted in a single shot, is a rendered mermaid SVG, handed over by a library, as a string.

sanitize is the only moment the SVG is ours

Pele takes a sanitize hook: it hands you the SVG string and inserts whatever node you hand back. That hook is the one point in the pipeline where the diagram passes through outl’s hands before it is live in the document, so it does two jobs:

export function adoptDiagramSvg(svg: string): Node {
  const parsed = new DOMParser().parseFromString(svg, "image/svg+xml");
  if (parsed.querySelector("parsererror")) {
    return document.createTextNode("The diagram could not be drawn.");
  }
  const imported = document.importNode(parsed.documentElement, true);
  if (!(imported instanceof Element)) {
    return document.createTextNode("The diagram could not be drawn.");
  }
  scrub(imported);
  return imported;
}

The load-bearing half of a function called sanitize is not a filter. It is the last function in crates/outl-frontend-shared/src/diagram/sanitize.ts, and it moves a declaration from one place to another without changing a character of it:

function applyStyle(el: Element): void {
  if (!(el instanceof HTMLElement) && !(el instanceof SVGElement)) return;
  const style = el.getAttribute("style");
  if (style === null || style === "") return;
  if (el.style.cssText === style) return;
  try {
    el.style.cssText = style;
  } catch {
    el.removeAttribute("style");
  }
}

Read it as a security function and it does nothing: the same CSS, same element, no value rejected. Read it as a CSP function and it is the entire feature. The declaration stops being markup the document carries and becomes a property script set, and script was already allowed.

The catch is the one place a declaration is actually lost. If the CSSOM refuses the text, the attribute comes off rather than being left in place to look like styling that works. An attribute nothing will honour is worse than no attribute: it is a value a later reader can see and believe.

a filter you were going to need anyway

Because moving the styles means parsing the SVG, the scrub is a second walk over a tree that already exists, rather than a regex pass over a string. Which is the only reason it is worth doing well.

What comes out: script, foreignObject, style, animate, set, animateMotion and animateTransform elements, and every on* attribute. href and xlink:href survive only for http, https, mailto, or a #fragment, and the scheme is normalised before the check rather than after:

const scheme = trimmed
  .slice(0, colon)
  .toLowerCase()
  .replace(/[\s\u0000-\u001f\u007f-\u009f]/g, "");
return ALLOWED_SCHEMES.has(scheme);

The test list in sanitize.test.ts is the argument for each of those .replace ranges: java\nscript:, java\tscript:, java\u0000script:, javascript:, JAVASCRIPT:. All removed. So are data:text/html,hi, tel:, //evil.example/x and every relative path, which is why a mermaid click A "pages/journal" does nothing and the way to open a page from a diagram is class A internal-link instead, routed through outl’s own navigation rather than through an href.

None of that is novel. It is the ordinary SVG allowlist. It is in this file because the CSP forced a parse, and once you hold the tree the allowlist costs one extra function.

zoom, where repairing what a library writes is not enough

The first version of this shipped a repair pass. Pele writes its own chrome after sanitize has returned, so a MutationObserver watched the host and lifted each new attribute onto the CSSOM, with a reentrancy flag so its own writes did not retrigger it:

/**
 * Pele adds zoom controls after `sanitize` returns, and a resize can
 * add them again. Watch the host and send each new `style` attribute
 * through the CSSOM. `applyStyle` skips a value it already wrote, so
 * the observer does not loop.
 */
export function watchStyleAttributes(root: HTMLElement): () => void {

It is gone. The whole thing, the observer and liftStyleAttributes with it, deleted in the last commit of the branch, and the mount now passes zoom: false.

A repair pass can fix what a library writes. It cannot fix what a library reads. The reason is in the module doc that replaced it:

Zoom is off: Pele writes the control bar with setAttribute("style") and then reads it back through the CSSOM, which keeps cursor and drops the rest, so the buttons do nothing under a nonce.

An observer is late by construction. The attribute Pele sets is blocked, Pele reads the element’s style back for its own arithmetic and gets whatever it set property by property, and the repair arrives after the decision has been made with the wrong numbers. The visible result is the worst available one: the control bar renders, the cursor is right, the layout is not, and clicking does nothing. A missing feature is legible. A rendered control that is inert reads as a broken app.

So it ships off, and the cost is plain. There is no zoom on a mermaid diagram in outl. A large flowchart scrolls sideways inside its block, which is the overflow-x: auto on .outl-diagram and nothing more. autoDirection is left at Pele’s default for the same kind of reason, so a wide left-to-right flowchart turns top-to-bottom on a narrow page without being asked. The test that holds the first of those is does not render zoom controls, and it has to stub SVGElement.prototype.getBoundingClientRect to make the library believe the diagram overflows, because a renderer that thinks everything fits never had a reason to draw the bar.

the property no library documents

images: false, linkSchemes, zoom: false, sanitize: all documented options, all in the mount call in crates/outl-frontend-shared/src/diagram/renderer.ts. “Writes its control bar with setAttribute” is in no option list and no changelog, and a patch release could change it in either direction without anybody describing that as a change.

Which means the question “can this renderer run under a nonce” has no answer you can look up. It is not about the library’s feature set, its security posture or its sanitiser. It is about which of two interchangeable DOM calls its authors happened to type, in each separate place they style something, and the only way to find out is to run it with the nonce on and look.

This is the same shape as the mistake in reading mlua’s “safe” in my own vocabulary rather than theirs. There, a library’s word meant memory-safe and I read sandboxed. Here, a library’s option list describes what it renders and I read it as describing how. Both times the gap was not in the library’s documentation being wrong. It was in my assuming the documentation covered the axis I cared about.

what the tests pin, and what nobody has run

The regression net is not “the diagram has the right colours”, which would pass under 'unsafe-inline' forever. It is that no declaration is left sitting somewhere a policy could ignore:

for (const el of host.querySelectorAll("*")) {
  if (!(el instanceof HTMLElement) && !(el instanceof SVGElement)) continue;
  const raw = el.getAttribute("style");
  if (raw !== null) expect(el.style.cssText.length).toBeGreaterThan(0);
}

Every element under the host that carries a style attribute must also have a non-empty inline declaration, which is only true for an attribute that went through cssText. fixtures.test.tsx adds the other end: it reads every ```mermaid fence out of docs/, eleven of them across four files today, and fails if one does not reach an <svg>.

Now the gap, and it is the real one. None of this has been seen under a nonce on a phone. The verification was Chromium, with the production CSP and a style-src nonce added by hand. iOS WebKit is untested, and the mobile policy as configured still carries 'unsafe-inline', so in the build a user runs today the CSSOM path is a prediction rather than an observation. On a client where I could not attach a debugger to the webview at all, that distinction has cost me a week before.

What I would not accept is the alternative reading of the same facts: that since 'unsafe-inline' is in the file, none of this was needed yet. A policy whose 'unsafe-inline' is one build-time hash away from being inert is not a guarantee, it is a coincidence, and the failure it produces is a diagram that renders with no strokes and no error anywhere. Writing against the stricter policy costs a DOMParser pass per render. Not writing against it costs the feature, silently, in a build nobody changed on purpose.

The lesson I am taking is narrower than “respect your CSP”. It is that style-src is usually described as a list of where styles may come from, and that description hides what it actually governs: not the declaration, but the API that delivered it. Two calls that produce a byte-identical DOM are not interchangeable under it, and no type signature, lint or review will tell you which one a dependency chose.

The renderer, the sanitizer and the policy it encodes are in crates/outl-frontend-shared/src/diagram/ in the outl repository.