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.
package/dist/purify.js CHANGED
@@ -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 (global, factory) {
4
4
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
@@ -302,7 +302,7 @@
302
302
  }
303
303
  }
304
304
 
305
- 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']);
305
+ 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']);
306
306
  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']);
307
307
  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']);
308
308
  // List of SVG elements that are disallowed by default.
@@ -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.4';
412
+ DOMPurify.version = '3.4.6';
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)
@@ -1018,11 +1030,100 @@
1018
1030
  /**
1019
1031
  * _isClobbered
1020
1032
  *
1033
+ * Detect DOM-clobbering on HTMLFormElement nodes. Form is the only HTML
1034
+ * interface with [LegacyOverrideBuiltIns]; a descendant element with a
1035
+ * `name` attribute matching a prototype property shadows that property
1036
+ * on direct reads. We use this check at the IN_PLACE entry-point and
1037
+ * during attribute sanitization to refuse clobbered forms.
1038
+ *
1039
+ * Realm safety (GHSA-hpcv-96wg-7vj8): every check in this function must
1040
+ * work for foreign-realm forms — e.g. a <form> created inside a same-
1041
+ * origin iframe and then handed to a parent-realm DOMPurify instance
1042
+ * with IN_PLACE: true. The original implementation used
1043
+ * `element instanceof HTMLFormElement` and `element.attributes
1044
+ * instanceof NamedNodeMap`, both of which are realm-bound: a foreign-
1045
+ * realm form is an instance of the *foreign* realm's HTMLFormElement,
1046
+ * not the parent realm's. The instanceof short-circuited to false and
1047
+ * the function returned false (= not clobbered) regardless of how
1048
+ * thoroughly the form was clobbered. Sanitize then walked a clobbered
1049
+ * .attributes and missed every attribute on the form root, leaving
1050
+ * onmouseover / onclick / formaction / etc. intact.
1051
+ *
1052
+ * The realm-independent replacements:
1053
+ * - HTMLFormElement detection — read the tag name through the cached
1054
+ * Node.prototype.nodeName getter. WebIDL getters operate on internal
1055
+ * slots that exist on every real Node regardless of which realm
1056
+ * minted the JS wrapper, so getNodeName(foreignForm) === "FORM".
1057
+ * - NamedNodeMap detection — compare the direct .attributes read
1058
+ * against the cached Element.prototype.attributes getter. Same
1059
+ * equality-probe pattern we use for .childNodes: if a clobbering
1060
+ * child shadows the named property, the two reads diverge; if not,
1061
+ * both return the same NamedNodeMap (same-realm OR foreign-realm —
1062
+ * doesn't matter, both are the canonical attributes object for the
1063
+ * node).
1064
+ *
1021
1065
  * @param element element to check for clobbering attacks
1022
1066
  * @return true if clobbered, false if safe
1023
1067
  */
1024
1068
  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');
1069
+ // Realm-independent tag-name probe. If we can't determine the tag
1070
+ // name at all, we can't reason about clobbering — return false
1071
+ // (the caller's other defences still apply).
1072
+ const realTagName = getNodeName ? getNodeName(element) : null;
1073
+ if (typeof realTagName !== 'string') {
1074
+ return false;
1075
+ }
1076
+ if (transformCaseFunc(realTagName) !== 'form') {
1077
+ return false;
1078
+ }
1079
+ return typeof element.nodeName !== 'string' || typeof element.textContent !== 'string' || typeof element.removeChild !== 'function' ||
1080
+ // Realm-safe NamedNodeMap detection: equality against the cached
1081
+ // prototype getter. Clobbered .attributes (e.g. <input name="attributes">)
1082
+ // makes the direct read diverge from the cached read; a clean form
1083
+ // (same-realm OR foreign-realm) has both reads pointing at the same
1084
+ // canonical NamedNodeMap.
1085
+ element.attributes !== getAttributes(element) || typeof element.removeAttribute !== 'function' || typeof element.setAttribute !== 'function' || typeof element.namespaceURI !== 'string' || typeof element.insertBefore !== 'function' || typeof element.hasChildNodes !== 'function' ||
1086
+ // HTMLFormElement has [LegacyOverrideBuiltIns]: a descendant named
1087
+ // "childNodes" shadows the prototype getter. Direct reads of
1088
+ // form.childNodes from a clobbered form return the named child
1089
+ // instead of the real NodeList, so any walk that reads it directly
1090
+ // skips the form's real children. Compare the direct read to the
1091
+ // cached Node.prototype getter — when the form's named-property
1092
+ // getter intercepts the read, the two values differ and we flag
1093
+ // the form. This catches every clobbering child type (input,
1094
+ // select, etc.) regardless of whether the named child happens to
1095
+ // carry a numeric .length, which a typeof-based probe would miss
1096
+ // (e.g. HTMLSelectElement.length is a defined unsigned-long).
1097
+ element.childNodes !== getChildNodes(element);
1098
+ };
1099
+ /**
1100
+ * Checks whether the given value is a DocumentFragment from any realm.
1101
+ *
1102
+ * Realm safety (GHSA-hpcv-96wg-7vj8): the original sites used
1103
+ * `value instanceof DocumentFragment`, which is realm-bound — a fragment
1104
+ * from a foreign realm (template content or shadow root from an iframe
1105
+ * document) is an instance of the foreign realm's DocumentFragment, not
1106
+ * the parent realm's, so the check returned false and the template-
1107
+ * content / shadow-root recursion was silently skipped. The attacker
1108
+ * payload inside survived untouched.
1109
+ *
1110
+ * The realm-independent replacement reads `nodeType` through the cached
1111
+ * Node.prototype getter and compares to the DOCUMENT_FRAGMENT_NODE
1112
+ * constant (11). nodeType is a numeric value resolved from the node's
1113
+ * internal slot, identical across realms for the same kind of node.
1114
+ *
1115
+ * @param value object to check
1116
+ * @return true if value is a DocumentFragment-shaped node from any realm
1117
+ */
1118
+ const _isDocumentFragment = function _isDocumentFragment(value) {
1119
+ if (!getNodeType || typeof value !== 'object' || value === null) {
1120
+ return false;
1121
+ }
1122
+ try {
1123
+ return getNodeType(value) === NODE_TYPE.documentFragment;
1124
+ } catch (_) {
1125
+ return false;
1126
+ }
1026
1127
  };
