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

Fix checking whether source address in X-Forwarded-For is ipv4 or ipv6 #72

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Changes from 2 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
21 changes: 14 additions & 7 deletions libservice/src/Aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,43 @@ static std::string getEndpoint(const std::string& host, const std::string& url)
static bool isIpv4ClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) {
in_addr_t clientAddrBinary;
if (inet_pton(AF_INET, addr.c_str(), &clientAddrBinary) != 1) {
throw std::runtime_error("Cannot parse IPv4 client address: {}" + addr);
return true;
hparzych marked this conversation as resolved.
Show resolved Hide resolved
}
return ipChecker.isV4AddressExternal(clientAddrBinary);
}

static bool isIpv6ClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) {
in6_addr clientAddrBinary{};
if (inet_pton(AF_INET6, addr.c_str(), &clientAddrBinary) != 1) {
throw std::runtime_error("Cannot parse IPv6 client address: {}" + addr);
return true;
}
return ipChecker.isV6AddressExternal(clientAddrBinary);
}

static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr) {
bool isPossiblyIpv6{addr.find(':') != std::string::npos};
return isPossiblyIpv6 ? isIpv6ClientExternal(ipChecker, addr) : isIpv4ClientExternal(ipChecker, addr);
}

static bool isClientExternal(const IpAddressChecker& ipChecker, const std::string& addr, bool isIpV6) {
return isIpV6 ? isIpv6ClientExternal(ipChecker, addr) : isIpv4ClientExternal(ipChecker, addr);
}

static void incrementServiceClientsNumber(
const IpAddressChecker& ipChecker, Service& service, const httpparser::HttpRequest& request, const DiscoverySessionMeta& meta) {
std::string clientAddr;
bool isExternal{false};
if (!request.xForwardedFor.empty()) {
clientAddr = request.xForwardedFor.front();
isExternal = isClientExternal(ipChecker, request.xForwardedFor.front());
} else if (discoverySessionFlagsIsIPv4(meta.flags)) {
clientAddr = ipv4ToString(meta.sourceIPData);
isExternal = isClientExternal(ipChecker, ipv4ToString(meta.sourceIPData), false);
} else if (discoverySessionFlagsIsIPv6(meta.flags)) {
clientAddr = ipv6ToString(meta.sourceIPData);
isExternal = isClientExternal(ipChecker, ipv6ToString(meta.sourceIPData), true);
} else {
return;
}

try {
if (isClientExternal(ipChecker, clientAddr, discoverySessionFlagsIsIPv6(meta.flags))) {
if (isExternal) {
++service.externalClientsNumber;
} else {
++service.internalClientsNumber;
Expand Down