XSS Prevention

XSS Prevention

Guides an AI agent to prevent cross-site scripting with input sanitisation, output encoding and Content Security Policy in Node, Python and React apps.

Skill files6 files

references/

templates/

SKILL.md97 lines
1
---
2
name: xss-prevention
3
description: >
4
Prevent Cross-Site Scripting (XSS) attacks through input sanitization, output
5
encoding, and Content Security Policy. Use when handling user-generated
6
content in web applications.
7
---
8
9
# XSS Prevention
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Implement comprehensive Cross-Site Scripting (XSS) prevention using input sanitization, output encoding, CSP headers, and secure coding practices.
22
23
## When to Use
24
25
- User-generated content display
26
- Rich text editors
27
- Comment systems
28
- Search functionality
29
- Dynamic HTML generation
30
- Template rendering
31
32
## Quick Start
33
34
Minimal working example:
35
36
```javascript
37
// xss-prevention.js
38
const createDOMPurify = require("dompurify");
39
const { JSDOM } = require("jsdom");
40
const he = require("he");
41
42
const window = new JSDOM("").window;
43
const DOMPurify = createDOMPurify(window);
44
45
class XSSPrevention {
46
/**
47
* HTML Entity Encoding - Safest for text content
48
*/
49
static encodeHTML(str) {
50
return he.encode(str, {
51
useNamedReferences: true,
52
encodeEverything: false,
53
});
54
}
55
56
/**
57
* Sanitize HTML - For rich content
58
*/
59
static sanitizeHTML(dirty) {
60
const config = {
61
ALLOWED_TAGS: [
62
// ... (see reference guides for full implementation)
63
```
64
65
## Reference Guides
66
67
Detailed implementations in the `references/` directory:
68
69
| Guide | Contents |
70
|---|---|
71
| [Node.js XSS Prevention](references/nodejs-xss-prevention.md) | Node.js XSS Prevention |
72
| [Python XSS Prevention](references/python-xss-prevention.md) | Python XSS Prevention |
73
| [React XSS Prevention](references/react-xss-prevention.md) | React XSS Prevention |
74
| [Content Security Policy](references/content-security-policy.md) | Content Security Policy |
75
76
## Best Practices
77
78
### ✅ DO
79
80
- Encode output by default
81
- Use templating engines
82
- Implement CSP headers
83
- Sanitize rich content
84
- Validate URLs
85
- Use HTTPOnly cookies
86
- Regular security testing
87
- Use secure frameworks
88
89
### ❌ DON'T
90
91
- Trust user input
92
- Use innerHTML directly
93
- Skip output encoding
94
- Allow inline scripts
95
- Use eval()
96
- Mix contexts (HTML/JS)
97

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…