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

Handle ipv6 remote client addresses for secure_gatway. #4030

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions src/ripple/rpc/impl/Role.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,20 @@ forwardedFor(http_request_type const& request)
return pos;
}());

return *boost::beast::http::token_list(boost::string_view(found, pos))
.begin();
return boost::string_view(found, pos);
// return *boost::beast::http::token_list(boost::string_view(found, pos))
// .begin();
}

it = request.find("X-Forwarded-For");
if (it != request.end())
{
return *boost::beast::http::token_list(it->value()).begin();
// This call to boost::beast::http::token_list() truncates an
// IPv6 address at the first colon.
std::size_t const found = it->value().find(',');
return found == boost::string_view::npos ? it->value() :
it->value().substr(0, found);
// return *boost::beast::http::token_list(it->value()).begin();
}

return {};
Expand Down
41 changes: 36 additions & 5 deletions src/test/rpc/Roles_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Roles_test : public beast::unit_test::suite
!wsRes.isMember("unlimited") || !wsRes["unlimited"].asBool());

std::unordered_map<std::string, std::string> headers;
// IPv4 tests.
headers["X-Forwarded-For"] = "12.34.56.78";
auto rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
Expand All @@ -83,11 +84,12 @@ class Roles_test : public beast::unit_test::suite
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");

headers["Forwarded"] =
"what=where;for=55.66.77.88, 99.00.11.22;"
"who=3";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");
// This test isn't formatted according to the spec.
// headers["Forwarded"] =
// "what=where;for=55.66.77.88, 99.00.11.22;"
// "who=3";
// rpcRes = env.rpc(headers, "ping")["result"];
// BEAST_EXPECT(rpcRes["ip"] == "55.66.77.88");

wsRes = makeWSClient(env.app().config(), true, 2, headers)
->invoke("ping")["result"];
Expand All @@ -103,6 +105,35 @@ class Roles_test : public beast::unit_test::suite
wsRes = makeWSClient(env.app().config(), true, 2, headers)
->invoke("ping")["result"];
BEAST_EXPECT(wsRes["unlimited"].asBool());

// IPv6 tests.
headers = {};
headers["X-Forwarded-For"] = "2001:db8:3333:4444:5555:6666:7777:8888";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
BEAST_EXPECT(rpcRes["ip"] == "2001:db8:3333:4444:5555:6666:7777:8888");

headers["X-Forwarded-For"] = "2001:db8:3333:4444:5555:6666:7777:9999, a:b:c:d:e:f, g:h:i:j:k:l";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
BEAST_EXPECT(rpcRes["ip"] == "2001:db8:3333:4444:5555:6666:7777:9999");

headers["X-Forwarded-For"] = "[2001:db8:3333:4444:5555:6666:7777:8888]";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
BEAST_EXPECT(rpcRes["ip"] == "[2001:db8:3333:4444:5555:6666:7777:8888]");

headers["X-Forwarded-For"] = "[2001:db8:3333:4444:5555:6666:7777:9999], [a:b:c:d:e:f], [g:h:i:j:k:l]";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
BEAST_EXPECT(rpcRes["ip"] == "[2001:db8:3333:4444:5555:6666:7777:9999]");

headers = {};
headers["Forwarded"] = "for=2001:db8:3333:4444:5555:6666:7777:aaaa";
rpcRes = env.rpc(headers, "ping")["result"];
BEAST_EXPECT(rpcRes["role"] == "proxied");
BEAST_EXPECT(rpcRes["ip"] =="2001:db8:3333:4444:5555:6666:7777:aaaa");
// IPv6 (dual) tests.
}
}

Expand Down