1027
1128
  /**
1028
1129
  * Checks whether the given object is a DOM node, including nodes that
@@ -1127,8 +1228,14 @@
1127
1228
  _forceRemove(currentNode);
1128
1229
  return true;
1129
1230
  }
1130
- /* Check whether element has a valid namespace */
1131
- if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
1231
+ /* Check whether element has a valid namespace.
1232
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use the cached Node.prototype
1233
+ nodeType getter rather than `instanceof Element`, which is realm-
1234
+ bound and short-circuits to false for any node minted in a different
1235
+ realm — letting a foreign-realm element with a forbidden namespace
1236
+ slip past the namespace check entirely. */
1237
+ const nt = getNodeType ? getNodeType(currentNode) : currentNode.nodeType;
1238
+ if (nt === NODE_TYPE.element && !_checkValidNamespace(currentNode)) {
1132
1239
  _forceRemove(currentNode);
1133
1240
  return true;
1134
1241
  }
@@ -1355,8 +1462,11 @@
1355
1462
  _sanitizeElements(shadowNode);
1356
1463
  /* Check attributes next */
1357
1464
  _sanitizeAttributes(shadowNode);
1358
- /* Deep shadow DOM detected */
1359
- if (shadowNode.content instanceof DocumentFragment) {
1465
+ /* Deep shadow DOM detected.
1466
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType against the
1467
+ DOCUMENT_FRAGMENT_NODE constant rather than instanceof, so we
1468
+ recurse into <template>.content from foreign realms too. */
1469
+ if (_isDocumentFragment(shadowNode.content)) {
1360
1470
  _sanitizeShadowDOM2(shadowNode.content);
1361
1471
  }
1362
1472
  }
@@ -1380,21 +1490,42 @@
1380
1490
  * existing _sanitizeShadowDOM template-content recursion) stay
1381
1491
  * untouched — string-input paths are not affected.
1382
1492
  *
1493
+ * DOM-Clobbering hardening: HTMLFormElement carries the WebIDL
1494
+ * [LegacyOverrideBuiltIns] extended attribute, so a descendant element
1495
+ * named `nodeType`, `shadowRoot`, or `childNodes` shadows the matching
1496
+ * prototype getter on the form. Reading those properties directly off
1497
+ * the node would let an attacker steer this walk past shadow hosts
1498
+ * (e.g. <input name="childNodes"> collapses the form's child list to
1499
+ * the input itself, so descent stops dead and any shadow root deeper
1500
+ * in the subtree is never sanitized). Every property access here is
1501
+ * therefore routed through the cached prototype getter; the form's
1502
+ * named-property getter cannot intercept those reads.
1503
+ *
1383
1504
  * @param root the subtree root to walk for attached shadow roots
1384
1505
  */
1385
1506
  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);
