Details
Indirect / second-order
- Editor / preview libraries that compose with DOMPurify — if any consumer registers a hook that mutates
data.allowedTags, every other consumer's sanitize calls inherit the widening. - Test suites that exercise multiple sanitize configurations — once a test's hook pollutes the defaults, later tests that assume default behavior may pass with widened defaults and miss real regressions.
- Long-running servers (SSR, edge functions) that reuse a single DOMPurify instance — pollution accumulates over the process lifetime.
Why the existing maintainer defense for ADD_TAGS doesn't catch this
src/purify.ts:696-700 already documents awareness:
} else if (arrayIsArray(cfg.ADD_TAGS)) {
if (ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
ALLOWED_TAGS = clone(ALLOWED_TAGS);
}
addToSet(ALLOWED_TAGS, cfg.ADD_TAGS, transformCaseFunc);
}
The clone-before-mutate pattern is exactly what's needed at the hook callsites (:1206-1209 and :1494-1500) but was not extended there. The new entries this report's bypass adds to the defaults survive the same way ADD_TAGS array entries would have survived before that fix landed.
Suggested fix
Three minimal-impact options, in order of preference:
- Hand the hook a defensive copy (most surgical):
_executeHooks(hooks.uponSanitizeElement, currentNode, {
tagName,
allowedTags: { ...ALLOWED_TAGS }, // shallow copy; mutations stay scoped
});
Doc note: "data.allowedTags is a snapshot; to widen the live set, use cfg.ADD_TAGS or set the value to true in the snapshot and check the snapshot from a subsequent attribute hook." Hooks that read it for inspection still work; hooks that intended cross-call mutation must be rewritten to use a proper config path (which is the correct API anyway).
- Clone-on-write inside the hook path, mirroring the existing
ADD_TAGSdefense at:696-700: detect thatALLOWED_TAGS === DEFAULT_ALLOWED_TAGSafter the hook returns, and if so, replace it with a clone for subsequent processing. This preserves the live-mutation semantics for in-call effects while preventing cross-call leakage.
- Lazy-clone `ALLOWED_TAGS`/`ALLOWED_ATTR` from defaults on first mutation: install a Proxy or accessor that triggers a clone before mutation. Largest surface area, but bulletproof.
Option (1) is the cleanest API contract: hook event objects should be event-local, never references to library-internal state.