Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS authentication not enabled #255

Open
2 tasks done
TheBillLang opened this issue Nov 1, 2018 · 4 comments
Open
2 tasks done

CORS authentication not enabled #255

TheBillLang opened this issue Nov 1, 2018 · 4 comments

Comments

@TheBillLang
Copy link

TheBillLang commented Nov 1, 2018

Use case: description, code

Print with easyprint a react leaflet map from a server requiring authentication

Expected behavior

Pass authentication token to map server

Actual behavior (stack traces, console logs etc)

CORS request refused

Code change required

Add:
request.withCredentials = true;
at line 476

Browsers

  • Chrome 65+
  • Firefox 52+
@iqdoq-dfischer
Copy link

Authentication credentials may also be required in function makeImage.

Additionally we had to add ...
image.crossOrigin = 'use-credentials';
... in that makeImage function.

See "MDN: Allowing cross-origin use of images and canvas" .

Sending (existing) credentials for authentication of external URI (CORS) requests should be configurable.

Requirement: The remote webserver hosting the images/resources has to respond with appropriate CORS headers to let the browser know that the cross domain request from that origin is safe/allowed.

I will provide a patch on my own fork if I find time...

iqdoq-dfischer added a commit to iqdoq-dfischer/dom-to-image that referenced this issue Jan 14, 2019
@TheBillLang
Copy link
Author

Excellent change. It turns my hack into real code. Thanks.

Alas, my situation became more complex. When only some resource servers require authentication 'use-credentials' must be not only configurable but also conditional. I my case the map server requires authentication but the photo server does not. The server urls are distinct, so I ended up enclosing 'use-credentials' with

if (url.search(new RegExp("api", "i")) > 0)

A generic solution for that situation will be much more difficult.

IDisposable pushed a commit to 1904labs/dom-to-image-more that referenced this issue Feb 6, 2019
marcandrews added a commit to marcandrews/dom-to-image that referenced this issue Jan 28, 2020
* toCanvas returns a promise that is fulfilled with a canvas object.

* ignore image with invalid src

* Prepare for new package name.

Adds Chrome fix to copy over the font when cloning.
Adds scaling up the image to reduce blur.

* Force-passed some tests that no longer work

Made scale an option so tests can pass (defaults to 1)
Fixed errors looking at external style-sheet rules.
For now, don't test Firefox, don't check rendered canvas ::before ::after content, don't expect access to external sheets.

* Fix badge

* Bump release to 2.7.1

* 2.7.2

* Moving repository to 1904labs organization

2.7.3

* Fix JSFiddle link in issue template

* Removed and excluded node_modules

* Upgrade all packages.

Fix all audit fails

* 2.7.4

Bump version

* Updating travis for node 8

* Allow text rendering test to run a bit longer

* Optionally send (existing) credentials for authentication of external URI (CORS) requests.

Cherry picked from iqdoq-dfischer@2bae568
Resolves tsayen#255

* Release 2.7.5

Includes ability to pass authentication on for CORS

* fix export of 0 size canvas elements #3

fix for 1904labs#3

* Add tests and prepare release

* 2.7.6

* Fix bower version

* Update dev dependencies and fix test

* Release 2.7.7

Upgrades all devDependencies and fixes unit test.

* Extend timeout for image rendering test.

* FIX: File extension error when URL has query parameters

When a URL pattern has a query string the file-extension isn't matched correctly IFF the URL has . in the query parameters.
This fix is from original dom-to-image repo by:
https://github.com/CG-man 
tsayen#285

* Prepare for 2.8.0 release

* fix font mangling

* restore dist

* remove postbuild

* fix getCssRules

* 1904labs > tsayen

* revert

* revert

* yarn.lock

Co-authored-by: Andreas Girgensohn <[email protected]>
Co-authored-by: ioslh <[email protected]>
Co-authored-by: Marc Brooks <[email protected]>
Co-authored-by: Aidas Klimas <[email protected]>
Co-authored-by: Adrien Pyke <[email protected]>
steven-coding added a commit to steven-coding/leaflet-easyprint-bbox that referenced this issue Oct 28, 2020
Fixing following issue:
CORS authentication not enabled #255
tsayen/dom-to-image#255
@MSCAU
Copy link

MSCAU commented Nov 19, 2020

image.crossOrigin = 'use-credentials';

@iqdoq-dfischer - I have added the line that you have proposed:

        function makeImage(uri) {
            return new Promise(function (resolve, reject) {
                var image = new Image();
                image.crossOrigin = 'Anonymous';
                image.onload = function () {
                    resolve(image);
                };
                image.onerror = reject;
                image.src = uri;
            });
        }

I obviously don't have access to the maps.stamen.com server and I still get the CORS error when trying to save a Leaflet map using dom-to-image. Oddly don't have this issue at all in Firefox which suggests it's a (recent) Chrome limitation. Do you have any suggestions?

See also stamen/maps.stamen.com#158

@danhyltoftlund
Copy link

danhyltoftlund commented Jun 9, 2022

What is the chance of getting request.withCredentials = true; added or a possibility turn it on or off.

If I do a fork, implement the change and create a PR. Is there any chance it would be approved and added to the release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants