sanitizer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. * Bootstrap sanitizer.js v5.3.7 (https://getbootstrap.com/)
  3. * Copyright 2011-2025 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  8. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Sanitizer = {}));
  10. })(this, (function (exports) { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap util/sanitizer.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. // js-docs-start allow-list
  18. const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i;
  19. const DefaultAllowlist = {
  20. // Global attributes allowed on any supplied element below.
  21. '*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
  22. a: ['target', 'href', 'title', 'rel'],
  23. area: [],
  24. b: [],
  25. br: [],
  26. col: [],
  27. code: [],
  28. dd: [],
  29. div: [],
  30. dl: [],
  31. dt: [],
  32. em: [],
  33. hr: [],
  34. h1: [],
  35. h2: [],
  36. h3: [],
  37. h4: [],
  38. h5: [],
  39. h6: [],
  40. i: [],
  41. img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
  42. li: [],
  43. ol: [],
  44. p: [],
  45. pre: [],
  46. s: [],
  47. small: [],
  48. span: [],
  49. sub: [],
  50. sup: [],
  51. strong: [],
  52. u: [],
  53. ul: []
  54. };
  55. // js-docs-end allow-list
  56. const uriAttributes = new Set(['background', 'cite', 'href', 'itemtype', 'longdesc', 'poster', 'src', 'xlink:href']);
  57. /**
  58. * A pattern that recognizes URLs that are safe wrt. XSS in URL navigation
  59. * contexts.
  60. *
  61. * Shout-out to Angular https://github.com/angular/angular/blob/15.2.8/packages/core/src/sanitization/url_sanitizer.ts#L38
  62. */
  63. const SAFE_URL_PATTERN = /^(?!javascript:)(?:[a-z0-9+.-]+:|[^&:/?#]*(?:[/?#]|$))/i;
  64. const allowedAttribute = (attribute, allowedAttributeList) => {
  65. const attributeName = attribute.nodeName.toLowerCase();
  66. if (allowedAttributeList.includes(attributeName)) {
  67. if (uriAttributes.has(attributeName)) {
  68. return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue));
  69. }
  70. return true;
  71. }
  72. // Check if a regular expression validates the attribute.
  73. return allowedAttributeList.filter(attributeRegex => attributeRegex instanceof RegExp).some(regex => regex.test(attributeName));
  74. };
  75. function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) {
  76. if (!unsafeHtml.length) {
  77. return unsafeHtml;
  78. }
  79. if (sanitizeFunction && typeof sanitizeFunction === 'function') {
  80. return sanitizeFunction(unsafeHtml);
  81. }
  82. const domParser = new window.DOMParser();
  83. const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html');
  84. const elements = [].concat(...createdDocument.body.querySelectorAll('*'));
  85. for (const element of elements) {
  86. const elementName = element.nodeName.toLowerCase();
  87. if (!Object.keys(allowList).includes(elementName)) {
  88. element.remove();
  89. continue;
  90. }
  91. const attributeList = [].concat(...element.attributes);
  92. const allowedAttributes = [].concat(allowList['*'] || [], allowList[elementName] || []);
  93. for (const attribute of attributeList) {
  94. if (!allowedAttribute(attribute, allowedAttributes)) {
  95. element.removeAttribute(attribute.nodeName);
  96. }
  97. }
  98. }
  99. return createdDocument.body.innerHTML;
  100. }
  101. exports.DefaultAllowlist = DefaultAllowlist;
  102. exports.sanitizeHtml = sanitizeHtml;
  103. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  104. }));
  105. //# sourceMappingURL=sanitizer.js.map