i’m trying to sanitize html string before sending it into database, i’m using sanitize-html npm package but it doesn’t work
if (noteContent) {
const resultContent = sanitize(noteContent);
console.log(resultContent);
} else {
setErrorMessages((prevState) => ["note content cannot be empty"]);
}
here when i’m entering <img src=? onerror="alert('hello')" />
it returns:
<p><img src=? onerror="alert('hello')" /></p>
i don’t get it what am i doing wrong?
2
Answers
According to the documentation available on Github, the function you are looking for should be called
sanitizeHtml()
and notsanitize()
However, if you read closely, per default it does allow quite some html tags and attributes listed here: https://github.com/apostrophecms/sanitize-html?tab=readme-ov-file#default-options
In order to be more restrict, follow the example provided in their documentation.
For your example provided this would look something like this:
See also this part of the documentation.
Also, There is another option. Dompurify
It is simple.