Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap (v4.6.2): tools/sanitizer.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/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', 'srcset', '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 https://github.com/angular/angular/blob/12.2.x/packages/core/src/sanitization/url_sanitizer.ts
59
 */
60
const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file|sms):|[^#&/:?]*(?:[#/?]|$))/i
61
 
62
/**
63
 * A pattern that matches safe data URLs. Only matches image, video and audio types.
64
 *
65
 * Shoutout to Angular https://github.com/angular/angular/blob/12.2.x/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,[\d+/a-z]+=*$/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(SAFE_URL_PATTERN.test(attr.nodeValue) || DATA_URL_PATTERN.test(attr.nodeValue))
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, len = regExp.length; i < len; i++) {
84
    if (regExp[i].test(attrName)) {
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
    // eslint-disable-next-line unicorn/prefer-spread
118
    const whitelistedAttributes = [].concat(whiteList['*'] || [], whiteList[elName] || [])
119
 
120
    attributeList.forEach(attr => {
121
      if (!allowedAttribute(attr, whitelistedAttributes)) {
122
        el.removeAttribute(attr.nodeName)
123
      }
124
    })
125
  }
126
 
127
  return createdDocument.body.innerHTML
128
}