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