Skip to content

Commit 32b7ea3

Browse files
author
Kristján Oddsson
authored
Merge pull request from GHSA-gpfj-4j6g-c4w9
Fix Clipboard-based DOM XSS
2 parents c07aff4 + 4bb7b1a commit 32b7ea3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/paste-markdown-table.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ function generateText(transfer: DataTransfer): string | undefined {
8787
const html = transfer.getData('text/html')
8888
if (!/<table/i.test(html)) return
8989

90-
const el = document.createElement('div')
91-
el.innerHTML = html
92-
let table = el.querySelector('table')
90+
const parser = new DOMParser()
91+
const parsedDocument = parser.parseFromString(html, 'text/html')
92+
93+
let table = parsedDocument.querySelector('table')
9394
table = !table || table.closest('[data-paste-markdown-skip]') ? null : table
9495
if (!table) return
9596

test/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ describe('paste-markdown', function () {
3838
assert.include(textarea.value, 'name | origin\n-- | --\nhubot | github\nbender | futurama')
3939
})
4040

41+
it("doesn't execute JavaScript", async function () {
42+
let alertCalled = false
43+
window.secretFunction = function () {
44+
alertCalled = true
45+
}
46+
const data = {
47+
'text/html': `XSS<img/src/onerror=secretFunction()><table>`
48+
}
49+
paste(textarea, data)
50+
51+
await wait(100)
52+
53+
assert.isFalse(alertCalled, 'A XSS was possible as alert was called')
54+
})
55+
4156
it('retains text around tables', async function () {
4257
const data = {
4358
'text/html': `
@@ -97,3 +112,7 @@ function paste(textarea, data) {
97112
})
98113
textarea.dispatchEvent(event)
99114
}
115+
116+
function wait(ms) {
117+
return new Promise(resolve => setTimeout(resolve, ms))
118+
}

0 commit comments

Comments
 (0)