Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE10 and Cross-origin resource sharing (CORS) issues with Image / Canvas

Tags:

I was under the impression that Internet Explorer 10 fully supported CORS, but now I'm not sure.

We have a JS/HTML5 App that uses multiple domains, and reads image data. We are loading images in the JS from another domain, imageDraw()ing the image to our canvas, and then using getImageData on the canvas. (We aren't using cross-domain XMLHttpRequests). For this to work we have had to set response headers on the server that's serving the images:

access-control-allow-origin: * access-control-allow-credentials: true 

And set this on the image object in the JS before loading:

image.crossOrigin = 'Anonymous'; //Also tried lowercase

This is working fine for all new browsers, apart from IE10 which is throwing security errors when we try to read the data.

SCRIPT5022: SecurityError 

Is there something more that needs to be done for IE10 to treat these cross domain images as not tainting?

UPDATE:

I noticed this answer to a previous question. Interestingly this JSFiddle also does not work for IE10 - can anyone confirm that this does not work in their IE10?

like image 990
UpTheCreek Avatar asked Jun 06 '13 07:06

UpTheCreek


People also ask

How do you allow cross-origin use of images and canvas?

The key is to use the crossorigin attribute by setting crossOrigin on the HTMLImageElement into which the image will be loaded. This tells the browser to request cross-origin access when trying to download the image data.

How is CORS Cross-Origin Resource Sharing possible?

CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.

How do I allow CORS Access-Control allow origin?

Simply add a header to your HttpServletResponse by calling addHeader : response. addHeader("Access-Control-Allow-Origin", "*");

How do you deal with CORS problems?

Solving CORS error the right way For example, if you want to solve this on the express. js then all you have to do is use a certain middleware that will handle the origin key. But, for any non-standard HTTP request like PUT, PATCH, DELETE, you'll have to preflight them.


2 Answers

Unfortunately, IE10 still remains the only popular browser that doesn't support CORS for image drawn to Canvas even when CORS headers are properly set. But there is workaround for that via XMLHttpRequest:

var xhr = new XMLHttpRequest(); xhr.onload = function () {     var url = URL.createObjectURL(this.response), img = new Image();     img.onload = function () {         // here you can use img for drawing to canvas and handling         // ...         // don't forget to free memory up when you're done (you can do this as soon as image is drawn to canvas)         URL.revokeObjectURL(url);     };     img.src = url; }; xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.send(); 
like image 53
RReverser Avatar answered Nov 11 '22 16:11

RReverser


Confirmed: IE10 does not support CORS images in an HTML 5 canvas. See RReverser's answer for a workaround.

Edit

Sorry, I haven't dealt with CORS images before and thought this question was about an AJAX request.

According to Mozilla Developer Network you need to set image.crossOrigin to anonymous or use-credentials. Also, according to that page today, these attributes are not supported in IE, Safari, or Opera. This test was made to demonstrate that IE9 did not support it and it seems that same test still fails in IE10, so even if Safari and Opera have added support since the MDN article was written, it is quite possible that IE10 still lacks support.

The only tip I can give you is that in general, allow-credentials is incompatible with a wildcard allow-origin. Either drop the allow-credentials or echo the request Origin in the allow-origin.

Below is for AJAX calls, not image or video canvas resources

Early versions of IE10 were known to have AJAX bugs,

  • http://bugs.idsl.pl/ie/xhr.htm
  • http://connect.microsoft.com/IE/feedback/details/771552/i-e-10-and-the-post-method

so it could be another browser bug. Then again, CORS is deceptively tricky to get right. I recommend the following steps to debug CORS problems.

  1. Point the browser at http://test-cors.appspot.com/#technical to get a compatibility report. If anything fails then you have a bug or lack of support for CORS in the browser.
  2. If everything passes, use the CORS headers the test is sending as a starting point to get your CORS request working. Then change one header at a time and retest until you get the headers the way you want for your application or you run into a failure you cannot explain or work around.
  3. If necessary, post a question about that one tiny change that breaks the CORS request, posting both the working "before" and the failing "after". It always helps if you can include a runnable example.

The complete code for the CORS test client and server (Python script for Google App Engine) is available at https://github.com/rapportive-oss/cors-test to get you started.

like image 29
Old Pro Avatar answered Nov 11 '22 17:11

Old Pro