CSS Injection Can Outlive a Strict script-src Policy
A content security policy that blocks inline and remote scripts can still leave an injected stylesheet with useful authority. CSS cannot execute JavaScript, but it can select elements, read their rendered state through selectors, and cause the browser to request an attacker-controlled URL. When a page accepts untrusted CSS, the resulting leak may happen without script execution and may not appear in JavaScript error monitoring.
The Content Security Policy specification explicitly discusses nonce exfiltration through content attributes. Its example uses CSS attribute selectors and a background: url(...) value to send selected data to another origin. The exact attack depends on the selectors, the application's DOM, the browser's parsing behavior, and the policy. The design lesson does not depend on those details: a stylesheet is executable content even when it is not script.
script-src is not style-src
CSP directives are scoped by resource type. script-src governs scripts. style-src governs stylesheets, and style-src-attr governs inline style attributes when it is supported. A policy that sets only script-src does not make an inline <style> block or a style attribute harmless.
An effective policy normally needs a relationship between several directives:
-
style-srcshould allow only reviewed stylesheet origins and the nonce or hash needed for intentional inline styles. -
style-src-attrshould be considered separately when the application permits inline style attributes. -
img-srcshould restrict the destinations available to CSS image loads and other image requests. -
default-srcshould not accidentally provide a permissive fallback.
The policy should be tested in report-only mode before enforcement on a complex application. Reports reveal which stylesheets, widgets, and legacy template paths would break. They also show whether the application is still accepting style input from a path the team thought was sanitized.
Treat user themes as code-adjacent content
A feature that lets users set colors, spacing, or a theme may be safe when it accepts a small set of validated properties rendered from a fixed template. It becomes a different problem when it stores a full stylesheet, a CSS fragment, or a selector supplied by a user. Custom CSS features should have a separate trust decision from ordinary profile text.
Do not rely on a general HTML sanitizer to decide what CSS is safe. CSS has its own grammar, imports, functions, selector behavior, and browser-specific extensions. Normalize the accepted syntax, reject unknown constructs, and render the result in a context where the policy and destination controls still apply.
Detection and response
Review CSP reports for blocked style and image requests, but do not treat a quiet report stream as proof of safety. Reports may be sampled, delayed, blocked by an extension, or sent to an endpoint that has lost its owner. Add server-side review for changes to user-controlled theme records and keep an audit trail that identifies the author and the old value.
If a CSS injection is suspected, preserve the rendered page, the policy headers, the stored style input, and the relevant access logs. The browser request generated by CSS may be the only network evidence, and its destination may look like an image request rather than an exploit. Contain the affected template or tenant, remove the stored value, and rotate any data that may have been selected through the page.
Limits
Strict CSP can reduce data exfiltration paths but cannot repair unsafe DOM construction. A page that places a secret into an attribute available to selectors may leak it through another channel. CSS policies should be combined with output encoding, review of user-controlled presentation data, and network controls that do not assume every request carrying an image-looking path is benign.
Top comments (0)