1507
+ const nodeType = getNodeType ? getNodeType(root) : root.nodeType;
1508
+ if (nodeType === NODE_TYPE.element) {
1509
+ const sr = getShadowRoot ? getShadowRoot(root) : root.shadowRoot;
1510
+ // Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType-based
1511
+ // detection rather than `instanceof DocumentFragment`, which is
1512
+ // realm-bound and silently skipped shadow roots whose host element
1513
+ // belonged to a foreign realm (e.g. iframe.contentDocument
1514
+ // attachShadow). A foreign-realm ShadowRoot extends the foreign
1515
+ // realm's DocumentFragment, not ours, so the old instanceof check
1516
+ // returned false and the shadow subtree was never walked.
1517
+ if (_isDocumentFragment(sr)) {
1518
+ // Recurse first so that nested shadow roots are reached even if
1519
+ // _sanitizeShadowDOM removes hosts at this level.
1520
+ _sanitizeAttachedShadowRoots2(sr);
1521
+ _sanitizeShadowDOM2(sr);
1522
+ }
1392
1523
  }
1393
1524
  // Snapshot children before recursing. Sanitization of one subtree
1394
1525
  // (e.g. via an uponSanitizeShadowNode hook) may detach siblings,
1395
1526
  // and naive nextSibling traversal would silently skip the rest of
1396
1527
  // the list once a node is detached.
1397
- const childNodes = root.childNodes;
1528
+ const childNodes = getChildNodes ? getChildNodes(root) : root.childNodes;
1398
1529
  if (!childNodes) {
1399
1530
  return;
1400
1531
  }
@@ -1442,14 +1573,31 @@
1442
1573
  IN_PLACE = false;
1443
1574
  }
1444
1575
  if (IN_PLACE) {
1445
- /* Do some early pre-sanitization to avoid unsafe root nodes */
1446
- const nn = dirty.nodeName;
1576
+ /* Do some early pre-sanitization to avoid unsafe root nodes.
1577
+ Read nodeName through the cached prototype getter — a clobbering
1578
+ child named "nodeName" on the form root would otherwise shadow
1579
+ the property and let this check skip the root-allowlist
1580
+ validation entirely. */
1581
+ const nn = getNodeName ? getNodeName(dirty) : dirty.nodeName;
1447
1582
  if (typeof nn === 'string') {
1448
1583
  const tagName = transformCaseFunc(nn);
1449
1584
  if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
1450
1585
  throw typeErrorCreate('root node is forbidden and cannot be sanitized in-place');
1451
1586
  }
1452
1587
  }
1588
+ /* Pre-flight the root through _isClobbered. The iterator-driven
1589
+ removal path can not detach a parent-less root: _forceRemove
1590
+ falls through to Element.prototype.remove(), which per spec
1591
+ is a no-op on a node with no parent. A clobbered root would
1592
+ then survive the main loop with its attributes uninspected,
1593
+ because _sanitizeAttributes early-returns on _isClobbered. The
1594
+ result would be an attacker-controlled form, complete with any
1595
+ event-handler attributes the caller passed in, handed back to
1596
+ the application unsanitized. Refuse to sanitize such a root
1597
+ the same way we refuse a forbidden tag. GHSA-r47g-fvhr-h676. */
1598
+ if (_isClobbered(dirty)) {
1599
+ throw typeErrorCreate('root node is clobbered and cannot be sanitized in-place');
1600
+ }
1453
1601
  /* Sanitize attached shadow roots before the main iterator runs.
1454
1602
  The iterator does not descend into shadow trees. */
1455
1603
  _sanitizeAttachedShadowRoots2(dirty);
@@ -1469,7 +1617,9 @@
1469
1617
  }
1470
1618
  /* Clonable shadow roots are deep-cloned by importNode(); sanitize
1471
1619
  them before the main iterator runs, since the iterator does not
1472
- descend into shadow trees. */
1620
+ descend into shadow trees. The walk routes every read through a
1621
+ cached prototype getter so clobbering descendants on a form root
1622
+ cannot hide a shadow host from this pass. */
1473
1623
  _sanitizeAttachedShadowRoots2(importedNode);
1474
1624
  } else {
1475
1625
  /* Exit directly if we have nothing to do */
@@ -1497,8 +1647,11 @@
1497
1647
  _sanitizeElements(currentNode);
1498
1648
  /* Check attributes next */
1499
1649
  _sanitizeAttributes(currentNode);
1500
- /* Shadow DOM detected, sanitize it */
1501
- if (currentNode.content instanceof DocumentFragment) {
1650
+ /* Shadow DOM detected, sanitize it.
1651
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
1652
+ instead of instanceof, so foreign-realm <template>.content is
1653
+ walked correctly. */
1654
+ if (_isDocumentFragment(currentNode.content)) {
1502
1655
  _sanitizeShadowDOM2(currentNode.content);
1503
1656
  }
1504
1657
  }