The vulnerability lies in DOMPurify's handling of DOM nodes from different JavaScript realms when using the IN_PLACE: true option. The library correctly accepts nodes from foreign realms (like iframes) for sanitization. However, several internal functions responsible for security checks use the instanceof operator to determine the type of a node (e.g., instanceof HTMLFormElement, instanceof DocumentFragment).
The instanceof operator is realm-bound, meaning it checks the object's prototype chain against the constructors of the current realm. When a node from a foreign realm is passed, these instanceof checks return false because the node was constructed in a different realm with its own set of constructors.
This leads to a failure in protection mechanisms. Specifically:
_isClobbered fails to detect clobbered <form> elements, allowing attribute sanitization to be bypassed.
_sanitizeShadowDOM fails to recognize the content of foreign <template> elements as DocumentFragments, skipping their sanitization.
_sanitizeAttachedShadowRoots fails to identify foreign shadow roots as DocumentFragments, skipping the sanitization of the entire shadow DOM.
As a result, attacker-controlled markup within these structures remains intact and can lead to Cross-Site Scripting (XSS) when the sanitized node is used in the application. The patch replaces these realm-bound instanceof checks with realm-agnostic methods, such as checking the nodeType or nodeName properties, to ensure consistent behavior across realms.