fix: correctly handle allowTaint canvas cloning (#2649)

This commit is contained in:
Niklas von Hertzen 2021-08-13 17:32:55 +08:00 committed by GitHub
parent 2b4de68e92
commit c378e22069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import {Context} from '../core/context';
export interface CloneOptions { export interface CloneOptions {
ignoreElements?: (element: Element) => boolean; ignoreElements?: (element: Element) => boolean;
onclone?: (document: Document, element: HTMLElement) => void; onclone?: (document: Document, element: HTMLElement) => void;
allowTaint?: boolean;
} }
export interface WindowOptions { export interface WindowOptions {
@ -201,14 +202,16 @@ export class DocumentCloner {
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
const clonedCtx = clonedCanvas.getContext('2d'); const clonedCtx = clonedCanvas.getContext('2d');
if (clonedCtx) { if (clonedCtx) {
if (ctx) { if (!this.options.allowTaint && ctx) {
clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0); clonedCtx.putImageData(ctx.getImageData(0, 0, canvas.width, canvas.height), 0, 0);
} else { } else {
clonedCtx.drawImage(canvas, 0, 0); clonedCtx.drawImage(canvas, 0, 0);
} }
} }
return clonedCanvas; return clonedCanvas;
} catch (e) {} } catch (e) {
this.context.logger.info(`Unable to clone canvas as it is tainted`);
}
return clonedCanvas; return clonedCanvas;
} }

View File

@ -74,6 +74,7 @@ const renderElement = async (element: HTMLElement, opts: Partial<Options>): Prom
const foreignObjectRendering = opts.foreignObjectRendering ?? false; const foreignObjectRendering = opts.foreignObjectRendering ?? false;
const cloneOptions: CloneConfigurations = { const cloneOptions: CloneConfigurations = {
allowTaint: opts.allowTaint ?? false,
onclone: opts.onclone, onclone: opts.onclone,
ignoreElements: opts.ignoreElements, ignoreElements: opts.ignoreElements,
inlineImages: foreignObjectRendering, inlineImages: foreignObjectRendering,