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. Can't connect to the node #2158

Closed
reardenlife opened this issue Jan 4, 2019 · 4 comments
Closed

CORS. Can't connect to the node #2158

reardenlife opened this issue Jan 4, 2019 · 4 comments

Comments

@reardenlife
Copy link

reardenlife commented Jan 4, 2019

I'm using latest stable version of web3.js (0.20.7).

I have a private ethereum network and one node connected to it. I ran that node like this:

#!/usr/bin/env bash
geth --datadir /var/.eth/pn \
  --nodiscover --networkid 4242 \
  --cache=128 \
  --verbosity 5 \
  --rpc --rpcapi db,eth,net,web3,personal,miner,admin \
  --rpcport 8545 --rpcaddr "0.0.0.0" --rpccorsdomain '*' \
  >> geth_privnetwork.log 2>&1 &

So the node is listening to all IP addresses on 8545 port. I set it up only for test/debug purposes.

Then I wrote a html page with web3.js v0.20.7:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Geth Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="common/css/bootstrap.min.css">
<link rel="stylesheet" href="common/css/jquery-confirm.min.css">
<style>
</style>
<script src="common/js/jquery.min.js"></script>
<script src="common/js/popper.min.js"></script>
<script src="common/js/bootstrap.min.js"></script>
<script src="common/js/jquery-confirm.min.js"></script>
<script src="common/js/web3.js"></script>
<!--
-->
<script src="common/firebug-lite/build/firebug-lite.js#startOpened=true">
</script>
<script>
(function(){
  var console = window.firebug || window.console;
})();
</script>
<script>
window.onerror = function (message, url, lineNo){
    console.error('Error: ' + message + '\n' + 'Line Number: ' + lineNo);
    return true;
}
</script>
<!--
-->
<script>
var host = window.location.hostname;
web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider("http://" + host + ":8545"));
if(!web3.isConnected())
    console.log("not connected");
else
    console.log("connected");
r = web3.version.node;
console.log(r);

</script>
</head>
<body>
</body>
</html>

I put this page on the server where ethereum node is running and requested it through the 80 port of the webserver by acceasing it from different device (smartphone).

In the firebug lite console I see "not connected" and empty json response error.

I though that something is wrong with a node and wrote a curl script:

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' http://<My IP>:8545

I got the response no problem:

[root@ .eth]# ./curl.sh
{"jsonrpc":"2.0","id":1,"result":"Geth/v1.9.0-unstable-a4af7343/linux-amd64/go1.11.2"}

Then I took tcpdump and looked up what the browser actually sends to the server:

tcpdump -A -i eth0 -w 0001.pcap port 8545

So here is the HTTP headers from dump:

OPTIONS / HTTP/1.1
Host: <My IP>:8545
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://<My IP>
User-Agent: Mozilla/5.0 (Linux; Android 7.0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://<My IP>/geth/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,es-US;q=0.8,es-ES;q=0.7,es;q=0.6,en-CA;q=0.5

...
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: http://<My IP>
Access-Control-Max-Age: 600
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Date: Fri, 04 Jan 2019 02:31:27 GMT
Content-Length: 0

...
�PTIONS / HTTP/1.1
Host: <My IP>:8545
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://<My IP>
User-Agent: Mozilla/5.0 (Linux; Android 7.0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://<My IP>/geth/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,es-US;q=0.8,es-ES;q=0.7,es;q=0.6,en-CA;q=0.5

...
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: http://<My IP>
Access-Control-Max-Age: 600
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Date: Fri, 04 Jan 2019 02:31:28 GMT
Content-Length: 0

So instead of POST requests the browser issuing preflight CORS requests. I looked at them closely but I still unable to understand why the browser doesn't issuing any of original post requests.

It looks like some bug of web3.js to me. But I am unable to locate it.

Any ideas?

@reardenlife
Copy link
Author

The problem is definitely in web3.js.
I just tried to send POST request from the browser manually.

The code is:

function reqListener () {
 console.log(this.responseText);
    };
var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance
xmlhttp.onload = reqListener;
xmlhttp.open("POST", "http://<My IP>:8545/");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify( {"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1 }));

It worked fine.

Well... Let me see how HTTP packets differ...

@reardenlife
Copy link
Author

Aha. I see. It just sent the POST request directly, without any OPTIONS requests - so without any CORS support.

So it looks like that web3.js and not the browser itself is trying to implement the CORS support. And it just doesn't work.

@reardenlife
Copy link
Author

I tried my above mentioned code with XMLHttpRequest on three browsers: Chrome, Opera, Firefox.

Interestingly, Firefox crashed a few times but eventually displayed the response in Firebug lite console. I'm not sure if it is my code that is causing Firefox to crash though. So, all in all, my code is working fine, vut web3.js is not working at all.

Honestly, I don't really see the purpose of web3.js then. :)

@nivida nivida added the support label Jan 10, 2019
@nivida
Copy link
Contributor

nivida commented Mar 25, 2019

This got fixed with PR #2564 and will be released this week.

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

2 participants