MCPcopy Create free account
hub / github.com/cure53/DOMPurify / _forceRemove

Function _forceRemove

src/purify.ts:1205–1240  ·  view source on GitHub ↗
(node: Node)

Source from the content-addressed store, hash-verified

1203 * @param node a DOM node
1204 */
1205 const _forceRemove = function (node: Node): void {
1206 arrayPush(DOMPurify.removed, { element: node });
1207
1208 try {
1209 // eslint-disable-next-line unicorn/prefer-dom-node-remove
1210 getParentNode(node).removeChild(node);
1211 } catch (_) {
1212 /* The normal detach failed — this is reached for a parentless node
1213 (getParentNode() is null, so .removeChild throws). Element.prototype
1214 .remove() is itself a spec no-op on a parentless node, so a recorded
1215 "removal" would otherwise hand the caller back an intact,
1216 payload-bearing node (e.g. a detached IN_PLACE root the mXSS canary or
1217 the style-with-element-child rule decided to kill). Fail closed by
1218 throwing — exactly as a clobbered root does at the IN_PLACE entry —
1219 rather than trying to "neutralize" the node via its own methods.
1220 Neutralizing would mean calling getAttributeNames()/removeAttribute()
1221 on the node, both of which a <form> root can clobber via a named child
1222 (and _isClobbered does not even probe getAttributeNames), so the
1223 neutralize step could itself be silently defeated, leaving the payload
1224 intact. A throw touches only the cached, clobber-safe remove() and
1225 getParentNode(). Generalizes GHSA-r47g-fvhr-h676 (clobbered-form root)
1226 to every root-kill reason. REPORT-3.
1227
1228 This lives inside the catch, so it never fires for a normally-removed
1229 in-tree node: those have a parent, removeChild() succeeds, and the
1230 catch is not entered. Only a kept (parentless) root reaches here. */
1231 remove(node);
1232
1233 if (!getParentNode(node)) {
1234 throw typeErrorCreate(
1235 'a node selected for removal could not be detached from its tree ' +
1236 'and cannot be safely returned; refusing to sanitize in place'
1237 );
1238 }
1239 }
1240 };
1241
1242 /**
1243 * _stripAttributeNode

Callers 5

_removeAttributeFunction · 0.85
_sanitizeDisallowedNodeFunction · 0.85
_sanitizeElementsFunction · 0.85
_setAttributeValueFunction · 0.85
createDOMPurifyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected