File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed
Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -87,9 +87,10 @@ function generateText(transfer: DataTransfer): string | undefined {
8787 const html = transfer . getData ( 'text/html' )
8888 if ( ! / < t a b l e / 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
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments