Skip to content

Commit

Permalink
Merge pull request from GHSA-gpfj-4j6g-c4w9
Browse files Browse the repository at this point in the history
Fix Clipboard-based DOM XSS
  • Loading branch information
Kristján Oddsson authored Aug 12, 2021
2 parents c07aff4 + 4bb7b1a commit 32b7ea3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/paste-markdown-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ function generateText(transfer: DataTransfer): string | undefined {
const html = transfer.getData('text/html')
if (!/<table/i.test(html)) return

const el = document.createElement('div')
el.innerHTML = html
let table = el.querySelector('table')
const parser = new DOMParser()
const parsedDocument = parser.parseFromString(html, 'text/html')

let table = parsedDocument.querySelector('table')
table = !table || table.closest('[data-paste-markdown-skip]') ? null : table
if (!table) return

Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ describe('paste-markdown', function () {
assert.include(textarea.value, 'name | origin\n-- | --\nhubot | github\nbender | futurama')
})

it("doesn't execute JavaScript", async function () {
let alertCalled = false
window.secretFunction = function () {
alertCalled = true
}
const data = {
'text/html': `XSS<img/src/onerror=secretFunction()><table>`
}
paste(textarea, data)

await wait(100)

assert.isFalse(alertCalled, 'A XSS was possible as alert was called')
})

it('retains text around tables', async function () {
const data = {
'text/html': `
Expand Down Expand Up @@ -97,3 +112,7 @@ function paste(textarea, data) {
})
textarea.dispatchEvent(event)
}

function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

0 comments on commit 32b7ea3

Please # to comment.