igv 3.8.1 → 3.8.2

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/igv.js CHANGED
@@ -10612,7 +10612,7 @@
10612
10612
  return 1 === unique.length
10613
10613
  }
10614
10614
 
10615
- /*! @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 */
10615
+ /*! @license DOMPurify 3.4.10 | (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.10/LICENSE */
10616
10616
 
10617
10617
  function _arrayLikeToArray(r, a) {
10618
10618
  (null == a || a > r.length) && (a = r.length);
@@ -10941,6 +10941,13 @@
10941
10941
  );
10942
10942
  const DOCTYPE_NAME = seal(/^html$/i);
10943
10943
  const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
10944
+ // Markup-significant character probes used by _sanitizeElements.
10945
+ // Shared module-level instances are safe despite the sticky /g flags:
10946
+ // unapply() resets lastIndex for RegExp receivers before every call.
10947
+ const ELEMENT_MARKUP_PROBE = seal(/<[/\w!]/g);
10948
+ const COMMENT_MARKUP_PROBE = seal(/<[/\w]/g);
10949
+ const FALLBACK_TAG_CLOSE = seal(/<\/no(script|embed|frames)/i);
10950
+ const SELF_CLOSING_TAG = seal(/\/>/i);
10944
10951
 
10945
10952
  /* eslint-disable @typescript-eslint/indent */
10946
10953
  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
@@ -10953,7 +10960,7 @@
10953
10960
  // Deprecated
10954
10961
  entityNode: 6,
10955
10962
  // Deprecated
10956
- progressingInstruction: 7,
10963
+ processingInstruction: 7,
10957
10964
  comment: 8,
10958
10965
  document: 9,
10959
10966
  documentType: 10,
@@ -11014,10 +11021,25 @@
11014
11021
  uponSanitizeShadowNode: []
11015
11022
  };
11016
11023
  };
