|
|
@@ -455,3 +455,65 @@ TEST(SsrfBlockReason, ReasonNamesTheOffendingAddressAndRange) {
|
|
|
EXPECT_NE(why->find("10.0.0.5"), std::string::npos);
|
|
|
EXPECT_NE(why->find("mixed.example"), std::string::npos);
|
|
|
}
|
|
|
+
|
|
|
+// ------------------------------------------- settings: union, not replace
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AddsToTheDefaultsRatherThanReplacingThem) {
|
|
|
+ // The whole point of this entry point. A replacing list meant an operator
|
|
|
+ // adding one CIDR silently reopened loopback, RFC1918 and the cloud
|
|
|
+ // metadata endpoint - a footgun with no warning attached.
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({"203.0.55.0/24"}, false, nullptr);
|
|
|
+ EXPECT_TRUE(p.isBlocked("203.0.55.9")) << "the operator's own entry must apply";
|
|
|
+ EXPECT_TRUE(p.isBlocked("127.0.0.1")) << "loopback was silently unblocked";
|
|
|
+ EXPECT_TRUE(p.isBlocked("10.1.2.3")) << "RFC1918 was silently unblocked";
|
|
|
+ EXPECT_TRUE(p.isBlocked("169.254.169.254")) << "the metadata endpoint was unblocked";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AnEmptySettingsListIsExactlyTheDefaults) {
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({}, false, nullptr);
|
|
|
+ EXPECT_EQ(p.size(), SsrfPolicy::withDefaults().size());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AllowPrivateRelaxesPrivateAndLoopbackOnly) {
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({}, /*allowPrivate*/ true, nullptr);
|
|
|
+ EXPECT_FALSE(p.isBlocked("127.0.0.1")) << "loopback should be reachable when opted in";
|
|
|
+ EXPECT_FALSE(p.isBlocked("10.1.2.3"));
|
|
|
+ EXPECT_FALSE(p.isBlocked("192.168.1.10"));
|
|
|
+ EXPECT_FALSE(p.isBlocked("172.16.0.5"));
|
|
|
+ EXPECT_FALSE(p.isBlocked("fc00::1"));
|
|
|
+ EXPECT_FALSE(p.isBlocked("::1"));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AllowPrivateStillBlocksTheMetadataEndpoint) {
|
|
|
+ // The highest-value prize an SSRF has, and nothing legitimate targets it.
|
|
|
+ // Opting into private egress must not hand it over.
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({}, /*allowPrivate*/ true, nullptr);
|
|
|
+ EXPECT_TRUE(p.isBlocked("169.254.169.254"));
|
|
|
+ EXPECT_TRUE(p.isBlocked("169.254.0.1"));
|
|
|
+ EXPECT_TRUE(p.isBlocked("fe80::1")) << "IPv6 link-local is the same exposure";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AllowPrivateDoesNotReopenTheIpv6SpellingsOfIpv4) {
|
|
|
+ // Relaxing these would reopen by the back door what allowPrivate relaxes by
|
|
|
+ // the front, and worse: NAT64 and 6to4 can express ANY IPv4 destination.
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({}, /*allowPrivate*/ true, nullptr);
|
|
|
+ EXPECT_TRUE(p.isBlocked("64:ff9b::1")) << "NAT64 prefix";
|
|
|
+ EXPECT_TRUE(p.isBlocked("2002::1")) << "6to4 prefix";
|
|
|
+ EXPECT_TRUE(p.isBlocked("ff02::1")) << "multicast";
|
|
|
+ EXPECT_TRUE(p.isBlocked("240.0.0.1")) << "reserved";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, AllowPrivateStillHonoursTheOperatorsOwnEntries) {
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({"10.9.0.0/16"}, /*allowPrivate*/ true, nullptr);
|
|
|
+ EXPECT_TRUE(p.isBlocked("10.9.1.1")) << "an explicit block must survive allowPrivate";
|
|
|
+ EXPECT_FALSE(p.isBlocked("10.8.1.1")) << "the rest of RFC1918 is relaxed as asked";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(SsrfFromSettings, ReportsUnparseableEntriesWithoutDroppingTheDefaults) {
|
|
|
+ std::vector<std::string> invalid;
|
|
|
+ const SsrfPolicy p = SsrfPolicy::fromSettings({"not-a-cidr", "10.9.0.0/16"}, false, &invalid);
|
|
|
+ ASSERT_EQ(invalid.size(), 1u);
|
|
|
+ EXPECT_EQ(invalid[0], "not-a-cidr");
|
|
|
+ EXPECT_TRUE(p.isBlocked("127.0.0.1")) << "a typo must not take the defaults with it";
|
|
|
+ EXPECT_TRUE(p.isBlocked("10.9.1.1"));
|
|
|
+}
|