--- author: [Teemu Lahjalahti](https://www.linkedin.com/in/teemulahjalahti/) index: https://salty-css.dev/llms.txt --- # Security Salty does its work before anyone visits your site. Your `.css.ts` files are evaluated in Node during the build, the result is hashed into one static stylesheet, and that file is what a browser gets. There's no style injection during render, no CSSOM insertion, no step anywhere in the pipeline that turns a string into a CSS rule while your app is running — the only styling code in your client bundle is a small mapper that picks pre-compiled class names from variant props, and it doesn't author anything. For a lot of projects that's the entire security story: the CSS on your site is CSS you wrote, compiled before deploy, served from your own origin. Never say never, though. Four things sit outside that guarantee — styles that arrive as **data** at request time, values a **user** hands to a prop token, stylesheets pulled from **someone else's server**, and the **build itself**, which is code running with your credentials. Three of the four come down to a single habit, so it's worth stating before the details: decide what a value is allowed to be at the point it enters your system, not at the point you render it. --- ## What a style object can actually do Calibration first, because CSS has a reputation as the harmless one and that reputation cuts both ways. The scary-sounding old tricks are dead. `url(javascript:…)` doesn't execute in any browser you're targeting, IE's `expression()` is a fossil, and an SVG loaded as a background image gets no script execution. If your mental model is "CSS injection means remote code execution," you can relax slightly. Then un-relax, because what's left isn't nothing. - **CSS makes network requests.** Any `url()` fetches from any host. That's a beacon: your visitor's IP and user agent handed to whoever wrote the payload, plus confirmation that a specific page was viewed. Attribute selectors turn it into a coarse read on what's actually in the DOM. - **CSS owns the interface.** A fixed-position rule can cover the page, hide the line of text stating how much money is about to move, or use `content` on a pseudo-element to put words on your page that you never wrote. Nothing crashes — but your users are looking at it, on your domain, and they trust both. - **CSS can stop being CSS.** A payload that escapes the `` in the middle of a generated rule is HTML injection, and that path ends where you'd expect it to. So: not remote code execution, and more than nothing. Treat a style payload roughly the way you'd treat user-supplied HTML you're about to render. --- ## Runtime styles [`defineRuntime`](/next/docs/basics/runtime-styles) is the one API that takes a style object at request time and turns it into real CSS — from a CMS field, a database row, whatever somebody saved in a settings panel three minutes ago. **It does not sanitize what you hand it.** Property values pass through as text. Not sanitizing *silently* is a deliberate choice rather than a gap I'm hoping you won't notice. A filter strict enough to be genuinely safe would block a good share of what this API exists for, and one loose enough to stay out of the way would mostly sell you confidence you hadn't earned. So the validation boundary is yours — which is the right place for it anyway, because you're the one who knows whether a payload came from a colleague with a CMS login or from an anonymous signup form. What that doesn't rule out is help. Some kind of opt-in validation helper is likely to land at some point — something you call, with a shape you chose, rather than something that quietly filters your styles behind your back. Until it does, and honestly after it does too, the code below is the pattern. ### How you define it Don't hand a stored object to the parser. Write the shape you support, validate into it, and let that be the only way data becomes style: ```ts // a plain .ts file const HEX = /^#[0-9a-f]{6}$/i; const RADII = ["0", "4px", "12px", "999px"]; export type ProfileStyle = { bg: string; text: string; radius: string }; export function toProfileStyle(row: Record): ProfileStyle { const hex = (v: unknown, fallback: string) => typeof v === "string" && HEX.test(v) ? v : fallback; return { bg: hex(row.bg, "#ffffff"), text: hex(row.text, "#111111"), radius: RADII.includes(row.radius as string) ? (row.radius as string) : "12px", }; } ``` Three fields, two shapes, a fallback each. Everything not on that list is not a thing a profile can be. ### How you use it ```tsx const style = toProfileStyle(profile.style); const { className, css } = await runtime.resolve({ background: style.bg, color: style.text, borderRadius: style.radius, padding: "1.5rem", border: "1px solid {colors.grey.light}", }); ``` The person picked three values. They did not pick which properties exist — so `background-image: url(…)` was never on the table, and neither was anything else. Do this one thing and most of this page stops applying to you. Four details around the edges of that API: - **The `scope` argument is a selector.** `runtime.css(styles, scope)` writes it straight into the stylesheet, so untrusted text there escapes the rule and gets to write its own. Scopes come from your code, always. - **Mind the raw-HTML sinks.** The risk isn't putting CSS in a `` child isn't a raw sink; Astro's `` against a `style-src 'nonce-…'` policy. Worth flipping around, though, because CSP isn't only a thing to accommodate here — it's also the best mitigation available for the section above. A payload's `url()` can only phone home to a host your policy allows, so if you're shipping user-supplied styles at all, tighten `img-src`, `font-src` and `connect-src` to the origins you actually use. It's a layer rather than a fix — clever people keep finding side channels that need no network request at all — but it turns a quiet exfiltration into a blocked request and a violation report, which is a much better day. One dev-mode caveat, because it costs people an afternoon: bundler dev servers usually inject CSS through JavaScript instead of serving the built file. A CSP violation that only shows up in development and never in a production build is usually that, not you. ---