|
|
@@ -0,0 +1,251 @@
|
|
|
+#include <gtest/gtest.h>
|
|
|
+#include "function_record.hpp"
|
|
|
+using namespace svapi;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+KeyScope scopeWith(const std::string& collection, std::set<KeyOp> ops,
|
|
|
+ uint32_t rateLimit = 0) {
|
|
|
+ KeyScope s;
|
|
|
+ KeyScopeRule r;
|
|
|
+ r.collection = collection;
|
|
|
+ r.ops = std::move(ops);
|
|
|
+ s.rules.push_back(r);
|
|
|
+ s.rateLimitPerMin = rateLimit;
|
|
|
+ return s;
|
|
|
+}
|
|
|
+
|
|
|
+FunctionRecord stored(const std::string& id, KeyScope policy) {
|
|
|
+ FunctionRecord f;
|
|
|
+ f.functionId = id;
|
|
|
+ f.path = "functions/" + id + ".js";
|
|
|
+ f.source = "export default () => 1";
|
|
|
+ f.contentHash = functionContentHash(f.source);
|
|
|
+ f.descriptor = {{"id", id}, {"type", "http"}, {"route", "/" + id}};
|
|
|
+ f.policy = std::move(policy);
|
|
|
+ f.enabled = true;
|
|
|
+ f.valid = true;
|
|
|
+ f.createdAt = 1000;
|
|
|
+ f.updatedAt = 1000;
|
|
|
+ return f;
|
|
|
+}
|
|
|
+
|
|
|
+/// Does `policy` let `op` through on `collection`?
|
|
|
+bool allows(const KeyScope& policy, const std::string& collection, KeyOp op) {
|
|
|
+ return policy.findRule(collection, op) != nullptr;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+// ----------------------------------------------------------- content hash
|
|
|
+
|
|
|
+TEST(FunctionContentHash, IsStableAndLowerCaseHex) {
|
|
|
+ const std::string h = functionContentHash("export default () => 1");
|
|
|
+ EXPECT_EQ(h.size(), 64u) << "sha256 hex is 64 characters";
|
|
|
+ EXPECT_EQ(h, functionContentHash("export default () => 1"));
|
|
|
+ for (const char c : h)
|
|
|
+ EXPECT_TRUE((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) << "not lower-case hex: " << c;
|
|
|
+}
|
|
|
+
|
|
|
+TEST(FunctionContentHash, ChangesWhenASingleByteChanges) {
|
|
|
+ // The runner caches compiled bytecode by this hash. A collision between two
|
|
|
+ // different sources would run yesterday's code.
|
|
|
+ EXPECT_NE(functionContentHash("a"), functionContentHash("b"));
|
|
|
+ EXPECT_NE(functionContentHash("return 1"), functionContentHash("return 1 "));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(FunctionContentHash, HandlesEmptySource) {
|
|
|
+ EXPECT_EQ(functionContentHash("").size(), 64u);
|
|
|
+}
|
|
|
+
|
|
|
+// ------------------------------------------------------------ JSON shape
|
|
|
+
|
|
|
+TEST(FunctionRecord, RoundTripsThroughJson) {
|
|
|
+ const FunctionRecord in = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read, KeyOp::List}, 60));
|
|
|
+ const FunctionRecord out = FunctionRecord::fromJson(in.toJson());
|
|
|
+
|
|
|
+ EXPECT_EQ(out.functionId, in.functionId);
|
|
|
+ EXPECT_EQ(out.path, in.path);
|
|
|
+ EXPECT_EQ(out.source, in.source);
|
|
|
+ EXPECT_EQ(out.contentHash, in.contentHash);
|
|
|
+ EXPECT_EQ(out.descriptor, in.descriptor);
|
|
|
+ EXPECT_EQ(out.enabled, in.enabled);
|
|
|
+ EXPECT_EQ(out.valid, in.valid);
|
|
|
+ EXPECT_EQ(out.createdAt, in.createdAt);
|
|
|
+ EXPECT_EQ(out.updatedAt, in.updatedAt);
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+ EXPECT_FALSE(allows(out.policy, "kb_chunks", KeyOp::Delete));
|
|
|
+ EXPECT_EQ(out.policy.rateLimitPerMin, 60u);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(FunctionRecord, StoresIdentityAsFunctionIdNotId) {
|
|
|
+ // The database client strips a top-level `id` from stored bodies, so a
|
|
|
+ // record keyed on `id` would come back with no identity at all.
|
|
|
+ const nlohmann::json j = stored("kb-search", KeyScope{}).toJson();
|
|
|
+ EXPECT_TRUE(j.contains("function_id"));
|
|
|
+ EXPECT_EQ(j.at("function_id"), "kb-search");
|
|
|
+ EXPECT_FALSE(j.contains("id")) << "a top-level id would be stripped by the DB client";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(FunctionRecord, SummaryOmitsTheSourceButKeepsTheHash) {
|
|
|
+ // Listing every function should not ship every function's whole body; the
|
|
|
+ // hash is what a client needs to tell whether its copy is current.
|
|
|
+ const nlohmann::json j = stored("kb-search", KeyScope{}).toSummaryJson();
|
|
|
+ EXPECT_FALSE(j.contains("source"));
|
|
|
+ EXPECT_TRUE(j.contains("content_hash"));
|
|
|
+ EXPECT_EQ(j.at("function_id"), "kb-search");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(FunctionRecord, MissingFieldsParseToSafeDefaults) {
|
|
|
+ const FunctionRecord f = FunctionRecord::fromJson(nlohmann::json::object());
|
|
|
+ EXPECT_TRUE(f.functionId.empty());
|
|
|
+ EXPECT_TRUE(f.policy.rules.empty()) << "an absent policy must grant nothing";
|
|
|
+ EXPECT_TRUE(f.descriptor.is_object());
|
|
|
+}
|
|
|
+
|
|
|
+// --------------------------------------------- the code-push merge rule
|
|
|
+
|
|
|
+TEST(MergeForCodePush, FirstRegistrationInheritsTheProjectDefault) {
|
|
|
+ const KeyScope projectDefault = scopeWith("kb_*", {KeyOp::Read, KeyOp::List});
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(nullptr, incoming, projectDefault);
|
|
|
+
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+ EXPECT_FALSE(allows(out.policy, "kb_chunks", KeyOp::Delete));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, APushCannotWidenItsOwnPolicy) {
|
|
|
+ // The whole point. A function author pushing code is not an admin, so a
|
|
|
+ // policy in the payload is ignored - never merged, never unioned.
|
|
|
+ const FunctionRecord existing = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read}));
|
|
|
+ FunctionRecord incoming = stored("kb-search", scopeWith("*", {KeyOp::Read, KeyOp::Insert,
|
|
|
+ KeyOp::Update, KeyOp::Delete}));
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+ EXPECT_FALSE(allows(out.policy, "kb_chunks", KeyOp::Delete)) << "the push widened its policy";
|
|
|
+ EXPECT_FALSE(allows(out.policy, "secrets", KeyOp::Read)) << "the push reached a new collection";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, APushCannotNarrowOrClearItsPolicyEither) {
|
|
|
+ // Not a security hole, but the same rule: policy is server state. A push
|
|
|
+ // carrying an empty policy must not wipe what an admin granted, or every
|
|
|
+ // deploy from a client that omits the field would silently break the
|
|
|
+ // function.
|
|
|
+ const FunctionRecord existing = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read}));
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, FirstRegistrationIgnoresAClaimedPolicyToo) {
|
|
|
+ // A first push must not be able to grant itself anything by arriving with a
|
|
|
+ // policy before any admin has looked at it.
|
|
|
+ FunctionRecord incoming = stored("kb-search", scopeWith("*", {KeyOp::Delete}));
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(nullptr, incoming, scopeWith("kb_*", {KeyOp::Read}));
|
|
|
+
|
|
|
+ EXPECT_FALSE(allows(out.policy, "anything", KeyOp::Delete));
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, CodeAndDescriptorComeFromThePush) {
|
|
|
+ const FunctionRecord existing = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read}));
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+ incoming.source = "export default () => 2";
|
|
|
+ incoming.contentHash = functionContentHash(incoming.source);
|
|
|
+ incoming.descriptor = {{"id", "kb-search"}, {"type", "http"}, {"route", "/kb/v2"}};
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_EQ(out.source, "export default () => 2");
|
|
|
+ EXPECT_EQ(out.contentHash, functionContentHash("export default () => 2"));
|
|
|
+ EXPECT_EQ(out.descriptor.at("route"), "/kb/v2");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, ARenameKeepsThePolicyAndMovesThePath) {
|
|
|
+ // Identity lives in the script's `on` block, so moving the file changes
|
|
|
+ // `path` and nothing else. If policy were keyed on the path, every rename
|
|
|
+ // would silently reset a function's privileges.
|
|
|
+ const FunctionRecord existing = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read}));
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+ incoming.path = "lib/search/kb.js";
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_EQ(out.path, "lib/search/kb.js");
|
|
|
+ EXPECT_EQ(out.functionId, "kb-search");
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read)) << "a rename dropped the policy";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, ADisabledFunctionStaysDisabledAcrossAPush) {
|
|
|
+ // An operator turned this off, probably because it was misbehaving.
|
|
|
+ // Re-deploying must not quietly start it again.
|
|
|
+ FunctionRecord existing = stored("kb-search", KeyScope{});
|
|
|
+ existing.enabled = false;
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+ incoming.enabled = true;
|
|
|
+
|
|
|
+ EXPECT_FALSE(mergeForCodePush(&existing, incoming, KeyScope{}).enabled);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, PreservesTheOriginalCreationTime) {
|
|
|
+ const FunctionRecord existing = stored("kb-search", KeyScope{});
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+ incoming.createdAt = 9999;
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+ EXPECT_EQ(out.createdAt, 1000u) << "created_at belongs to the first registration";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, AFailedPushMarksTheRowInvalidWithoutLosingThePolicy) {
|
|
|
+ // A function whose source no longer compiles keeps its row and its policy,
|
|
|
+ // so fixing the syntax error does not also mean re-granting access.
|
|
|
+ const FunctionRecord existing = stored("kb-search", scopeWith("kb_chunks", {KeyOp::Read}));
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+ incoming.valid = false;
|
|
|
+ incoming.lastError = "SyntaxError: unexpected token";
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_FALSE(out.valid);
|
|
|
+ EXPECT_EQ(out.lastError, "SyntaxError: unexpected token");
|
|
|
+ EXPECT_TRUE(allows(out.policy, "kb_chunks", KeyOp::Read));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, ASuccessfulPushClearsAPreviousError) {
|
|
|
+ FunctionRecord existing = stored("kb-search", KeyScope{});
|
|
|
+ existing.valid = false;
|
|
|
+ existing.lastError = "SyntaxError: unexpected token";
|
|
|
+ FunctionRecord incoming = stored("kb-search", KeyScope{});
|
|
|
+
|
|
|
+ const FunctionRecord out = mergeForCodePush(&existing, incoming, KeyScope{});
|
|
|
+
|
|
|
+ EXPECT_TRUE(out.valid);
|
|
|
+ EXPECT_TRUE(out.lastError.empty()) << "a stale error would be reported forever";
|
|
|
+}
|
|
|
+
|
|
|
+// ------------------------------------------------------- the default floor
|
|
|
+
|
|
|
+TEST(BuiltinDefaultFunctionPolicy, GrantsNoCollectionAccess) {
|
|
|
+ // Whatever this policy is, it is what unreviewed code runs with. If the
|
|
|
+ // posture is ever widened, widen it here and this test says so out loud
|
|
|
+ // rather than the change passing unnoticed.
|
|
|
+ const KeyScope p = builtinDefaultFunctionPolicy();
|
|
|
+ EXPECT_TRUE(p.rules.empty());
|
|
|
+ for (const KeyOp op : {KeyOp::Read, KeyOp::List, KeyOp::Search, KeyOp::Insert,
|
|
|
+ KeyOp::Update, KeyOp::Delete})
|
|
|
+ EXPECT_EQ(p.findRule("any_collection", op), nullptr);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(MergeForCodePush, AProjectWithNoConfiguredDefaultGrantsNothing) {
|
|
|
+ FunctionRecord incoming;
|
|
|
+ incoming.functionId = "kb-search";
|
|
|
+ const FunctionRecord out =
|
|
|
+ mergeForCodePush(nullptr, incoming, builtinDefaultFunctionPolicy());
|
|
|
+ EXPECT_TRUE(out.policy.rules.empty());
|
|
|
+}
|