dompurify 3.4.5 → 3.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  DOMPurify is a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG.
8
8
 
9
- It's also very simple to use and get started with. DOMPurify was [started in February 2014](https://github.com/cure53/DOMPurify/commit/a630922616927373485e0e787ab19e73e3691b2b) and, meanwhile, has reached version **v3.4.5**.
9
+ It's also very simple to use and get started with. DOMPurify was [started in February 2014](https://github.com/cure53/DOMPurify/commit/a630922616927373485e0e787ab19e73e3691b2b) and, meanwhile, has reached version **v3.4.7**.
10
10
 
11
11
  DOMPurify runs as JavaScript and works in all modern browsers (Safari (10+), Opera (15+), Edge, Firefox and Chrome - as well as almost anything else using Blink, Gecko or WebKit). It doesn't break on MSIE or other legacy browsers. It simply does nothing.
12
12
 
@@ -519,4 +519,4 @@ Feature releases will not be announced to this list.
519
519
 
520
520
  Many people have helped DOMPurify become what it is today, and they deserve to be acknowledged!
521
521
 
522
- [lukewarlow](https://github.com/lukewarlow), [DEMON1A](https://github.com/DEMON1A), [fg0x0](https://github.com/fg0x0), [kodareef5](https://github.com/kodareef5), [DavidOliver](https://github.com/DavidOliver), [1Jesper1](https://github.com/1Jesper1), [bencalif](https://github.com/bencalif), [trace37labs](https://github.com/trace37labs), [eddieran](https://github.com/eddieran), [christos-eth](https://github.com/christos-eth), [researchatfluidattacks](https://github.com/researchatfluidattacks), [frevadiscor](https://github.com/frevadiscor), [Rotzbua](https://github.com/Rotzbua), [binhpv](https://github.com/binhpv), [MariusRumpf](https://github.com/MariusRumpf), [prasadrajandran](https://github.com/prasadrajandran), [Cybozu 💛💸](https://github.com/cybozu), [hata6502 💸](https://github.com/hata6502), [openclaw 💸](https://github.com/openclaw), [intra-mart-dh 💸](https://github.com/intra-mart-dh), [nelstrom ❤️](https://github.com/nelstrom), [hash_kitten ❤️](https://twitter.com/hash_kitten), [kevin_mizu ❤️](https://twitter.com/kevin_mizu), [icesfont ❤️](https://github.com/icesfont), [reduckted ❤️](https://github.com/reduckted), [dcramer 💸](https://github.com/dcramer), [JGraph 💸](https://github.com/jgraph), [baekilda 💸](https://github.com/baekilda), [Healthchecks 💸](https://github.com/healthchecks), [Sentry 💸](https://github.com/getsentry), [jarrodldavis 💸](https://github.com/jarrodldavis), [CynegeticIO](https://github.com/CynegeticIO), [ssi02014 ❤️](https://github.com/ssi02014), [GrantGryczan](https://github.com/GrantGryczan), [Lowdefy](https://twitter.com/lowdefy), [granlem](https://twitter.com/MaximeVeit), [oreoshake](https://github.com/oreoshake), [tdeekens ❤️](https://github.com/tdeekens), [peernohell ❤️](https://github.com/peernohell), [is2ei](https://github.com/is2ei), [SoheilKhodayari](https://github.com/SoheilKhodayari), [franktopel](https://github.com/franktopel), [NateScarlet](https://github.com/NateScarlet), [neilj](https://github.com/neilj), [fhemberger](https://github.com/fhemberger), [Joris-van-der-Wel](https://github.com/Joris-van-der-Wel), [ydaniv](https://github.com/ydaniv), [terjanq](https://twitter.com/terjanq), [filedescriptor](https://github.com/filedescriptor), [ConradIrwin](https://github.com/ConradIrwin), [gibson042](https://github.com/gibson042), [choumx](https://github.com/choumx), [0xSobky](https://github.com/0xSobky), [styfle](https://github.com/styfle), [koto](https://github.com/koto), [tlau88](https://github.com/tlau88), [strugee](https://github.com/strugee), [oparoz](https://github.com/oparoz), [mathiasbynens](https://github.com/mathiasbynens), [edg2s](https://github.com/edg2s), [dnkolegov](https://github.com/dnkolegov), [dhardtke](https://github.com/dhardtke), [wirehead](https://github.com/wirehead), [thorn0](https://github.com/thorn0), [styu](https://github.com/styu), [mozfreddyb](https://github.com/mozfreddyb), [mikesamuel](https://github.com/mikesamuel), [jorangreef](https://github.com/jorangreef), [jimmyhchan](https://github.com/jimmyhchan), [jameydeorio](https://github.com/jameydeorio), [jameskraus](https://github.com/jameskraus), [hyderali](https://github.com/hyderali), [hansottowirtz](https://github.com/hansottowirtz), [hackvertor](https://github.com/hackvertor), [freddyb](https://github.com/freddyb), [flavorjones](https://github.com/flavorjones), [djfarrelly](https://github.com/djfarrelly), [devd](https://github.com/devd), [camerondunford](https://github.com/camerondunford), [buu700](https://github.com/buu700), [buildog](https://github.com/buildog), [alabiaga](https://github.com/alabiaga), [Vector919](https://github.com/Vector919), [Robbert](https://github.com/Robbert), [GreLI](https://github.com/GreLI), [FuzzySockets](https://github.com/FuzzySockets), [ArtemBernatskyy](https://github.com/ArtemBernatskyy), [@garethheyes](https://twitter.com/garethheyes), [@shafigullin](https://twitter.com/shafigullin), [@mmrupp](https://twitter.com/mmrupp), [@irsdl](https://twitter.com/irsdl),[ShikariSenpai](https://github.com/ShikariSenpai), [ansjdnakjdnajkd](https://github.com/ansjdnakjdnajkd), [@asutherland](https://twitter.com/asutherland), [@mathias](https://twitter.com/mathias), [@cgvwzq](https://twitter.com/cgvwzq), [@robbertatwork](https://twitter.com/robbertatwork), [@giutro](https://twitter.com/giutro), [@CmdEngineer\_](https://twitter.com/CmdEngineer_), [@avr4mit](https://twitter.com/avr4mit), [davecardwell](https://github.com/davecardwell) and especially [@securitymb ❤️](https://twitter.com/securitymb) & [@masatokinugawa ❤️](https://twitter.com/masatokinugawa)
522
+ [offset](https://github.com/offset), [Bankde](https://github.com/Bankde), [lukewarlow](https://github.com/lukewarlow), [DEMON1A](https://github.com/DEMON1A), [fg0x0](https://github.com/fg0x0), [kodareef5](https://github.com/kodareef5), [DavidOliver](https://github.com/DavidOliver), [1Jesper1](https://github.com/1Jesper1), [bencalif](https://github.com/bencalif), [trace37labs](https://github.com/trace37labs), [eddieran](https://github.com/eddieran), [christos-eth](https://github.com/christos-eth), [researchatfluidattacks](https://github.com/researchatfluidattacks), [frevadiscor](https://github.com/frevadiscor), [Rotzbua](https://github.com/Rotzbua), [binhpv](https://github.com/binhpv), [MariusRumpf](https://github.com/MariusRumpf), [prasadrajandran](https://github.com/prasadrajandran), [Cybozu 💛💸](https://github.com/cybozu), [hata6502 💸](https://github.com/hata6502), [openclaw 💸](https://github.com/openclaw), [intra-mart-dh 💸](https://github.com/intra-mart-dh), [nelstrom ❤️](https://github.com/nelstrom), [hash_kitten ❤️](https://twitter.com/hash_kitten), [kevin_mizu ❤️](https://twitter.com/kevin_mizu), [icesfont ❤️](https://github.com/icesfont), [reduckted ❤️](https://github.com/reduckted), [dcramer 💸](https://github.com/dcramer), [JGraph 💸](https://github.com/jgraph), [baekilda 💸](https://github.com/baekilda), [Healthchecks 💸](https://github.com/healthchecks), [Sentry 💸](https://github.com/getsentry), [jarrodldavis 💸](https://github.com/jarrodldavis), [CynegeticIO](https://github.com/CynegeticIO), [ssi02014 ❤️](https://github.com/ssi02014), [GrantGryczan](https://github.com/GrantGryczan), [Lowdefy](https://twitter.com/lowdefy), [granlem](https://twitter.com/MaximeVeit), [oreoshake](https://github.com/oreoshake), [tdeekens ❤️](https://github.com/tdeekens), [peernohell ❤️](https://github.com/peernohell), [is2ei](https://github.com/is2ei), [SoheilKhodayari](https://github.com/SoheilKhodayari), [franktopel](https://github.com/franktopel), [NateScarlet](https://github.com/NateScarlet), [neilj](https://github.com/neilj), [fhemberger](https://github.com/fhemberger), [Joris-van-der-Wel](https://github.com/Joris-van-der-Wel), [ydaniv](https://github.com/ydaniv), [terjanq](https://twitter.com/terjanq), [filedescriptor](https://github.com/filedescriptor), [ConradIrwin](https://github.com/ConradIrwin), [gibson042](https://github.com/gibson042), [choumx](https://github.com/choumx), [0xSobky](https://github.com/0xSobky), [styfle](https://github.com/styfle), [koto](https://github.com/koto), [tlau88](https://github.com/tlau88), [strugee](https://github.com/strugee), [oparoz](https://github.com/oparoz), [mathiasbynens](https://github.com/mathiasbynens), [edg2s](https://github.com/edg2s), [dnkolegov](https://github.com/dnkolegov), [dhardtke](https://github.com/dhardtke), [wirehead](https://github.com/wirehead), [thorn0](https://github.com/thorn0), [styu](https://github.com/styu), [mozfreddyb](https://github.com/mozfreddyb), [mikesamuel](https://github.com/mikesamuel), [jorangreef](https://github.com/jorangreef), [jimmyhchan](https://github.com/jimmyhchan), [jameydeorio](https://github.com/jameydeorio), [jameskraus](https://github.com/jameskraus), [hyderali](https://github.com/hyderali), [hansottowirtz](https://github.com/hansottowirtz), [hackvertor](https://github.com/hackvertor), [freddyb](https://github.com/freddyb), [flavorjones](https://github.com/flavorjones), [djfarrelly](https://github.com/djfarrelly), [devd](https://github.com/devd), [camerondunford](https://github.com/camerondunford), [buu700](https://github.com/buu700), [buildog](https://github.com/buildog), [alabiaga](https://github.com/alabiaga), [Vector919](https://github.com/Vector919), [Robbert](https://github.com/Robbert), [GreLI](https://github.com/GreLI), [FuzzySockets](https://github.com/FuzzySockets), [ArtemBernatskyy](https://github.com/ArtemBernatskyy), [@garethheyes](https://twitter.com/garethheyes), [@shafigullin](https://twitter.com/shafigullin), [@mmrupp](https://twitter.com/mmrupp), [@irsdl](https://twitter.com/irsdl),[ShikariSenpai](https://github.com/ShikariSenpai), [ansjdnakjdnajkd](https://github.com/ansjdnakjdnajkd), [@asutherland](https://twitter.com/asutherland), [@mathias](https://twitter.com/mathias), [@cgvwzq](https://twitter.com/cgvwzq), [@robbertatwork](https://twitter.com/robbertatwork), [@giutro](https://twitter.com/giutro), [@CmdEngineer\_](https://twitter.com/CmdEngineer_), [@avr4mit](https://twitter.com/avr4mit), [davecardwell](https://github.com/davecardwell) and especially [@securitymb ❤️](https://twitter.com/securitymb) & [@masatokinugawa ❤️](https://twitter.com/masatokinugawa)
@@ -1,4 +1,4 @@
1
- /*! @license DOMPurify 3.4.5 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.4.5/LICENSE */
1
+ /*! @license DOMPurify 3.4.7 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.4.7/LICENSE */
2
2
 
3
3
  import { TrustedTypePolicy, TrustedTypesWindow, TrustedHTML } from 'trusted-types/lib/index.js';
4
4
 
@@ -1,4 +1,4 @@
1
- /*! @license DOMPurify 3.4.5 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.4.5/LICENSE */
1
+ /*! @license DOMPurify 3.4.7 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.4.7/LICENSE */
2
2
 
3
3
  'use strict';
4
4
 
@@ -334,11 +334,20 @@ const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
334
334
  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
335
335
  const NODE_TYPE = {
336
336
  element: 1,
337
+ attribute: 2,
337
338
  text: 3,
339
+ cdataSection: 4,
340
+ entityReference: 5,
341
+ // Deprecated
342
+ entityNode: 6,
338
343
  // Deprecated
339
344
  progressingInstruction: 7,
340
345
  comment: 8,
341
- document: 9};
346
+ document: 9,
347
+ documentType: 10,
348
+ documentFragment: 11,
349
+ notation: 12 // Deprecated
350
+ };
342
351
  const getGlobal = function getGlobal() {
343
352
  return typeof window === 'undefined' ? null : window;
344
353
  };
@@ -396,7 +405,7 @@ const _createHooksMap = function _createHooksMap() {
396
405
  function createDOMPurify() {
397
406
  let window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGlobal();
398
407
  const DOMPurify = root => createDOMPurify(root);
399
- DOMPurify.version = '3.4.5';
408
+ DOMPurify.version = '3.4.7';
400
409
  DOMPurify.removed = [];
401
410
  if (!window || !window.document || window.document.nodeType !== NODE_TYPE.document || !window.Element) {
402
411
  // Not running in a browser, provide a factory function
@@ -407,15 +416,15 @@ function createDOMPurify() {
407
416
  let document = window.document;
408
417
  const originalDocument = document;
409
418
  const currentScript = originalDocument.currentScript;
410
- const DocumentFragment = window.DocumentFragment,
411
- HTMLTemplateElement = window.HTMLTemplateElement,
419
+ window.DocumentFragment;
420
+ const HTMLTemplateElement = window.HTMLTemplateElement,
412
421
  Node = window.Node,
413
422
  Element = window.Element,
414
423
  NodeFilter = window.NodeFilter,
415
- _window$NamedNodeMap = window.NamedNodeMap,
416
- NamedNodeMap = _window$NamedNodeMap === void 0 ? window.NamedNodeMap || window.MozNamedAttrMap : _window$NamedNodeMap,
417
- HTMLFormElement = window.HTMLFormElement,
418
- DOMParser = window.DOMParser,
424
+ _window$NamedNodeMap = window.NamedNodeMap;
425
+ _window$NamedNodeMap === void 0 ? window.NamedNodeMap || window.MozNamedAttrMap : _window$NamedNodeMap;
426
+ window.HTMLFormElement;
427
+ const DOMParser = window.DOMParser,
419
428
  trustedTypes = window.trustedTypes;
420
429
  const ElementPrototype = Element.prototype;
421
430
  const cloneNode = lookupGetter(ElementPrototype, 'cloneNode');
@@ -423,7 +432,10 @@ function createDOMPurify() {
423
432
  const getNextSibling = lookupGetter(ElementPrototype, 'nextSibling');
424
433
  const getChildNodes = lookupGetter(ElementPrototype, 'childNodes');
425
434
  const getParentNode = lookupGetter(ElementPrototype, 'parentNode');
435
+ const getShadowRoot = lookupGetter(ElementPrototype, 'shadowRoot');
436
+ const getAttributes = lookupGetter(ElementPrototype, 'attributes');
426
437
  const getNodeType = Node && Node.prototype ? lookupGetter(Node.prototype, 'nodeType') : null;
438
+ const getNodeName = Node && Node.prototype ? lookupGetter(Node.prototype, 'nodeName') : null;
427
439
  // As per issue #47, the web-components registry is inherited by a
428
440
  // new document created via createHTMLDocument. As per the spec
429
441
  // (http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries)
@@ -778,6 +790,21 @@ function createDOMPurify() {
778
790
  emptyHTML = trustedTypesPolicy.createHTML('');
779
791
  }
780
792
  }
793
+ /*
794
+ * Mirror the clone-before-mutate pattern already applied above for
795
+ * cfg.ADD_TAGS / cfg.ADD_ATTR: if any uponSanitize* hook is
796
+ * registered AND the set still points at the default constant,
797
+ * clone it. The hook then mutates the clone (in-call widening
798
+ * still works exactly as documented) and the next default-cfg
799
+ * call rebinds to the untouched original via the reassignment at
800
+ * the top of this function.
801
+ */
802
+ if ((hooks.uponSanitizeElement.length > 0 || hooks.uponSanitizeAttribute.length > 0) && ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
803
+ ALLOWED_TAGS = clone(ALLOWED_TAGS);
804
+ }
805
+ if (hooks.uponSanitizeAttribute.length > 0 && ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
806
+ ALLOWED_ATTR = clone(ALLOWED_ATTR);
807
+ }
781
808
  // Prevent further manipulation of configuration.
782
809
  // Not available in IE8, Safari 5, etc.
783
810
  if (freeze) {
@@ -1014,11 +1041,74 @@ function createDOMPurify() {
1014
1041
  /**
1015
1042
  * _isClobbered
1016
1043
  *
1044
+ * Detect DOM-clobbering on HTMLFormElement nodes. Form is the only HTML
1045
+ * interface with [LegacyOverrideBuiltIns]; a descendant element with a
1046
+ * `name` attribute matching a prototype property shadows that property
1047
+ * on direct reads. We use this check at the IN_PLACE entry-point and
1048
+ * during attribute sanitization to refuse clobbered forms.
1049
+ *
1017
1050
  * @param element element to check for clobbering attacks
1018
1051
  * @return true if clobbered, false if safe
1019
1052
  */
1020
1053
  const _isClobbered = function _isClobbered(element) {
1021
- return element instanceof HTMLFormElement && (typeof element.nodeName !== 'string' || typeof element.textContent !== 'string' || typeof element.removeChild !== 'function' || !(element.attributes instanceof NamedNodeMap) || typeof element.removeAttribute !== 'function' || typeof element.setAttribute !== 'function' || typeof element.namespaceURI !== 'string' || typeof element.insertBefore !== 'function' || typeof element.hasChildNodes !== 'function');
1054
+ // Realm-independent tag-name probe. If we can't determine the tag
1055
+ // name at all, we can't reason about clobbering — return false
1056
+ // (the caller's other defences still apply).
1057
+ const realTagName = getNodeName ? getNodeName(element) : null;
1058
+ if (typeof realTagName !== 'string') {
1059
+ return false;
1060
+ }
1061
+ if (transformCaseFunc(realTagName) !== 'form') {
1062
+ return false;
1063
+ }
1064
+ return typeof element.nodeName !== 'string' || typeof element.textContent !== 'string' || typeof element.removeChild !== 'function' ||
1065
+ // Realm-safe NamedNodeMap detection: equality against the cached
1066
+ // prototype getter. Clobbered .attributes (e.g. <input name="attributes">)
1067
+ // makes the direct read diverge from the cached read; a clean form
1068
+ // (same-realm OR foreign-realm) has both reads pointing at the same
1069
+ // canonical NamedNodeMap.
1070
+ element.attributes !== getAttributes(element) || typeof element.removeAttribute !== 'function' || typeof element.setAttribute !== 'function' || typeof element.namespaceURI !== 'string' || typeof element.insertBefore !== 'function' || typeof element.hasChildNodes !== 'function' ||
1071
+ // NodeType clobbering probe. Cached Node.prototype.nodeType getter
1072
+ // returns the integer 1 for any Element regardless of realm; direct
1073
+ // read on a clobbered form (e.g. <input name="nodeType">) returns
1074
+ // the named child element. Cheap addition — nodeType is read from
1075
+ // an internal slot, no serialization cost — and removes a residual
1076
+ // clobbering surface used by several mXSS / PI / comment branches
1077
+ // in _sanitizeElements that compare currentNode.nodeType directly.
1078
+ element.nodeType !== getNodeType(element) ||
1079
+ // HTMLFormElement has [LegacyOverrideBuiltIns]: a descendant named
1080
+ // "childNodes" shadows the prototype getter. Direct reads of
1081
+ // form.childNodes from a clobbered form return the named child
1082
+ // instead of the real NodeList, so any walk that reads it directly
1083
+ // skips the form's real children. Compare the direct read to the
1084
+ // cached Node.prototype getter — when the form's named-property
1085
+ // getter intercepts the read, the two values differ and we flag
1086
+ // the form. This catches every clobbering child type (input,
1087
+ // select, etc.) regardless of whether the named child happens to
1088
+ // carry a numeric .length, which a typeof-based probe would miss
1089
+ // (e.g. HTMLSelectElement.length is a defined unsigned-long).
1090
+ element.childNodes !== getChildNodes(element);
1091
+ };
1092
+ /**
1093
+ * Checks whether the given value is a DocumentFragment from any realm.
1094
+ *
1095
+ * The realm-independent replacement reads `nodeType` through the cached
1096
+ * Node.prototype getter and compares to the DOCUMENT_FRAGMENT_NODE
1097
+ * constant (11). nodeType is a numeric value resolved from the node's
1098
+ * internal slot, identical across realms for the same kind of node.
1099
+ *
1100
+ * @param value object to check
1101
+ * @return true if value is a DocumentFragment-shaped node from any realm
1102
+ */
1103
+ const _isDocumentFragment = function _isDocumentFragment(value) {
1104
+ if (!getNodeType || typeof value !== 'object' || value === null) {
1105
+ return false;
1106
+ }
1107
+ try {
1108
+ return getNodeType(value) === NODE_TYPE.documentFragment;
1109
+ } catch (_) {
1110
+ return false;
1111
+ }
1022
1112
  };
1023
1113
  /**
1024
1114
  * Checks whether the given object is a DOM node, including nodes that
@@ -1028,12 +1118,6 @@ function createDOMPurify() {
1028
1118
  * sanitize() to silently stringify them and reset IN_PLACE to false,
1029
1119
  * returning the original node unsanitized. See GHSA-4w3q-35jp-p934.
1030
1120
  *
1031
- * Implementation: call the cached `nodeType` getter from Node.prototype
1032
- * directly on the value. This bypasses any clobbered instance property
1033
- * (e.g. a child element named "nodeType") and works across realms
1034
- * because the WebIDL `nodeType` getter reads an internal slot that
1035
- * every real Node has, regardless of which window minted it.
1036
- *
1037
1121
  * @param value object to check whether it's a DOM node
1038
1122
  * @return true if value is a DOM node from any realm
1039
1123
  */
@@ -1108,10 +1192,17 @@ function createDOMPurify() {
1108
1192
  return false;
1109
1193
  }
1110
1194
  }
1111
- /* Keep content except for bad-listed elements */
1195
+ /* Keep content except for bad-listed elements.
1196
+ Use the cached prototype getters exclusively — the previous code
1197
+ had `|| currentNode.parentNode` / `|| currentNode.childNodes`
1198
+ fallbacks, but the cached getters always return the canonical
1199
+ value (or null for a real parent-less node), so the fallback
1200
+ path was dead in safe cases and a clobbering surface in unsafe
1201
+ ones. Falsy cached results stay falsy; the `if (childNodes &&
1202
+ parentNode)` check already gates correctly. */
1112
1203
  if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
1113
- const parentNode = getParentNode(currentNode) || currentNode.parentNode;
1114
- const childNodes = getChildNodes(currentNode) || currentNode.childNodes;
1204
+ const parentNode = getParentNode(currentNode);
1205
+ const childNodes = getChildNodes(currentNode);
1115
1206
  if (childNodes && parentNode) {
1116
1207
  const childCount = childNodes.length;
1117
1208
  for (let i = childCount - 1; i >= 0; --i) {
@@ -1123,8 +1214,14 @@ function createDOMPurify() {
1123
1214
  _forceRemove(currentNode);
1124
1215
  return true;
1125
1216
  }
1126
- /* Check whether element has a valid namespace */
1127
- if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
1217
+ /* Check whether element has a valid namespace.
1218
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use the cached Node.prototype
1219
+ nodeType getter rather than `instanceof Element`, which is realm-
1220
+ bound and short-circuits to false for any node minted in a different
1221
+ realm — letting a foreign-realm element with a forbidden namespace
1222
+ slip past the namespace check entirely. */
1223
+ const nt = getNodeType ? getNodeType(currentNode) : currentNode.nodeType;
1224
+ if (nt === NODE_TYPE.element && !_checkValidNamespace(currentNode)) {
1128
1225
  _forceRemove(currentNode);
1129
1226
  return true;
1130
1227
  }
@@ -1351,10 +1448,31 @@ function createDOMPurify() {
1351
1448
  _sanitizeElements(shadowNode);
1352
1449
  /* Check attributes next */
1353
1450
  _sanitizeAttributes(shadowNode);
1354
- /* Deep shadow DOM detected */
1355
- if (shadowNode.content instanceof DocumentFragment) {
1451
+ /* Deep shadow DOM detected.
1452
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType against the
1453
+ DOCUMENT_FRAGMENT_NODE constant rather than instanceof, so we
1454
+ recurse into <template>.content from foreign realms too. */
1455
+ if (_isDocumentFragment(shadowNode.content)) {
1356
1456
  _sanitizeShadowDOM2(shadowNode.content);
1357
1457
  }
1458
+ /* An element iterated here may itself host an attached
1459
+ shadow root. The default NodeIterator does not enter shadow
1460
+ trees, so a shadow root nested inside template.content was
1461
+ previously reached by no walk at all (the pre-pass at
1462
+ _sanitizeAttachedShadowRoots descends via childNodes, which
1463
+ doesn't enter template.content; the template-content recursion
1464
+ above iterates the content but never inspected shadowRoot).
1465
+ Walk it explicitly. The nodeType guard avoids reading
1466
+ shadowRoot off text / comment / CDATA / PI nodes that the
1467
+ iterator also surfaces. */
1468
+ const shadowNodeType = getNodeType ? getNodeType(shadowNode) : shadowNode.nodeType;
1469
+ if (shadowNodeType === NODE_TYPE.element) {
1470
+ const innerSr = getShadowRoot ? getShadowRoot(shadowNode) : shadowNode.shadowRoot;
1471
+ if (_isDocumentFragment(innerSr)) {
1472
+ _sanitizeAttachedShadowRoots2(innerSr);
1473
+ _sanitizeShadowDOM2(innerSr);
1474
+ }
1475
+ }
1358
1476
  }
1359
1477
  /* Execute a hook if present */
1360
1478
  _executeHooks(hooks.afterSanitizeShadowDOM, fragment, null);
@@ -1379,18 +1497,28 @@ function createDOMPurify() {
1379
1497
  * @param root the subtree root to walk for attached shadow roots
1380
1498
  */
1381
1499
  const _sanitizeAttachedShadowRoots2 = function _sanitizeAttachedShadowRoots(root) {
1382
- if (root.nodeType === NODE_TYPE.element && root.shadowRoot instanceof DocumentFragment) {
1383
- const sr = root.shadowRoot;
1384
- // Recurse first so that nested shadow roots are reached even if
1385
- // _sanitizeShadowDOM removes hosts at this level.
1386
- _sanitizeAttachedShadowRoots2(sr);
1387
- _sanitizeShadowDOM2(sr);
1500
+ const nodeType = getNodeType ? getNodeType(root) : root.nodeType;
1501
+ if (nodeType === NODE_TYPE.element) {
1502
+ const sr = getShadowRoot ? getShadowRoot(root) : root.shadowRoot;
1503
+ // Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType-based
1504
+ // detection rather than `instanceof DocumentFragment`, which is
1505
+ // realm-bound and silently skipped shadow roots whose host element
1506
+ // belonged to a foreign realm (e.g. iframe.contentDocument
1507
+ // attachShadow). A foreign-realm ShadowRoot extends the foreign
1508
+ // realm's DocumentFragment, not ours, so the old instanceof check
1509
+ // returned false and the shadow subtree was never walked.
1510
+ if (_isDocumentFragment(sr)) {
1511
+ // Recurse first so that nested shadow roots are reached even if
1512
+ // _sanitizeShadowDOM removes hosts at this level.
1513
+ _sanitizeAttachedShadowRoots2(sr);
1514
+ _sanitizeShadowDOM2(sr);
1515
+ }
1388
1516
  }
1389
1517
  // Snapshot children before recursing. Sanitization of one subtree
1390
1518
  // (e.g. via an uponSanitizeShadowNode hook) may detach siblings,
1391
1519
  // and naive nextSibling traversal would silently skip the rest of
1392
1520
  // the list once a node is detached.
1393
- const childNodes = root.childNodes;
1521
+ const childNodes = getChildNodes ? getChildNodes(root) : root.childNodes;
1394
1522
  if (!childNodes) {
1395
1523
  return;
1396
1524
  }
@@ -1401,6 +1529,16 @@ function createDOMPurify() {
1401
1529
  for (const child of snapshot) {
1402
1530
  _sanitizeAttachedShadowRoots2(child);
1403
1531
  }
1532
+ /* When the root is a <template>, also descend into root.content */
1533
+ if (nodeType === NODE_TYPE.element) {
1534
+ const rootName = getNodeName ? getNodeName(root) : null;
1535
+ if (typeof rootName === 'string' && transformCaseFunc(rootName) === 'template') {
1536
+ const content = root.content;
1537
+ if (_isDocumentFragment(content)) {
1538
+ _sanitizeAttachedShadowRoots2(content);
1539
+ }
1540
+ }
1541
+ }
1404
1542
  };
1405
1543
  // eslint-disable-next-line complexity
1406
1544
  DOMPurify.sanitize = function (dirty) {
@@ -1438,14 +1576,31 @@ function createDOMPurify() {
1438
1576
  IN_PLACE = false;
1439
1577
  }
1440
1578
  if (IN_PLACE) {
1441
- /* Do some early pre-sanitization to avoid unsafe root nodes */
1442
- const nn = dirty.nodeName;
1579
+ /* Do some early pre-sanitization to avoid unsafe root nodes.
1580
+ Read nodeName through the cached prototype getter — a clobbering
1581
+ child named "nodeName" on the form root would otherwise shadow
1582
+ the property and let this check skip the root-allowlist
1583
+ validation entirely. */
1584
+ const nn = getNodeName ? getNodeName(dirty) : dirty.nodeName;
1443
1585
  if (typeof nn === 'string') {
1444
1586
  const tagName = transformCaseFunc(nn);
1445
1587
  if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
1446
1588
  throw typeErrorCreate('root node is forbidden and cannot be sanitized in-place');
1447
1589
  }
1448
1590
  }
1591
+ /* Pre-flight the root through _isClobbered. The iterator-driven
1592
+ removal path can not detach a parent-less root: _forceRemove
1593
+ falls through to Element.prototype.remove(), which per spec
1594
+ is a no-op on a node with no parent. A clobbered root would
1595
+ then survive the main loop with its attributes uninspected,
1596
+ because _sanitizeAttributes early-returns on _isClobbered. The
1597
+ result would be an attacker-controlled form, complete with any
1598
+ event-handler attributes the caller passed in, handed back to
1599
+ the application unsanitized. Refuse to sanitize such a root
1600
+ the same way we refuse a forbidden tag. GHSA-r47g-fvhr-h676. */
1601
+ if (_isClobbered(dirty)) {
1602
+ throw typeErrorCreate('root node is clobbered and cannot be sanitized in-place');
1603
+ }
1449
1604
  /* Sanitize attached shadow roots before the main iterator runs.
1450
1605
  The iterator does not descend into shadow trees. */
1451
1606
  _sanitizeAttachedShadowRoots2(dirty);
@@ -1465,7 +1620,9 @@ function createDOMPurify() {
1465
1620
  }
1466
1621
  /* Clonable shadow roots are deep-cloned by importNode(); sanitize
1467
1622
  them before the main iterator runs, since the iterator does not
1468
- descend into shadow trees. */
1623
+ descend into shadow trees. The walk routes every read through a
1624
+ cached prototype getter so clobbering descendants on a form root
1625
+ cannot hide a shadow host from this pass. */
1469
1626
  _sanitizeAttachedShadowRoots2(importedNode);
1470
1627
  } else {
1471
1628
  /* Exit directly if we have nothing to do */
@@ -1493,8 +1650,11 @@ function createDOMPurify() {
1493
1650
  _sanitizeElements(currentNode);
1494
1651
  /* Check attributes next */
1495
1652
  _sanitizeAttributes(currentNode);
1496
- /* Shadow DOM detected, sanitize it */
1497
- if (currentNode.content instanceof DocumentFragment) {
1653
+ /* Shadow DOM detected, sanitize it.
1654
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
1655
+ instead of instanceof, so foreign-realm <template>.content is
1656
+ walked correctly. */
1657
+ if (_isDocumentFragment(currentNode.content)) {
1498
1658
  _sanitizeShadowDOM2(currentNode.content);
1499
1659
  }
1500
1660
  }