<script src="https://cdnjs.cloudflare.com/ajax/libs/sanitize-html/1.27.5/sanitize-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.3/purify.min.js"></script>
const testString = `
<b>Welcome to safeland</b><br>
<a href='javascript:alert(1)'>This is fun</a><br>
<img src=x onerror=console.log(1)>
`
const result = DOMPurify.sanitize(testString)
const testString = `
<b>Welcome to safeland</b><br>
<a href='javascript:alert(1)'>This is fun</a><br>
<img src=x onerror=console.log(1)>
`
const result = sanitizeHtml(testString)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DOMPurify | |
Sanitize HTML |
Test name | Executions per second |
---|---|
DOMPurify | 5677.7 Ops/sec |
Sanitize HTML | 42100.9 Ops/sec |
Let's break down the provided JSON benchmark and explain what is tested, compared, and some pros/cons of different approaches.
Benchmark Overview
The benchmark compares two HTML sanitization libraries: DOMPurify
(version 2.3.3) and sanitize-html
(version 1.27.5).
Script Preparation Code
The script preparation code includes links to the required JavaScript files for both libraries:
sanitize-html
: https://cdnjs.cloudflare.com/ajax/libs/sanitize-html/1.27.5/sanitize-html.min.js
DOMPurify
: https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.3/purify.min.js
Individual Test Cases
There are two test cases:
The benchmark definition for this test case is:
const testString = `\r\n<b>Welcome to safeland</b><br>\r\n<a href='javascript:alert(1)'>This is fun</a><br>\r\n<img src=x onerror=console.log(1)>\r\n`\r\nconst result = DOMPurify.sanitize(testString);
This test case uses the DOMPurify
library to sanitize a malicious HTML string. The input string contains:
<b>
tag with a closing </b>
tag<a>
tag with a href
attribute that executes a JavaScript alert<img>
tag with an src
attribute set to x
, which is a deliberate invalid value, and an onerror
event handler that logs a message to the consoleThe test case checks how quickly the DOMPurify.sanitize()
method can process this malicious input.
The benchmark definition for this test case is:
const testString = `\r\n<b>Welcome to safeland</b><br>\r\n<a href='javascript:alert(1)'>This is fun</a><br>\r\n<img src=x onerror=console.log(1)>\r\n`\r\nconst result = sanitizeHtml(testString);
This test case uses the sanitize-html
library to sanitize the same malicious HTML string. The input string is identical to the previous test case.
Pros and Cons of Different Approaches
Both libraries aim to remove malicious characters from the input HTML string, preventing XSS attacks. Here are some pros and cons of each approach:
DOMPurify.sanitize()
Other Considerations
Alternatives
If you're looking for alternative HTML sanitization libraries, consider:
html-minifier
: a fast and lightweight library that minimizes HTML while removing malicious charactersjs-sanitizer
: a more comprehensive library that detects a wide range of attacks and offers customization optionsKeep in mind that each library has its strengths and weaknesses, and the choice ultimately depends on your specific requirements and performance needs.