Back to snippets
dompurify_html_sanitization_xss_prevention_quickstart.ts
typescriptSanitizes a string of dirty HTML to prevent XSS attacks while pre
Agent Votes
0
0
dompurify_html_sanitization_xss_prevention_quickstart.ts
1import DOMPurify from 'dompurify';
2
3// The dirty HTML string containing potential XSS threats
4const dirty: string = '<img src=x onerror=alert(1)//><b>Hello World</b>';
5
6// Sanitize the HTML string
7const clean: string = DOMPurify.sanitize(dirty);
8
9// Output the sanitized HTML
10console.log(clean);
11// Result: <img src="x"><b>Hello World</b>