Skip to main content
Web DevAuth & SecurityDiagramBeginner

XSS (Cross-Site Scripting)

KO: XSS (크로스 사이트 스크립팅)·EN: XSS (Cross-Site Scripting)·JA: XSS(クロスサイトスクリプティング)·Cross-Site Scripting

A critical web vulnerability where untrusted input injects malicious JavaScript into a trusted page, executing inside the victim's browser origin

Table of Contents

Structure Diagram

XSS Injection Vector vs Encoding & Sanitization DefenseTap a node
Comment / Safe OutpuUnescaped Untrusted…Template …Escaped +…Raw inner…
ROOTUntrusted Input (<script>)
1 link
Quick Answer

What is XSS (Cross-Site Scripting)?

A critical web vulnerability where untrusted input injects malicious JavaScript into a trusted page, executing inside the victim's browser origin. It is a core concept in Auth & Security within Web Dev.

Core Principles

Cross-Site Scripting (XSS) occurs when an application renders untrusted user input into HTML or DOM sinks without proper encoding or sanitization, allowing an attacker's JavaScript to run inside the victim's browser under the trusted site's origin.

It comes in three forms: Stored XSS (persisted in a database), Reflected XSS (echoed from a URL/query), and DOM-based XSS (introduced by unsafe client-side sinks like innerHTML). Once executed, attacker scripts can steal tokens from localStorage, hijack sessions, or impersonate the user.

Practical Takeaways

  • Rely on framework auto-escaping: Modern templates (React JSX, Vue) escape text nodes by default. Avoid dangerouslySetInnerHTML, v-html, innerHTML, and eval.
  • Sanitize rich HTML with allowlists: Whenever user HTML or Markdown is required, sanitize it with DOMPurify (never regex) and reject javascript: URLs in href/src attributes.
  • Defense in depth: Store auth credentials in HttpOnly cookies rather than localStorage, and enforce a strict CSP to block unauthorized inline scripts.

Notable Examples

  • UGC comments and Markdown editors: Combining DOMPurify with nonce-based CSP headers to neutralize embedded payloads.
  • Safe redirect handling: Validating next redirect URLs against https:/relative-path allowlists to block javascript: URI execution.