瀏覽代碼

Merge pull request #1343 from transloadit/fix/taint

thumbnail-generator: do not export tainted canvas, fixes #1321
Artur Paikin 6 年之前
父節點
當前提交
9e5ff894ec
共有 1 個文件被更改,包括 14 次插入1 次删除
  1. 14 1
      packages/@uppy/thumbnail-generator/src/index.js

+ 14 - 1
packages/@uppy/thumbnail-generator/src/index.js

@@ -170,9 +170,22 @@ module.exports = class ThumbnailGenerator extends Plugin {
    * @return {Promise}
    * @return {Promise}
    */
    */
   canvasToBlob (canvas, type, quality) {
   canvasToBlob (canvas, type, quality) {
+    try {
+      canvas.getContext('2d').getImageData(0, 0, 1, 1)
+    } catch (err) {
+      if (err.code === 18) {
+        return Promise.reject(new Error('cannot read image, probably an svg with external resources'))
+      }
+    }
+
     if (canvas.toBlob) {
     if (canvas.toBlob) {
       return new Promise(resolve => {
       return new Promise(resolve => {
         canvas.toBlob(resolve, type, quality)
         canvas.toBlob(resolve, type, quality)
+      }).then((blob) => {
+        if (blob === null) {
+          throw new Error('cannot read image, probably an svg with external resources')
+        }
+        return blob
       })
       })
     }
     }
     return Promise.resolve().then(() => {
     return Promise.resolve().then(() => {
@@ -217,7 +230,7 @@ module.exports = class ThumbnailGenerator extends Plugin {
           this.uppy.emit('thumbnail:generated', this.uppy.getFile(file.id), preview)
           this.uppy.emit('thumbnail:generated', this.uppy.getFile(file.id), preview)
         })
         })
         .catch(err => {
         .catch(err => {
-          this.uppy.log(`[ThumbnailGenerator] Failed thumbnail for ${file.id}`)
+          this.uppy.log(`[ThumbnailGenerator] Failed thumbnail for ${file.id}:`, 'warning')
           this.uppy.log(err, 'warning')
           this.uppy.log(err, 'warning')
           this.uppy.emit('thumbnail:error', this.uppy.getFile(file.id), err)
           this.uppy.emit('thumbnail:error', this.uppy.getFile(file.id), err)
         })
         })