|
@@ -4261,3 +4261,70 @@ TEST_F(ApiFixture, NoListingRouteRefusesAnIncidentalParameter) {
|
|
|
EXPECT_EQ(res->status, 200) << path << ": " << res->body;
|
|
EXPECT_EQ(res->status, 200) << path << ": " << res->body;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// A COLLECTION-SCOPED KEY MUST NOT ENUMERATE THE WHOLE PROJECT. Project access
|
|
|
|
|
+// alone used to be the only check here, so a key granted exactly one
|
|
|
|
|
+// collection could read every collection's name, document count and size -
|
|
|
|
|
+// while the index-values route next door consulted the scope rules through
|
|
|
|
|
+// `requireCapability`. That inconsistency is what marked it an oversight.
|
|
|
|
|
+TEST_F(ApiFixture, TheCollectionsListingIsFilteredByTheKeysScope) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ for (const char* n : {"scopemine", "scopetheirs"}) {
|
|
|
|
|
+ c.Delete((base + "/" + n).c_str());
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"name", n}, {"kind", "json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201) << n;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ auto mk = c.Post("/api/v1/keys",
|
|
|
|
|
+ nlohmann::json{{"label", "scopedlister"}, {"projects", {project_}}, {"admin", false},
|
|
|
|
|
+ {"scope", {{"rules", {{{"collection", "scopemine"},
|
|
|
|
|
+ {"ops", {"read"}}}}}}}}
|
|
|
|
|
+ .dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(mk); ASSERT_EQ(mk->status, 201) << mk->body;
|
|
|
|
|
+ const std::string scoped = nlohmann::json::parse(mk->body)["key"];
|
|
|
|
|
+
|
|
|
|
|
+ auto listed = request("GET", base, "", "", scoped);
|
|
|
|
|
+ ASSERT_TRUE(listed);
|
|
|
|
|
+ ASSERT_EQ(listed->status, 200) << listed->body;
|
|
|
|
|
+ const nlohmann::json j = nlohmann::json::parse(listed->body);
|
|
|
|
|
+ bool sawMine = false, sawTheirs = false;
|
|
|
|
|
+ for (const auto& m : j["collections"]) {
|
|
|
|
|
+ if (m["name"] == "scopemine") sawMine = true;
|
|
|
|
|
+ if (m["name"] == "scopetheirs") sawTheirs = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ // BOTH directions, or this proves nothing: a check that only asserts the
|
|
|
|
|
+ // absence would pass against a route that returns an empty list, and one
|
|
|
|
|
+ // that only asserts the presence would pass against no filtering at all.
|
|
|
|
|
+ EXPECT_TRUE(sawMine) << "a key granted a collection must still see it: " << listed->body;
|
|
|
|
|
+ EXPECT_FALSE(sawTheirs) << "scope does not reach this collection: " << listed->body;
|
|
|
|
|
+
|
|
|
|
|
+ // `read` is enough. The op asked is "any rule mentions it", deliberately
|
|
|
|
|
+ // weaker than `list`: a key that may read a document already knows the
|
|
|
|
|
+ // collection exists, and the listing exists so nobody has to hardcode
|
|
|
|
|
+ // names into a backup script.
|
|
|
|
|
+ EXPECT_TRUE(j["scope_filtered"].get<bool>()) << listed->body;
|
|
|
|
|
+
|
|
|
|
|
+ // An unscoped key is told its view is COMPLETE, which is what keeps an
|
|
|
|
|
+ // empty list from reading as an empty project for everyone else.
|
|
|
|
|
+ auto full = c.Get(base.c_str());
|
|
|
|
|
+ ASSERT_EQ(full->status, 200) << full->body;
|
|
|
|
|
+ EXPECT_FALSE(nlohmann::json::parse(full->body)["scope_filtered"].get<bool>()) << full->body;
|
|
|
|
|
+
|
|
|
|
|
+ // One listing must cost ONE rate-limit token, not one per collection
|
|
|
|
|
+ // examined. Charging per collection would let a caller rate-limit itself
|
|
|
|
|
+ // on a single request, which is why the rule is looked up directly rather
|
|
|
|
|
+ // than through requireCapabilityCore.
|
|
|
|
|
+ auto mk2 = c.Post("/api/v1/keys",
|
|
|
|
|
+ nlohmann::json{{"label", "scopedlister2"}, {"projects", {project_}}, {"admin", false},
|
|
|
|
|
+ {"scope", {{"rate_limit_per_min", 2},
|
|
|
|
|
+ {"rules", {{{"collection", "*"}, {"ops", {"read"}}}}}}}}
|
|
|
|
|
+ .dump(), "application/json");
|
|
|
|
|
+ ASSERT_EQ(mk2->status, 201) << mk2->body;
|
|
|
|
|
+ const std::string limited = nlohmann::json::parse(mk2->body)["key"];
|
|
|
|
|
+ auto one = request("GET", base, "", "", limited);
|
|
|
|
|
+ ASSERT_TRUE(one);
|
|
|
|
|
+ EXPECT_EQ(one->status, 200) << "a two-per-minute key must afford one listing of "
|
|
|
|
|
+ "several collections: " << one->body;
|
|
|
|
|
+}
|