11024
+ /**
11025
+ * Resolve a set-valued configuration option: a fresh set built from
11026
+ * cfg[key] when it is an own array property (seeded with a clone of
11027
+ * options.base when given, case-normalized via options.transform),
11028
+ * the fallback set otherwise.
11029
+ *
11030
+ * @param cfg the cloned, prototype-free configuration object
11031
+ * @param key the configuration property to read
11032
+ * @param fallback the set to use when the option is absent or not an array
11033
+ * @param options transform and optional base set to merge into
11034
+ * @returns the resolved set
11035
+ */
11036
+ const _resolveSetOption = function _resolveSetOption(cfg, key, fallback, options) {
11037
+ return objectHasOwnProperty(cfg, key) && arrayIsArray(cfg[key]) ? addToSet(options.base ? clone(options.base) : {}, cfg[key], options.transform) : fallback;
11038
+ };
11017
11039
  function createDOMPurify() {
11018
11040
  let window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGlobal();
11019
11041
  const DOMPurify = root => createDOMPurify(root);
11020
- DOMPurify.version = '3.4.7';
11042
+ DOMPurify.version = '3.4.10';
11021
11043
  DOMPurify.removed = [];
11022
11044
  if (!window || !window.document || window.document.nodeType !== NODE_TYPE.document || !window.Element) {
11023
11045
  // Not running in a browser, provide a factory function
@@ -11062,6 +11084,54 @@
11062
11084
  }
11063
11085
  let trustedTypesPolicy;
11064
11086
  let emptyHTML = '';
11087
+ // The instance's own internal Trusted Types policy. Unlike a caller-supplied
11088
+ // `TRUSTED_TYPES_POLICY`, this is created at most once — Trusted Types throws
11089
+ // on duplicate policy names — and is the only policy allowed to persist
11090
+ // across configurations and survive `clearConfig()`.
11091
+ let defaultTrustedTypesPolicy;
11092
+ let defaultTrustedTypesPolicyResolved = false;
11093
+ // Tracks whether we are already inside a call to the configured Trusted Types
11094
+ // policy (`createHTML` or `createScriptURL`). If a supplied policy callback
11095
+ // itself calls `DOMPurify.sanitize` (the cause of #1422), `sanitize` would
11096
+ // re-enter the policy and recurse until the stack overflows. We detect that
11097
+ // re-entry and throw a clear, actionable error instead. The guard is shared
11098
+ // across both callbacks, because either one re-entering `sanitize` triggers
11099
+ // the same unbounded recursion.
11100
+ let IN_TRUSTED_TYPES_POLICY = 0;
11101
+ const _assertNotInTrustedTypesPolicy = function _assertNotInTrustedTypesPolicy() {
11102
+ if (IN_TRUSTED_TYPES_POLICY > 0) {
11103
+ throw typeErrorCreate('A configured TRUSTED_TYPES_POLICY callback (createHTML or ' + 'createScriptURL) must not call DOMPurify.sanitize, as that causes ' + 'infinite recursion. Do not pass a policy whose callbacks wrap ' + 'DOMPurify as TRUSTED_TYPES_POLICY; see the "DOMPurify and Trusted ' + 'Types" section of the README.');
11104
+ }
11105
+ };
11106
+ const _createTrustedHTML = function _createTrustedHTML(html) {
11107
+ _assertNotInTrustedTypesPolicy();
11108
+ IN_TRUSTED_TYPES_POLICY++;
11109
+ try {
11110
+ return trustedTypesPolicy.createHTML(html);
11111
+ } finally {
11112
+ IN_TRUSTED_TYPES_POLICY--;
11113
+ }
11114
+ };
11115
+ const _createTrustedScriptURL = function _createTrustedScriptURL(scriptUrl) {
11116
+ _assertNotInTrustedTypesPolicy();
11117
+ IN_TRUSTED_TYPES_POLICY++;
11118
+ try {
11119
+ return trustedTypesPolicy.createScriptURL(scriptUrl);
11120
+ } finally {
11121
+ IN_TRUSTED_TYPES_POLICY--;
11122
+ }
11123
+ };
11124
+ // Lazily resolve (and cache) the instance's internal default policy.
11125
+ // Resolution is attempted at most once: a successful `createPolicy` cannot be
11126
+ // repeated (Trusted Types throws on duplicate names), and a failed or
11127
+ // unsupported attempt must not be retried on every parse.
11128
+ const _getDefaultTrustedTypesPolicy = function _getDefaultTrustedTypesPolicy() {
11129
+ if (!defaultTrustedTypesPolicyResolved) {
11130
+ defaultTrustedTypesPolicy = _createTrustedTypesPolicy(trustedTypes, currentScript);
11131
+ defaultTrustedTypesPolicyResolved = true;
11132
+ }
11133
+ return defaultTrustedTypesPolicy;
11134
+ };
11065
11135
  const _document = document,
11066
11136
  implementation = _document.implementation,
11067
11137
  createNodeIterator = _document.createNodeIterator,
@@ -11200,7 +11270,17 @@
11200
11270
  let USE_PROFILES = {};
11201
11271
  /* Tags to ignore content of when KEEP_CONTENT is true */
11202
11272
  let FORBID_CONTENTS = null;
11203
- const DEFAULT_FORBID_CONTENTS = addToSet({}, ['annotation-xml', 'audio', 'colgroup', 'desc', 'foreignobject', 'head', 'iframe', 'math', 'mi', 'mn', 'mo', 'ms', 'mtext', 'noembed', 'noframes', 'noscript', 'plaintext', 'script', 'style', 'svg', 'template', 'thead', 'title', 'video', 'xmp']);
11273
+ const DEFAULT_FORBID_CONTENTS = addToSet({}, ['annotation-xml', 'audio', 'colgroup', 'desc', 'foreignobject', 'head', 'iframe', 'math', 'mi', 'mn', 'mo', 'ms', 'mtext', 'noembed', 'noframes', 'noscript', 'plaintext', 'script',
11274
+ // <selectedcontent> mirrors the selected <option>'s subtree, cloned by
11275
+ // the UA (customizable <select>) — including any on* handlers — and the
11276
+ // engine re-mirrors synchronously whenever a removal changes which
11277
+ // option/selectedcontent is current, even inside DOMPurify's inert
11278
+ // DOMParser document. Hoisting its children on removal re-inserts a fresh
11279
+ // mirror target ahead of the walk, which the engine refills, looping
11280
+ // forever (DoS) and amplifying output. Dropping its content on removal
11281
+ // (rather than hoisting) breaks that cascade; the content is a duplicate
11282
+ // of the option, which is sanitized on its own. See campaign-3 F1/F6.
11283
+ 'selectedcontent', 'style', 'svg', 'template', 'thead', 'title', 'video', 'xmp']);
11204
11284
  /* Tags that are safe for data: URIs */
11205
11285
  let DATA_URI_TAGS = null;
11206
11286
  const DEFAULT_DATA_URI_TAGS = addToSet({}, ['audio', 'video', 'img', 'source', 'image', 'track']);
@@ -11216,8 +11296,10 @@
11216
11296
  /* Allowed XHTML+XML namespaces */
11217
11297
  let ALLOWED_NAMESPACES = null;
11218
11298
  const DEFAULT_ALLOWED_NAMESPACES = addToSet({}, [MATHML_NAMESPACE, SVG_NAMESPACE, HTML_NAMESPACE], stringToString);
11219
- let MATHML_TEXT_INTEGRATION_POINTS = addToSet({}, ['mi', 'mo', 'mn', 'ms', 'mtext']);
11220
- let HTML_INTEGRATION_POINTS = addToSet({}, ['annotation-xml']);
11299
+ const DEFAULT_MATHML_TEXT_INTEGRATION_POINTS = freeze(['mi', 'mo', 'mn', 'ms', 'mtext']);
11300
+ let MATHML_TEXT_INTEGRATION_POINTS = addToSet({}, DEFAULT_MATHML_TEXT_INTEGRATION_POINTS);
11301
+ const DEFAULT_HTML_INTEGRATION_POINTS = freeze(['annotation-xml']);
11302
+ let HTML_INTEGRATION_POINTS = addToSet({}, DEFAULT_HTML_INTEGRATION_POINTS);
11221
11303
  // Certain elements are allowed in both SVG and HTML
11222
11304
  // namespace. We need to specify them explicitly
11223
11305
  // so that they don't get erroneously deleted from
@@ -11259,14 +11341,32 @@
11259
11341
  // HTML tags and attributes are not case-sensitive, converting to lowercase. Keeping XHTML as is.
11260
11342
  transformCaseFunc = PARSER_MEDIA_TYPE === 'application/xhtml+xml' ? stringToString : stringToLowerCase;
11261
11343
  /* Set configuration parameters */
11262
- ALLOWED_TAGS = objectHasOwnProperty(cfg, 'ALLOWED_TAGS') && arrayIsArray(cfg.ALLOWED_TAGS) ? addToSet({}, cfg.ALLOWED_TAGS, transformCaseFunc) : DEFAULT_ALLOWED_TAGS;
11263
- ALLOWED_ATTR = objectHasOwnProperty(cfg, 'ALLOWED_ATTR') && arrayIsArray(cfg.ALLOWED_ATTR) ? addToSet({}, cfg.ALLOWED_ATTR, transformCaseFunc) : DEFAULT_ALLOWED_ATTR;
11264
- ALLOWED_NAMESPACES = objectHasOwnProperty(cfg, 'ALLOWED_NAMESPACES') && arrayIsArray(cfg.ALLOWED_NAMESPACES) ? addToSet({}, cfg.ALLOWED_NAMESPACES, stringToString) : DEFAULT_ALLOWED_NAMESPACES;
11265
- URI_SAFE_ATTRIBUTES = objectHasOwnProperty(cfg, 'ADD_URI_SAFE_ATTR') && arrayIsArray(cfg.ADD_URI_SAFE_ATTR) ? addToSet(clone(DEFAULT_URI_SAFE_ATTRIBUTES), cfg.ADD_URI_SAFE_ATTR, transformCaseFunc) : DEFAULT_URI_SAFE_ATTRIBUTES;
11266
- DATA_URI_TAGS = objectHasOwnProperty(cfg, 'ADD_DATA_URI_TAGS') && arrayIsArray(cfg.ADD_DATA_URI_TAGS) ? addToSet(clone(DEFAULT_DATA_URI_TAGS), cfg.ADD_DATA_URI_TAGS, transformCaseFunc) : DEFAULT_DATA_URI_TAGS;
11267
- FORBID_CONTENTS = objectHasOwnProperty(cfg, 'FORBID_CONTENTS') && arrayIsArray(cfg.FORBID_CONTENTS) ? addToSet({}, cfg.FORBID_CONTENTS, transformCaseFunc) : DEFAULT_FORBID_CONTENTS;
11268
- FORBID_TAGS = objectHasOwnProperty(cfg, 'FORBID_TAGS') && arrayIsArray(cfg.FORBID_TAGS) ? addToSet({}, cfg.FORBID_TAGS, transformCaseFunc) : clone({});
11269
- FORBID_ATTR = objectHasOwnProperty(cfg, 'FORBID_ATTR') && arrayIsArray(cfg.FORBID_ATTR) ? addToSet({}, cfg.FORBID_ATTR, transformCaseFunc) : clone({});
11344
+ ALLOWED_TAGS = _resolveSetOption(cfg, 'ALLOWED_TAGS', DEFAULT_ALLOWED_TAGS, {
11345
+ transform: transformCaseFunc
11346
+ });
11347
+ ALLOWED_ATTR = _resolveSetOption(cfg, 'ALLOWED_ATTR', DEFAULT_ALLOWED_ATTR, {
11348
+ transform: transformCaseFunc
11349
+ });
11350
+ ALLOWED_NAMESPACES = _resolveSetOption(cfg, 'ALLOWED_NAMESPACES', DEFAULT_ALLOWED_NAMESPACES, {
11351
+ transform: stringToString
11352
+ });
11353
+ URI_SAFE_ATTRIBUTES = _resolveSetOption(cfg, 'ADD_URI_SAFE_ATTR', DEFAULT_URI_SAFE_ATTRIBUTES, {
11354
+ transform: transformCaseFunc,
11355
+ base: DEFAULT_URI_SAFE_ATTRIBUTES
11356
+ });
11357
+ DATA_URI_TAGS = _resolveSetOption(cfg, 'ADD_DATA_URI_TAGS', DEFAULT_DATA_URI_TAGS, {
11358
+ transform: transformCaseFunc,
11359
+ base: DEFAULT_DATA_URI_TAGS
11360
+ });
11361
+ FORBID_CONTENTS = _resolveSetOption(cfg, 'FORBID_CONTENTS', DEFAULT_FORBID_CONTENTS, {
11362
+ transform: transformCaseFunc
11363
+ });
11364
+ FORBID_TAGS = _resolveSetOption(cfg, 'FORBID_TAGS', clone({}), {
11365
+ transform: transformCaseFunc
11366
+ });
11367
+ FORBID_ATTR = _resolveSetOption(cfg, 'FORBID_ATTR', clone({}), {
11368
+ transform: transformCaseFunc
11369
+ });
11270
11370
  USE_PROFILES = objectHasOwnProperty(cfg, 'USE_PROFILES') ? cfg.USE_PROFILES && typeof cfg.USE_PROFILES === 'object' ? clone(cfg.USE_PROFILES) : cfg.USE_PROFILES : false;
11271
11371
  ALLOW_ARIA_ATTR = cfg.ALLOW_ARIA_ATTR !== false; // Default true
11272
11372
  ALLOW_DATA_ATTR = cfg.ALLOW_DATA_ATTR !== false; // Default true
@@ -11285,8 +11385,8 @@
11285
11385
  IN_PLACE = cfg.IN_PLACE || false; // Default false
11286
11386
  IS_ALLOWED_URI$1 = isRegex(cfg.ALLOWED_URI_REGEXP) ? cfg.ALLOWED_URI_REGEXP : IS_ALLOWED_URI; // Default regexp
11287
11387
  NAMESPACE = typeof cfg.NAMESPACE === 'string' ? cfg.NAMESPACE : HTML_NAMESPACE; // Default HTML namespace
11288
- MATHML_TEXT_INTEGRATION_POINTS = objectHasOwnProperty(cfg, 'MATHML_TEXT_INTEGRATION_POINTS') && cfg.MATHML_TEXT_INTEGRATION_POINTS && typeof cfg.MATHML_TEXT_INTEGRATION_POINTS === 'object' ? clone(cfg.MATHML_TEXT_INTEGRATION_POINTS) : addToSet({}, ['mi', 'mo', 'mn', 'ms', 'mtext']); // Default built-in map
11289
- HTML_INTEGRATION_POINTS = objectHasOwnProperty(cfg, 'HTML_INTEGRATION_POINTS') && cfg.HTML_INTEGRATION_POINTS && typeof cfg.HTML_INTEGRATION_POINTS === 'object' ? clone(cfg.HTML_INTEGRATION_POINTS) : addToSet({}, ['annotation-xml']); // Default built-in map
11388
+ MATHML_TEXT_INTEGRATION_POINTS = objectHasOwnProperty(cfg, 'MATHML_TEXT_INTEGRATION_POINTS') && cfg.MATHML_TEXT_INTEGRATION_POINTS && typeof cfg.MATHML_TEXT_INTEGRATION_POINTS === 'object' ? clone(cfg.MATHML_TEXT_INTEGRATION_POINTS) : addToSet({}, DEFAULT_MATHML_TEXT_INTEGRATION_POINTS); // Default built-in map
11389
+ HTML_INTEGRATION_POINTS = objectHasOwnProperty(cfg, 'HTML_INTEGRATION_POINTS') && cfg.HTML_INTEGRATION_POINTS && typeof cfg.HTML_INTEGRATION_POINTS === 'object' ? clone(cfg.HTML_INTEGRATION_POINTS) : addToSet({}, DEFAULT_HTML_INTEGRATION_POINTS); // Default built-in map
11290
11390
  const customElementHandling = objectHasOwnProperty(cfg, 'CUSTOM_ELEMENT_HANDLING') && cfg.CUSTOM_ELEMENT_HANDLING && typeof cfg.CUSTOM_ELEMENT_HANDLING === 'object' ? clone(cfg.CUSTOM_ELEMENT_HANDLING) : create(null);
11291
11391
  CUSTOM_ELEMENT_HANDLING = create(null);
11292
11392
  if (objectHasOwnProperty(customElementHandling, 'tagNameCheck') && isRegexOrFunction(customElementHandling.tagNameCheck)) {
@@ -11298,6 +11398,7 @@
11298
11398
  if (objectHasOwnProperty(customElementHandling, 'allowCustomizedBuiltInElements') && typeof customElementHandling.allowCustomizedBuiltInElements === 'boolean') {
11299
11399
  CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements = customElementHandling.allowCustomizedBuiltInElements; // Default undefined
11300
11400
  }
11401
+ seal(CUSTOM_ELEMENT_HANDLING);
11301
11402
  if (SAFE_FOR_TEMPLATES) {
11302
11403
  ALLOW_DATA_ATTR = false;
11303
11404
  }
@@ -11381,6 +11482,13 @@
11381
11482
  addToSet(ALLOWED_TAGS, ['tbody']);
11382
11483
  delete FORBID_TAGS.tbody;
11383
11484
  }
11485
+ // Re-derive the active Trusted Types policy from this configuration on
11486
+ // every parse. The active policy must never be sticky closure state that
11487
+ // outlives the config that set it: a caller-supplied policy left in place
11488
+ // after `clearConfig()` — or after a later call that supplied none, or
11489
+ // `TRUSTED_TYPES_POLICY: null` — could sign a subsequent "default"
11490
+ // `RETURN_TRUSTED_TYPE` result with a foreign, possibly unsafe policy.
11491
+ // See GHSA-vxr8-fq34-vvx9.
11384
11492
  if (cfg.TRUSTED_TYPES_POLICY) {
11385
11493
  if (typeof cfg.TRUSTED_TYPES_POLICY.createHTML !== 'function') {
11386
11494
  throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createHTML" hook.');
@@ -11388,18 +11496,45 @@
11388
11496
  if (typeof cfg.TRUSTED_TYPES_POLICY.createScriptURL !== 'function') {
11389
11497
  throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createScriptURL" hook.');
11390
11498
  }
11391
- // Overwrite existing TrustedTypes policy.
11499
+ // A caller-supplied policy applies to this configuration only.
11500
+ const previousTrustedTypesPolicy = trustedTypesPolicy;
11392
11501
  trustedTypesPolicy = cfg.TRUSTED_TYPES_POLICY;
11393
- // Sign local variables required by `sanitize`.
11394
- emptyHTML = trustedTypesPolicy.createHTML('');
11502
+ // Sign local variables required by `sanitize`. If the supplied policy's
11503
+ // `createHTML` is circular (i.e. it calls `DOMPurify.sanitize`), this
11504
+ // throws via the re-entrancy guard. Restore the previous policy first so
11505
+ // the instance is not left in a poisoned state. See #1422.
11506
+ try {
11507
+ emptyHTML = _createTrustedHTML('');
11508
+ } catch (error) {
11509
+ trustedTypesPolicy = previousTrustedTypesPolicy;
11510
+ throw error;
11511
+ }
11512
+ } else if (cfg.TRUSTED_TYPES_POLICY === null) {
11513
+ // Explicit opt-out for this call: perform no Trusted Types signing and
11514
+ // create nothing (so a strict `trusted-types` CSP that disallows a
11515
+ // `dompurify` policy can still call `sanitize` from inside its own
11516
+ // policy — see #1422). Resetting to `undefined` rather than a sticky
11517
+ // `null` also drops any previously retained caller policy, so it cannot
11518
+ // resurface on a later call, while still allowing the next config-less
11519
+ // call to restore the internal default policy. See GHSA-vxr8-fq34-vvx9.
11520
+ trustedTypesPolicy = undefined;
11521
+ emptyHTML = '';
11395
11522
  } else {
11396
- // Uninitialized policy, attempt to initialize the internal dompurify policy.
11523
+ // No policy supplied: keep the currently active policy if one is set — a
11524
+ // previously supplied policy is intentionally sticky across config-less
11525
+ // calls — otherwise fall back to the instance's own internal policy,
11526
+ // created at most once. (A policy supplied for a *single* call still
11527
+ // lingers by design; what must not linger is a policy whose configuration
11528
+ // has been torn down via `clearConfig()`, which restores the default.)
11397
11529
  if (trustedTypesPolicy === undefined) {
11398
- trustedTypesPolicy = _createTrustedTypesPolicy(trustedTypes, currentScript);
11530
+ trustedTypesPolicy = _getDefaultTrustedTypesPolicy();
11399
11531
  }
11400
- // If creating the internal policy succeeded sign internal variables.
11401
- if (trustedTypesPolicy !== null && typeof emptyHTML === 'string') {
11402
- emptyHTML = trustedTypesPolicy.createHTML('');
11532
+ // Sign internal variables only when a policy is active. A falsy policy
11533
+ // (Trusted Types unsupported, creation failed, or an explicit opt-out)
11534
+ // leaves `emptyHTML` as a plain string, so we never call `.createHTML` on
11535
+ // a non-policy and throw. See #1422.
11536
+ if (trustedTypesPolicy && typeof emptyHTML === 'string') {
11537
+ emptyHTML = _createTrustedHTML('');
11403
11538
  }
11404
11539
  }
11405
11540
  /*
@@ -11429,6 +11564,77 @@
11429
11564
  * correctly. */
11430
11565
  const ALL_SVG_TAGS = addToSet({}, [...svg$1, ...svgFilters, ...svgDisallowed]);
11431
11566
  const ALL_MATHML_TAGS = addToSet({}, [...mathMl$1, ...mathMlDisallowed]);
11567
+ /**
11568
+ * Namespace rules for an element in the SVG namespace.
11569
+ *
11570
+ * @param tagName the element's lowercase tag name
11571
+ * @param parent the (possibly simulated) parent node
11572
+ * @param parentTagName the parent's lowercase tag name
11573
+ * @returns true if a spec-compliant parser could produce this element
11574
+ */
11575
+ const _checkSvgNamespace = function _checkSvgNamespace(tagName, parent, parentTagName) {
11576
+ // The only way to switch from HTML namespace to SVG
11577
+ // is via <svg>. If it happens via any other tag, then
11578
+ // it should be killed.
11579
+ if (parent.namespaceURI === HTML_NAMESPACE) {
11580
+ return tagName === 'svg';
11581
+ }
11582
+ // The only way to switch from MathML to SVG is via <svg>
11583
+ // if the parent is either <annotation-xml> or a MathML
11584
+ // text integration point.
11585
+ if (parent.namespaceURI === MATHML_NAMESPACE) {
11586
+ return tagName === 'svg' && (parentTagName === 'annotation-xml' || MATHML_TEXT_INTEGRATION_POINTS[parentTagName]);
11587
+ }
11588
+ // We only allow elements that are defined in SVG
11589
+ // spec. All others are disallowed in SVG namespace.
11590
+ return Boolean(ALL_SVG_TAGS[tagName]);
11591
+ };
11592
+ /**
11593
+ * Namespace rules for an element in the MathML namespace.
11594
+ *
11595
+ * @param tagName the element's lowercase tag name
11596
+ * @param parent the (possibly simulated) parent node
11597
+ * @param parentTagName the parent's lowercase tag name
11598
+ * @returns true if a spec-compliant parser could produce this element
11599
+ */
11600
+ const _checkMathMlNamespace = function _checkMathMlNamespace(tagName, parent, parentTagName) {
11601
+ // The only way to switch from HTML namespace to MathML
11602
+ // is via <math>. If it happens via any other tag, then
11603
+ // it should be killed.
11604
+ if (parent.namespaceURI === HTML_NAMESPACE) {
11605
+ return tagName === 'math';
11606
+ }
11607
+ // The only way to switch from SVG to MathML is via
11608
+ // <math> and HTML integration points
11609
+ if (parent.namespaceURI === SVG_NAMESPACE) {
11610
+ return tagName === 'math' && HTML_INTEGRATION_POINTS[parentTagName];
11611
+ }
11612
+ // We only allow elements that are defined in MathML
11613
+ // spec. All others are disallowed in MathML namespace.
11614
+ return Boolean(ALL_MATHML_TAGS[tagName]);
11615
+ };
11616
+ /**
11617
+ * Namespace rules for an element in the HTML namespace.
11618
+ *
11619
+ * @param tagName the element's lowercase tag name
11620
+ * @param parent the (possibly simulated) parent node
11621
+ * @param parentTagName the parent's lowercase tag name
11622
+ * @returns true if a spec-compliant parser could produce this element
11623
+ */
11624
+ const _checkHtmlNamespace = function _checkHtmlNamespace(tagName, parent, parentTagName) {
11625
+ // The only way to switch from SVG to HTML is via
11626
+ // HTML integration points, and from MathML to HTML
11627
+ // is via MathML text integration points
11628
+ if (parent.namespaceURI === SVG_NAMESPACE && !HTML_INTEGRATION_POINTS[parentTagName]) {
11629
+ return false;
11630
+ }
11631
+ if (parent.namespaceURI === MATHML_NAMESPACE && !MATHML_TEXT_INTEGRATION_POINTS[parentTagName]) {
11632
+ return false;
11633
+ }
11634
+ // We disallow tags that are specific for MathML
11635
+ // or SVG and should never appear in HTML namespace
11636
+ return !ALL_MATHML_TAGS[tagName] && (COMMON_SVG_AND_HTML_ELEMENTS[tagName] || !ALL_SVG_TAGS[tagName]);
11637
+ };
11432
11638
  /**
11433
11639
  * @param element a DOM element whose namespace is being checked
11434
11640
  * @returns Return false if the element has a
@@ -11451,51 +11657,13 @@
11451
11657
  return false;
11452
11658
  }
11453
11659
  if (element.namespaceURI === SVG_NAMESPACE) {
11454
- // The only way to switch from HTML namespace to SVG
11455
- // is via <svg>. If it happens via any other tag, then
11456
- // it should be killed.
11457
- if (parent.namespaceURI === HTML_NAMESPACE) {
11458
- return tagName === 'svg';
11459
- }
11460
- // The only way to switch from MathML to SVG is via`
11461
- // svg if parent is either <annotation-xml> or MathML
11462
- // text integration points.
11463
- if (parent.namespaceURI === MATHML_NAMESPACE) {
11464
- return tagName === 'svg' && (parentTagName === 'annotation-xml' || MATHML_TEXT_INTEGRATION_POINTS[parentTagName]);
11465
- }
11466
- // We only allow elements that are defined in SVG
11467
- // spec. All others are disallowed in SVG namespace.
11468
- return Boolean(ALL_SVG_TAGS[tagName]);
11660
+ return _checkSvgNamespace(tagName, parent, parentTagName);
11469
11661
  }
11470
11662
  if (element.namespaceURI === MATHML_NAMESPACE) {
11471
- // The only way to switch from HTML namespace to MathML
11472
- // is via <math>. If it happens via any other tag, then
11473
- // it should be killed.
11474
- if (parent.namespaceURI === HTML_NAMESPACE) {
11475
- return tagName === 'math';
11476
- }
11477
- // The only way to switch from SVG to MathML is via
11478
- // <math> and HTML integration points
11479
- if (parent.namespaceURI === SVG_NAMESPACE) {
11480
- return tagName === 'math' && HTML_INTEGRATION_POINTS[parentTagName];
11481
- }
11482
- // We only allow elements that are defined in MathML
11483
- // spec. All others are disallowed in MathML namespace.
11484
- return Boolean(ALL_MATHML_TAGS[tagName]);
11663
+ return _checkMathMlNamespace(tagName, parent, parentTagName);
11485
11664
  }
11486
11665
  if (element.namespaceURI === HTML_NAMESPACE) {
11487
- // The only way to switch from SVG to HTML is via
11488
- // HTML integration points, and from MathML to HTML
11489
- // is via MathML text integration points
11490
- if (parent.namespaceURI === SVG_NAMESPACE && !HTML_INTEGRATION_POINTS[parentTagName]) {
11491
- return false;
11492
- }
11493
- if (parent.namespaceURI === MATHML_NAMESPACE && !MATHML_TEXT_INTEGRATION_POINTS[parentTagName]) {
11494
- return false;
11495
- }
11496
- // We disallow tags that are specific for MathML
11497
- // or SVG and should never appear in HTML namespace
11498
- return !ALL_MATHML_TAGS[tagName] && (COMMON_SVG_AND_HTML_ELEMENTS[tagName] || !ALL_SVG_TAGS[tagName]);
11666
+ return _checkHtmlNamespace(tagName, parent, parentTagName);
11499
11667
  }
11500
11668
  // For XHTML and XML documents that support custom namespaces
11501
11669
  if (PARSER_MEDIA_TYPE === 'application/xhtml+xml' && ALLOWED_NAMESPACES[element.namespaceURI]) {
@@ -11520,7 +11688,74 @@
11520
11688
  // eslint-disable-next-line unicorn/prefer-dom-node-remove
11521
11689
  getParentNode(node).removeChild(node);
11522
11690
  } catch (_) {
11691
+ /* The normal detach failed — this is reached for a parentless node
11692
+ (getParentNode() is null, so .removeChild throws). Element.prototype
11693
+ .remove() is itself a spec no-op on a parentless node, so a recorded
11694
+ "removal" would otherwise hand the caller back an intact,
11695
+ payload-bearing node (e.g. a detached IN_PLACE root the mXSS canary or
11696
+ the style-with-element-child rule decided to kill). Fail closed by
11697
+ throwing — exactly as a clobbered root does at the IN_PLACE entry —
11698
+ rather than trying to "neutralize" the node via its own methods.
11699
+ Neutralizing would mean calling getAttributeNames()/removeAttribute()
11700
+ on the node, both of which a <form> root can clobber via a named child
11701
+ (and _isClobbered does not even probe getAttributeNames), so the
11702
+ neutralize step could itself be silently defeated, leaving the payload
11703
+ intact. A throw touches only the cached, clobber-safe remove() and
11704
+ getParentNode(). Generalizes GHSA-r47g-fvhr-h676 (clobbered-form root)
11705
+ to every root-kill reason. REPORT-3.
11706
+ This lives inside the catch, so it never fires for a normally-removed
11707
+ in-tree node: those have a parent, removeChild() succeeds, and the
11708
+ catch is not entered. Only a kept (parentless) root reaches here. */
11523
11709
  remove(node);
11710
+ if (!getParentNode(node)) {
11711
+ throw typeErrorCreate('a node selected for removal could not be detached from its tree ' + 'and cannot be safely returned; refusing to sanitize in place');
11712
+ }
11713
+ }
11714
+ };
11715
+ /**
11716
+ * _neutralizeRoot
11717
+ *
11718
+ * Fail-closed teardown of an in-place root after the sanitize walk aborts
11719
+ * (campaign-3 F2). An internal throw mid-walk — e.g. a page-registered
11720
+ * custom element's reaction detaches a node so `_forceRemove`'s deliberate
11721
+ * parentless guard throws, or any other re-entrant engine mutation — would
11722
+ * otherwise leave the caller's *live* tree half-sanitized, with everything
11723
+ * after the abort point still carrying its handlers. There is no safe way
11724
+ * to resume the walk (the tree mutated under us), so we strip the root bare:
11725
+ * remove every child and every attribute, then let the caller's catch see
11726
+ * the original error. Clobber-safe (cached `remove`/`childNodes`/`attributes`
11727
+ * getters; the root was already clobber-pre-flighted at the IN_PLACE entry).
11728
+ *
11729
+ * @param root the in-place root to empty
11730
+ */
11731
+ const _neutralizeRoot = function _neutralizeRoot(root) {
11732
+ const childNodes = getChildNodes(root);
11733
+ if (childNodes) {
11734
+ const snapshot = [];
11735
+ arrayForEach(childNodes, child => {
11736
+ arrayPush(snapshot, child);
11737
+ });
11738
+ arrayForEach(snapshot, child => {
11739
+ try {
11740
+ remove(child);
11741
+ } catch (_) {
11742
+ /* Best-effort teardown; a still-attached child is handled below */
11743
+ }
11744
+ });
11745
+ }
11746
+ const attributes = getAttributes(root);
11747
+ if (attributes) {
11748
+ for (let i = attributes.length - 1; i >= 0; --i) {
11749
+ const attribute = attributes[i];
11750
+ const name = attribute && attribute.name;
11751
+ if (typeof name === 'string') {
11752
+ try {
11753
+ root.removeAttribute(name);
11754
+ } catch (_) {
11755
+ /* Clobbered removeAttribute — ignore (fail-closed best effort) */
11756
+ }
11757
+ }
11758
+ }
11524
11759
  }
11525
11760
  };
11526
11761
  /**
@@ -11555,6 +11790,72 @@
11555
11790
  }
11556
11791
  }
11557
11792
  };
11793
+ /**
11794
+ * _stripDisallowedAttributes
11795
+ *
11796
+ * Removes every attribute the active configuration does not allow from a
11797
+ * single element, using the same allowlist as the main attribute pass (so
11798
+ * `on*` handlers go, but no `/^on/` blocklist is introduced). Used only to
11799
+ * neutralise nodes that are being discarded from an in-place tree.
11800
+ *
11801
+ * @param element the element to strip
11802
+ */
11803
+ const _stripDisallowedAttributes = function _stripDisallowedAttributes(element) {
11804
+ const attributes = getAttributes(element);
11805
+ if (!attributes) {
11806
+ return;
11807
+ }
11808
+ for (let i = attributes.length - 1; i >= 0; --i) {
11809
+ const attribute = attributes[i];
11810
+ const name = attribute && attribute.name;
11811
+ if (typeof name !== 'string' || ALLOWED_ATTR[transformCaseFunc(name)]) {
11812
+ continue;
11813
+ }
11814
+ try {
11815
+ element.removeAttribute(name);
11816
+ } catch (_) {
11817
+ /* Clobbered removeAttribute on a doomed node — ignore */
11818
+ }
11819
+ }
11820
+ };
11821
+ /**
11822
+ * _neutralizeSubtree
11823
+ *
11824
+ * Completes the audit-5 F1 fix across every removal path. The KEEP_CONTENT
11825
+ * move-hoist neutralises only disallowed-tag removals; clobber, mXSS-canary,
11826
+ * namespace, comment, processing-instruction and KEEP_CONTENT:false removals
11827
+ * all drop their subtree wholesale via `_forceRemove`. On the IN_PLACE path
11828
+ * those dropped nodes are detached from the caller's LIVE tree but a
11829
+ * handler-bearing original among them (an `<img onerror>`/`<video>` that was
11830
+ * loading) keeps its queued resource event, which fires in page scope after
11831
+ * sanitize returns. This walks a removed subtree and strips every attribute
11832
+ * the active configuration does not allow — so `on*` handlers are cancelled
11833
+ * through the SAME allowlist that governs kept nodes, not a separate `/^on/`
11834
+ * blocklist. Run synchronously before sanitize returns, i.e. before any
11835
+ * queued event can fire. Hook-free by design: these nodes leave the output,
11836
+ * so firing attribute hooks for them would be surprising. Clobber-safe reads;
11837
+ * a doomed clobbered node may shadow `removeAttribute` (its own attributes are
11838
+ * irrelevant — it is discarded — while its non-clobbered descendants, e.g.
11839
+ * the `<img>`, are reached and scrubbed).
11840
+ *
11841
+ * @param root the root of a removed subtree to neutralise
11842
+ */
11843
+ const _neutralizeSubtree = function _neutralizeSubtree(root) {
11844
+ const stack = [root];
11845
+ while (stack.length > 0) {
11846
+ const node = stack.pop();
11847
+ const nodeType = getNodeType ? getNodeType(node) : node.nodeType;
11848
+ if (nodeType === NODE_TYPE.element) {
11849
+ _stripDisallowedAttributes(node);
11850
+ }
11851
+ const childNodes = getChildNodes(node);
11852
+ if (childNodes) {
11853
+ for (let i = childNodes.length - 1; i >= 0; --i) {
11854
+ stack.push(childNodes[i]);
11855
+ }
11856
+ }
11857
+ }
11858
+ };
11558
11859
  /**
11559
11860
  * _initDocument
11560
11861
  *
@@ -11576,7 +11877,7 @@
11576
11877
  // Root of XHTML doc must contain xmlns declaration (see https://www.w3.org/TR/xhtml1/normative.html#strict)
11577
11878
  dirty = '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>' + dirty + '</body></html>';
11578
11879
  }
11579
- const dirtyPayload = trustedTypesPolicy ? trustedTypesPolicy.createHTML(dirty) : dirty;
11880
+ const dirtyPayload = trustedTypesPolicy ? _createTrustedHTML(dirty) : dirty;
11580
11881
  /*
11581
11882
  * Use the DOMParser API by default, fallback later if needs be
11582
11883
  * DOMParser not work for svg when has multiple root element.
@@ -11616,6 +11917,20 @@
11616
11917
  // eslint-disable-next-line no-bitwise
11617
11918
  NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT | NodeFilter.SHOW_PROCESSING_INSTRUCTION | NodeFilter.SHOW_CDATA_SECTION, null);
11618
11919
  };
11920
+ /**
11921
+ * Replace template expression syntax (mustache, ERB, template
11922
+ * literal) with a space; shared by all SAFE_FOR_TEMPLATES scrub
11923
+ * sites. Order matters: mustache, then ERB, then template literal.
11924
+ *
11925
+ * @param value the string to scrub
11926
+ * @returns the scrubbed string
11927
+ */
11928
+ const _stripTemplateExpressions = function _stripTemplateExpressions(value) {
11929
+ value = stringReplace(value, MUSTACHE_EXPR$1, ' ');
11930
+ value = stringReplace(value, ERB_EXPR$1, ' ');
11931
+ value = stringReplace(value, TMPLIT_EXPR$1, ' ');
11932
+ return value;
11933
+ };
11619
11934
  /**
11620
11935
  * Strip template-engine expressions ({{...}}, ${...}, <%...%>) from the
11621
11936
  * character data of an element subtree. Used as the final safety net for
@@ -11635,20 +11950,28 @@
11635
11950
  *
11636
11951
  * @param node The root element whose character data should be scrubbed.
11637
11952
  */
11638
- const _scrubTemplateExpressions = function _scrubTemplateExpressions(node) {
11953
+ const _scrubTemplateExpressions2 = function _scrubTemplateExpressions(node) {
11954
+ var _node$querySelectorAl;
11639
11955
  node.normalize();
11640
11956
  const walker = createNodeIterator.call(node.ownerDocument || node, node,
11641
11957
  // eslint-disable-next-line no-bitwise
11642
11958
  NodeFilter.SHOW_TEXT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_CDATA_SECTION | NodeFilter.SHOW_PROCESSING_INSTRUCTION, null);
11643
11959
  let currentNode = walker.nextNode();
11644
11960
  while (currentNode) {
11645
- let data = currentNode.data;
11646
- arrayForEach([MUSTACHE_EXPR$1, ERB_EXPR$1, TMPLIT_EXPR$1], expr => {
11647
- data = stringReplace(data, expr, ' ');
11648
- });
11649
- currentNode.data = data;
11961
+ currentNode.data = _stripTemplateExpressions(currentNode.data);
11650
11962
  currentNode = walker.nextNode();
11651
11963
  }
11964
+ // NodeIterator does not descend into <template>.content per the DOM spec,
11965
+ // so we must explicitly recurse into each template's content fragment,
11966
+ // mirroring the approach used by _sanitizeShadowDOM.
11967
+ const templates = (_node$querySelectorAl = node.querySelectorAll) === null || _node$querySelectorAl === void 0 ? void 0 : _node$querySelectorAl.call(node, 'template');
11968
+ if (templates) {
11969
+ arrayForEach(templates, tmpl => {
11970
+ if (_isDocumentFragment(tmpl.content)) {
11971
+ _scrubTemplateExpressions2(tmpl.content);
11972
+ }
11973
+ });
11974
+ }
11652
11975
  };
11653
11976
  /**
11654
11977
  * _isClobbered
@@ -11744,67 +12067,64 @@
11744
12067
  }
11745
12068
  };
11746
12069
  function _executeHooks(hooks, currentNode, data) {
12070
+ if (hooks.length === 0) {
12071
+ return;
12072
+ }
11747
12073
  arrayForEach(hooks, hook => {
11748
12074
  hook.call(DOMPurify, currentNode, data, CONFIG);
11749
12075
  });
11750
12076
  }
11751
12077
  /**
11752
- * _sanitizeElements
12078
+ * Structural-threat checks that condemn a node regardless of the
12079
+ * allowlists: mXSS via namespace confusion, risky CSS construction,
12080
+ * processing instructions, markup-bearing comments. Pure predicate;
12081
+ * the caller removes. Check order is load-bearing.
11753
12082
  *
11754
- * @protect nodeName
11755
- * @protect textContent
11756
- * @protect removeChild
11757
- * @param currentNode to check for permission to exist
11758
- * @return true if node was killed, false if left alive
12083
+ * @param currentNode the node to inspect
12084
+ * @param tagName the node's transformCaseFunc'd tag name
12085
+ * @return true if the node must be removed
11759
12086
  */
11760
- const _sanitizeElements = function _sanitizeElements(currentNode) {
11761
- let content = null;
11762
- /* Execute a hook if present */
11763
- _executeHooks(hooks.beforeSanitizeElements, currentNode, null);
11764
- /* Check if element is clobbered or can clobber */
11765
- if (_isClobbered(currentNode)) {
11766
- _forceRemove(currentNode);
11767
- return true;
11768
- }
11769
- /* Now let's check the element's type and name */
11770
- const tagName = transformCaseFunc(currentNode.nodeName);
11771
- /* Execute a hook if present */
11772
- _executeHooks(hooks.uponSanitizeElement, currentNode, {
11773
- tagName,
11774
- allowedTags: ALLOWED_TAGS
11775
- });
12087
+ const _isUnsafeNode = function _isUnsafeNode(currentNode, tagName) {
11776
12088
  /* Detect mXSS attempts abusing namespace confusion */
11777
- if (SAFE_FOR_XML && currentNode.hasChildNodes() && !_isNode(currentNode.firstElementChild) && regExpTest(/<[/\w!]/g, currentNode.innerHTML) && regExpTest(/<[/\w!]/g, currentNode.textContent)) {
11778
- _forceRemove(currentNode);
12089
+ if (SAFE_FOR_XML && currentNode.hasChildNodes() && !_isNode(currentNode.firstElementChild) && regExpTest(ELEMENT_MARKUP_PROBE, currentNode.textContent) && regExpTest(ELEMENT_MARKUP_PROBE, currentNode.innerHTML)) {
11779
12090
  return true;
11780
12091
  }
11781
12092
  /* Remove risky CSS construction leading to mXSS */
11782
12093
  if (SAFE_FOR_XML && currentNode.namespaceURI === HTML_NAMESPACE && tagName === 'style' && _isNode(currentNode.firstElementChild)) {
11783
- _forceRemove(currentNode);
11784
12094
  return true;
11785
12095
  }
11786
12096
  /* Remove any occurrence of processing instructions */
11787
- if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
11788
- _forceRemove(currentNode);
12097
+ if (currentNode.nodeType === NODE_TYPE.processingInstruction) {
11789
12098
  return true;
11790
12099
  }
11791
12100
  /* Remove any kind of possibly harmful comments */
11792
- if (SAFE_FOR_XML && currentNode.nodeType === NODE_TYPE.comment && regExpTest(/<[/\w]/g, currentNode.data)) {
11793
- _forceRemove(currentNode);
12101
+ if (SAFE_FOR_XML && currentNode.nodeType === NODE_TYPE.comment && regExpTest(COMMENT_MARKUP_PROBE, currentNode.data)) {
11794
12102
  return true;
11795
12103
  }
11796
- /* Remove element if anything forbids its presence */
11797
- if (FORBID_TAGS[tagName] || !(EXTRA_ELEMENT_HANDLING.tagCheck instanceof Function && EXTRA_ELEMENT_HANDLING.tagCheck(tagName)) && !ALLOWED_TAGS[tagName]) {
11798
- /* Check if we have a custom element to handle */
11799
- if (!FORBID_TAGS[tagName] && _isBasicCustomElement(tagName)) {
11800
- if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, tagName)) {
11801
- return false;
11802
- }
11803
- if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(tagName)) {
11804
- return false;
11805
- }
12104
+ return false;
12105
+ };
12106
+ /**
12107
+ * Handle a node whose tag is forbidden or not allowlisted: keep
12108
+ * allowed custom elements (false return exits _sanitizeElements
12109
+ * early - namespace/fallback checks and the afterSanitizeElements
12110
+ * hook are intentionally skipped for kept custom elements), else
12111
+ * hoist content per KEEP_CONTENT and remove.
12112
+ *
12113
+ * @param currentNode the disallowed node
12114
+ * @param tagName the node's transformCaseFunc'd tag name
12115
+ * @return true if the node was removed, false if kept
12116
+ */
12117
+ const _sanitizeDisallowedNode = function _sanitizeDisallowedNode(currentNode, tagName) {
12118
+ /* Check if we have a custom element to handle */
12119
+ if (!FORBID_TAGS[tagName] && _isBasicCustomElement(tagName)) {
12120
+ if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, tagName)) {
12121
+ return false;
11806
12122
  }
11807
- /* Keep content except for bad-listed elements.
12123
+ if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(tagName)) {
12124
+ return false;
12125
+ }
12126
+ }
12127
+ /* Keep content except for bad-listed elements.
11808
12128
  Use the cached prototype getters exclusively — the previous code
11809
12129
  had `|| currentNode.parentNode` / `|| currentNode.childNodes`
11810
12130
  fallbacks, but the cached getters always return the canonical
@@ -11812,20 +12132,72 @@
11812
12132
  path was dead in safe cases and a clobbering surface in unsafe
11813
12133
  ones. Falsy cached results stay falsy; the `if (childNodes &&
11814
12134
  parentNode)` check already gates correctly. */
11815
- if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
11816
- const parentNode = getParentNode(currentNode);
11817
- const childNodes = getChildNodes(currentNode);
11818
- if (childNodes && parentNode) {
11819
- const childCount = childNodes.length;
11820
- for (let i = childCount - 1; i >= 0; --i) {
11821
- const childClone = cloneNode(childNodes[i], true);
11822
- parentNode.insertBefore(childClone, getNextSibling(currentNode));
11823
- }
12135
+ if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
12136
+ const parentNode = getParentNode(currentNode);
12137
+ const childNodes = getChildNodes(currentNode);
12138
+ if (childNodes && parentNode) {
12139
+ const childCount = childNodes.length;
12140
+ /* In-place: hoist the *original* children so the iterator visits
12141
+ and sanitises them through the same allowlist pass as every other
12142
+ node. The caller built the tree in the live document, so the
12143
+ originals carry already-queued resource events (`<img onerror>`,
12144
+ `<video>`/`<audio>` error, lazy/`onload`, …); cloning would leave
12145
+ those originals detached but still armed, firing in page scope
12146
+ while the returned tree looked clean. Moving is safe in-place: the
12147
+ root is pre-validated as an allowed tag and so is never the node
12148
+ being removed, which keeps `parentNode` inside the iterator root
12149
+ and the relocated child inside the serialised tree.
12150
+ Otherwise (string / DOM-copy paths): clone. The iterator is rooted
12151
+ at — and the result serialised from — `body`, so a restrictive
12152
+ ALLOWED_TAGS that removes `body` itself must leave its content in
12153
+ place, which only cloning does; and those paths parse into an
12154
+ inert document, so their discarded originals never had a queued
12155
+ event to neutralise.
12156
+ `childNodes` is live; a tail-to-head walk keeps `childNodes[i]`
12157
+ valid whether we move (drops the trailing entry) or clone (leaves
12158
+ the list intact). */
12159
+ for (let i = childCount - 1; i >= 0; --i) {
12160
+ const hoisted = IN_PLACE ? childNodes[i] : cloneNode(childNodes[i], true);
12161
+ parentNode.insertBefore(hoisted, getNextSibling(currentNode));
11824
12162
  }
11825
12163
  }
12164
+ }
12165
+ _forceRemove(currentNode);
12166
+ return true;
12167
+ };
12168
+ /**
12169
+ * _sanitizeElements
12170
+ *
12171
+ * @protect nodeName
12172
+ * @protect textContent
12173
+ * @protect removeChild
12174
+ * @param currentNode to check for permission to exist
12175
+ * @return true if node was killed, false if left alive
12176
+ */
12177
+ const _sanitizeElements = function _sanitizeElements(currentNode) {
12178
+ /* Execute a hook if present */
12179
+ _executeHooks(hooks.beforeSanitizeElements, currentNode, null);
12180
+ /* Check if element is clobbered or can clobber */
12181
+ if (_isClobbered(currentNode)) {
12182
+ _forceRemove(currentNode);
12183
+ return true;
12184
+ }
12185
+ /* Now let's check the element's type and name */
12186
+ const tagName = transformCaseFunc(getNodeName ? getNodeName(currentNode) : currentNode.nodeName);
12187
+ /* Execute a hook if present */
12188
+ _executeHooks(hooks.uponSanitizeElement, currentNode, {
12189
+ tagName,
12190
+ allowedTags: ALLOWED_TAGS
12191
+ });
12192
+ /* Remove mXSS vectors, processing instructions and risky comments */
12193
+ if (_isUnsafeNode(currentNode, tagName)) {
11826
12194
  _forceRemove(currentNode);
11827
12195
  return true;
11828
12196
  }
12197
+ /* Remove element if anything forbids its presence */
12198
+ if (FORBID_TAGS[tagName] || !(EXTRA_ELEMENT_HANDLING.tagCheck instanceof Function && EXTRA_ELEMENT_HANDLING.tagCheck(tagName)) && !ALLOWED_TAGS[tagName]) {
12199
+ return _sanitizeDisallowedNode(currentNode, tagName);
12200
+ }
11829
12201
  /* Check whether element has a valid namespace.
11830
12202
  Realm-safe check (GHSA-hpcv-96wg-7vj8): use the cached Node.prototype
11831
12203
  nodeType getter rather than `instanceof Element`, which is realm-
@@ -11838,17 +12210,14 @@
11838
12210
  return true;
11839
12211
  }
11840
12212
  /* Make sure that older browsers don't get fallback-tag mXSS */
11841
- if ((tagName === 'noscript' || tagName === 'noembed' || tagName === 'noframes') && regExpTest(/<\/no(script|embed|frames)/i, currentNode.innerHTML)) {
12213
+ if ((tagName === 'noscript' || tagName === 'noembed' || tagName === 'noframes') && regExpTest(FALLBACK_TAG_CLOSE, currentNode.innerHTML)) {
11842
12214
  _forceRemove(currentNode);
11843
12215
  return true;
11844
12216
  }
11845
12217
  /* Sanitize element content to be template-safe */
11846
12218
  if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {
11847
12219
  /* Get the element's text content */
11848
- content = currentNode.textContent;
11849
- arrayForEach([MUSTACHE_EXPR$1, ERB_EXPR$1, TMPLIT_EXPR$1], expr => {
11850
- content = stringReplace(content, expr, ' ');
11851
- });
12220
+ const content = _stripTemplateExpressions(currentNode.textContent);
11852
12221
  if (currentNode.textContent !== content) {
11853
12222
  arrayPush(DOMPurify.removed, {
11854
12223
  element: currentNode.cloneNode()
@@ -11883,7 +12252,7 @@
11883
12252
  (https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes)
11884
12253
  XML-compatible (https://html.spec.whatwg.org/multipage/infrastructure.html#xml-compatible and http://www.w3.org/TR/xml/#d0e804)
11885
12254
  We don't need to check the value; it's always URI safe. */
11886
- if (ALLOW_DATA_ATTR && !FORBID_ATTR[lcName] && regExpTest(DATA_ATTR$1, lcName)) ; else if (ALLOW_ARIA_ATTR && regExpTest(ARIA_ATTR$1, lcName)) ; else if (!nameIsPermitted || FORBID_ATTR[lcName]) {
12255
+ if (ALLOW_DATA_ATTR && regExpTest(DATA_ATTR$1, lcName)) ; else if (ALLOW_ARIA_ATTR && regExpTest(ARIA_ATTR$1, lcName)) ; else if (!nameIsPermitted) {
11887
12256
  if (
11888
12257
  // First condition does a very basic check if a) it's basically a valid custom element tagname AND
11889
12258
  // b) if the tagName passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
@@ -11915,6 +12284,63 @@
11915
12284
  const _isBasicCustomElement = function _isBasicCustomElement(tagName) {
11916
12285
  return !RESERVED_CUSTOM_ELEMENT_NAMES[stringToLowerCase(tagName)] && regExpTest(CUSTOM_ELEMENT$1, tagName);
11917
12286
  };
12287
+ /**
12288
+ * Wrap an attribute value in the matching Trusted Types object when
12289
+ * the active policy requires it. Namespaced attributes pass through
12290
+ * unchanged (no TT support yet, see
12291
+ * https://bugs.chromium.org/p/chromium/issues/detail?id=1305293).
12292
+ *
12293
+ * @param lcTag lowercase tag name of the containing element
12294
+ * @param lcName lowercase attribute name
12295
+ * @param namespaceURI the attribute's namespace, if any
12296
+ * @param value the attribute value to wrap
12297
+ * @return the value, wrapped when Trusted Types demand it
12298
+ */
12299
+ const _applyTrustedTypesToAttribute = function _applyTrustedTypesToAttribute(lcTag, lcName, namespaceURI, value) {
12300
+ if (trustedTypesPolicy && typeof trustedTypes === 'object' && typeof trustedTypes.getAttributeType === 'function' && !namespaceURI) {
12301
+ switch (trustedTypes.getAttributeType(lcTag, lcName)) {
12302
+ case 'TrustedHTML':
12303
+ {
12304
+ return _createTrustedHTML(value);
12305
+ }
12306
+ case 'TrustedScriptURL':
12307
+ {
12308
+ return _createTrustedScriptURL(value);
12309
+ }
12310
+ }
12311
+ }
12312
+ return value;
12313
+ };
12314
+ /**
12315
+ * Write a modified attribute value back onto the element. On
12316
+ * success, re-probe for clobbering introduced by the new value and
12317
+ * remove the element when found; otherwise pop the removal entry
12318
+ * recorded by the earlier _removeAttribute (long-standing pairing
12319
+ * with the SANITIZE_NAMED_PROPS path - do not "fix" casually). On
12320
+ * failure, remove the attribute instead.
12321
+ *
12322
+ * @param currentNode the element carrying the attribute
12323
+ * @param name the attribute name as present on the element
12324
+ * @param namespaceURI the attribute's namespace, if any
12325
+ * @param value the new attribute value
12326
+ */
12327
+ const _setAttributeValue = function _setAttributeValue(currentNode, name, namespaceURI, value) {
12328
+ try {
12329
+ if (namespaceURI) {
12330
+ currentNode.setAttributeNS(namespaceURI, name, value);
12331
+ } else {
12332
+ /* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
12333
+ currentNode.setAttribute(name, value);
12334
+ }
12335
+ if (_isClobbered(currentNode)) {
12336
+ _forceRemove(currentNode);
12337
+ } else {
12338
+ arrayPop(DOMPurify.removed);
12339
+ }
12340
+ } catch (_) {
12341
+ _removeAttribute(name, currentNode);
12342
+ }
12343
+ };
11918
12344
  /**
11919
12345
  * _sanitizeAttributes
11920
12346
  *
@@ -11941,6 +12367,7 @@
11941
12367
  forceKeepAttr: undefined
11942
12368
  };
11943
12369
  let l = attributes.length;
12370
+ const lcTag = transformCaseFunc(currentNode.nodeName);
11944
12371
  /* Go backwards over all attributes; safely remove bad ones */
11945
12372
  while (l--) {
11946
12373
  const attr = attributes[l];
@@ -11978,7 +12405,7 @@
11978
12405
  _removeAttribute(name, currentNode);
11979
12406
  continue;
11980
12407
  }
11981
- /* Did the hooks approve of the attribute? */
12408
+ /* Did the hooks force-keep the attribute? */
11982
12409
  if (hookEvent.forceKeepAttr) {
11983
12410
  continue;
11984
12411
  }
@@ -11988,56 +12415,24 @@
11988
12415
  continue;
11989
12416
  }
11990
12417
  /* Work around a security issue in jQuery 3.0 */
11991
- if (!ALLOW_SELF_CLOSE_IN_ATTR && regExpTest(/\/>/i, value)) {
12418
+ if (!ALLOW_SELF_CLOSE_IN_ATTR && regExpTest(SELF_CLOSING_TAG, value)) {
11992
12419
  _removeAttribute(name, currentNode);
11993
12420
  continue;
11994
12421
  }
11995
12422
  /* Sanitize attribute content to be template-safe */
11996
12423
  if (SAFE_FOR_TEMPLATES) {
11997
- arrayForEach([MUSTACHE_EXPR$1, ERB_EXPR$1, TMPLIT_EXPR$1], expr => {
11998
- value = stringReplace(value, expr, ' ');
11999
- });
12424
+ value = _stripTemplateExpressions(value);
12000
12425
  }
12001
12426
  /* Is `value` valid for this attribute? */
12002
- const lcTag = transformCaseFunc(currentNode.nodeName);
12003
12427
  if (!_isValidAttribute(lcTag, lcName, value)) {
12004
12428
  _removeAttribute(name, currentNode);
12005
12429
  continue;
12006
12430
  }
12007
12431
  /* Handle attributes that require Trusted Types */
12008
- if (trustedTypesPolicy && typeof trustedTypes === 'object' && typeof trustedTypes.getAttributeType === 'function') {
12009
- if (namespaceURI) ; else {
12010
- switch (trustedTypes.getAttributeType(lcTag, lcName)) {
12011
- case 'TrustedHTML':
12012
- {
12013
- value = trustedTypesPolicy.createHTML(value);
12014
- break;
12015
- }
12016
- case 'TrustedScriptURL':
12017
- {
12018
- value = trustedTypesPolicy.createScriptURL(value);
12019
- break;
12020
- }
12021
- }
12022
- }
12023
- }
12432
+ value = _applyTrustedTypesToAttribute(lcTag, lcName, namespaceURI, value);
12024
12433
  /* Handle invalid data-* attribute set by try-catching it */
12025
12434
  if (value !== initValue) {
12026
- try {
12027
- if (namespaceURI) {
12028
- currentNode.setAttributeNS(namespaceURI, name, value);
12029
- } else {
12030
- /* Fallback to setAttribute() for browser-unrecognized namespaces e.g. "x-schema". */
12031
- currentNode.setAttribute(name, value);
12032
- }
12033
- if (_isClobbered(currentNode)) {
12034
- _forceRemove(currentNode);
12035
- } else {
12036
- arrayPop(DOMPurify.removed);
12037
- }
12038
- } catch (_) {
12039
- _removeAttribute(name, currentNode);
12040
- }
12435
+ _setAttributeValue(currentNode, name, namespaceURI, value);
12041
12436
  }
12042
12437
  }
12043
12438
  /* Execute a hook if present */
@@ -12079,9 +12474,9 @@
12079
12474
  iterator also surfaces. */
12080
12475
  const shadowNodeType = getNodeType ? getNodeType(shadowNode) : shadowNode.nodeType;
12081
12476
  if (shadowNodeType === NODE_TYPE.element) {
12082
- const innerSr = getShadowRoot ? getShadowRoot(shadowNode) : shadowNode.shadowRoot;
12477
+ const innerSr = getShadowRoot(shadowNode);
12083
12478
  if (_isDocumentFragment(innerSr)) {
12084
- _sanitizeAttachedShadowRoots2(innerSr);
12479
+ _sanitizeAttachedShadowRoots(innerSr);
12085
12480
  _sanitizeShadowDOM2(innerSr);
12086
12481
  }
12087
12482
  }
@@ -12108,46 +12503,81 @@
12108
12503
  *
12109
12504
  * @param root the subtree root to walk for attached shadow roots
12110
12505
  */
12111
- const _sanitizeAttachedShadowRoots2 = function _sanitizeAttachedShadowRoots(root) {
12112
- const nodeType = getNodeType ? getNodeType(root) : root.nodeType;
12113
- if (nodeType === NODE_TYPE.element) {
12114
- const sr = getShadowRoot ? getShadowRoot(root) : root.shadowRoot;
12115
- // Realm-safe check (GHSA-hpcv-96wg-7vj8): use nodeType-based
12116
- // detection rather than `instanceof DocumentFragment`, which is
12117
- // realm-bound and silently skipped shadow roots whose host element
12118
- // belonged to a foreign realm (e.g. iframe.contentDocument
12119
- // attachShadow). A foreign-realm ShadowRoot extends the foreign
12120
- // realm's DocumentFragment, not ours, so the old instanceof check
12121
- // returned false and the shadow subtree was never walked.
12122
- if (_isDocumentFragment(sr)) {
12123
- // Recurse first so that nested shadow roots are reached even if
12124
- // _sanitizeShadowDOM removes hosts at this level.
12125
- _sanitizeAttachedShadowRoots2(sr);
12126
- _sanitizeShadowDOM2(sr);
12506
+ const _sanitizeAttachedShadowRoots = function _sanitizeAttachedShadowRoots(root) {
12507
+ /* Iterative (explicit stack) rather than per-child recursion. DOM APIs
12508
+ impose no depth cap, so an attacker-shaped tree (JSON/CRDT/editor data
12509
+ built straight into the DOM — the IN_PLACE surface) deeper than the JS
12510
+ call-stack budget would otherwise overflow native recursion here and
12511
+ throw at the IN_PLACE entry pre-pass, before a single node is
12512
+ sanitized, leaving the caller's live tree untouched (fail-open). See
12513
+ campaign-3 F4. A heap stack keeps depth off the call stack.
12514
+ Each work item is either a node to descend into, or a deferred
12515
+ `_sanitizeShadowDOM` for an already-walked shadow root. The deferred
12516
+ form preserves the original post-order discipline: a shadow root's
12517
+ nested shadow roots are discovered before the outer shadow is
12518
+ sanitized (which may remove hosts). Pushes are in reverse of the
12519
+ desired processing order (LIFO): template content, then children, then
12520
+ the shadow-sanitize, then the shadow walk — so the order matches the
12521
+ previous recursion exactly. */
12522
+ const stack = [{
12523
+ node: root,
12524
+ shadow: null
12525
+ }];
12526
+ while (stack.length > 0) {
12527
+ const item = stack.pop();
12528
+ /* Deferred shadow-DOM sanitisation: runs after its subtree was walked. */
12529
+ if (item.shadow) {
12530
+ _sanitizeShadowDOM2(item.shadow);
12531
+ continue;
12127
12532
  }
12128
- }
12129
- // Snapshot children before recursing. Sanitization of one subtree
12130
- // (e.g. via an uponSanitizeShadowNode hook) may detach siblings,
12131
- // and naive nextSibling traversal would silently skip the rest of
12132
- // the list once a node is detached.
12133
- const childNodes = getChildNodes ? getChildNodes(root) : root.childNodes;
12134
- if (!childNodes) {
12135
- return;
12136
- }
12137
- const snapshot = [];
12138
- arrayForEach(childNodes, child => {
12139
- arrayPush(snapshot, child);
12140
- });
12141
- for (const child of snapshot) {
12142
- _sanitizeAttachedShadowRoots2(child);
12143
- }
12144
- /* When the root is a <template>, also descend into root.content */
12145
- if (nodeType === NODE_TYPE.element) {
12146
- const rootName = getNodeName ? getNodeName(root) : null;
12147
- if (typeof rootName === 'string' && transformCaseFunc(rootName) === 'template') {
12148
- const content = root.content;
12149
- if (_isDocumentFragment(content)) {
12150
- _sanitizeAttachedShadowRoots2(content);
12533
+ const node = item.node;
12534
+ const nodeType = getNodeType ? getNodeType(node) : node.nodeType;
12535
+ const isElement = nodeType === NODE_TYPE.element;
12536
+ /* (pushed last processed first) Children, snapshotted in reverse so
12537
+ the first child is processed first. Snapshotting matters because a
12538
+ hook may detach siblings mid-walk. */
12539
+ const childNodes = getChildNodes(node);
12540
+ if (childNodes) {
12541
+ for (let i = childNodes.length - 1; i >= 0; --i) {
12542
+ stack.push({
12543
+ node: childNodes[i],
12544
+ shadow: null
12545
+ });
12546
+ }
12547
+ }
12548
+ /* (pushed before children → processed after them, matching the old
12549
+ "template content last" order) When the node is a <template>,
12550
+ descend into its content. */
12551
+ if (isElement) {
12552
+ const rootName = getNodeName ? getNodeName(node) : null;
12553
+ if (typeof rootName === 'string' && transformCaseFunc(rootName) === 'template') {
12554
+ const content = node.content;
12555
+ if (_isDocumentFragment(content)) {
12556
+ stack.push({
12557
+ node: content,
12558
+ shadow: null
12559
+ });
12560
+ }
12561
+ }
12562
+ }
12563
+ /* Shadow root (processed first): walk its subtree, then sanitise it.
12564
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
12565
+ rather than `instanceof DocumentFragment`, which is realm-bound and
12566
+ silently skipped foreign-realm shadow roots (e.g.
12567
+ iframe.contentDocument attachShadow). */
12568
+ if (isElement) {
12569
+ const sr = getShadowRoot(node);
12570
+ if (_isDocumentFragment(sr)) {
12571
+ /* Push the deferred sanitise first so it pops after the shadow
12572
+ walk we push next, i.e. nested shadow roots are discovered
12573
+ before this one is sanitised. */
12574
+ stack.push({
12575
+ node: null,
12576
+ shadow: sr
12577
+ }, {
12578
+ node: sr,
12579
+ shadow: null
12580
+ });
12151
12581
  }
12152
12582
  }
12153
12583
  }
@@ -12183,11 +12613,14 @@
12183
12613
  }
12184
12614
  /* Clean up removed elements */
12185
12615
  DOMPurify.removed = [];
12186
- /* Check if dirty is correctly typed for IN_PLACE */
12187
- if (typeof dirty === 'string') {
12188
- IN_PLACE = false;
12189
- }
12190
- if (IN_PLACE) {
12616
+ /* Resolve IN_PLACE for this call without mutating persistent config.
12617
+ Writing the IN_PLACE closure variable here leaks under setConfig(),
12618
+ where _parseConfig is skipped on later calls: a single string call would
12619
+ disable in-place mode for every subsequent node call, returning a
12620
+ sanitized copy while leaving the caller's node — which in-place callers
12621
+ keep using and whose return value they ignore — unsanitized. REPORT-2. */
12622
+ const inPlace = IN_PLACE && typeof dirty !== 'string' && _isNode(dirty);
12623
+ if (inPlace) {
12191
12624
  /* Do some early pre-sanitization to avoid unsafe root nodes.
12192
12625
  Read nodeName through the cached prototype getter — a clobbering
12193
12626
  child named "nodeName" on the form root would otherwise shadow
@@ -12214,8 +12647,16 @@
12214
12647
  throw typeErrorCreate('root node is clobbered and cannot be sanitized in-place');
12215
12648
  }
12216
12649
  /* Sanitize attached shadow roots before the main iterator runs.
12217
- The iterator does not descend into shadow trees. */
12218
- _sanitizeAttachedShadowRoots2(dirty);
12650
+ The iterator does not descend into shadow trees. Same fail-closed
12651
+ barrier as the main walk (campaign-3 F2): a custom-element reaction
12652
+ inside a shadow root could abort this pre-pass before the walk runs,
12653
+ which would otherwise leave the entire live tree unsanitized. */
12654
+ try {
12655
+ _sanitizeAttachedShadowRoots(dirty);
12656
+ } catch (error) {
12657
+ _neutralizeRoot(dirty);
12658
+ throw error;
12659
+ }
12219
12660
  } else if (_isNode(dirty)) {
12220
12661
  /* If dirty is a DOM element, append to an empty document to avoid
12221
12662
  elements being stripped by the parser */
@@ -12235,13 +12676,13 @@
12235
12676
  descend into shadow trees. The walk routes every read through a
12236
12677
  cached prototype getter so clobbering descendants on a form root
12237
12678
  cannot hide a shadow host from this pass. */
12238
- _sanitizeAttachedShadowRoots2(importedNode);
12679
+ _sanitizeAttachedShadowRoots(importedNode);
12239
12680
  } else {
12240
12681
  /* Exit directly if we have nothing to do */
12241
12682
  if (!RETURN_DOM && !SAFE_FOR_TEMPLATES && !WHOLE_DOCUMENT &&
12242
12683
  // eslint-disable-next-line unicorn/prefer-includes
12243
12684
  dirty.indexOf('<') === -1) {
12244
- return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(dirty) : dirty;
12685
+ return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? _createTrustedHTML(dirty) : dirty;
12245
12686
  }
12246
12687
  /* Initialize the document to work on */
12247
12688
  body = _initDocument(dirty);
@@ -12255,32 +12696,59 @@
12255
12696
  _forceRemove(body.firstChild);
12256
12697
  }
12257
12698
  /* Get node iterator */
12258
- const nodeIterator = _createNodeIterator(IN_PLACE ? dirty : body);
12259
- /* Now start iterating over the created document */
12260
- while (currentNode = nodeIterator.nextNode()) {
12261
- /* Sanitize tags and elements */
12262
- _sanitizeElements(currentNode);
12263
- /* Check attributes next */
12264
- _sanitizeAttributes(currentNode);
12265
- /* Shadow DOM detected, sanitize it.
12266
- Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
12267
- instead of instanceof, so foreign-realm <template>.content is
12268
- walked correctly. */
12269
- if (_isDocumentFragment(currentNode.content)) {
12270
- _sanitizeShadowDOM2(currentNode.content);
12699
+ const nodeIterator = _createNodeIterator(inPlace ? dirty : body);
12700
+ /* Now start iterating over the created document.
12701
+ The walk runs inside an exception barrier (campaign-3 F2): a re-entrant
12702
+ engine/custom-element mutation can detach a node mid-walk so
12703
+ `_forceRemove`'s parentless guard throws, aborting the loop. Without the
12704
+ barrier the caller's in-place tree would be left half-sanitized with the
12705
+ unvisited tail still armed. On any throw we fail closed — strip the
12706
+ in-place root bare then rethrow so the existing throw contract is
12707
+ preserved. (String/DOM-copy paths never return the partial body, so the
12708
+ propagating throw is already fail-closed there.) */
12709
+ try {
12710
+ while (currentNode = nodeIterator.nextNode()) {
12711
+ /* Sanitize tags and elements */
12712
+ _sanitizeElements(currentNode);
12713
+ /* Check attributes next */
12714
+ _sanitizeAttributes(currentNode);
12715
+ /* Shadow DOM detected, sanitize it.
12716
+ Realm-safe check (GHSA-hpcv-96wg-7vj8): nodeType-based detection
12717
+ instead of instanceof, so foreign-realm <template>.content is
12718
+ walked correctly. */
12719
+ if (_isDocumentFragment(currentNode.content)) {
12720
+ _sanitizeShadowDOM2(currentNode.content);
12721
+ }
12722
+ }
12723
+ } catch (error) {
12724
+ if (inPlace) {
12725
+ _neutralizeRoot(dirty);
12271
12726
  }
12727
+ throw error;
12272
12728
  }
12273
12729
  /* If we sanitized `dirty` in-place, return it. */
12274
- if (IN_PLACE) {
12730
+ if (inPlace) {
12731
+ /* Fail-closed completion of the audit-5 F1 fix: every node removed from
12732
+ the caller's live tree is detached but may still hold a queued
12733
+ resource-event handler that fires in page scope after we return. The
12734
+ move-hoist covers only disallowed-tag KEEP_CONTENT removals; strip the
12735
+ non-allow-listed attributes off every other removed subtree (clobber,
12736
+ mXSS, namespace, comments, KEEP_CONTENT:false, …) so those handlers are
12737
+ cancelled before any event can fire. Runs synchronously, pre-return. */
12738
+ arrayForEach(DOMPurify.removed, entry => {
12739
+ if (entry.element) {
12740
+ _neutralizeSubtree(entry.element);
12741
+ }
12742
+ });
12275
12743
  if (SAFE_FOR_TEMPLATES) {
12276
- _scrubTemplateExpressions(dirty);
12744
+ _scrubTemplateExpressions2(dirty);
12277
12745
  }
12278
12746
  return dirty;
12279
12747
  }
12280
12748
  /* Return sanitized string or DOM */
12281
12749
  if (RETURN_DOM) {
12282
12750
  if (SAFE_FOR_TEMPLATES) {
12283
- _scrubTemplateExpressions(body);
12751
+ _scrubTemplateExpressions2(body);
12284
12752
  }
12285
12753
  if (RETURN_DOM_FRAGMENT) {
12286
12754
  returnNode = createDocumentFragment.call(body.ownerDocument);
@@ -12310,11 +12778,9 @@
12310
12778
  }
12311
12779
  /* Sanitize final string template-safe */
12312
12780
  if (SAFE_FOR_TEMPLATES) {
12313
- arrayForEach([MUSTACHE_EXPR$1, ERB_EXPR$1, TMPLIT_EXPR$1], expr => {
12314
- serializedHTML = stringReplace(serializedHTML, expr, ' ');
12315
- });
12781
+ serializedHTML = _stripTemplateExpressions(serializedHTML);
12316
12782
  }
12317
- return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(serializedHTML) : serializedHTML;
12783
+ return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? _createTrustedHTML(serializedHTML) : serializedHTML;
12318
12784
  };
12319
12785
  DOMPurify.setConfig = function () {
12320
12786
  let cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
@@ -12324,6 +12790,12 @@
12324
12790
  DOMPurify.clearConfig = function () {
12325
12791
  CONFIG = null;
12326
12792
  SET_CONFIG = false;
12793
+ // Drop any caller-supplied Trusted Types policy so it cannot poison later
12794
+ // `RETURN_TRUSTED_TYPE` output. The internal default policy (cached, and
12795
+ // never recreated — Trusted Types throws on duplicate names) is restored by
12796
+ // the next `_parseConfig`. See GHSA-vxr8-fq34-vvx9.
12797
+ trustedTypesPolicy = defaultTrustedTypesPolicy;
12798
+ emptyHTML = '';
12327
12799
  };
12328
12800
  DOMPurify.isValidAttribute = function (tag, attr, value) {
12329
12801
  /* Initialize shared config vars if necessary. */
@@ -76662,6 +77134,9 @@ ${indent}columns: ${matrix.columns}
76662
77134
  indexURL: r.getAttribute("index"),
76663
77135
  order: idx
76664
77136
  };
77137
+ if (r.hasAttribute("format")) {
77138
+ config.format = r.getAttribute("format");
77139
+ }
76665
77140
  resourceMap.set(config.url, config);
76666
77141
  if (!hasTrackElements) {
76667
77142
  tracks.push(config);
@@ -76749,6 +77224,13 @@ ${indent}columns: ${matrix.columns}
76749
77224
 
76750
77225
  config.name = track.getAttribute("name");
76751
77226
 
77227
+ if(track.hasAttribute("type")) {
77228
+ config.type = track.getAttribute("type");
77229
+ }
77230
+ if(track.hasAttribute("format")) {
77231
+ config.format = track.getAttribute("format");
77232
+ }
77233
+
76752
77234
  const color = track.getAttribute("color");
76753
77235
  if (color) {
76754
77236
  config.color = "rgb(" + color + ")";
@@ -77060,7 +77542,7 @@ ${indent}columns: ${matrix.columns}
77060
77542
  })
77061
77543
  }
77062
77544
 
77063
- const _version = "3.8.1";
77545
+ const _version = "3.8.2";
77064
77546
  function version() {
77065
77547
  return _version
77066
77548
  }