This covers the essential XSS prevention techniques you actually need: HTML entity encoding for text content, DOMPurify for sanitizing rich HTML, and CSP headers to lock down what can execute. The quick start shows a practical Node.js implementation, and the reference guides extend it to Python and React. Worth noting it takes the right approach by defaulting to encoding rather than sanitization, only using the heavier sanitization when you truly need rich content. The best practices section is short but hits the critical points, especially the warning against innerHTML and mixing contexts. If you're displaying user generated content anywhere, this gives you a solid defensive setup without overthinking it.

skills.sh installs
482
GitHub stars
379
last updated
Aug 21, 2026

Install

$ npx -y skills add aj-geddes/useful-ai-prompts --skill xss-prevention --agent claude-code

Installs into .claude/skills of the current project.

Files

XSS Prevention

Table of Contents

Overview

Implement comprehensive Cross-Site Scripting (XSS) prevention using input sanitization, output encoding, CSP headers, and secure coding practices.

When to Use

  • User-generated content display
  • Rich text editors
  • Comment systems
  • Search functionality
  • Dynamic HTML generation
  • Template rendering

Quick Start

Minimal working example:

// xss-prevention.js
const createDOMPurify = require("dompurify");
const { JSDOM } = require("jsdom");
const he = require("he");

const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);

class XSSPrevention {
  /**
   * HTML Entity Encoding - Safest for text content
   */
  static encodeHTML(str) {
    return he.encode(str, {
      useNamedReferences: true,
      encodeEverything: false,
    });
  }

  /**
   * Sanitize HTML - For rich content
   */
  static sanitizeHTML(dirty) {
    const config = {
      ALLOWED_TAGS: [
// ... (see reference guides for full implementation)

Sponsors

Details

Repository
github.com/aj-geddes/useful-ai-prompts
Categories
Security, AI & Agent Building
First seen
Jun 3, 2026

More from aj-geddes/useful-ai-prompts

All 258

Recommended

More Security
1aj-geddes avatarcloud-security-configurationaj-geddes/useful-ai-promptsImplement comprehensive cloud security across AWS, Azure, and GCP with IAM, encryption, network security, compliance, and threat detection.472skills.sh installs
2jezweb avataroauth-integrationsjezweb/claude-skillsImplement OAuth 2.0 authentication with GitHub and Microsoft Entra (Azure AD) in Cloudflare Workers and other edge environments. Covers provider-specific quirks356skills.sh installs
3dhruvanbhalara avatarflutter-securitydhruvanbhalara/skillsEnforce architect-level security standards including AES-256-GCM encryption, secure storage, biometric gates, and memory safety. Use when handling sensitive dat348skills.sh installs
4bobmatnyc avatarvercel-security-accessbobmatnyc/claude-mpm-skillsVercel security and access controls including RBAC, SSO, deployment protection, firewall, bot defense, audit logs, and 2FA. Use when securing Vercel projects or338skills.sh installs
5patricio0312rev avatarrbac-permissions-builderpatricio0312rev/skillsImplements role-based access control with permission matrix, route guards, policy functions, and UI permission hints. Provides middleware/guards, helper utiliti333skills.sh installs
6jorgealves avatarpython-security-scannerjorgealves/agent_skillsDetect common Python vulnerabilities such as SQL injection, unsafe deserialization, and hardcoded secrets. Use as part of a secure SDLC for Python projects.315skills.sh installs