JAVASCRIPT   24

sanitizer js

Guest on 6th July 2022 01:14:01 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.