|
|
|
@ -1,21 +1,17 @@
|
|
|
|
|
const {ejs} = require("../basic")
|
|
|
|
|
const {ejs, ElemJS} = require("../basic")
|
|
|
|
|
const hljs = require("highlight.js")
|
|
|
|
|
const {HighlightedCode} = require("./components")
|
|
|
|
|
const DOMPurify = require("dompurify")
|
|
|
|
|
const {resolveMxc} = require("../functions")
|
|
|
|
|
const {MatrixEvent} = require("./event")
|
|
|
|
|
|
|
|
|
|
const purifier = DOMPurify()
|
|
|
|
|
|
|
|
|
|
purifier.setConfig({
|
|
|
|
|
ALLOWED_URI_REGEXP: /^mxc:\/\/[a-zA-Z0-9\.]+\/[a-zA-Z0-9]+$/, // As per the spec we only allow mxc uris
|
|
|
|
|
ALLOWED_TAGS: ['font', 'del', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'sup', 'sub', 'li', 'b', 'i', 'u', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'caption', 'pre', 'span', 'img'],
|
|
|
|
|
purifier.addHook("uponSanitizeAttribute", (node, hookevent, config) => {
|
|
|
|
|
//If purifier already rejected an attribute there is no point in checking it
|
|
|
|
|
if (hookevent.keepAttr === false) return;
|
|
|
|
|
|
|
|
|
|
// In case we mess up none of those attributes should read to XSS
|
|
|
|
|
ALLOWED_ATTR: ["data-mx-bg-color", "data-mx-color", "color",
|
|
|
|
|
"name", "target", "href",
|
|
|
|
|
"width", "height", "alt", "title", "src", "data-mx-emoticon",
|
|
|
|
|
"start", "class"],
|
|
|
|
|
//Custom config option that allows the array of attributes for a given tag
|
|
|
|
|
ALLOWED_ATTR_CUSTOM: {
|
|
|
|
|
const allowedElementAttributes = {
|
|
|
|
|
"FONT": ["data-mx-bg-color", "data-mx-color", "color"],
|
|
|
|
|
"SPAN": ["data-mx-bg-color", "data-mx-color", "color"],
|
|
|
|
|
"A": ["name", "target", "href"],
|
|
|
|
@ -23,58 +19,73 @@ purifier.setConfig({
|
|
|
|
|
"OL": ["start"],
|
|
|
|
|
"CODE": ["class"],
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//Handle our custom tag
|
|
|
|
|
purifier.addHook("uponSanitizeAttribute", (node, hookevent, config) => {
|
|
|
|
|
//If purifier already rejected an attribute there is no point in checking it
|
|
|
|
|
if (hookevent.keepAttr === false) return;
|
|
|
|
|
|
|
|
|
|
const allowed_attributes = config.ALLOWED_ATTR_CUSTOM[node.tagName] || []
|
|
|
|
|
const allowed_attributes = allowedElementAttributes[node.tagName] || []
|
|
|
|
|
hookevent.keepAttr = allowed_attributes.indexOf(hookevent.attrName) > -1;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//Remove bad classes from our code element
|
|
|
|
|
purifier.addHook("uponSanitizeElement", (node, hookevent, config) => {
|
|
|
|
|
if (node.tagName != "CODE") return
|
|
|
|
|
if (node.tagName == "CODE") {
|
|
|
|
|
node.classList.forEach(c => {
|
|
|
|
|
if (!c.startsWith("language-")) {
|
|
|
|
|
node.classList.remove(c)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return node
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
purifier.addHook("afterSanitizeAttributes", (node, hookevent, config) => {
|
|
|
|
|
if (node.tagName == "IMG") {
|
|
|
|
|
let src = node.getAttribute("src")
|
|
|
|
|
if (src) src = resolveMxc(src)
|
|
|
|
|
|
|
|
|
|
node.setAttribute("src", src)
|
|
|
|
|
} else if (node.tagName == "A") {
|
|
|
|
|
}
|
|
|
|
|
if (node.tagName == "A") {
|
|
|
|
|
node.setAttribute("rel", "noopener")
|
|
|
|
|
} else if (node.tagName == "FONT" || node.tagName == "SPAN") {
|
|
|
|
|
const color = node.getAttribute("data-mx-color")
|
|
|
|
|
const bgColor = node.getAttribute("data-mx-bg-color")
|
|
|
|
|
if (color) node.style.color = color;
|
|
|
|
|
if (bgColor) node.style.backgroundColor = bgColor;
|
|
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function cleanHTML(html) {
|
|
|
|
|
const config = {
|
|
|
|
|
ALLOWED_URI_REGEXP: /^mxc:\/\/[a-zA-Z0-9\.]+\/[a-zA-Z0-9]+$/, // As per the spec we only allow mxc uris
|
|
|
|
|
ALLOWED_TAGS: ['font', 'del', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'sup', 'sub', 'li', 'b', 'i', 'u', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'caption', 'pre', 'span', 'img'],
|
|
|
|
|
|
|
|
|
|
function sanitize(html) {
|
|
|
|
|
// In case we mess up in the uponSanitizeAttribute hook
|
|
|
|
|
ALLOWED_ATTR: ["data-mx-bg-color", "data-mx-color", "color",
|
|
|
|
|
"name", "target", "href",
|
|
|
|
|
"width", "height", "alt", "title", "src", "data-mx-emoticon",
|
|
|
|
|
"start", "class"],
|
|
|
|
|
}
|
|
|
|
|
return purifier.sanitize(html)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Here we put all the processing of the messages that isn't as likely to potentially lead to security issues
|
|
|
|
|
function postProcessElements(rootNode) {
|
|
|
|
|
const element = rootNode.element
|
|
|
|
|
element.querySelectorAll("code").forEach((n) => rootNode.child(new HighlightedCode(n)))
|
|
|
|
|
|
|
|
|
|
element.querySelectorAll("img").forEach((n) => {
|
|
|
|
|
let src = n.getAttribute("src")
|
|
|
|
|
if (src) src = resolveMxc(src)
|
|
|
|
|
n.setAttribute("src", src)
|
|
|
|
|
})
|
|
|
|
|
element.querySelectorAll("font, span").forEach((n) => {
|
|
|
|
|
const color = n.getAttribute("data-mx-color") || n.getAttribute("color")
|
|
|
|
|
const bgColor = n.getAttribute("data-mx-bg-color")
|
|
|
|
|
if (color) n.style.color = color;
|
|
|
|
|
if (bgColor) n.style.backgroundColor = bgColor;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HTMLMessage extends MatrixEvent {
|
|
|
|
|
render() {
|
|
|
|
|
super.render()
|
|
|
|
|
this.clearChildren()
|
|
|
|
|
|
|
|
|
|
let html = this.data.content.formatted_body
|
|
|
|
|
const content = ejs("div")
|
|
|
|
|
html = sanitize(html)
|
|
|
|
|
|
|
|
|
|
html = cleanHTML(html)
|
|
|
|
|
content.html(html)
|
|
|
|
|
postProcessElements(content)
|
|
|
|
|
|
|
|
|
|
this.child(content)
|
|
|
|
|
|
|
|
|
|
super.render()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static canRender(event) {
|
|
|
|
@ -91,8 +102,8 @@ class HTMLMessage extends MatrixEvent {
|
|
|
|
|
|
|
|
|
|
class TextMessage extends MatrixEvent {
|
|
|
|
|
render() {
|
|
|
|
|
super.render()
|
|
|
|
|
return this.text(this.data.content.body)
|
|
|
|
|
this.text(this.data.content.body)
|
|
|
|
|
return super.render()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static canRender(event) {
|
|
|
|
|
style: can we put spaces after // ?