xss HTML whitelist filter: working versions (tested 2026-10-16)
Filter untrusted HTML with xss using a safe tag whitelist, stripping scripts and event handlers while keeping safe markup offline.
passed5 passed · 0 failed2026-10-16 15:21 UTC- Status
- passed
- Last passed
- 2026-10-16 15:21 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
xss@1.0.15@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38923127324
The code
Exactly the files that passed. Your agent can pull them with get_recipe("xss-filter-html") (install), or see them on GitHub.
package.json
{
"name": "xss-filter-html",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"xss": "1.0.15"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/sanitize.ts
import xss from "xss";
export const safeTags: string[] = [
"a",
"b",
"strong",
"i",
"em",
"u",
"p",
"br",
"ul",
"ol",
"li",
"code",
"pre",
"blockquote",
"h1",
"h2",
"h3",
];
export const whiteList: Record<string, string[]> = {
a: ["href", "title"],
b: [],
strong: [],
i: [],
em: [],
u: [],
p: [],
br: [],
ul: [],
ol: [],
li: [],
code: [],
pre: [],
blockquote: [],
h1: [],
h2: [],
h3: [],
};
export function sanitizeHtml(input: string): string {
return xss(input, {
whiteList,
stripIgnoreTag: true,
stripIgnoreTagBody: ["script", "style"],
allowCommentTag: false,
});
}
test/sanitize.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { sanitizeHtml } from "../src/sanitize.ts";
test("keeps whitelisted markup intact", () => {
const out = sanitizeHtml("<p>Hello <b>world</b> and <em>friends</em></p>");
assert.equal(out, "<p>Hello <b>world</b> and <em>friends</em></p>");
});
test("strips script tags and their contents", () => {
const out = sanitizeHtml("<p>ok</p><script>alert(1)</script>");
assert.ok(out.includes("<p>ok</p>"));
assert.ok(!out.toLowerCase().includes("<script"));
assert.ok(!out.includes("alert(1)"));
});
test("removes event handler attributes but keeps safe ones", () => {
const out = sanitizeHtml('<a href="https://example.com" onclick="alert(1)">link</a>');
assert.ok(out.includes("https://example.com"));
assert.ok(out.includes(">link</a>"));
assert.ok(!out.toLowerCase().includes("onclick"));
assert.ok(!out.includes("alert(1)"));
});
test("removes javascript: urls", () => {
const out = sanitizeHtml('<a href="javascript:alert(1)">x</a>');
assert.ok(!out.toLowerCase().includes("javascript:"));
assert.ok(out.includes(">x</a>"));
});
test("strips disallowed tags and their handlers", () => {
const out = sanitizeHtml('<img src=x onerror=alert(1)><iframe src="https://evil.example"></iframe>');
assert.ok(!out.toLowerCase().includes("<img"));
assert.ok(!out.toLowerCase().includes("<iframe"));
assert.ok(!out.includes("onerror"));
assert.ok(!out.includes("evil.example"));
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.