Pārlūkot izejas kodu

thumbnail-generator: do not export tainted canvas

When an SVG image includes external resources, we're not allowed to read
the resized version from the canvas.

Trying to do so would log a SecurityError. Now, Uppy still logs a
warning when `debug: true`, but is silent when `debug: false`.
Renée Kooi 6 gadi atpakaļ
vecāks
revīzija
514004549e
1 mainītis faili ar 14 papildinājumiem un 1 dzēšanām
  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)
         })
         })