Layout von Websiten mit Bootstrap und Foundation
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.

sanitizer.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.5.0): tools/sanitizer.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. const uriAttrs = [
  8. 'background',
  9. 'cite',
  10. 'href',
  11. 'itemtype',
  12. 'longdesc',
  13. 'poster',
  14. 'src',
  15. 'xlink:href'
  16. ]
  17. const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
  18. export const DefaultWhitelist = {
  19. // Global attributes allowed on any supplied element below.
  20. '*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
  21. a: ['target', 'href', 'title', 'rel'],
  22. area: [],
  23. b: [],
  24. br: [],
  25. col: [],
  26. code: [],
  27. div: [],
  28. em: [],
  29. hr: [],
  30. h1: [],
  31. h2: [],
  32. h3: [],
  33. h4: [],
  34. h5: [],
  35. h6: [],
  36. i: [],
  37. img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
  38. li: [],
  39. ol: [],
  40. p: [],
  41. pre: [],
  42. s: [],
  43. small: [],
  44. span: [],
  45. sub: [],
  46. sup: [],
  47. strong: [],
  48. u: [],
  49. ul: []
  50. }
  51. /**
  52. * A pattern that recognizes a commonly useful subset of URLs that are safe.
  53. *
  54. * Shoutout to Angular 7 https://github.com/angular/angular/blob/7.2.4/packages/core/src/sanitization/url_sanitizer.ts
  55. */
  56. const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file):|[^#&/:?]*(?:[#/?]|$))/gi
  57. /**
  58. * A pattern that matches safe data URLs. Only matches image, video and audio types.
  59. *
  60. * Shoutout to Angular 7 https://github.com/angular/angular/blob/7.2.4/packages/core/src/sanitization/url_sanitizer.ts
  61. */
  62. const DATA_URL_PATTERN = /^data:(?:image\/(?:bmp|gif|jpeg|jpg|png|tiff|webp)|video\/(?:mpeg|mp4|ogg|webm)|audio\/(?:mp3|oga|ogg|opus));base64,[\d+/a-z]+=*$/i
  63. function allowedAttribute(attr, allowedAttributeList) {
  64. const attrName = attr.nodeName.toLowerCase()
  65. if (allowedAttributeList.indexOf(attrName) !== -1) {
  66. if (uriAttrs.indexOf(attrName) !== -1) {
  67. return Boolean(attr.nodeValue.match(SAFE_URL_PATTERN) || attr.nodeValue.match(DATA_URL_PATTERN))
  68. }
  69. return true
  70. }
  71. const regExp = allowedAttributeList.filter((attrRegex) => attrRegex instanceof RegExp)
  72. // Check if a regular expression validates the attribute.
  73. for (let i = 0, len = regExp.length; i < len; i++) {
  74. if (attrName.match(regExp[i])) {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. export function sanitizeHtml(unsafeHtml, whiteList, sanitizeFn) {
  81. if (unsafeHtml.length === 0) {
  82. return unsafeHtml
  83. }
  84. if (sanitizeFn && typeof sanitizeFn === 'function') {
  85. return sanitizeFn(unsafeHtml)
  86. }
  87. const domParser = new window.DOMParser()
  88. const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html')
  89. const whitelistKeys = Object.keys(whiteList)
  90. const elements = [].slice.call(createdDocument.body.querySelectorAll('*'))
  91. for (let i = 0, len = elements.length; i < len; i++) {
  92. const el = elements[i]
  93. const elName = el.nodeName.toLowerCase()
  94. if (whitelistKeys.indexOf(el.nodeName.toLowerCase()) === -1) {
  95. el.parentNode.removeChild(el)
  96. continue
  97. }
  98. const attributeList = [].slice.call(el.attributes)
  99. const whitelistedAttributes = [].concat(whiteList['*'] || [], whiteList[elName] || [])
  100. attributeList.forEach((attr) => {
  101. if (!allowedAttribute(attr, whitelistedAttributes)) {
  102. el.removeAttribute(attr.nodeName)
  103. }
  104. })
  105. }
  106. return createdDocument.body.innerHTML
  107. }