CSS injection and privacy
Definition and purpose
CSS injection occurs when untrusted input is treated as stylesheet syntax. Styles can affect visibility, interactions and resource requests; an apparently harmless color setting must not grant arbitrary stylesheet control.
Mapped palette
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { color: var(--safe-color, navy); } </style> </head> <body> <p id="a">Preview label</p> <a id="b" href="#local">Local link</a> <script> const palettes={ocean:"navy",forest:"green"}; function theme(key){return Object.hasOwn(palettes,key)?palettes[key]:palettes.ocean;} document.querySelector("#a").style.setProperty("--safe-color",theme("forest")); </script> </body></html>The script chooses a fixed green value rather than interpreting the input as CSS.
Syntax and mental model
Use a fixed mapping from allowed theme identifiers to application-owned values, as in this local example. Avoid concatenating user strings into selectors, declarations or style markup. CSS.supports checks syntax, not trust. CSP style-src and resource directives are defense in depth, not a sanitizer; check real response headers and script policy. Browsers restrict :visited styling and conceal visited state from style queries to protect browsing history. No external resources or secret data are used in these examples.
Ordinary link styles
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b:link { color: navy; } #b:visited { color: purple; } </style> </head> <body> <p id="a">Preview label</p> <a id="b" href="#local">Local link</a> <script> const palettes={ocean:"navy",forest:"green"}; function theme(key){return Object.hasOwn(palettes,key)?palettes[key]:palettes.ocean;} document.querySelector("#a").style.setProperty("--safe-color",theme("forest")); </script> </body></html>Computed style does not disclose visited history. This is not a history-detection test.
Common mistakes
Never accept a whole CSS fragment because one declaration parses. A custom property can carry arbitrary tokens; use only known values in a known consuming context.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Layout stability and font performance. Next lesson: Reading specifications and maturity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CSS injection and privacy — knowledge check
Five questions on css injection and privacy.
What is an appropriate theme input?
Does CSS.supports validate trust?
What role does CSP play?
Why restrict visited styles?
Are custom properties inherently safe for untrusted input?