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

Function _sanitizeShadowDOM

src/purify.ts:2363–2412  ·  view source on GitHub ↗
(fragment: DocumentFragment)

Source from the content-addressed store, hash-verified

2361 * @param fragment to iterate over recursively
2362 */
2363 const _sanitizeShadowDOM = function (fragment: DocumentFragment): void {
2364 let shadowNode = null;
2365 const shadowIterator = _createNodeIterator(fragment);
2366
2367 /* Execute a hook if present */
2368 _executeHooks(hooks.beforeSanitizeShadowDOM, fragment, null);
2369
2370 while ((shadowNode = shadowIterator.nextNode())) {
2371 /* Execute a hook if present */
2372 _executeHooks(hooks.uponSanitizeShadowNode, shadowNode, null);
2373
2374 /* Sanitize tags and elements */
2375 _sanitizeElements(shadowNode, fragment);
2376
2377 /* Check attributes next */
2378 _sanitizeAttributes(shadowNode);
2379
2380 /* Deep shadow DOM detected.
2381 Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType against the
2382 DOCUMENT_FRAGMENT_NODE constant rather than instanceof, so we
2383 recurse into <template>.content from foreign realms too. */
2384 if (_isDocumentFragment(shadowNode.content)) {
2385 _sanitizeShadowDOM(shadowNode.content);
2386 }
2387
2388 /* An element iterated here may itself host an attached
2389 shadow root. The default NodeIterator does not enter shadow
2390 trees, so a shadow root nested inside template.content was
2391 previously reached by no walk at all (the pre-pass at
2392 _sanitizeAttachedShadowRoots descends via childNodes, which
2393 doesn't enter template.content; the template-content recursion
2394 above iterates the content but never inspected shadowRoot).
2395 Walk it explicitly. The nodeType guard avoids reading
2396 shadowRoot off text / comment / CDATA / PI nodes that the
2397 iterator also surfaces. */
2398 const shadowNodeType = getNodeType
2399 ? getNodeType(shadowNode)
2400 : shadowNode.nodeType;
2401 if (shadowNodeType === NODE_TYPE.element) {
2402 const innerSr = getShadowRoot(shadowNode);
2403 if (_isDocumentFragment(innerSr)) {
2404 _sanitizeAttachedShadowRoots(innerSr);
2405 _sanitizeShadowDOM(innerSr);
2406 }
2407 }
2408 }
2409
2410 /* Execute a hook if present */
2411 _executeHooks(hooks.afterSanitizeShadowDOM, fragment, null);
2412 };
2413
2414 /**
2415 * _sanitizeAttachedShadowRoots

Callers 2

createDOMPurifyFunction · 0.85

Calls 6

_createNodeIteratorFunction · 0.85
_executeHooksFunction · 0.85
_sanitizeElementsFunction · 0.85
_sanitizeAttributesFunction · 0.85
_isDocumentFragmentFunction · 0.85

Tested by

no test coverage detected