dompurify 3.4.4 → 3.4.6

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.4 | (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.4/LICENSE */
1
+ /*! @license DOMPurify 3.4.6 | (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.6/LICENSE */
2
2
 
3
3
  function _arrayLikeToArray(r, a) {
4
4
  (null == a || a > r.length) && (a = r.length);
@@ -296,7 +296,7 @@ function isRegex(value) {
296
296
  }
297
297
  }
298
298
 
299
- const html$1 = freeze(['a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'marquee', 'menu', 'menuitem', 'meter', 'nav', 'nobr', 'ol', 'optgroup', 'option', 'output', 'p', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'search', 'section', 'select', 'selectedcontent', 'shadow', 'slot', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr']);
299
+ const html$1 = freeze(['a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'marquee', 'menu', 'menuitem', 'meter', 'nav', 'nobr', 'ol', 'optgroup', 'option', 'output', 'p', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'search', 'section', 'select', 'shadow', 'slot', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr']);
300
300
  const svg$1 = freeze(['svg', 'a', 'altglyph', 'altglyphdef', 'altglyphitem', 'animatecolor', 'animatemotion', 'animatetransform', 'circle', 'clippath', 'defs', 'desc', 'ellipse', 'enterkeyhint', 'exportparts', 'filter', 'font', 'g', 'glyph', 'glyphref', 'hkern', 'image', 'inputmode', 'line', 'lineargradient', 'marker', 'mask', 'metadata', 'mpath', 'part', 'path', 'pattern', 'polygon', 'polyline', 'radialgradient', 'rect', 'stop', 'style', 'switch', 'symbol', 'text', 'textpath', 'title', 'tref', 'tspan', 'view', 'vkern']);
301
301
  const svgFilters = freeze(['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feDistantLight', 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence']);
302
302
  // List of SVG elements that are disallowed by default.
@@ -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.4';
406
+ DOMPurify.version = '3.4.6';
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)
@@ -1012,11 +1024,100 @@ function createDOMPurify() {
1012
1024
  /**
1013
1025
  * _isClobbered
1014
1026
  *
1027
+ * Detect DOM-clobbering on HTMLFormElement nodes. Form is the only HTML
1028
+ * interface with [LegacyOverrideBuiltIns]; a descendant element with a
1029
+ * `name` attribute matching a prototype property shadows that property
1030
+ * on direct reads. We use this check at the IN_PLACE entry-point and
1031
+ * during attribute sanitization to refuse clobbered forms.
1032
+ *
1033
+ * Realm safety (GHSA-hpcv-96wg-7vj8): every check in this function must
1034
+ * work for foreign-realm forms — e.g. a <form> created inside a same-
1035
+ * origin iframe and then handed to a parent-realm DOMPurify instance
1036
+ * with IN_PLACE: true. The original implementation used
1037
+ * `element instanceof HTMLFormElement` and `element.attributes
1038
+ * instanceof NamedNodeMap`, both of which are realm-bound: a foreign-
1039
+ * realm form is an instance of the *foreign* realm's HTMLFormElement,
1040
+ * not the parent realm's. The instanceof short-circuited to false and
1041
+ * the function returned false (= not clobbered) regardless of how
1042
+ * thoroughly the form was clobbered. Sanitize then walked a clobbered
1043
+ * .attributes and missed every attribute on the form root, leaving
1044
+ * onmouseover / onclick / formaction / etc. intact.
1045
+ *
1046
+ * The realm-independent replacements:
1047
+ * - HTMLFormElement detection — read the tag name through the cached
1048
+ * Node.prototype.nodeName getter. WebIDL getters operate on internal
1049
+ * slots that exist on every real Node regardless of which realm
1050
+ * minted the JS wrapper, so getNodeName(foreignForm) === "FORM".
1051
+ * - NamedNodeMap detection — compare the direct .attributes read
1052
+ * against the cached Element.prototype.attributes getter. Same
1053
+ * equality-probe pattern we use for .childNodes: if a clobbering
1054
+ * child shadows the named property, the two reads diverge; if not,
1055
+ * both return the same NamedNodeMap (same-realm OR foreign-realm —
1056
+ * doesn't matter, both are the canonical attributes object for the
1057
+ * node).
1058
+ *
1015
1059
  * @param element element to check for clobbering attacks
1016
1060
  * @return true if clobbered, false if safe
1017
1061
  */
1018
1062
  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');
1063
+ // Realm-independent tag-name probe. If we can't determine the tag
1064
+ // name at all, we can't reason about clobbering — return false
1065
+ // (the caller's other defences still apply).
1066
+ const realTagName = getNodeName ? getNodeName(element) : null;
1067
+ if (typeof realTagName !== 'string') {
1068
+ return false;
1069
+ }
1070
+ if (transformCaseFunc(realTagName) !== 'form') {
1071
+ return false;
1072
+ }
1073
+ return typeof element.nodeName !== 'string' || typeof element.textContent !== 'string' || typeof element.removeChild !== 'function' ||
1074
+ // Realm-safe NamedNodeMap detection: equality against the cached
1075
+ // prototype getter. Clobbered .attributes (e.g. <input name="attributes">)
1076
+ // makes the direct read diverge from the cached read; a clean form
1077
+ // (same-realm OR foreign-realm) has both reads pointing at the same
1078
+ // canonical NamedNodeMap.
1079
+ element.attributes !== getAttributes(element) || typeof element.removeAttribute !== 'function' || typeof element.setAttribute !== 'function' || typeof element.namespaceURI !== 'string' || typeof element.insertBefore !== 'function' || typeof element.hasChildNodes !== 'function' ||
1080
+ // HTMLFormElement has [LegacyOverrideBuiltIns]: a descendant named
1081
+ // "childNodes" shadows the prototype getter. Direct reads of
1082
+ // form.childNodes from a clobbered form return the named child
1083
+ // instead of the real NodeList, so any walk that reads it directly
1084
+ // skips the form's real children. Compare the direct read to the
1085
+ // cached Node.prototype getter — when the form's named-property
1086
+ // getter intercepts the read, the two values differ and we flag
1087
+ // the form. This catches every clobbering child type (input,
1088
+ // select, etc.) regardless of whether the named child happens to
1089
+ // carry a numeric .length, which a typeof-based probe would miss
1090
+ // (e.g. HTMLSelectElement.length is a defined unsigned-long).
1091
+ element.childNodes !== getChildNodes(element);
1092
+ };
1093
+ /**
1094
+ * Checks whether the given value is a DocumentFragment from any realm.
1095
+ *
1096
+ * Realm safety (GHSA-hpcv-96wg-7vj8): the original sites used
1097
+ * `value instanceof DocumentFragment`, which is realm-bound — a fragment
1098
+ * from a foreign realm (template content or shadow root from an iframe
1099
+ * document) is an instance of the foreign realm's DocumentFragment, not
1100
+ * the parent realm's, so the check returned false and the template-
1101
+ * content / shadow-root recursion was silently skipped. The attacker
1102
+ * payload inside survived untouched.
1103
+ *
1104
+ * The realm-independent replacement reads `nodeType` through the cached
1105
+ * Node.prototype getter and compares to the DOCUMENT_FRAGMENT_NODE
1106
+ * constant (11). nodeType is a numeric value resolved from the node's
1107
+ * internal slot, identical across realms for the same kind of node.
1108
+ *
1109
+ * @param value object to check
1110
+ * @return true if value is a DocumentFragment-shaped node from any realm
1111
+ */
1112
+ const _isDocumentFragment = function _isDocumentFragment(value) {
1113
+ if (!getNodeType || typeof value !== 'object' || value === null) {
1114
+ return false;
1115
+ }
1116
+ try {
1117
+ return getNodeType(value) === NODE_TYPE.documentFragment;
1118
+ } catch (_) {
1119
+ return false;
1120
+ }
1020
1121
  };
1021
1122
  /**
1022
1123
  * Checks whether the given object is a DOM node, including nodes that
@@ -1121,8 +1222,14 @@ function createDOMPurify() {
1121
1222
  _forceRemove(currentNode);
1122
1223
  return true;
1123
1224
  }
1124
- /* Check whether element has a valid namespace */
1125
- if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
1225
+ /* Check whether element has a valid namespace.
1226
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use the cached Node.prototype
1227
+ nodeType getter rather than `instanceof Element`, which is realm-
1228
+ bound and short-circuits to false for any node minted in a different
1229
+ realm — letting a foreign-realm element with a forbidden namespace
1230
+ slip past the namespace check entirely. */
1231
+ const nt = getNodeType ? getNodeType(currentNode) : currentNode.nodeType;
1232
+ if (nt === NODE_TYPE.element && !_checkValidNamespace(currentNode)) {
1126
1233
  _forceRemove(currentNode);
1127
1234
  return true;
1128
1235
  }
@@ -1349,8 +1456,11 @@ function createDOMPurify() {
1349
1456
  _sanitizeElements(shadowNode);
1350
1457
  /* Check attributes next */
1351
1458
  _sanitizeAttributes(shadowNode);
1352
- /* Deep shadow DOM detected */
1353
- if (shadowNode.content instanceof DocumentFragment) {
1459
+ /* Deep shadow DOM detected.
1460
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType against the
1461
+ DOCUMENT_FRAGMENT_NODE constant rather than instanceof, so we
1462
+ recurse into <template>.content from foreign realms too. */
1463
+ if (_isDocumentFragment(shadowNode.content)) {
1354
1464
  _sanitizeShadowDOM2(shadowNode.content);
1355
1465
  }
1356
1466
  }
@@ -1374,21 +1484,42 @@ function createDOMPurify() {
1374
1484
  * existing _sanitizeShadowDOM template-content recursion) stay
1375
1485
  * untouched — string-input paths are not affected.
1376
1486
  *
1487
+ * DOM-Clobbering hardening: HTMLFormElement carries the WebIDL
1488
+ * [LegacyOverrideBuiltIns] extended attribute, so a descendant element
1489
+ * named `nodeType`, `shadowRoot`, or `childNodes` shadows the matching
1490
+ * prototype getter on the form. Reading those properties directly off
1491
+ * the node would let an attacker steer this walk past shadow hosts
1492
+ * (e.g. <input name="childNodes"> collapses the form's child list to
1493
+ * the input itself, so descent stops dead and any shadow root deeper
1494
+ * in the subtree is never sanitized). Every property access here is
1495
+ * therefore routed through the cached prototype getter; the form's
1496
+ * named-property getter cannot intercept those reads.
1497
+ *
1377
1498
  * @param root the subtree root to walk for attached shadow roots
1378
1499
  */
1379
1500
  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);
1501
+ const nodeType = getNodeType ? getNodeType(root) : root.nodeType;
1502
+ if (nodeType === NODE_TYPE.element) {
1503
+ const sr = getShadowRoot ? getShadowRoot(root) : root.shadowRoot;
1504
+ // Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType-based
1505
+ // detection rather than `instanceof DocumentFragment`, which is
1506
+ // realm-bound and silently skipped shadow roots whose host element
1507
+ // belonged to a foreign realm (e.g. iframe.contentDocument
1508
+ // attachShadow). A foreign-realm ShadowRoot extends the foreign
1509
+ // realm's DocumentFragment, not ours, so the old instanceof check
1510
+ // returned false and the shadow subtree was never walked.
1511
+ if (_isDocumentFragment(sr)) {
1512
+ // Recurse first so that nested shadow roots are reached even if
1513
+ // _sanitizeShadowDOM removes hosts at this level.
1514
+ _sanitizeAttachedShadowRoots2(sr);
1515
+ _sanitizeShadowDOM2(sr);
1516
+ }
1386
1517
  }
1387
1518
  // Snapshot children before recursing. Sanitization of one subtree
1388
1519
  // (e.g. via an uponSanitizeShadowNode hook) may detach siblings,
1389
1520
  // and naive nextSibling traversal would silently skip the rest of
1390
1521
  // the list once a node is detached.
1391
- const childNodes = root.childNodes;
1522
+ const childNodes = getChildNodes ? getChildNodes(root) : root.childNodes;
1392
1523
  if (!childNodes) {
1393
1524
  return;
1394
1525
  }
@@ -1436,14 +1567,31 @@ function createDOMPurify() {
1436
1567
  IN_PLACE = false;
1437
1568
  }
1438
1569
  if (IN_PLACE) {
1439
- /* Do some early pre-sanitization to avoid unsafe root nodes */
1440
- const nn = dirty.nodeName;
1570
+ /* Do some early pre-sanitization to avoid unsafe root nodes.
1571
+ Read nodeName through the cached prototype getter — a clobbering
1572
+ child named "nodeName" on the form root would otherwise shadow
1573
+ the property and let this check skip the root-allowlist
1574
+ validation entirely. */
1575
+ const nn = getNodeName ? getNodeName(dirty) : dirty.nodeName;
1441
1576
  if (typeof nn === 'string') {
1442
1577
  const tagName = transformCaseFunc(nn);
1443
1578
  if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
1444
1579
  throw typeErrorCreate('root node is forbidden and cannot be sanitized in-place');
1445
1580
  }
1446
1581
  }
1582
+ /* Pre-flight the root through _isClobbered. The iterator-driven
1583
+ removal path can not detach a parent-less root: _forceRemove
1584
+ falls through to Element.prototype.remove(), which per spec
1585
+ is a no-op on a node with no parent. A clobbered root would
1586
+ then survive the main loop with its attributes uninspected,
1587
+ because _sanitizeAttributes early-returns on _isClobbered. The
1588
+ result would be an attacker-controlled form, complete with any
1589
+ event-handler attributes the caller passed in, handed back to
1590
+ the application unsanitized. Refuse to sanitize such a root
1591
+ the same way we refuse a forbidden tag. GHSA-r47g-fvhr-h676. */
1592
+ if (_isClobbered(dirty)) {
1593
+ throw typeErrorCreate('root node is clobbered and cannot be sanitized in-place');
1594
+ }
1447
1595
  /* Sanitize attached shadow roots before the main iterator runs.
1448
1596
  The iterator does not descend into shadow trees. */
1449
1597
  _sanitizeAttachedShadowRoots2(dirty);
@@ -1463,7 +1611,9 @@ function createDOMPurify() {
1463
1611
  }
1464
1612
  /* Clonable shadow roots are deep-cloned by importNode(); sanitize
1465
1613
  them before the main iterator runs, since the iterator does not
1466
- descend into shadow trees. */
1614
+ descend into shadow trees. The walk routes every read through a
1615
+ cached prototype getter so clobbering descendants on a form root
1616
+ cannot hide a shadow host from this pass. */
1467
1617
  _sanitizeAttachedShadowRoots2(importedNode);
1468
1618
  } else {
1469
1619
  /* Exit directly if we have nothing to do */
@@ -1491,8 +1641,11 @@ function createDOMPurify() {
1491
1641
  _sanitizeElements(currentNode);
1492
1642
  /* Check attributes next */
1493
1643
  _sanitizeAttributes(currentNode);
1494
- /* Shadow DOM detected, sanitize it */
1495
- if (currentNode.content instanceof DocumentFragment) {
1644
+ /* Shadow DOM detected, sanitize it.
1645
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
1646
+ instead of instanceof, so foreign-realm <template>.content is
1647
+ walked correctly. */
1648
+ if (_isDocumentFragment(currentNode.content)) {
1496
1649
  _sanitizeShadowDOM2(currentNode.content);
1497
1650
  }
1498
1651
  }