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

[3.2] response to disallowed host #996

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ class beast_http_session : public detail::abstract_conn {
}

try {
if(!derived().allow_host(req))
if(!derived().allow_host(req)) {
error_results results{static_cast<uint16_t>(http::status::bad_request), "Disallowed HTTP HOST header in the request"};
send_response( fc::json::to_string( results, fc::time_point::maximum() ),
static_cast<unsigned int>(http::status::bad_request) );
return;
}

if(!plugin_state_->access_control_allow_origin.empty()) {
res_->set("Access-Control-Allow-Origin", plugin_state_->access_control_allow_origin);
Expand Down
31 changes: 23 additions & 8 deletions tests/http_plugin_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

from TestHarness import Cluster, Node, ReturnType, TestHelper, Utils
import urllib.request
import sys

###############################################################
# http_plugin_tests.py
Expand All @@ -23,7 +25,7 @@

Utils.Debug=debug
https_port = 5555
cluster=Cluster(walletd=True)
cluster=Cluster(host="127.0.0.1", walletd=True)

testSuccessful=False

Expand All @@ -35,21 +37,34 @@
TestHelper.printSystemInfo("BEGIN")

Print("Stand up cluster")
# standup cluster with HTTPS enabled, but not configured
# HTTP should still work
extraArgs={ 0 : "--https-server-address 127.0.0.1:5555" }
# specificExtraNodeosArgs=extraArgs

if cluster.launch(dontBootstrap=True, loadSystemContract=False) is False:
if cluster.launch(dontBootstrap=True, loadSystemContract=False, specificExtraNodeosArgs = {0: "--http-validate-host true"}) is False:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need --http-validate-host true here? It's the default.

Copy link
Contributor Author

@huangminghuang huangminghuang Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have to. Somewhere in the TestHarness changed it to false.

http-validate-host = false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay, it's interesting because you can't --http-validate-host true --http-validate-host true nor

http-validate-host = true 
http-validate-host = true 

But you can http-validate-host = true & --http-validate-host true

cmdError("launcher")
errorExit("Failed to stand up eos cluster.")

Print("Getting cluster info")
cluster.getInfos()

node0 = cluster.nodes[0]
## HTTP plugin listens to 127.0.0.1:8888 by default. With the --http-validate-host=true,
## the HTTP request to "http://localhost:8888" should fail because the HOST header doesn't
## match "127.0.0.1".

def get_info_status(url):
try:
req = urllib.request.Request( url, method = "GET")
return urllib.request.urlopen(req, data=None).code
except urllib.error.HTTPError as response:
return response.code
except:
e = sys.exc_info()[0]
return e
url = node0.endpointHttp.replace("127.0.0.1", "localhost") + "/v1/chain/get_info"
code = get_info_status(url)
assert code == 400, f"Expected HTTP returned code 400, got {code}"
testSuccessful = True

finally:
TestHelper.shutdown(cluster, None, testSuccessful, killEosInstances, True, keepLogs, killAll, dumpErrorDetails)

exitCode = 0 if testSuccessful else 1
exit(exitCode)