🔒 Detect unsafe
{@html}usages in Svelte components — compatible with Svelte 3, 4, and 5 (legacy mode).
Svelte allows rendering raw HTML via the {@html ...} tag, which can expose your app to XSS attacks if the content isn’t sanitized.
This package statically analyzes .svelte files and detects unsafe {@html} insertions that:
sanitize() or escapeHtml()).<!-- svelte-ignore unsafe_html -->.Compatible with:
npm install @svelte-safe-html/core --save-dev
Or
pnpm add -D @svelte-safe-html/core
Since Svelte is added as a peer dependency, make sure it’s installed in your project:
npm install svelte
import { findUnsafeHtml } from '@svelte-safe-html/core';
import fs from 'fs';
const code = fs.readFileSync('src/App.svelte', 'utf8');
const result = findUnsafeHtml(code, 'App.svelte', ['sanitize', 'escape']);
console.log(result);
/*
{
filename: 'App.svelte',
parsed: true,
error: null,
warnings: [
{
filename: 'App.svelte',
start: { line: 10, column: 8 },
end: { line: 10, column: 40 },
message: 'Unsafe raw HTML insertion without sanitizer'
}
]
}
*/
findUnsafeHtml(code, filename, ignoreFunctions?, runes?)
| Parameter | Type | Description |
|:---:|:---:|:---:|
| code | string | The Svelte source code to analyze. |
| filename | string | Used for warning messages. |
| ignoreFunctions | string[] (optional) | Array of sanitizer functions to treat as safe. |
| runes | boolean (optional, default = false) | Enables parsing in Svelte 5 runes mode. |
{
filename: string;
parsed: boolean;
error: Error | null;
warnings: {
filename: string;
start: {
line: number;
column: number;
};
end: {
line: number;
column: number;
};
message: string;
}[];
}
To run the full compatibility suite locally:
npm run test:all
This will:
You can also run tests for one specific version (inside its folder):
cd tests/svelte4
npm test
If you have ideas, suggestions, or found issues — PRs are welcome! Let’s make Svelte security simpler and safer for everyone 🚀