deck.gl icon indicating copy to clipboard operation
deck.gl copied to clipboard

Images in deck.gl layers fail CORS checks on same domain on Edge 17.17134

Open SterlingMcCall opened this issue 6 years ago • 12 comments

Description

Edge 42.17134 (EdgeHTML 17.17134) was released with Windows 10 version 1803 in April 2018. It contains a CORS bug that will block requests made from the same domain unless you fetch() with { credentials: 'same-origin' }. (https://stackoverflow.com/a/56085437).

This is fixed in Edge 44.17763 (EdgeHTML 18.17763) released with Windows 10 version 1809 in October/November 2018. However, due to the fact that pre-chromium Edge is tied to the OS version, it's not as simple as just "updating Edge".

Could a workaround be put in place that the credentials setting is set to same-origin automatically, or that it can be configured via another option on the Icon object, or have deck.gl (technically loaders.gl https://github.com/uber-web/loaders.gl/blob/master/modules/core/src/lib/fetch/fetch-file.js) automatically add the credentials same-origin option?

Prior to deck.gl 8.x.x, I had been able to work around this issue by providing a base64 encoded data URI instead of a path. That does not work anymore for Edge 17.x nor 18.x, and the images simply do not show up and there are no errors in the console.

Repro Steps

Set the url on the icon object returned by getIcon() in an IconLayer to a URL of an image on the same domain as the application. Attempt to view the layer, see that the icons are not showing up, and look at the console to see the the error.

Environment (please complete the following information):

  • Framework Version: All versions to date
  • Browser Version: Edge 42.17134 (EdgeHTML 17.17134)
  • OS: Windows 10 version 1803

Logs

SEC7120: [CORS] The origin 'http://localhost:8080' did not find 'http://localhost:8080' in the Access-Control-Allow-Origin response header for cross-origin resource at 'http://localhost:8080/assets/images/icon.svg'

SterlingMcCall avatar Feb 26 '20 17:02 SterlingMcCall

Data URI should work. Did you test it with the latest release? Can you share an example so that we can repro?

Pessimistress avatar Feb 26 '20 18:02 Pessimistress

@Pessimistress https://codepen.io/sterlingmccall/pen/OJVpZOz using deck.gl 8.0.16 That demo will work on all browsers except for non-chromium Edge. I tried both a base64 encoded data URI with both svg and png and they both work on chrome, firefox, and chromium edge.

Deck.gl version base64 data URI working on non-chromium Edge?
https://unpkg.com/[email protected]/dist.min.js no
https://unpkg.com/[email protected]/dist/dist.dev.js no
https://unpkg.com/[email protected]/dist.min.js yes
https://unpkg.com/[email protected]/dist/dist.dev.js no

SterlingMcCall avatar Feb 26 '20 18:02 SterlingMcCall

@Pessimistress My best guess is this change resulted in the changes made most recently to https://github.com/uber-web/loaders.gl/blob/master/modules/core/src/lib/load.js to get pulled in to deck which somehow breaks data URIs.

SterlingMcCall avatar Feb 26 '20 18:02 SterlingMcCall

We can add default fetch options when we register the ImageLoader here.

The dataURL bug should be investigated inside loaders.gl. @xintongxia

Edit: @SterlingMcCall you should be able to specify the options parameter sent to load via the loadOptions prop of your layer.

Pessimistress avatar Mar 01 '20 08:03 Pessimistress

@Pessimistress I addded loadOptions to my icon layer but it did not seem to have any effect.

loadOptions: {
  credentials: 'same-origin',
  headers: {
    'x-custom-header': 'will this header be added to the image request? '
  }
}

It does not get around the Edge 17 CORS bug, nor does it appear that it is having any effect because I do not see the header added to the request. Looking at the code that loads the images for the icon layer, it seems to be calling load() without providing the loadOptions prop.

Could _loadIcons() in icon-manager.js be updated to respect the loadOptions?

In regards to the data URL loaders.gl bug, I did find this issue opened 3 weeks ago that seems somewhat related, but it has not be responded to. I have opened a new issue detailing the bug with links to demos.

SterlingMcCall avatar Mar 04 '20 15:03 SterlingMcCall

@Pessimistress after adding <script src="https://unpkg.com/[email protected]/lib/encoding.js"></script> to my index.html, base64 encoded data URLs work once again in Edge. It would seem that deck.gl isn't properly pulling in the polyfill for TextDecoder via its dependencies.

SterlingMcCall avatar Mar 04 '20 20:03 SterlingMcCall

Ah, that is likely intentional. At least in loaders.gl we leave polyfills optional as they are really just needed for Edge and IE11. There is a short blurb about this on the home page: https://loaders.gl/docs#supported-platforms

ibgreen avatar Mar 04 '20 20:03 ibgreen

@ibgreen simply adding @loaders.gl/polyfills as a dependency to my package.json does not fix the issue. While <script src="https://unpkg.com/[email protected]/lib/encoding.js"></script> does fix the issue, it doesn't seem like the proper way of adding the polyfill to my app. What is the correct way to get the polyfills to run when imported as an npm dependency?

Edit: I also tried importing it in the same file where I use deck.gl (just adding import '@loaders.gl/polyfills'; per the polyfill docs). However that results in errors:

Error: Uncaught (in promise): ReferenceError: global is not defined
ReferenceError: global is not defined
    at Module../node_modules/@loaders.gl/polyfills/dist/esm/text-encoding/encoding.js (encoding.js:2)
    at __webpack_require__ (bootstrap:84)
    at Module../node_modules/@loaders.gl/polyfills/dist/esm/index.js (index.js:1)
...

But it looks like there is info about this error online, so I'll keep digging.

SterlingMcCall avatar Mar 04 '20 21:03 SterlingMcCall

It would appear ReferenceError: global is not defined is an issue stemming from my use of the angular framework, which intentionally omits the node global object. Their explanation for removing the global shim has merit and perhaps deck.gl/loaders.gl should not be relying on the node global object for a framework meant to run in a browser.

Regardless, the fix for it is easy: polyfills.ts

// Add global to window, assigning the value of window itself.
(window as any).global = window;

The conversation here has gotten a little sidetracked from the title of the issue, but I still think there are takeaways.

  • The deck.gl docs do not mention that its dependence on loaders.gl may require the following changes: Add @loaders.gl/polyfills to package.json dependencies (npm i @loaders.gl/polyfills --save) Import the loaders.gl polyfills before using deck.gl
import '@loaders.gl/polyfills';

SterlingMcCall avatar Mar 04 '20 21:03 SterlingMcCall

@SterlingMcCall I think we already do some careful checks around the global object. If you have identified how angular strips the global object we can add extra checks. Also, there seems to be a recent move towards globalThis. I haven't looked into that yet, maybe doing that would solve some of these legacy issues - or not...

ibgreen avatar Mar 04 '20 23:03 ibgreen

@ibgreen https://github.com/angular/angular-cli/pull/9812/files It seems that the default angular webpack config was modified to no longer include the node global shims. https://webpack.js.org/configuration/node/#nodeglobal

SterlingMcCall avatar Mar 05 '20 13:03 SterlingMcCall

@SterlingMcCall loadOptions should be fixed in v8.1.1.

Pessimistress avatar Apr 19 '20 04:04 Pessimistress