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.
@@ -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
  function _arrayLikeToArray(r, a) {
4
4
  (null == a || a > r.length) && (a = r.length);
@@ -332,11 +332,20 @@ const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
332
332
  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
333
333
  const NODE_TYPE = {
334
334
  element: 1,
335
+ attribute: 2,
335
336
  text: 3,
337
+ cdataSection: 4,
338
+ entityReference: 5,
339
+ // Deprecated
340
+ entityNode: 6,
336
341
  // Deprecated
337
342
  progressingInstruction: 7,
338
343
  comment: 8,
339
- document: 9};
344
+ document: 9,
345
+ documentType: 10,
346
+ documentFragment: 11,
347
+ notation: 12 // Deprecated
348
+ };
340
349
  const getGlobal = function getGlobal() {
341
350
  return typeof window === 'undefined' ? null : window;
342
351
  };
@@ -394,7 +403,7 @@ const _createHooksMap = function _createHooksMap() {
394
403
  function createDOMPurify() {
395
404
  let window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGlobal();
396
405
  const DOMPurify = root => createDOMPurify(root);
397
- DOMPurify.version = '3.4.5';
406
+ DOMPurify.version = '3.4.7';
398
407
  DOMPurify.removed = [];
399
408
  if (!window || !window.document || window.document.nodeType !== NODE_TYPE.document || !window.Element) {
400
409
  // Not running in a browser, provide a factory function
@@ -405,15 +414,15 @@ function createDOMPurify() {
405
414
  let document = window.document;
406
415
  const originalDocument = document;
407
416
  const currentScript = originalDocument.currentScript;
408
- const DocumentFragment = window.DocumentFragment,
409
- HTMLTemplateElement = window.HTMLTemplateElement,
417
+ window.DocumentFragment;
418
+ const HTMLTemplateElement = window.HTMLTemplateElement,
410
419
  Node = window.Node,
411
420
  Element = window.Element,
412
421
  NodeFilter = window.NodeFilter,
413
- _window$NamedNodeMap = window.NamedNodeMap,
414
- NamedNodeMap = _window$NamedNodeMap === void 0 ? window.NamedNodeMap || window.MozNamedAttrMap : _window$NamedNodeMap,
415
- HTMLFormElement = window.HTMLFormElement,
416
- DOMParser = window.DOMParser,
422
+ _window$NamedNodeMap = window.NamedNodeMap;
423
+ _window$NamedNodeMap === void 0 ? window.NamedNodeMap || window.MozNamedAttrMap : _window$NamedNodeMap;
424
+ window.HTMLFormElement;
425
+ const DOMParser = window.DOMParser,
417
426
  trustedTypes = window.trustedTypes;
418
427
  const ElementPrototype = Element.prototype;
419
428
  const cloneNode = lookupGetter(ElementPrototype, 'cloneNode');
@@ -421,7 +430,10 @@ function createDOMPurify() {
421
430
  const getNextSibling = lookupGetter(ElementPrototype, 'nextSibling');
422
431
  const getChildNodes = lookupGetter(ElementPrototype, 'childNodes');
423
432
  const getParentNode = lookupGetter(ElementPrototype, 'parentNode');
433
+ const getShadowRoot = lookupGetter(ElementPrototype, 'shadowRoot');
434
+ const getAttributes = lookupGetter(ElementPrototype, 'attributes');
424
435
  const getNodeType = Node && Node.prototype ? lookupGetter(Node.prototype, 'nodeType') : null;
436
+ const getNodeName = Node && Node.prototype ? lookupGetter(Node.prototype, 'nodeName') : null;
425
437
  // As per issue #47, the web-components registry is inherited by a
426
438
  // new document created via createHTMLDocument. As per the spec
427
439
  // (http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries)
@@ -776,6 +788,21 @@ function createDOMPurify() {
776
788
  emptyHTML = trustedTypesPolicy.createHTML('');
777
789
  }
778
790
  }
791
+ /*
792
+ * Mirror the clone-before-mutate pattern already applied above for
793
+ * cfg.ADD_TAGS / cfg.ADD_ATTR: if any uponSanitize* hook is
794
+ * registered AND the set still points at the default constant,
795
+ * clone it. The hook then mutates the clone (in-call widening
796
+ * still works exactly as documented) and the next default-cfg
797
+ * call rebinds to the untouched original via the reassignment at
798
+ * the top of this function.
799
+ */
800
+ if ((hooks.uponSanitizeElement.length > 0 || hooks.uponSanitizeAttribute.length > 0) && ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
801
+ ALLOWED_TAGS = clone(ALLOWED_TAGS);
802
+ }
803
+ if (hooks.uponSanitizeAttribute.length > 0 && ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
804
+ ALLOWED_ATTR = clone(ALLOWED_ATTR);
805
+ }
779
806
  // Prevent further manipulation of configuration.
780
807
  // Not available in IE8, Safari 5, etc.
781
808
  if (freeze) {
@@ -1012,11 +1039,74 @@ function createDOMPurify() {
1012
1039
  /**
1013
1040
  * _isClobbered
1014
1041
  *
1042
+ * Detect DOM-clobbering on HTMLFormElement nodes. Form is the only HTML
1043
+ * interface with [LegacyOverrideBuiltIns]; a descendant element with a
1044
+ * `name` attribute matching a prototype property shadows that property
1045
+ * on direct reads. We use this check at the IN_PLACE entry-point and
1046
+ * during attribute sanitization to refuse clobbered forms.
1047
+ *
1015
1048
  * @param element element to check for clobbering attacks
1016
1049
  * @return true if clobbered, false if safe
1017
1050
  */
1018
1051
  const _isClobbered = function _isClobbered(element) {
1019
- 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');
1052
+ // Realm-independent tag-name probe. If we can't determine the tag
1053
+ // name at all, we can't reason about clobbering — return false
1054
+ // (the caller's other defences still apply).
1055
+ const realTagName = getNodeName ? getNodeName(element) : null;
1056
+ if (typeof realTagName !== 'string') {
1057
+ return false;
1058
+ }
1059
+ if (transformCaseFunc(realTagName) !== 'form') {
1060
+ return false;
1061
+ }
1062
+ return typeof element.nodeName !== 'string' || typeof element.textContent !== 'string' || typeof element.removeChild !== 'function' ||
1063
+ // Realm-safe NamedNodeMap detection: equality against the cached
1064
+ // prototype getter. Clobbered .attributes (e.g. <input name="attributes">)
1065
+ // makes the direct read diverge from the cached read; a clean form
1066
+ // (same-realm OR foreign-realm) has both reads pointing at the same
1067
+ // canonical NamedNodeMap.
1068
+ element.attributes !== getAttributes(element) || typeof element.removeAttribute !== 'function' || typeof element.setAttribute !== 'function' || typeof element.namespaceURI !== 'string' || typeof element.insertBefore !== 'function' || typeof element.hasChildNodes !== 'function' ||
1069
+ // NodeType clobbering probe. Cached Node.prototype.nodeType getter
1070
+ // returns the integer 1 for any Element regardless of realm; direct
1071
+ // read on a clobbered form (e.g. <input name="nodeType">) returns
1072
+ // the named child element. Cheap addition — nodeType is read from
1073
+ // an internal slot, no serialization cost — and removes a residual
1074
+ // clobbering surface used by several mXSS / PI / comment branches
1075
+ // in _sanitizeElements that compare currentNode.nodeType directly.
1076
+ element.nodeType !== getNodeType(element) ||
1077
+ // HTMLFormElement has [LegacyOverrideBuiltIns]: a descendant named
1078
+ // "childNodes" shadows the prototype getter. Direct reads of
1079
+ // form.childNodes from a clobbered form return the named child
1080
+ // instead of the real NodeList, so any walk that reads it directly
1081
+ // skips the form's real children. Compare the direct read to the
1082
+ // cached Node.prototype getter — when the form's named-property
1083
+ // getter intercepts the read, the two values differ and we flag
1084
+ // the form. This catches every clobbering child type (input,
1085
+ // select, etc.) regardless of whether the named child happens to
1086
+ // carry a numeric .length, which a typeof-based probe would miss
1087
+ // (e.g. HTMLSelectElement.length is a defined unsigned-long).
1088
+ element.childNodes !== getChildNodes(element);
1089
+ };
1090
+ /**
1091
+ * Checks whether the given value is a DocumentFragment from any realm.
1092
+ *
1093
+ * The realm-independent replacement reads `nodeType` through the cached
1094
+ * Node.prototype getter and compares to the DOCUMENT_FRAGMENT_NODE
1095
+ * constant (11). nodeType is a numeric value resolved from the node's
1096
+ * internal slot, identical across realms for the same kind of node.
1097
+ *
1098
+ * @param value object to check
1099
+ * @return true if value is a DocumentFragment-shaped node from any realm
1100
+ */
1101
+ const _isDocumentFragment = function _isDocumentFragment(value) {
1102
+ if (!getNodeType || typeof value !== 'object' || value === null) {
1103
+ return false;
1104
+ }
1105
+ try {
1106
+ return getNodeType(value) === NODE_TYPE.documentFragment;
1107
+ } catch (_) {
1108
+ return false;
1109
+ }
1020
1110
  };
1021
1111
  /**
1022
1112
  * Checks whether the given object is a DOM node, including nodes that
@@ -1026,12 +1116,6 @@ function createDOMPurify() {
1026
1116
  * sanitize() to silently stringify them and reset IN_PLACE to false,
1027
1117
  * returning the original node unsanitized. See GHSA-4w3q-35jp-p934.
1028
1118
  *
1029
- * Implementation: call the cached `nodeType` getter from Node.prototype
1030
- * directly on the value. This bypasses any clobbered instance property
1031
- * (e.g. a child element named "nodeType") and works across realms
1032
- * because the WebIDL `nodeType` getter reads an internal slot that
1033
- * every real Node has, regardless of which window minted it.
1034
- *
1035
1119
  * @param value object to check whether it's a DOM node
1036
1120
  * @return true if value is a DOM node from any realm
1037
1121
  */
@@ -1106,10 +1190,17 @@ function createDOMPurify() {
1106
1190
  return false;
1107
1191
  }
1108
1192
  }
1109
- /* Keep content except for bad-listed elements */
1193
+ /* Keep content except for bad-listed elements.
1194
+ Use the cached prototype getters exclusively — the previous code
1195
+ had `|| currentNode.parentNode` / `|| currentNode.childNodes`
1196
+ fallbacks, but the cached getters always return the canonical
1197
+ value (or null for a real parent-less node), so the fallback
1198
+ path was dead in safe cases and a clobbering surface in unsafe
1199
+ ones. Falsy cached results stay falsy; the `if (childNodes &&
1200
+ parentNode)` check already gates correctly. */
1110
1201
  if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
1111
- const parentNode = getParentNode(currentNode) || currentNode.parentNode;
1112
- const childNodes = getChildNodes(currentNode) || currentNode.childNodes;
1202
+ const parentNode = getParentNode(currentNode);
1203
+ const childNodes = getChildNodes(currentNode);
1113
1204
  if (childNodes && parentNode) {
1114
1205
  const childCount = childNodes.length;
1115
1206
  for (let i = childCount - 1; i >= 0; --i) {
@@ -1121,8 +1212,14 @@ function createDOMPurify() {
1121
1212
  _forceRemove(currentNode);
1122
1213
  return true;
1123
1214
  }
1124
- /* Check whether element has a valid namespace */
1125
- if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
1215
+ /* Check whether element has a valid namespace.
1216
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use the cached Node.prototype
1217
+ nodeType getter rather than `instanceof Element`, which is realm-
1218
+ bound and short-circuits to false for any node minted in a different
1219
+ realm — letting a foreign-realm element with a forbidden namespace
1220
+ slip past the namespace check entirely. */
1221
+ const nt = getNodeType ? getNodeType(currentNode) : currentNode.nodeType;
1222
+ if (nt === NODE_TYPE.element && !_checkValidNamespace(currentNode)) {
1126
1223
  _forceRemove(currentNode);
1127
1224
  return true;
1128
1225
  }
@@ -1349,10 +1446,31 @@ function createDOMPurify() {
1349
1446
  _sanitizeElements(shadowNode);
1350
1447
  /* Check attributes next */
1351
1448
  _sanitizeAttributes(shadowNode);
1352
- /* Deep shadow DOM detected */
1353
- if (shadowNode.content instanceof DocumentFragment) {
1449
+ /* Deep shadow DOM detected.
1450
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType against the
1451
+ DOCUMENT_FRAGMENT_NODE constant rather than instanceof, so we
1452
+ recurse into <template>.content from foreign realms too. */
1453
+ if (_isDocumentFragment(shadowNode.content)) {
1354
1454
  _sanitizeShadowDOM2(shadowNode.content);
1355
1455
  }
1456
+ /* An element iterated here may itself host an attached
1457
+ shadow root. The default NodeIterator does not enter shadow
1458
+ trees, so a shadow root nested inside template.content was
1459
+ previously reached by no walk at all (the pre-pass at
1460
+ _sanitizeAttachedShadowRoots descends via childNodes, which
1461
+ doesn't enter template.content; the template-content recursion
1462
+ above iterates the content but never inspected shadowRoot).
1463
+ Walk it explicitly. The nodeType guard avoids reading
1464
+ shadowRoot off text / comment / CDATA / PI nodes that the
1465
+ iterator also surfaces. */
1466
+ const shadowNodeType = getNodeType ? getNodeType(shadowNode) : shadowNode.nodeType;
1467
+ if (shadowNodeType === NODE_TYPE.element) {
1468
+ const innerSr = getShadowRoot ? getShadowRoot(shadowNode) : shadowNode.shadowRoot;
1469
+ if (_isDocumentFragment(innerSr)) {
1470
+ _sanitizeAttachedShadowRoots2(innerSr);
1471
+ _sanitizeShadowDOM2(innerSr);
1472
+ }
1473
+ }
1356
1474
  }
1357
1475
  /* Execute a hook if present */
1358
1476
  _executeHooks(hooks.afterSanitizeShadowDOM, fragment, null);
@@ -1377,18 +1495,28 @@ function createDOMPurify() {
1377
1495
  * @param root the subtree root to walk for attached shadow roots
1378
1496
  */
1379
1497
  const _sanitizeAttachedShadowRoots2 = function _sanitizeAttachedShadowRoots(root) {
1380
- if (root.nodeType === NODE_TYPE.element && root.shadowRoot instanceof DocumentFragment) {
1381
- const sr = root.shadowRoot;
1382
- // Recurse first so that nested shadow roots are reached even if
1383
- // _sanitizeShadowDOM removes hosts at this level.
1384
- _sanitizeAttachedShadowRoots2(sr);
1385
- _sanitizeShadowDOM2(sr);
1498
+ const nodeType = getNodeType ? getNodeType(root) : root.nodeType;
1499
+ if (nodeType === NODE_TYPE.element) {
1500
+ const sr = getShadowRoot ? getShadowRoot(root) : root.shadowRoot;
1501
+ // Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType-based
1502
+ // detection rather than `instanceof DocumentFragment`, which is
1503
+ // realm-bound and silently skipped shadow roots whose host element
1504
+ // belonged to a foreign realm (e.g. iframe.contentDocument
1505
+ // attachShadow). A foreign-realm ShadowRoot extends the foreign
1506
+ // realm's DocumentFragment, not ours, so the old instanceof check
1507
+ // returned false and the shadow subtree was never walked.
1508
+ if (_isDocumentFragment(sr)) {
1509
+ // Recurse first so that nested shadow roots are reached even if
1510
+ // _sanitizeShadowDOM removes hosts at this level.
1511
+ _sanitizeAttachedShadowRoots2(sr);
1512
+ _sanitizeShadowDOM2(sr);
1513
+ }
1386
1514
  }
1387
1515
  // Snapshot children before recursing. Sanitization of one subtree
1388
1516
  // (e.g. via an uponSanitizeShadowNode hook) may detach siblings,
1389
1517
  // and naive nextSibling traversal would silently skip the rest of
1390
1518
  // the list once a node is detached.
1391
- const childNodes = root.childNodes;
1519
+ const childNodes = getChildNodes ? getChildNodes(root) : root.childNodes;
1392
1520
  if (!childNodes) {
1393
1521
  return;
1394
1522
  }
@@ -1399,6 +1527,16 @@ function createDOMPurify() {
1399
1527
  for (const child of snapshot) {
1400
1528
  _sanitizeAttachedShadowRoots2(child);
1401
1529
  }
1530
+ /* When the root is a <template>, also descend into root.content */
1531
+ if (nodeType === NODE_TYPE.element) {
1532
+ const rootName = getNodeName ? getNodeName(root) : null;
1533
+ if (typeof rootName === 'string' && transformCaseFunc(rootName) === 'template') {
1534
+ const content = root.content;
1535
+ if (_isDocumentFragment(content)) {
1536
+ _sanitizeAttachedShadowRoots2(content);
1537
+ }
1538
+ }
1539
+ }
1402
1540
  };
1403
1541
  // eslint-disable-next-line complexity
1404
1542
  DOMPurify.sanitize = function (dirty) {
@@ -1436,14 +1574,31 @@ function createDOMPurify() {
1436
1574
  IN_PLACE = false;
1437
1575
  }
1438
1576
  if (IN_PLACE) {
1439
- /* Do some early pre-sanitization to avoid unsafe root nodes */
1440
- const nn = dirty.nodeName;
1577
+ /* Do some early pre-sanitization to avoid unsafe root nodes.
1578
+ Read nodeName through the cached prototype getter — a clobbering
1579
+ child named "nodeName" on the form root would otherwise shadow
1580
+ the property and let this check skip the root-allowlist
1581
+ validation entirely. */
1582
+ const nn = getNodeName ? getNodeName(dirty) : dirty.nodeName;
1441
1583
  if (typeof nn === 'string') {
1442
1584
  const tagName = transformCaseFunc(nn);
1443
1585
  if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
1444
1586
  throw typeErrorCreate('root node is forbidden and cannot be sanitized in-place');
1445
1587
  }
1446
1588
  }
1589
+ /* Pre-flight the root through _isClobbered. The iterator-driven
1590
+ removal path can not detach a parent-less root: _forceRemove
1591
+ falls through to Element.prototype.remove(), which per spec
1592
+ is a no-op on a node with no parent. A clobbered root would
1593
+ then survive the main loop with its attributes uninspected,
1594
+ because _sanitizeAttributes early-returns on _isClobbered. The
1595
+ result would be an attacker-controlled form, complete with any
1596
+ event-handler attributes the caller passed in, handed back to
1597
+ the application unsanitized. Refuse to sanitize such a root
1598
+ the same way we refuse a forbidden tag. GHSA-r47g-fvhr-h676. */
1599
+ if (_isClobbered(dirty)) {
1600
+ throw typeErrorCreate('root node is clobbered and cannot be sanitized in-place');
1601
+ }
1447
1602
  /* Sanitize attached shadow roots before the main iterator runs.
1448
1603
  The iterator does not descend into shadow trees. */
1449
1604
  _sanitizeAttachedShadowRoots2(dirty);
@@ -1463,7 +1618,9 @@ function createDOMPurify() {
1463
1618
  }
1464
1619
  /* Clonable shadow roots are deep-cloned by importNode(); sanitize
1465
1620
  them before the main iterator runs, since the iterator does not
1466
- descend into shadow trees. */
1621
+ descend into shadow trees. The walk routes every read through a
1622
+ cached prototype getter so clobbering descendants on a form root
1623
+ cannot hide a shadow host from this pass. */
1467
1624
  _sanitizeAttachedShadowRoots2(importedNode);
1468
1625
  } else {
1469
1626
  /* Exit directly if we have nothing to do */
@@ -1491,8 +1648,11 @@ function createDOMPurify() {
1491
1648
  _sanitizeElements(currentNode);
1492
1649
  /* Check attributes next */
1493
1650
  _sanitizeAttributes(currentNode);
1494
- /* Shadow DOM detected, sanitize it */
1495
- if (currentNode.content instanceof DocumentFragment) {
1651
+ /* Shadow DOM detected, sanitize it.
1652
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
1653
+ instead of instanceof, so foreign-realm <template>.content is
1654
+ walked correctly. */
1655
+ if (_isDocumentFragment(currentNode.content)) {
1496
1656
  _sanitizeShadowDOM2(currentNode.content);
1497
1657
  }
1498
1658
  }