|
|
@@ -0,0 +1,116 @@
|
|
|
+// What a scope refusal is allowed to say.
|
|
|
+//
|
|
|
+// The status stays 403; only the message changed. These tests pin the
|
|
|
+// DISCLOSURE BOUNDARY, which is the part that could quietly reopen the leak
|
|
|
+// closed in 0.13.0: a scoped key must not be able to learn which collections
|
|
|
+// exist in its project outside its own scope. Echoing what the key IS granted
|
|
|
+// is safe - the holder can read that off their own key - while anything that
|
|
|
+// varied with the requested collection's EXISTENCE would move the leak from
|
|
|
+// the status code into the message.
|
|
|
+#include "scope_refusal.hpp"
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+using namespace svapi;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+KeyScope scopeWith(std::vector<KeyScopeRule> rules) {
|
|
|
+ KeyScope s;
|
|
|
+ s.rules = std::move(rules);
|
|
|
+ return s;
|
|
|
+}
|
|
|
+
|
|
|
+KeyScopeRule rule(const std::string& coll, std::set<KeyOp> ops) {
|
|
|
+ KeyScopeRule r;
|
|
|
+ r.collection = coll;
|
|
|
+ r.ops = std::move(ops);
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+TEST(ScopeRefusal, NamesTheCollectionAndTheOperation) {
|
|
|
+ // The whole complaint: the old message named neither, so a misspelling and
|
|
|
+ // a missing grant read identically and sent the holder to check their KEY.
|
|
|
+ const ScopeRefusal r =
|
|
|
+ describeScopeRefusal(scopeWith({rule("designs", {KeyOp::Read})}), "kitchen_presets",
|
|
|
+ KeyOp::Read);
|
|
|
+ EXPECT_NE(r.message.find("kitchen_presets"), std::string::npos);
|
|
|
+ EXPECT_NE(r.message.find("read"), std::string::npos);
|
|
|
+ EXPECT_EQ(r.details["collection"], "kitchen_presets");
|
|
|
+ EXPECT_EQ(r.details["operation"], "read");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ScopeRefusal, EchoesWhatTheKeyISGranted) {
|
|
|
+ const ScopeRefusal r = describeScopeRefusal(
|
|
|
+ scopeWith({rule("designs", {KeyOp::Read, KeyOp::List}), rule("quotes", {KeyOp::Insert})}),
|
|
|
+ "nope", KeyOp::Read);
|
|
|
+ EXPECT_NE(r.message.find("designs"), std::string::npos);
|
|
|
+ EXPECT_NE(r.message.find("quotes"), std::string::npos);
|
|
|
+ ASSERT_EQ(r.details["granted"].size(), 2u);
|
|
|
+ EXPECT_EQ(r.details["granted"][0]["collection"], "designs");
|
|
|
+ EXPECT_EQ(r.details["granted"][1]["collection"], "quotes");
|
|
|
+}
|
|
|
+
|
|
|
+// THE BOUNDARY TEST. Nothing in the answer may name a collection the key does
|
|
|
+// not hold, other than the one the caller themselves asked about.
|
|
|
+TEST(ScopeRefusal, LeaksNoCollectionTheKeyDoesNotHold) {
|
|
|
+ const ScopeRefusal r =
|
|
|
+ describeScopeRefusal(scopeWith({rule("designs", {KeyOp::Read})}), "asked", KeyOp::Read);
|
|
|
+ for (const char* secret : {"catalog_kitchen_modules", "settings", "quotes", "vectorapi_keys"}) {
|
|
|
+ EXPECT_EQ(r.message.find(secret), std::string::npos)
|
|
|
+ << "the refusal must not name " << secret;
|
|
|
+ EXPECT_EQ(r.details.dump().find(secret), std::string::npos)
|
|
|
+ << "the details must not name " << secret;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// The refusal cannot depend on whether the collection exists, and the cheapest
|
|
|
+// way to guarantee that is structural: this function is never TOLD. Pinned as
|
|
|
+// a test so a later "helpful" change that passes existence in has to delete
|
|
|
+// this assertion deliberately rather than by accident.
|
|
|
+TEST(ScopeRefusal, IsIdenticalForTwoNamesTheKeyEquallyLacks) {
|
|
|
+ const KeyScope s = scopeWith({rule("designs", {KeyOp::Read})});
|
|
|
+ const ScopeRefusal a = describeScopeRefusal(s, "exists_but_ungranted", KeyOp::Read);
|
|
|
+ const ScopeRefusal b = describeScopeRefusal(s, "never_created", KeyOp::Read);
|
|
|
+ // They may differ ONLY by the name the caller themselves supplied, which
|
|
|
+ // is echoed more than once, so normalise that out and demand the rest be
|
|
|
+ // byte-identical.
|
|
|
+ auto normalise = [](std::string msg, const std::string& name) {
|
|
|
+ for (size_t at = msg.find(name); at != std::string::npos; at = msg.find(name, at + 5))
|
|
|
+ msg.replace(at, name.size(), "NAME");
|
|
|
+ return msg;
|
|
|
+ };
|
|
|
+ EXPECT_EQ(normalise(a.message, "exists_but_ungranted"),
|
|
|
+ normalise(b.message, "never_created"))
|
|
|
+ << "the refusal must not vary with anything but the caller's own input";
|
|
|
+ EXPECT_EQ(a.details["granted"], b.details["granted"]);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ScopeRefusal, SaysTheTwoCausesAreIndistinguishable) {
|
|
|
+ // The sentence that would have saved three days on a live site: the holder
|
|
|
+ // is the only party who can tell a misspelling from an unprovisioned
|
|
|
+ // collection, so the message has to tell them that is the question.
|
|
|
+ const ScopeRefusal r =
|
|
|
+ describeScopeRefusal(scopeWith({rule("designs", {KeyOp::Read})}), "kitchen_presets",
|
|
|
+ KeyOp::Read);
|
|
|
+ EXPECT_NE(r.message.find("does not exist"), std::string::npos) << r.message;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ScopeRefusal, AKeyWithNoRulesSaysSoRatherThanShowingAnEmptyList) {
|
|
|
+ // A different mistake from the wrong rules, and an empty list reads like a
|
|
|
+ // rendering failure.
|
|
|
+ const ScopeRefusal r = describeScopeRefusal(scopeWith({}), "anything", KeyOp::Read);
|
|
|
+ EXPECT_NE(r.message.find("no collections at all"), std::string::npos) << r.message;
|
|
|
+ EXPECT_TRUE(r.details["granted"].empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ScopeRefusal, ReportsTheOpsOfEachGrantedRule) {
|
|
|
+ const ScopeRefusal r = describeScopeRefusal(
|
|
|
+ scopeWith({rule("designs", {KeyOp::Read, KeyOp::Insert})}), "x", KeyOp::Delete);
|
|
|
+ const std::string dumped = r.details["granted"].dump();
|
|
|
+ EXPECT_NE(dumped.find("read"), std::string::npos);
|
|
|
+ EXPECT_NE(dumped.find("insert"), std::string::npos);
|
|
|
+ EXPECT_EQ(r.details["operation"], "delete")
|
|
|
+ << "the op asked for must be the one reported, not one the key happens to hold";
|
|
|
+}
|