Don't render SVG nodes if it taints canvas

This commit is contained in:
MoyuScript
2017-08-13 13:11:03 +08:00
parent 0e17851ba6
commit 0c00c85029
3 changed files with 47 additions and 10 deletions

View File

@@ -7,6 +7,8 @@ import type Logger from './Logger';
export type ImageElement = Image | HTMLCanvasElement;
type ImageCache = {[string]: Promise<?ImageElement>};
import FEATURES from './Feature';
export default class ImageLoader {
origin: string;
options: Options;
@@ -30,10 +32,20 @@ export default class ImageLoader {
return src;
}
if (this.options.allowTaint === true || this.isInlineImage(src) || this.isSameOrigin(src)) {
return this.addImage(src, src);
} else if (typeof this.options.proxy === 'string' && !this.isSameOrigin(src)) {
// TODO proxy
if (isSVG(src)) {
if (this.options.allowTaint === true || FEATURES.SUPPORT_SVG_DRAWING) {
return this.addImage(src, src);
}
} else {
if (
this.options.allowTaint === true ||
isInlineImage(src) ||
this.isSameOrigin(src)
) {
return this.addImage(src, src);
} else if (typeof this.options.proxy === 'string' && !this.isSameOrigin(src)) {
// TODO proxy
}
}
}
@@ -43,10 +55,6 @@ export default class ImageLoader {
return key;
}
isInlineImage(src: string): boolean {
return /data:image\/.*;base64,/i.test(src);
}
hasImageInCache(key: string): boolean {
return typeof this.cache[key] !== 'undefined';
}
@@ -112,3 +120,11 @@ export class ImageStore {
return index === -1 ? null : this._images[index];
}
}
const INLINE_SVG = /^data:image\/svg\+xml/i;
const INLINE_IMG = /^data:image\/.*;base64,/i;
const isInlineImage = (src: string): boolean => INLINE_IMG.test(src);
const isSVG = (src: string): boolean =>
src.substr(-3).toLowerCase() === 'svg' || INLINE_SVG.test(src);