You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

8 months ago
  1. /*!
  2. * Bootstrap sanitizer.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 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. // eslint-disable-next-line unicorn/better-regex
  64. const SAFE_URL_PATTERN = /^(?!javascript:)(?:[a-z0-9+.-]+:|[^&:/?#]*(?:[/?#]|$))/i;
  65. const allowedAttribute = (attribute, allowedAttributeList) => {
  66. const attributeName = attribute.nodeName.toLowerCase();
  67. if (allowedAttributeList.includes(attributeName)) {
  68. if (uriAttributes.has(attributeName)) {
  69. return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue));
  70. }
  71. return true;
  72. }
  73. // Check if a regular expression validates the attribute.
  74. return allowedAttributeList.filter(attributeRegex => attributeRegex instanceof RegExp).some(regex => regex.test(attributeName));
  75. };
  76. function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) {
  77. if (!unsafeHtml.length) {
  78. return unsafeHtml;
  79. }
  80. if (sanitizeFunction && typeof sanitizeFunction === 'function') {
  81. return sanitizeFunction(unsafeHtml);
  82. }
  83. const domParser = new window.DOMParser();
  84. const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html');
  85. const elements = [].concat(...createdDocument.body.querySelectorAll('*'));
  86. for (const element of elements) {
  87. const elementName = element.nodeName.toLowerCase();
  88. if (!Object.keys(allowList).includes(elementName)) {
  89. element.remove();
  90. continue;
  91. }
  92. const attributeList = [].concat(...element.attributes);
  93. const allowedAttributes = [].concat(allowList['*'] || [], allowList[elementName] || []);
  94. for (const attribute of attributeList) {
  95. if (!allowedAttribute(attribute, allowedAttributes)) {
  96. element.removeAttribute(attribute.nodeName);
  97. }
  98. }
  99. }
  100. return createdDocument.body.innerHTML;
  101. }
  102. exports.DefaultAllowlist = DefaultAllowlist;
  103. exports.sanitizeHtml = sanitizeHtml;
  104. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  105. }));
  106. //# sourceMappingURL=sanitizer.js.map