Skip to content

Commit

Permalink
🐛 Wrap serializeVideos in try/catch to catch security errors (#848)
Browse files Browse the repository at this point in the history
Sometimes it's not possible to serialize videos into poster due to
security (`Tainted canvases may not be exported.`). Wrapping the
`serializeVideos` function call in a try/catch will allow the DOM serialization
to continue
  • Loading branch information
Robdel12 authored Mar 29, 2022
1 parent 18fce17 commit fabb11a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/dom/src/serialize-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export function serializeVideos(dom, clone) {
let canvas = document.createElement('canvas');
let width = canvas.width = video.videoWidth;
let height = canvas.height = video.videoHeight;
let dataUrl;

canvas.getContext('2d').drawImage(video, 0, 0, width, height);
try { dataUrl = canvas.toDataURL(); } catch {}

let dataUrl = canvas.toDataURL();
// If the canvas produces a blank image, skip
if (!dataUrl || dataUrl === 'data:,') continue;

Expand Down
11 changes: 11 additions & 0 deletions packages/dom/test/serialize-videos.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ describe('serializeVideos', () => {
$ = parseDOM(serializeDOM());
expect($('#video')[0].getAttribute('poster')).toBe(null);
});

it('does not hang serialization when there is an error thrown', () => {
withExample(`
<video src="//:0" id="video" />
`);

spyOn(window.HTMLCanvasElement.prototype, 'toDataURL').and.throwError(new Error('An error'));

$ = parseDOM(serializeDOM());
expect($('#video')[0].getAttribute('poster')).toBe(null);
});
});

0 comments on commit fabb11a

Please sign in to comment.