Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas tainted by cross-origin data

Tags:

I'm loading a motion jpeg from third-party site, which I can trust. I'm trying to getImageData() but the browser (Chrome 23.0) complains that:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data. 

There are some similar questions on SO, but they are using local file and I'm using third party media. My script runs on a shared server and I don't own the remote server.

I tried img.crossOrigin = 'Anonymous' or img.crossOrigin = '' (see this post on the Chromium blog about CORS), but it didn't help. Any idea on how can I getImageData on a canvas with cross-origin data? Thanks!

like image 253
clwen Avatar asked Dec 02 '12 22:12

clwen


People also ask

How do you make a canvas not tainted?

By loading the canvas from cross origin domain, you are tainting the canvas. You can prevent this by setting crossorigin="anonymous" . However, CRAZILY enough, the order of the attribute on the img element does matter.

What is a tainted canvas?

A tainted canvas is one which is no longer considered secure, and any attempts to retrieve image data back from the canvas will cause an exception to be thrown.

How can you tell if a canvas is tainted?

The website requires a canvas with images from a different origin which does not allow Cross Origin Resource Sharing (CORS). A canvas is considered 'tainted' by a browser if it has loaded images from a different origin that does not set the Access-Control-Allow-Origin header correctly.

How do you Untaint a canvas?

Also, there is no way to "untaint" a canvas, even if clearRect is called on the full area of the canvas, the canvas will still be flagged as tainted. You have to create a new canvas and redraw everything except the compromised part in such cases.


1 Answers

You cannot reset the crossOrigin flag once it is tainted, but if you know before hand what the image is you can convert it to a data url, see Drawing an image from a data URL to a canvas

But no, you cannot and should not be using getImageData() from external sources that don't support CORS

like image 132
Paul Kaplan Avatar answered Sep 28 '22 08:09

Paul Kaplan