openseadragon: CrossOriginPolicy for OSD Plugins not Working

Does anyone know why or how to fix a CORS issue for OpenSeadragon + ViaWebGL Plugin? Right now I have my Viewer and TileSources and TiledImages all routed with “use-credentials” for the deployment and false for local dev. But when using the ViaWebGL Plugin the whole TileSource turns black and no tiles load in the canvas. I made sure to pass the same crossOriginPolicy to the custom "tile-loaded" and "tile-drawing" handlers in the ViaWebGL plugin and inside on the .getContext("webgl", { crossOrigin }) is set to the same. But still on deployments I get zero errors, just a black canvas.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (15 by maintainers)

Most upvoted comments

I suppose you could do the old-school trick of proxying all of your image data from the foreign server to your local server and then up to the client. Obviously that’s not ideal, but if you have no other option…

Completion callback does not ‘throw’ (have) any race conditions. If you get the callback function synchronously, the tile load event will wait for you to call it. If you get the callback inside onload function which gets possibly called after the tile load event finished, no completion callback will help you. How is the event supposed to know whether you’ve finished? The race condition here is whether you manage to retrieve the completion callback before the event ends when called inside image onload.

e.image.onload = e.getCompletionCallback();

I’m just guessing here, but could this line be leading to a race condition of some kind, where some tiles are calling the completion callback with the original data and others with the processed data?

This can be done as far as I know in two ways, either the crossorigin which you cannot use, or properly setting Access-Control-Allow-Origin headers on the server side.

I agree with @Aiosa - this is all about whether the data is tainted or not.

Regardless of your client-side settings, the server must set the Access-Control-Allow-Origin header appropriately if you want access to the pixel data. So if that isn’t configured correctly and you don’t control it, you’re out of luck. If Access-Control-Allow-Origin isn’t set, the image data is considered “tainted” and pixel access is not allowed. The upshot is that the “basic” viewer can display the pixels but as soon as you try to access the pixel data there will be problems. WebGL textures provide access to pixels, so the browser won’t let you do this for tainted data. That’s why it isn’t a problem until you try to use a plugin that uses webgl.

I pulled the code you abstracted from the original webgl plugin and tried it and was able to reproduce the error with CORS and a tainted Canvas on my application, but without adding either plugin to OSD I do not get the errors

Then it seems the problem is really loading the texture to a GPU. If you do not use these plugins, you do not use GPU (at least directly) and therefore you don’t see any errors. As I¨ve already noted:

IMHO image objects can be transformed into a canvas and can be still tainted, the issue shows up once you try to load it to a GPU

From MDN:

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.

OSD will just use the tainted canvas to render into another canvas so that seems to be OK (does not leave the same context…?), but if you try reading the data for textures, it internally tries to pull the data out of the canvas. So you need to ensure the canvas is not tainted. This can be done as far as I know in two ways, either the crossorigin which you cannot use, or properly setting Access-Control-Allow-Origin headers on the server side.

As soon as you draw into a canvas any data that was loaded from another origin without CORS approval, the canvas becomes tainted.

And even with the upcoming release of v5 of osd. This plugin is the only one that brings WebGL Shader technology in.

Since we now support webgl via the WebGLDrawer, it will be easier to integrate custom shaders moving forward. The goal with the initial implementation was just to get the tile composition pipeline working and tested. The next step will be adding a mechanism to allow users to provide custom fragment shaders. I think there are reasonable ways to approach this that won’t be too difficult and won’t add too much complexity to the OpenSeadragon codebase. Happy to talk about what such a mechanism might look like if you’re interested…