|
|
@@ -0,0 +1,102 @@
|
|
|
+#include "client_ip.hpp"
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+using namespace svapi;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+ClientIpConfig loopbackTrust(const std::string& header = "X-Real-IP", bool isList = false) {
|
|
|
+ return clientIpConfigFrom(header, isList, {"127.0.0.0/8", "::1/128"});
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+// --- the security property ---------------------------------------------------
|
|
|
+//
|
|
|
+// These two are the whole point of the file. If `resolveClientIp`'s
|
|
|
+// trusted-proxy arm is deleted, the first goes red; nothing else here would.
|
|
|
+
|
|
|
+TEST(ClientIp, AHeaderFromAnUNTRUSTEDPeerIsIgnoredEntirely) {
|
|
|
+ const auto cfg = loopbackTrust();
|
|
|
+ // A caller reaching us directly and claiming to be someone else.
|
|
|
+ EXPECT_EQ(resolveClientIp("203.0.113.9", "198.51.100.1", cfg), "203.0.113.9");
|
|
|
+ // Claiming to be the proxy itself buys nothing either.
|
|
|
+ EXPECT_EQ(resolveClientIp("203.0.113.9", "127.0.0.1", cfg), "203.0.113.9");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, AHeaderFromATrustedProxyIsBelieved) {
|
|
|
+ const auto cfg = loopbackTrust();
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1", cfg), "198.51.100.1");
|
|
|
+ EXPECT_EQ(resolveClientIp("::1", "2001:db8::5", cfg), "2001:db8::5");
|
|
|
+}
|
|
|
+
|
|
|
+// --- the list form, and the element nobody picks correctly -------------------
|
|
|
+
|
|
|
+TEST(ClientIp, TheLASTElementOfAForwardedForListIsTaken) {
|
|
|
+ const auto cfg = loopbackTrust("X-Forwarded-For", /*isList=*/true);
|
|
|
+ // nginx's $proxy_add_x_forwarded_for is "what the client sent, plus the
|
|
|
+ // real peer appended". The caller forged the first element; the last is
|
|
|
+ // the only one the proxy vouched for.
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1, 203.0.113.9", cfg), "203.0.113.9");
|
|
|
+ // Taking the FIRST would return the forged 198.51.100.1 here, which is the
|
|
|
+ // bypass. Spelled out so a later "simplification" has to delete an
|
|
|
+ // assertion that says why.
|
|
|
+ EXPECT_NE(resolveClientIp("127.0.0.1", "198.51.100.1, 203.0.113.9", cfg), "198.51.100.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, AListWithTrailingSeparatorsOrSpacesStillYieldsTheLastRealElement) {
|
|
|
+ const auto cfg = loopbackTrust("X-Forwarded-For", true);
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1, 203.0.113.9 ,", cfg), "203.0.113.9");
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", " 203.0.113.9", cfg), "203.0.113.9");
|
|
|
+}
|
|
|
+
|
|
|
+// --- degrading to the peer, in every direction ------------------------------
|
|
|
+
|
|
|
+TEST(ClientIp, NoHeaderConfiguredMeansThePeerIsTheAnswer) {
|
|
|
+ const auto cfg = loopbackTrust("");
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1", cfg), "127.0.0.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, AnEmptyTrustedProxyListTrustsNOBODYRatherThanEverybody) {
|
|
|
+ const auto cfg = clientIpConfigFrom("X-Real-IP", false, {});
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1", cfg), "127.0.0.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, AnAbsentOrUnusableHeaderValueFallsBackToThePeer) {
|
|
|
+ const auto cfg = loopbackTrust();
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "", cfg), "127.0.0.1");
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "not-an-address", cfg), "127.0.0.1");
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "unknown", cfg), "127.0.0.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, ARANGEInTheHeaderIsRefusedBecauseAHeaderMustNameOneHost) {
|
|
|
+ const auto cfg = loopbackTrust();
|
|
|
+ // parseCidr would accept this as a /8; recording it as a caller would be
|
|
|
+ // nonsense and matching an allow-list against it would be worse.
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "10.0.0.0/8", cfg), "127.0.0.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, ABracketedIPv6LiteralIsAcceptedAndUnwrapped) {
|
|
|
+ const auto cfg = loopbackTrust();
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "[2001:db8::5]", cfg), "2001:db8::5");
|
|
|
+}
|
|
|
+
|
|
|
+// --- config assembly --------------------------------------------------------
|
|
|
+
|
|
|
+TEST(ClientIp, AMalformedTrustedCidrIsDroppedWithoutDisablingTheRest) {
|
|
|
+ // One typo must not silently turn every request into the peer address.
|
|
|
+ const auto cfg = clientIpConfigFrom("X-Real-IP", false,
|
|
|
+ {"not-a-cidr", "127.0.0.0/8", "999.1.1.1/8"});
|
|
|
+ EXPECT_EQ(cfg.trustedProxies.size(), 1u);
|
|
|
+ EXPECT_EQ(resolveClientIp("127.0.0.1", "198.51.100.1", cfg), "198.51.100.1");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ClientIp, IsPlainIpLiteralAcceptsHostsAndRejectsEverythingElse) {
|
|
|
+ EXPECT_TRUE(isPlainIpLiteral("198.51.100.1"));
|
|
|
+ EXPECT_TRUE(isPlainIpLiteral("2001:db8::5"));
|
|
|
+ EXPECT_TRUE(isPlainIpLiteral("[2001:db8::5]"));
|
|
|
+ EXPECT_FALSE(isPlainIpLiteral(""));
|
|
|
+ EXPECT_FALSE(isPlainIpLiteral("10.0.0.0/8"));
|
|
|
+ EXPECT_FALSE(isPlainIpLiteral("example.com"));
|
|
|
+ EXPECT_FALSE(isPlainIpLiteral("[]"));
|
|
|
+}
|