2019-02-11 16:59:39 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2022-12-24 18:37:22 +02:00
|
|
|
* Bootstrap (v5.3.0-alpha1): util/sanitizer.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2019-02-11 16:59:39 +02:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2021-09-15 14:34:24 +03:00
|
|
|
const uriAttributes = new Set([
|
2019-02-11 16:59:39 +02:00
|
|
|
'background',
|
|
|
|
'cite',
|
|
|
|
'href',
|
|
|
|
'itemtype',
|
|
|
|
'longdesc',
|
|
|
|
'poster',
|
|
|
|
'src',
|
|
|
|
'xlink:href'
|
2020-05-02 16:56:23 +03:00
|
|
|
])
|
2019-02-11 16:59:39 +02:00
|
|
|
|
|
|
|
const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pattern that recognizes a commonly useful subset of URLs that are safe.
|
|
|
|
*
|
2022-07-18 10:02:41 +02:00
|
|
|
* Shout-out to Angular https://github.com/angular/angular/blob/12.2.x/packages/core/src/sanitization/url_sanitizer.ts
|
2019-02-11 16:59:39 +02:00
|
|
|
*/
|
2021-09-29 07:41:06 +03:00
|
|
|
const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file|sms):|[^#&/:?]*(?:[#/?]|$))/i
|
2019-02-11 16:59:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A pattern that matches safe data URLs. Only matches image, video and audio types.
|
|
|
|
*
|
2022-07-18 10:02:41 +02:00
|
|
|
* Shout-out to Angular https://github.com/angular/angular/blob/12.2.x/packages/core/src/sanitization/url_sanitizer.ts
|
2019-02-11 16:59:39 +02:00
|
|
|
*/
|
2019-12-24 18:23:17 +02:00
|
|
|
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
|
2019-02-11 16:59:39 +02:00
|
|
|
|
2021-09-15 14:34:24 +03:00
|
|
|
const allowedAttribute = (attribute, allowedAttributeList) => {
|
|
|
|
const attributeName = attribute.nodeName.toLowerCase()
|
2019-02-11 16:59:39 +02:00
|
|
|
|
2021-09-15 14:34:24 +03:00
|
|
|
if (allowedAttributeList.includes(attributeName)) {
|
|
|
|
if (uriAttributes.has(attributeName)) {
|
|
|
|
return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue) || DATA_URL_PATTERN.test(attribute.nodeValue))
|
2019-02-11 16:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a regular expression validates the attribute.
|
2021-10-06 00:23:14 +03:00
|
|
|
return allowedAttributeList.filter(attributeRegex => attributeRegex instanceof RegExp)
|
2021-10-07 17:48:36 +03:00
|
|
|
.some(regex => regex.test(attributeName))
|
2019-02-11 16:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 09:31:37 +01:00
|
|
|
export const DefaultAllowlist = {
|
2019-02-23 00:37:55 +02:00
|
|
|
// Global attributes allowed on any supplied element below.
|
|
|
|
'*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
|
|
|
|
a: ['target', 'href', 'title', 'rel'],
|
|
|
|
area: [],
|
|
|
|
b: [],
|
|
|
|
br: [],
|
|
|
|
col: [],
|
|
|
|
code: [],
|
|
|
|
div: [],
|
|
|
|
em: [],
|
|
|
|
hr: [],
|
|
|
|
h1: [],
|
|
|
|
h2: [],
|
|
|
|
h3: [],
|
|
|
|
h4: [],
|
|
|
|
h5: [],
|
|
|
|
h6: [],
|
|
|
|
i: [],
|
2020-01-07 22:46:28 +02:00
|
|
|
img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
|
2019-02-23 00:37:55 +02:00
|
|
|
li: [],
|
|
|
|
ol: [],
|
|
|
|
p: [],
|
|
|
|
pre: [],
|
|
|
|
s: [],
|
|
|
|
small: [],
|
|
|
|
span: [],
|
|
|
|
sub: [],
|
|
|
|
sup: [],
|
|
|
|
strong: [],
|
|
|
|
u: [],
|
|
|
|
ul: []
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
export function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) {
|
2019-02-23 00:37:55 +02:00
|
|
|
if (!unsafeHtml.length) {
|
2019-02-11 16:59:39 +02:00
|
|
|
return unsafeHtml
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
if (sanitizeFunction && typeof sanitizeFunction === 'function') {
|
|
|
|
return sanitizeFunction(unsafeHtml)
|
2019-02-11 16:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const domParser = new window.DOMParser()
|
|
|
|
const createdDocument = domParser.parseFromString(unsafeHtml, 'text/html')
|
2020-03-25 15:35:02 +01:00
|
|
|
const elements = [].concat(...createdDocument.body.querySelectorAll('*'))
|
2019-02-11 16:59:39 +02:00
|
|
|
|
2021-08-10 18:07:39 +03:00
|
|
|
for (const element of elements) {
|
2021-09-15 14:34:24 +03:00
|
|
|
const elementName = element.nodeName.toLowerCase()
|
2019-02-11 16:59:39 +02:00
|
|
|
|
2021-09-15 14:34:24 +03:00
|
|
|
if (!Object.keys(allowList).includes(elementName)) {
|
|
|
|
element.remove()
|
2019-02-11 16:59:39 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:34:24 +03:00
|
|
|
const attributeList = [].concat(...element.attributes)
|
|
|
|
const allowedAttributes = [].concat(allowList['*'] || [], allowList[elementName] || [])
|
2019-02-11 16:59:39 +02:00
|
|
|
|
2021-07-30 09:28:51 +03:00
|
|
|
for (const attribute of attributeList) {
|
2021-09-15 14:34:24 +03:00
|
|
|
if (!allowedAttribute(attribute, allowedAttributes)) {
|
|
|
|
element.removeAttribute(attribute.nodeName)
|
2019-02-11 16:59:39 +02:00
|
|
|
}
|
2021-07-30 09:28:51 +03:00
|
|
|
}
|
2019-02-11 16:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return createdDocument.body.innerHTML
|
|
|
|
}
|