Skip to content

Commit 1a5c68a

Browse files
author
Alberto Fernández-Capel
authored
Merge pull request #1149 from basecamp/paste-html-sanitize
Sanitize HTML content in data-trix-* attributes
2 parents 841ff19 + 14bac18 commit 1a5c68a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/test/system/pasting_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ testGroup("Pasting", { template: "editor_empty" }, () => {
104104
delete window.unsanitized
105105
})
106106

107+
test("paste data-trix-attachment unsafe html", async () => {
108+
window.unsanitized = []
109+
const pasteData = {
110+
"text/plain": "x",
111+
"text/html": `\
112+
copy<div data-trix-attachment="{&quot;contentType&quot;:&quot;text/html&quot;,&quot;content&quot;:&quot;&lt;img src=1 onerror=window.unsanitized.push(1)&gt;HELLO123&quot;}"></div>me
113+
`,
114+
}
115+
116+
await pasteContent(pasteData)
117+
await delay(20)
118+
assert.deepEqual(window.unsanitized, [])
119+
delete window.unsanitized
120+
})
121+
107122
test("prefers plain text when html lacks formatting", async () => {
108123
const pasteData = {
109124
"text/html": "<meta charset='utf-8'>a\nb",

src/trix/models/html_parser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ const blockForAttributes = (attributes = {}, htmlAttributes = {}) => {
4040

4141
const parseTrixDataAttribute = (element, name) => {
4242
try {
43-
return JSON.parse(element.getAttribute(`data-trix-${name}`))
43+
const data = JSON.parse(element.getAttribute(`data-trix-${name}`))
44+
45+
if (data.contentType === "text/html" && data.content) {
46+
data.content = HTMLSanitizer.sanitize(data.content).getHTML()
47+
}
48+
49+
return data
4450
} catch (error) {
4551
return {}
4652
}

0 commit comments

Comments
 (0)