Open tonypapousek opened 3 days ago
Could you not use DOMPurify.removed
Using your sanitize example:
const sanitized = DOMPurify.sanitize(html, config);
// const isMaybeClean = html === sanitized; // throw if false
const isClean = DOMPurify.removed.length === 0;
Briefly mentioned in the README: https://github.com/cure53/DOMPurify?tab=readme-ov-file#okay-makes-sense-lets-move-on
It does state not to use it for security decisions, so a dedicated and supported isDirty
function may be better
That sounds like a good idea. Anyone wants to spin up a PR?
This issue proposes a feature that would:
isDirty()
function that returnstrue
if DOMPurify does/would sanitize a given input<div>text</div>
,<html>...something</html>
and<body><div>text</div></body>
all returntrue
ifhtml
,div
, andbody
are allowed attributes, etc)Background & Context
I'm working on a project that takes user input as HTML and renders it elsewhere. There's a requirement to toss any input that would require sanitization, instead of just sanitizing+saving it. I'd like to augment an existing function that does this with an additional check using DOMPurify.
Because of how the output is built, something like this can't work reliably:
From what I can tell, there aren't enough hooks to accurately determine if DOMPurify had changed anything. Detecting unexpected attributes and elements works well enough with hooks, but escaped strings and the like remain a head-scratcher. I might just be missing something in the docs, though.
Feature
PS: Thanks for maintaining this package!