hackingDNA
← Attack techniques

Cross-Site Scripting (XSS)

intermediateAdditional Vector

Injecting malicious scripts into web pages viewed by other users to steal session tokens and cookies.

Detailed Overview

XSS attacks inject malicious JavaScript into trusted websites, executing in the context of the victim's browser. This allows attackers to access session cookies, local storage, and make requests as the authenticated user. XSS comes in three main types: stored (persistent), reflected (non-persistent), and DOM-based. Each variant poses unique challenges for session security.

Prevention Strategies

  • 01Implement Content Security Policy (CSP) headers
  • 02Validate and sanitize all user input
  • 03Use context-aware output encoding
  • 04Set HttpOnly flag on session cookies
  • 05Regular security code reviews and testing

Tools

Common software used to explore or defend against this technique.

  • Burp Suite
  • OWASP ZAP
  • Browser DevTools
  • DOMPurify

BYO — Build your code

Programmatic approaches that recreate what tools do — for learning, detection, and hardening on systems you are authorized to test.

01

Escape HTML output

typescript

Minimal context-aware escaping before reflecting user input into HTML.

export function escapeHtml(input: string): string {
  return input
    .replaceAll("&", "&")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#39;");
}

// Prefer framework-safe templating + CSP in production.
02

CSP header helper

javascript

Express middleware sketch that ships a conservative Content-Security-Policy.

export function cspMiddleware(_req, res, next) {
  res.setHeader(
    "Content-Security-Policy",
    "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'",
  );
  next();
}

Educational Videos

Curated collection of tutorials and explanations to deepen your understanding.

01

Cross-Site Scripting (XSS) Explained

https://www.youtube.com/watch?v=L5l9lSnNMxg
02

XSS Attack Tutorial - Complete Guide

https://www.youtube.com/watch?v=M_nIIcKTxGk
03

Preventing XSS Attacks - Web Security Best Practices

https://www.youtube.com/watch?v=pFZY6R5YVeo
04

Content Security Policy (CSP) Implementation

https://www.youtube.com/watch?v=TfRJ_t7_k5U
05

XSS Defense Techniques for Developers

https://www.youtube.com/watch?v=P_8-f0HLX9Y

Related Techniques