File contents 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)
Reference Guides
Detailed implementations in the references/ directory:
Guide
Contents
Node.js XSS Prevention
Node.js XSS Prevention
Python XSS Prevention
Python XSS Prevention
React XSS Prevention
React XSS Prevention
Content Security Policy
Content Security Policy
Best Practices
✅ DO
Encode output by default
Use templating engines
Implement CSP headers
Sanitize rich content
Validate URLs
Use HTTPOnly cookies
Regular security testing
Use secure frameworks
❌ DON'T
Trust user input
Use innerHTML directly
Skip output encoding
Allow inline scripts
Use eval()
Mix contexts (HTML/JS)
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1 --- 2 name: aj-geddes-useful-ai-prompts-xss-prevention 3 description: XSS Prevention 4 --- 5 6 # XSS Prevention 7 8 ## Table of Contents 9 10 - [Overview](#overview) 11 - [When to Use](#when-to-use) 12 - [Quick Start](#quick-start) 13 - [Reference Guides](#reference-guides) 14 - [Best Practices](#best-practices) 15 16 ## Overview 17 18 Implement comprehensive Cross-Site Scripting (XSS) prevention using input sanitization, output encoding, CSP headers, and secure coding practices. 19 20 ## When to Use 21 22 - User-generated content display 23 - Rich text editors 24 - Comment systems 25 - Search functionality 26 - Dynamic HTML generation 27 - Template rendering 28 29 ## Quick Start 30 31 Minimal working example: 32 33 ```javascript 34 // xss-prevention.js 35 const createDOMPurify = require("dompurify"); 36 const { JSDOM } = require("jsdom"); 37 const he = require("he"); 38 39 const window = new JSDOM("").window; 40 const DOMPurify = createDOMPurify(window); 41 42 class XSSPrevention { 43 /** 44 * HTML Entity Encoding - Safest for text content 45 */ 46 static encodeHTML(str) { 47 return he.encode(str, { 48 useNamedReferences: true, 49 encodeEverything: false, 50 }); 51 } 52 53 /** 54 * Sanitize HTML - For rich content 55 */ 56 static sanitizeHTML(dirty) { 57 const config = { 58 ALLOWED_TAGS: [ 59 // ... (see reference guides for full implementation) 60 ``` 61 62 ## Reference Guides 63 64 Detailed implementations in the `references/` directory: 65 66 | Guide | Contents | 67 |---|---| 68 | [Node.js XSS Prevention](references/nodejs-xss-prevention.md) | Node.js XSS Prevention | 69 | [Python XSS Prevention](references/python-xss-prevention.md) | Python XSS Prevention | 70 | [React XSS Prevention](references/react-xss-prevention.md) | React XSS Prevention | 71 | [Content Security Policy](references/content-security-policy.md) | Content Security Policy | 72 73 ## Best Practices 74 75 ### ✅ DO 76 77 - Encode output by default 78 - Use templating engines 79 - Implement CSP headers 80 - Sanitize rich content 81 - Validate URLs 82 - Use HTTPOnly cookies 83 - Regular security testing 84 - Use secure frameworks 85 86 ### ❌ DON'T 87 88 - Trust user input 89 - Use innerHTML directly 90 - Skip output encoding 91 - Allow inline scripts 92 - Use eval() 93 - Mix contexts (HTML/JS) 94 95 --- 96 > Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions. 97 <!-- tomevault:4.0:skill_md:2026-04-11 -->
tomevault-io/skills-registry/tree/main/aj-geddes--useful-ai-prompts--xss-prevention commit 6dae385509
Frequently asked questions How do I install the Aj Geddes Useful AI Prompts Xss Prevention skill? Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-xss-prevention in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Aj Geddes Useful AI Prompts Xss Prevention skill do? XSS Prevention It is listed under Security on SkillMD.
Is Aj Geddes Useful AI Prompts Xss Prevention safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: WARNING. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Aj Geddes Useful AI Prompts Xss Prevention? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Aj Geddes Useful AI Prompts Xss Prevention free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Aj Geddes Useful AI Prompts Xss Prevention? tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.