Net: expose peer/client addresses and trusted-proxy policy on ServerRequest (#57)
This commit is contained in:
1 parent
b3c1246291
commit
55a1399e01
4 files changed
+317
No files matched your search
@@ -391,3 +391,92 @@ Void::Testing::Test http_server_limits_smuggling_streaming_and_graceful_drain()
|
||||
Void::Testing::expect_eq(slow_result->body, std::string{"drained"});
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string response_body(const std::string& raw)
|
||||
{
|
||||
const std::size_t split = raw.find("\r\n\r\n");
|
||||
return split == std::string::npos ? std::string{} : raw.substr(split + 4);
|
||||
}
|
||||
|
||||
std::string peer_probe(std::uint16_t port, std::string_view forwarded_for)
|
||||
{
|
||||
std::string request = "GET /peer HTTP/1.1\r\nHost: 127.0.0.1\r\n";
|
||||
if (!forwarded_for.empty()) {
|
||||
request += "X-Forwarded-For: ";
|
||||
request += forwarded_for;
|
||||
request += "\r\n";
|
||||
}
|
||||
request += "Connection: close\r\n\r\n";
|
||||
return response_body(raw_http_exchange(port, request));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test http_server_peer_address_and_trusted_proxy_policy()
|
||||
{
|
||||
auto register_probe = [](HttpServer& server) {
|
||||
Void::Testing::require(
|
||||
server
|
||||
.add_route(Method::Get, "/peer",
|
||||
[](const ServerRequest& req) {
|
||||
return Void::Result::ok(ServerResponse{
|
||||
.status = 200,
|
||||
.body = req.peer.address + "|" +
|
||||
req.client.address + "|" +
|
||||
std::to_string(req.peer.port) +
|
||||
"|" +
|
||||
std::to_string(req.client.port)});
|
||||
})
|
||||
.has_value());
|
||||
};
|
||||
|
||||
// Untrusted peer: forwarding headers are ignored, peer is loopback.
|
||||
{
|
||||
HttpServerOptions opts;
|
||||
opts.bind_host = "127.0.0.1";
|
||||
HttpServer server{opts};
|
||||
register_probe(server);
|
||||
Void::Testing::require(server.start().has_value());
|
||||
const std::uint16_t port = server.bound_port();
|
||||
|
||||
const std::string direct = peer_probe(port, {});
|
||||
Void::Testing::expect(direct.starts_with("127.0.0.1|127.0.0.1|"));
|
||||
const std::size_t first = direct.find('|');
|
||||
const std::size_t second = direct.find('|', first + 1);
|
||||
const std::size_t third = direct.find('|', second + 1);
|
||||
Void::Testing::require(third != std::string::npos);
|
||||
Void::Testing::expect_eq(direct.substr(second + 1, third - second - 1),
|
||||
direct.substr(third + 1));
|
||||
|
||||
const std::string spoofed = peer_probe(port, "203.0.113.7");
|
||||
Void::Testing::expect(spoofed.starts_with("127.0.0.1|127.0.0.1|"));
|
||||
|
||||
server.shutdown(std::chrono::seconds{2});
|
||||
}
|
||||
|
||||
// Trusted loopback proxy: the rightmost non-trusted hop wins.
|
||||
{
|
||||
HttpServerOptions opts;
|
||||
opts.bind_host = "127.0.0.1";
|
||||
opts.trusted_proxies = {"127.0.0.0/8"};
|
||||
HttpServer server{opts};
|
||||
register_probe(server);
|
||||
Void::Testing::require(server.start().has_value());
|
||||
const std::uint16_t port = server.bound_port();
|
||||
|
||||
Void::Testing::expect(peer_probe(port, "203.0.113.7")
|
||||
.starts_with("127.0.0.1|203.0.113.7|"));
|
||||
Void::Testing::expect(
|
||||
peer_probe(port, "198.51.100.9, 127.0.0.5")
|
||||
.starts_with("127.0.0.1|198.51.100.9|"));
|
||||
Void::Testing::expect(peer_probe(port, "2001:db8::1")
|
||||
.starts_with("127.0.0.1|2001:db8::1|"));
|
||||
// Malformed chain: fail closed to the immediate peer.
|
||||
Void::Testing::expect(
|
||||
peer_probe(port, "203.0.113.7, not-an-ip")
|
||||
.starts_with("127.0.0.1|127.0.0.1|"));
|
||||
|
||||
server.shutdown(std::chrono::seconds{2});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user