|
|
@@ -4708,6 +4708,81 @@ public:
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
+// --- the ADDRESS gate ------------------------------------------------------
|
|
|
+//
|
|
|
+// Same no-runner trick as the auth gate below, and for the same reason: the
|
|
|
+// address check sits ahead of the runner check, so a refusal is observable
|
|
|
+// without one and a request that PASSES falls through to 503.
|
|
|
+//
|
|
|
+// The fixture's client connects from 127.0.0.1, which the default
|
|
|
+// `trusted_proxy_cidrs` trusts, so a test can set `X-Real-IP` and have it
|
|
|
+// BELIEVED. That is what makes the strongest form of these tests possible:
|
|
|
+// vary the caller's address and demand the answer varies with it, rather than
|
|
|
+// asserting a refusal that might equally be a dispatch refusing everything.
|
|
|
+namespace {
|
|
|
+svapi::KeyScope policyWithIpAllow(std::vector<std::string> cidrs) {
|
|
|
+ svapi::KeyScope s;
|
|
|
+ s.ipAllow = std::move(cidrs);
|
|
|
+ return s;
|
|
|
+}
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AFunctionWithAnAddressAllowListRefusesACallerOutsideIt) {
|
|
|
+ functions_->put(project_, fnRow("ipgate", "public"),
|
|
|
+ policyWithIpAllow({"203.0.113.0/24"}));
|
|
|
+ auto anon = noAuth();
|
|
|
+ auto res = anon.Get(("/fn/" + project_ + "/ipgate").c_str());
|
|
|
+ ASSERT_TRUE(res);
|
|
|
+ EXPECT_EQ(res->status, 403) << res->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(res->body)["error"]["code"], "ip_not_allowed") << res->body;
|
|
|
+ // The refusal must not hand a prober its own apparent address back.
|
|
|
+ EXPECT_EQ(res->body.find("127.0.0.1"), std::string::npos)
|
|
|
+ << "the message must not echo the caller's address: " << res->body;
|
|
|
+}
|
|
|
+
|
|
|
+// THE PAIRING, and it varies the INPUT rather than the policy: the same
|
|
|
+// function, the same list, a different caller address. Without this the test
|
|
|
+// above would pass just as well against a dispatch that refused everything.
|
|
|
+TEST_F(ApiFixture, TheSameFunctionADMITSACallerInsideTheListAndReachesTheRunnerCheck) {
|
|
|
+ functions_->put(project_, fnRow("ipgate2", "public"),
|
|
|
+ policyWithIpAllow({"203.0.113.0/24"}));
|
|
|
+ auto anon = noAuth();
|
|
|
+ httplib::Headers h{{"X-Real-IP", "203.0.113.9"}};
|
|
|
+ auto res = anon.Get(("/fn/" + project_ + "/ipgate2").c_str(), h);
|
|
|
+ ASSERT_TRUE(res);
|
|
|
+ // 503 because this deployment has no runner: the request got PAST the
|
|
|
+ // address gate and died on the next check down.
|
|
|
+ EXPECT_EQ(res->status, 503) << res->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(res->body)["error"]["code"], "runner_unavailable")
|
|
|
+ << res->body;
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, AnEmptyAllowListRestrictsNothing) {
|
|
|
+ functions_->put(project_, fnRow("ipgate3", "public"), policyWithIpAllow({}));
|
|
|
+ auto anon = noAuth();
|
|
|
+ auto res = anon.Get(("/fn/" + project_ + "/ipgate3").c_str());
|
|
|
+ ASSERT_TRUE(res);
|
|
|
+ EXPECT_EQ(res->status, 503) << "no list means no restriction: " << res->body;
|
|
|
+}
|
|
|
+
|
|
|
+// THE ORDERING, which is the load-bearing claim and the one a later edit is
|
|
|
+// most likely to break by moving the check somewhere tidier. A function that
|
|
|
+// is BOTH key-gated and address-restricted, called anonymously from a
|
|
|
+// disallowed address, must answer the ADDRESS refusal - not 401. If this
|
|
|
+// returns `unauthorized`, the address check has been moved after the auth
|
|
|
+// gate, and a caller who has no business here can once again make us look up
|
|
|
+// a token and charge a rate-limit bucket.
|
|
|
+TEST_F(ApiFixture, TheAddressGateRunsBEFOREAuthenticationSoARefusedCallerIsNeverLookedUp) {
|
|
|
+ functions_->put(project_, fnRow("ipgate4", "key"),
|
|
|
+ policyWithIpAllow({"203.0.113.0/24"}));
|
|
|
+ auto anon = noAuth();
|
|
|
+ auto res = anon.Get(("/fn/" + project_ + "/ipgate4").c_str());
|
|
|
+ ASSERT_TRUE(res);
|
|
|
+ EXPECT_EQ(res->status, 403) << res->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(res->body)["error"]["code"], "ip_not_allowed")
|
|
|
+ << "a 401 here would mean the address gate moved after authentication: " << res->body;
|
|
|
+}
|
|
|
+
|
|
|
TEST_F(ApiFixture, AGatedFunctionRefusesAnAnonymousCallerAndNamesHowToAuthenticate) {
|
|
|
functions_->put(project_, fnRow("gated", "key"), svapi::KeyScope{});
|
|
|
|
|
|
@@ -5003,3 +5078,85 @@ TEST_F(ApiFixture, PatchReportsAnAbsentDocumentWithoutAnExistenceProbe) {
|
|
|
EXPECT_EQ(body["v"], 1) << "the empty patch or the merge dropped an existing field";
|
|
|
EXPECT_EQ(body["w"], 2);
|
|
|
}
|
|
|
+
|
|
|
+// --- the documents listing's paging metadata ---------------------------------
|
|
|
+//
|
|
|
+// `total` and `has_more` are computed by the database BEFORE pagination and
|
|
|
+// were being discarded by our handler, which sent only `count` - the PAGE size.
|
|
|
+// A consumer therefore had to page an entire matching set to learn how big it
|
|
|
+// was, and one measured instance of that was a full scan plus 1750 documents of
|
|
|
+// encoding to obtain a single integer.
|
|
|
+
|
|
|
+TEST_F(ApiFixture, TheDocumentsListingReportsTheFILTEREDTotalNotThePageOrTheCollection) {
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","pagemeta"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/pagemeta/documents";
|
|
|
+ // SEVEN rows, FOUR of which match. Three distinct numbers - page 2, match
|
|
|
+ // total 4, collection 7 - so confusing any pair of them fails this test.
|
|
|
+ // With one shared number the assertions would pass under every mix-up.
|
|
|
+ for (const char* k : {"yes", "yes", "yes", "yes", "no", "no", "no"})
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"flag", k}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ auto first = c.Get((docs + "?filter=flag:eq:yes&limit=2").c_str());
|
|
|
+ ASSERT_TRUE(first); ASSERT_EQ(first->status, 200);
|
|
|
+ const nlohmann::json a = nlohmann::json::parse(first->body);
|
|
|
+ // PRESENCE BEFORE VALUE, in every arm. Indexing a missing key with
|
|
|
+ // `operator[]` on a const json ASSERTS inside nlohmann and aborts the whole
|
|
|
+ // binary - which in a suite that must run serially would take every later
|
|
|
+ // test with it and report the regression as a crash rather than as a named
|
|
|
+ // failing assertion. Found by deliberately falsifying this test.
|
|
|
+ ASSERT_TRUE(a.contains("total")) << "the listing stopped reporting `total`";
|
|
|
+ ASSERT_TRUE(a.contains("has_more")) << "the listing stopped reporting `has_more`";
|
|
|
+ EXPECT_EQ(a["documents"].size(), 2u);
|
|
|
+ EXPECT_EQ(a["count"], 2) << "`count` must stay the PAGE size; callers parse it already";
|
|
|
+ EXPECT_EQ(a["total"], 4) << "the filtered match total, not the page (2) and not the "
|
|
|
+ "collection (7)";
|
|
|
+ EXPECT_TRUE(a["has_more"].get<bool>());
|
|
|
+
|
|
|
+ // The PAIRING, and the half that makes `has_more` worth sending: the last
|
|
|
+ // page must say false while `total` is unchanged. Without this arm a
|
|
|
+ // handler hardcoding `true` would pass the assertion above.
|
|
|
+ auto last = c.Get((docs + "?filter=flag:eq:yes&limit=2&offset=2").c_str());
|
|
|
+ ASSERT_TRUE(last); ASSERT_EQ(last->status, 200);
|
|
|
+ const nlohmann::json b = nlohmann::json::parse(last->body);
|
|
|
+ ASSERT_TRUE(b.contains("total")); ASSERT_TRUE(b.contains("has_more"));
|
|
|
+ EXPECT_EQ(b["documents"].size(), 2u);
|
|
|
+ EXPECT_EQ(b["total"], 4);
|
|
|
+ EXPECT_FALSE(b["has_more"].get<bool>()) << "a full page and the LAST page must not look "
|
|
|
+ "identical - that was the original defect";
|
|
|
+
|
|
|
+ // Unfiltered, the total is the whole collection. Pins that the filter is
|
|
|
+ // what narrows `total`, rather than `total` happening to equal some
|
|
|
+ // constant this collection has.
|
|
|
+ auto all = c.Get((docs + "?limit=1").c_str());
|
|
|
+ ASSERT_TRUE(all); ASSERT_EQ(all->status, 200);
|
|
|
+ const nlohmann::json u = nlohmann::json::parse(all->body);
|
|
|
+ ASSERT_TRUE(u.contains("total"));
|
|
|
+ EXPECT_EQ(u["total"], 7);
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, ThePagingFieldsArePRESENTEvenWhenNothingMatches) {
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","pagempty"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/pagempty/documents";
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"flag","no"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ auto r = c.Get((docs + "?filter=flag:eq:nothingmatchesthis").c_str());
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200);
|
|
|
+ const nlohmann::json body = nlohmann::json::parse(r->body);
|
|
|
+ // KEY PRESENCE is the assertion, not the value. It is what makes "the field
|
|
|
+ // is absent" mean exactly one thing - a server older than this release -
|
|
|
+ // rather than also meaning "there was nothing interesting to report". A
|
|
|
+ // field emitted only when true is this codebase's most repeated trap.
|
|
|
+ ASSERT_TRUE(body.contains("total"));
|
|
|
+ ASSERT_TRUE(body.contains("has_more"));
|
|
|
+ EXPECT_EQ(body["total"], 0);
|
|
|
+ EXPECT_FALSE(body["has_more"].get<bool>());
|
|
|
+ EXPECT_TRUE(body["documents"].empty());
|
|
|
+}
|