|
|
@@ -1900,3 +1900,93 @@ TEST(HostContextMail, AnHonestBodyEndingInAParenAndCrlfIsStillDelivered) {
|
|
|
EXPECT_NE(value.dump().find("as it should"), std::string::npos) << value.dump();
|
|
|
server.stop();
|
|
|
}
|
|
|
+
|
|
|
+// ------------------------------------------------- the network egress gate
|
|
|
+//
|
|
|
+// One gate for BOTH outbound surfaces. The tests come in pairs on purpose: a
|
|
|
+// gate is only proven by showing it refuses when set to deny AND lets the call
|
|
|
+// through when it is not, because a gate that refused everything would pass
|
|
|
+// every denial test while taking the whole feature offline.
|
|
|
+
|
|
|
+TEST(HostContextEgressGate, CtxFetchIsRefusedWhenTheProjectMayNotReachTheNetwork) {
|
|
|
+ HostContextConfig cfg = httpTrigger({});
|
|
|
+ cfg.networkEgress = false;
|
|
|
+ auto out = runScript(std::move(cfg),
|
|
|
+ catching("ctx.fetch('https://example.com/')"));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), true) << out.result.value.dump();
|
|
|
+ EXPECT_EQ(out.result.value.at("code"), "egress_blocked") << out.result.value.dump();
|
|
|
+ EXPECT_NE(out.result.value.at("message").get<std::string>().find("network_egress"),
|
|
|
+ std::string::npos)
|
|
|
+ << out.result.value.dump();
|
|
|
+}
|
|
|
+
|
|
|
+TEST(HostContextEgressGate, CtxMailIsRefusedByTheSameGate) {
|
|
|
+ // The point of the shared gate: an operator who denies a tenant the network
|
|
|
+ // must not find the OTHER outbound surface still open. Gating only
|
|
|
+ // ctx.fetch would let the two drift, and mail carries credentials.
|
|
|
+ HostContextConfig cfg = mailTrigger();
|
|
|
+ cfg.networkEgress = false;
|
|
|
+ auto out = runScript(std::move(cfg),
|
|
|
+ catching("ctx.mail.read({ host: '127.0.0.1', port: 1143,"
|
|
|
+ " tls: false, user: 'u', password: 'p' })"));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), true) << out.result.value.dump();
|
|
|
+ EXPECT_EQ(out.result.value.at("code"), "egress_blocked") << out.result.value.dump();
|
|
|
+ EXPECT_NE(out.result.value.at("message").get<std::string>().find("network_egress"),
|
|
|
+ std::string::npos)
|
|
|
+ << out.result.value.dump();
|
|
|
+}
|
|
|
+
|
|
|
+TEST(HostContextEgressGate, TheRefusalHappensBeforeAnyNameIsResolved) {
|
|
|
+ // A project that may not reach the network must not emit a DNS query
|
|
|
+ // either: a lookup is an observable side effect and a channel of its own.
|
|
|
+ // A hostname that cannot resolve would report a resolution failure if the
|
|
|
+ // gate ran too late, so the slug tells the two apart.
|
|
|
+ HostContextConfig cfg = httpTrigger({});
|
|
|
+ cfg.networkEgress = false;
|
|
|
+ auto out = runScript(
|
|
|
+ std::move(cfg),
|
|
|
+ catching("ctx.fetch('https://this-name-does-not-exist.invalid/')"));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), true) << out.result.value.dump();
|
|
|
+ EXPECT_EQ(out.result.value.at("code"), "egress_blocked") << out.result.value.dump();
|
|
|
+ EXPECT_NE(out.result.value.at("message").get<std::string>().find("refused before resolving"),
|
|
|
+ std::string::npos)
|
|
|
+ << out.result.value.dump();
|
|
|
+}
|
|
|
+
|
|
|
+TEST(HostContextEgressGate, MailStillWorksWhenEgressIsAllowed) {
|
|
|
+ // The falsifying half, and the one that matters most: `allow` must leave
|
|
|
+ // the surface fully working. Asserted against a real socket and a real
|
|
|
+ // login rather than by checking an error message changed.
|
|
|
+ svapi::testing::FakeImapServer server;
|
|
|
+ ASSERT_TRUE(server.start());
|
|
|
+ HostContextConfig cfg = mailTrigger();
|
|
|
+ cfg.networkEgress = true;
|
|
|
+ auto out = runScript(std::move(cfg),
|
|
|
+ withPort(catching("ctx.mail.read({ host: '127.0.0.1', port: %PORT%,"
|
|
|
+ " tls: false, user: 'u', password: 'p' })"),
|
|
|
+ server.port()));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ EXPECT_EQ(out.result.value.at("threw"), false) << out.result.value.dump();
|
|
|
+ server.stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST(HostContextEgressGate, AnUnsetGateLeavesTheSsrfGuardAsTheOnlyRefusal) {
|
|
|
+ // With no project setting, behaviour must be exactly what shipped: the
|
|
|
+ // SSRF classifier still refuses a private address, and the refusal must be
|
|
|
+ // ITS reason rather than the new gate's - otherwise the default would have
|
|
|
+ // quietly become deny.
|
|
|
+ HostContextConfig cfg = httpTrigger({});
|
|
|
+ cfg.egressPolicy = std::make_shared<const svapi::functions::SsrfPolicy>(
|
|
|
+ svapi::functions::SsrfPolicy::withDefaults());
|
|
|
+ ASSERT_FALSE(cfg.networkEgress.has_value());
|
|
|
+ auto out = runScript(std::move(cfg), catching("ctx.fetch('http://169.254.169.254/latest/')"));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), true) << out.result.value.dump();
|
|
|
+ EXPECT_EQ(out.result.value.at("code"), "egress_blocked") << out.result.value.dump();
|
|
|
+ EXPECT_EQ(out.result.value.at("message").get<std::string>().find("network_egress"),
|
|
|
+ std::string::npos)
|
|
|
+ << "the default must not be deny: " << out.result.value.dump();
|
|
|
+}
|