|
|
@@ -4605,3 +4605,94 @@ TEST_F(ApiFixture, AScopeRefusalNamesTheCollectionTheOpAndWhatTheKeyHolds) {
|
|
|
ASSERT_EQ(body["error"]["details"]["granted"].size(), 1u);
|
|
|
EXPECT_EQ(body["error"]["details"]["granted"][0]["collection"], "designs");
|
|
|
}
|
|
|
+
|
|
|
+// ─── The listing answers to the Origin allow-list too (task #87) ──────────
|
|
|
+//
|
|
|
+// Found by a consumer measuring their own publishable key: the listing
|
|
|
+// answered 200 from a bogus Origin while a document read from that same
|
|
|
+// Origin answered 403. A publishable key ships in the browser, so any origin
|
|
|
+// holding it could enumerate the project inventory - names, document counts,
|
|
|
+// sizes - while being refused every document in it.
|
|
|
+TEST_F(ApiFixture, TheCollectionsListingAnswersToTheOriginAllowList) {
|
|
|
+ ASSERT_EQ(admin().Post(("/api/v1/projects/" + project_ + "/collections").c_str(),
|
|
|
+ nlohmann::json{{"name", "designs"}, {"kind", "json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ auto mk = admin().Post("/api/v1/keys",
|
|
|
+ nlohmann::json{{"label", "publishable"}, {"projects", {project_}}, {"admin", false},
|
|
|
+ {"scope", {{"origins", {"https://good.example.com"}},
|
|
|
+ {"rules", {{{"collection", "*"}, {"ops", {"read", "list"}}}}}}}}
|
|
|
+ .dump(), "application/json");
|
|
|
+ ASSERT_EQ(mk->status, 201) << mk->body;
|
|
|
+ const std::string pub = nlohmann::json::parse(mk->body)["key"];
|
|
|
+
|
|
|
+ const std::string listing = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ auto get = [&](const char* origin) {
|
|
|
+ httplib::Client c("127.0.0.1", port_);
|
|
|
+ httplib::Headers h{{"Authorization", "Bearer " + pub}};
|
|
|
+ if (origin) h.emplace("Origin", origin);
|
|
|
+ return c.Get(listing.c_str(), h);
|
|
|
+ };
|
|
|
+
|
|
|
+ auto bogus = get("https://evil.example.com");
|
|
|
+ ASSERT_TRUE(bogus);
|
|
|
+ EXPECT_EQ(bogus->status, 403) << "a non-allowlisted origin must not enumerate the "
|
|
|
+ "project inventory: " << bogus->body;
|
|
|
+
|
|
|
+ // The pairing: the gate is selective, not blanket. Without this the test
|
|
|
+ // above would pass against a listing that refused everyone, which would
|
|
|
+ // break every legitimate browser caller.
|
|
|
+ auto good = get("https://good.example.com");
|
|
|
+ ASSERT_TRUE(good);
|
|
|
+ EXPECT_EQ(good->status, 200) << "the allowlisted origin must still work: " << good->body;
|
|
|
+ EXPECT_TRUE(nlohmann::json::parse(good->body).contains("collections")) << good->body;
|
|
|
+}
|
|
|
+
|
|
|
+// The behaviour a consumer explicitly asked about, pinned because I am telling
|
|
|
+// them it is pre-existing rather than introduced: an ABSENT Origin is refused
|
|
|
+// when an allow-list is set, and it is refused IDENTICALLY on the listing and
|
|
|
+// on a document read. So a key intended for server-side use must carry no
|
|
|
+// allow-list - which was already true before the listing was gated.
|
|
|
+TEST_F(ApiFixture, AnAbsentOriginIsRefusedTheSameWayOnBothRoutes) {
|
|
|
+ ASSERT_EQ(admin().Post(("/api/v1/projects/" + project_ + "/collections").c_str(),
|
|
|
+ nlohmann::json{{"name", "designs"}, {"kind", "json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ auto mk = admin().Post("/api/v1/keys",
|
|
|
+ nlohmann::json{{"label", "pinned"}, {"projects", {project_}}, {"admin", false},
|
|
|
+ {"scope", {{"origins", {"https://good.example.com"}},
|
|
|
+ {"rules", {{{"collection", "*"}, {"ops", {"read", "list"}}}}}}}}
|
|
|
+ .dump(), "application/json");
|
|
|
+ ASSERT_EQ(mk->status, 201) << mk->body;
|
|
|
+ const std::string pub = nlohmann::json::parse(mk->body)["key"];
|
|
|
+
|
|
|
+ httplib::Client c("127.0.0.1", port_);
|
|
|
+ const httplib::Headers noOrigin{{"Authorization", "Bearer " + pub}};
|
|
|
+ auto listing = c.Get(("/api/v1/projects/" + project_ + "/collections").c_str(), noOrigin);
|
|
|
+ auto document = c.Get(
|
|
|
+ ("/api/v1/projects/" + project_ + "/collections/designs/documents?limit=1").c_str(),
|
|
|
+ noOrigin);
|
|
|
+ ASSERT_TRUE(listing);
|
|
|
+ ASSERT_TRUE(document);
|
|
|
+ EXPECT_EQ(listing->status, document->status)
|
|
|
+ << "the two routes must agree about an absent Origin; listing=" << listing->body
|
|
|
+ << " document=" << document->body;
|
|
|
+ EXPECT_EQ(listing->status, 403);
|
|
|
+}
|
|
|
+
|
|
|
+// And a key with NO allow-list is untouched by any of this: an empty list
|
|
|
+// means any origin, which is what every server-side key uses.
|
|
|
+TEST_F(ApiFixture, AKeyWithNoOriginAllowListStillListsFromAnywhere) {
|
|
|
+ auto mk = admin().Post("/api/v1/keys",
|
|
|
+ nlohmann::json{{"label", "serverside"}, {"projects", {project_}}, {"admin", false},
|
|
|
+ {"scope", {{"rules", {{{"collection", "*"}, {"ops", {"read", "list"}}}}}}}}
|
|
|
+ .dump(), "application/json");
|
|
|
+ ASSERT_EQ(mk->status, 201) << mk->body;
|
|
|
+ const std::string key = nlohmann::json::parse(mk->body)["key"];
|
|
|
+
|
|
|
+ httplib::Client c("127.0.0.1", port_);
|
|
|
+ const httplib::Headers headers{{"Authorization", "Bearer " + key},
|
|
|
+ {"Origin", "https://anywhere.example.com"}};
|
|
|
+ auto res = c.Get(("/api/v1/projects/" + project_ + "/collections").c_str(), headers);
|
|
|
+ ASSERT_TRUE(res);
|
|
|
+ EXPECT_EQ(res->status, 200) << "an empty allow-list means ANY origin: " << res->body;
|
|
|
+}
|