Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Access-Control-Allow-Credentials and Access-Control-Allow-Origin #282

Closed
wants to merge 8 commits into from
Closed

Access-Control-Allow-Credentials and Access-Control-Allow-Origin #282

wants to merge 8 commits into from

Conversation

dgendill
Copy link
Contributor

I was working through this tutorial and serving my app on a local server. When I tried to call my contract I got the Access-Control-Allow-Origin error in the console. It looks like that issue was solved with this recent PR.

However, I think web3.js sets Request.credentials to 'include', which means we also need Access-Control-Allow-Credentials set true and replace the wildcard in Access-Control-Allow-Origin to a specific domain (Request.include can't be used with a wildcard](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials). This was the error I was seeing...

image

So I think setting Access-Control-Allow-Origin to request.headers.origin and setting Access-Control-Allow-Credentials might be helpful.

…ead of *. Using web3 requires the access-control-allow-credentials header set to true, but you can't use that header if the access-control-allow-origin is a wildcard.
@davidmurdoch
Copy link
Member

Thanks for the PR, @dgendill! Looks like there is a Related issue at web3, too.

I'll need to add some tests for this and look into if we can conditionally set the Access-Control-Allow-Credentials header only when the client is asking for it (same with changing to * vs request.headers.origin). Feel free to add tests and conditional logic as we've got some other high-priority issues we currently working on.

Additionally, it is possible that request.headers.origin is undefined, so this case will need to be properly handled as well.

Thanks again!

… CORS. Now showing an error message for OPTIONS requests that are not preflight requests.
@dgendill
Copy link
Contributor Author

Thanks for taking a look at this, David, I'm glad to help. I added the check for the origin header, and a check to validating the preflight request.
I could be wrong, but it looks like there isn't a request header that is used to enable/disable the Access-Control-Allow-Credentials header in the response.

@davidmurdoch
Copy link
Member

Really great work @dgendill! We'll take a closer look at this and get it merged in soon!

@davidmurdoch davidmurdoch self-requested a review January 25, 2019 21:29
@davidmurdoch
Copy link
Member

Merged in 9633943! Thanks again @dgendill for this excellent PR!

@dgendill
Copy link
Contributor Author

Thanks, David!

@davidmurdoch
Copy link
Member

@dgendill we at Truffle were all seriously impressed by your thoroughness in this PR. Thanks again!

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

Successfully merging this pull request may close these issues.

2 participants