wikijs-fork/server/modules/rendering/html-security/renderer.js

43 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

const { JSDOM } = require('jsdom')
const createDOMPurify = require('dompurify')
2018-09-16 04:35:03 +00:00
2019-12-12 04:35:54 +00:00
module.exports = {
async init(input, config) {
if (config.safeHTML) {
const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)
const allowedAttrs = ['v-pre', 'v-slot:tabs', 'v-slot:content', 'target']
const allowedTags = ['tabset', 'template']
if (config.allowDrawIoUnsafe) {
allowedTags.push('foreignObject')
DOMPurify.addHook('uponSanitizeElement', (elm) => {
if (elm.querySelectorAll) {
const breaks = elm.querySelectorAll('foreignObject br, foreignObject p')
if (breaks && breaks.length) {
for (let i = 0; i < breaks.length; i++) {
breaks[i].parentNode.replaceChild(
window.document.createElement('div'),
breaks[i]
)
}
}
}
})
}
if (config.allowIFrames) {
allowedTags.push('iframe')
allowedAttrs.push('allow')
}
input = DOMPurify.sanitize(input, {
ADD_ATTR: allowedAttrs,
ADD_TAGS: allowedTags
2019-12-12 04:35:54 +00:00
})
}
return input
2018-09-16 04:35:03 +00:00
}
}