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

Native CORS support for defichain #330

Merged
merged 3 commits into from
Apr 21, 2021
Merged

Conversation

prasannavl
Copy link
Member

@prasannavl prasannavl commented Apr 18, 2021

What kind of PR is this?:

/kind feature

What this PR does / why we need it:

This adds native support for CORS for the RPC API.

This enables the following:

  • Allows whitelisting communication from browser, only for the specific given origins.
  • Ability to build web based apps that can use the RPC API when explicitly specified

Details

This adds the following flags to defid:

-rpcallowcors=<host> Allow CORS requests from the given host origin. 
Include scheme and port (eg: -rpcallowcors=http://127.0.0.1:5000)

When the defid is executed with, for instance: defid -rpcallowcors=http://127.0.0.1:5000

This PR adds CORS support that's needed by modern browsers to allow the specific origin to communicate with node directly.

Note: Using wildcards for Origin * is NOT supported, since this also sets up the Allow-Credentials flag that according to RFC requires that the the origin is explicit.

This enables scenarios like BirthdayResearch/defichain-app#766 natively with defichain without the need for proxies that often complicates end to end security.

Example:

Screenshot from 2021-04-18 18-43-52

Screenshot from 2021-04-18 18-57-01

@ShengguangXiao
Copy link
Contributor

ShengguangXiao commented Apr 19, 2021

@prasannavl How to test it using a brower or command line?

@prasannavl
Copy link
Member Author

The python test as a part of the PR should help with info on testing this with the command line.

For the browser, as one example, please see: BirthdayResearch/defichain-app#766. (It also requires the other PRs labelled in the comments).

@ShengguangXiao
Copy link
Contributor

I think i get it now, on testnet start with option -rpcallowcors=http://127.0.0.1:5000, then use command curl -X OPTIONS http://127.0.0.1:18554 -i can get below reply.

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://127.0.0.1:5000
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Date: Mon, 19 Apr 2021 04:25:25 GMT

@prasannavl
Copy link
Member Author

@ShengguangXiao indeed. Enables libevent to respond to OPTIONS, as well as add headers to all other (POST) requests.

@monstrobishi monstrobishi merged commit 73ec427 into DeFiCh:master Apr 21, 2021
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

Successfully merging this pull request may close these issues.

4 participants