Skip to content

Commit

Permalink
Use DOMParser to safely parse html without script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Venegas committed Aug 8, 2018
1 parent ae22be5 commit 16b6a6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ class Clipboard extends Module {
} else if (!html) {
return new Delta().insert(text || '');
}
const container = this.quill.root.ownerDocument.createElement('div');
container.innerHTML = html.replace(/>\r?\n +</g, '><'); // Remove spaces between tags
const doc = new DOMParser().parseFromString(
html.replace(/>\r?\n +</g, '><'), // Remove spaces between tags
'text/html',
);
const container = doc.body;
const nodeMatches = new WeakMap();
const [elementMatchers, textMatchers] = this.prepareMatching(
container,
Expand Down
11 changes: 10 additions & 1 deletion test/unit/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,20 @@ describe('Clipboard', function() {
expect(delta).toEqual(expected);
});

it('does not execute javascript', function() {
window.unsafeFunction = jasmine.createSpy('unsafeFunction');
const html =
"<img src='/assets/favicon.png' onload='window.unsafeFunction()'/>";
this.clipboard.convert({ html });
expect(window.unsafeFunction).not.toHaveBeenCalled();
delete window.unsafeFunction;
});

it('xss', function() {
const delta = this.clipboard.convert({
html: '<script>alert(2);</script>',
});
expect(delta).toEqual(new Delta().insert('alert(2);'));
expect(delta).toEqual(new Delta().insert(''));
});
});
});

0 comments on commit 16b6a6c

Please sign in to comment.