|
@@ -6206,3 +6206,296 @@ TEST_F(ApiFixture, ASameOriginGETWithNoOriginHeaderIsRefusedUnlessTheKeyOPTSIN)
|
|
|
ASSERT_TRUE(f);
|
|
ASSERT_TRUE(f);
|
|
|
EXPECT_EQ(f->status, 200) << "the normal cross-origin path broke";
|
|
EXPECT_EQ(f->status, 200) << "the normal cross-origin path broke";
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// ===========================================================================
|
|
|
|
|
+// PER-HOSTNAME STATIC SERVING (SV-132).
|
|
|
|
|
+//
|
|
|
|
|
+// Pairings throughout, because every one of these could be satisfied by a
|
|
|
|
|
+// server that serves nothing: the point is never "this 404s" but "this 404s
|
|
|
|
|
+// WHILE its sibling, differing in exactly one configured value, answers 200".
|
|
|
|
|
+//
|
|
|
|
|
+// Note what cleans up: HostGatingGuard empties the hostname list, and a host
|
|
|
|
|
+// removed takes its static block with it, so the per-host state needs no guard
|
|
|
|
|
+// of its own.
|
|
|
|
|
+// ===========================================================================
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+
|
|
|
|
|
+/// Register two tenant hostnames plus the deployment's own name, so host
|
|
|
|
|
+/// gating is in force and both tenant names resolve to this project.
|
|
|
|
|
+void twoTenantHosts(int port, const std::string& key, const std::string& project) {
|
|
|
|
|
+ httplib::Client c("127.0.0.1", port);
|
|
|
|
|
+ c.set_default_headers({{"Authorization", "Bearer " + key}});
|
|
|
|
|
+ (void)c.Put(("/api/v1/projects/" + project + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"a.example.com", "b.example.com"}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ (void)c.Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+httplib::Client onHost(int port, const char* host) {
|
|
|
|
|
+ httplib::Client c("127.0.0.1", port);
|
|
|
|
|
+ c.set_default_headers({{"Host", host}});
|
|
|
|
|
+ return c;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TwoHostnamesServeDIFFERENTTreesAndNeitherCanReachTheOthers) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ // Two files, in two subtrees, distinguishable by their bytes - because the
|
|
|
|
|
+ // failure this test exists to catch is one host answering with the OTHER's
|
|
|
|
|
+ // file, and two identical bodies could not tell that from success.
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "a.html", "SITE-A", "text/html", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/sites/a/index.html"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "b.html", "SITE-B", "text/html", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/sites/b/index.html"}})->status, 201);
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+ auto put = admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"enabled", false}, {"mount", "/"}, {"index", "index.html"},
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}, {"root", "/sites/a"}},
|
|
|
|
|
+ {{"host", "b.example.com"}, {"enabled", true}, {"root", "/sites/b"}}}},
|
|
|
|
|
+ }.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(put); ASSERT_EQ(put->status, 200) << put->body;
|
|
|
|
|
+ // The route reports every name's EFFECTIVE answer, so an admin can read off
|
|
|
|
|
+ // what each one does rather than infer it.
|
|
|
|
|
+ const auto view = nlohmann::json::parse(put->body);
|
|
|
|
|
+ ASSERT_EQ(view.at("hostnames").size(), 2u) << put->body;
|
|
|
|
|
+ EXPECT_TRUE(view.at("served").get<bool>())
|
|
|
|
|
+ << "`served` must mean 'some name publishes', not 'the project switch is on' - it is off "
|
|
|
|
|
+ "here and two names are serving";
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client a = onHost(port_, "a.example.com");
|
|
|
|
|
+ httplib::Client b = onHost(port_, "b.example.com");
|
|
|
|
|
+
|
|
|
|
|
+ // THE ROOT IS INVISIBLE: the same request path on two names resolves to two
|
|
|
|
|
+ // different stored files.
|
|
|
|
|
+ auto ra = a.Get("/");
|
|
|
|
|
+ auto rb = b.Get("/");
|
|
|
|
|
+ ASSERT_TRUE(ra); ASSERT_TRUE(rb);
|
|
|
|
|
+ ASSERT_EQ(ra->status, 200) << ra->body;
|
|
|
|
|
+ ASSERT_EQ(rb->status, 200) << rb->body;
|
|
|
|
|
+ EXPECT_EQ(ra->body, "SITE-A");
|
|
|
|
|
+ EXPECT_EQ(rb->body, "SITE-B") << "one hostname answered with the other's file, which is the "
|
|
|
|
|
+ "whole failure this feature must not have";
|
|
|
|
|
+
|
|
|
|
|
+ // AND THE STORAGE PATH IS NOT A URL. Asking host A for B's stored path
|
|
|
|
|
+ // resolves under A's root (/sites/a/sites/b/index.html), which exists
|
|
|
|
|
+ // nowhere - so the tree layout is not a way around the root. Without the
|
|
|
|
|
+ // index disabled this would be the fallback's 200, so it is turned off
|
|
|
|
|
+ // first to isolate the arm being tested.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{{"index", ""}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ auto crossed = a.Get("/sites/b/index.html");
|
|
|
|
|
+ ASSERT_TRUE(crossed);
|
|
|
|
|
+ EXPECT_EQ(crossed->status, 404) << "a host reached outside its own document root: "
|
|
|
|
|
+ << crossed->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "a.html").c_str());
|
|
|
|
|
+ admin().Delete((files + "b.html").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AHostnameCanBeTurnedOFFWhileItsSiblingKeepsServing) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "shared.txt", "SHARED", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/shared.txt"}})->status, 201);
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+ // The project switch is ON and one name overrides it to off: the "disable
|
|
|
|
|
+ // per host" half of the request, which is the direction a merge could not
|
|
|
|
|
+ // express.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"enabled", true}, {"mount", "/"},
|
|
|
|
|
+ {"hostnames", {{{"host", "b.example.com"}, {"enabled", false}}}},
|
|
|
|
|
+ }.dump(), "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client a = onHost(port_, "a.example.com");
|
|
|
|
|
+ httplib::Client b = onHost(port_, "b.example.com");
|
|
|
|
|
+ auto served = a.Get("/shared.txt");
|
|
|
|
|
+ auto refused = b.Get("/shared.txt");
|
|
|
|
|
+ ASSERT_TRUE(served); ASSERT_TRUE(refused);
|
|
|
|
|
+ EXPECT_EQ(served->status, 200) << "a name with NO block must still follow the project, which "
|
|
|
|
|
+ "is the rule every existing deployment relies on: "
|
|
|
|
|
+ << served->body;
|
|
|
|
|
+ EXPECT_EQ(refused->status, 404) << "a name whose own block says false must not serve: "
|
|
|
|
|
+ << refused->body;
|
|
|
|
|
+
|
|
|
|
|
+ // THE PAIRING THAT PROVES THE SWITCH IS WHAT DECIDES: flip that one field
|
|
|
|
|
+ // and nothing else, and the same request becomes 200. Without this the test
|
|
|
|
|
+ // above would pass against a server that 404s b.example.com for any reason
|
|
|
|
|
+ // at all.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"hostnames", {{{"host", "b.example.com"}, {"enabled", true}}}},
|
|
|
|
|
+ }.dump(), "application/json")->status, 200);
|
|
|
|
|
+ auto nowServed = b.Get("/shared.txt");
|
|
|
|
|
+ ASSERT_TRUE(nowServed);
|
|
|
|
|
+ EXPECT_EQ(nowServed->status, 200) << nowServed->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "shared.txt").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AHostnameCanPublishWhileTheProjectSwitchStaysOFF) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "one.txt", "ONE", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/one.txt"}})->status, 201);
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"enabled", false}, {"mount", "/"},
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}}}},
|
|
|
|
|
+ }.dump(), "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // A host's own `enabled: true` is AUTHORITATIVE: there is deliberately no
|
|
|
|
|
+ // AND against the project switch, because that would make this field a lie
|
|
|
|
|
+ // in exactly this state. The pairing is the sibling, which follows the
|
|
|
|
|
+ // project and therefore serves nothing.
|
|
|
|
|
+ auto published = onHost(port_, "a.example.com").Get("/one.txt");
|
|
|
|
|
+ auto following = onHost(port_, "b.example.com").Get("/one.txt");
|
|
|
|
|
+ ASSERT_TRUE(published); ASSERT_TRUE(following);
|
|
|
|
|
+ EXPECT_EQ(published->status, 200) << published->body;
|
|
|
|
|
+ EXPECT_EQ(following->status, 404) << following->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "one.txt").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AHostnamesPUTPRESERVESAStaticBlockAndREFUSESToWriteOne) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+ const std::string hbase = "/api/v1/projects/" + project_ + "/settings/hostnames";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "live.txt", "LIVE", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/live.txt"}})->status, 201);
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"enabled", false}, {"mount", "/"},
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}}}},
|
|
|
|
|
+ }.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(onHost(port_, "a.example.com").Get("/live.txt")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // THE UNPUBLISH HAZARD, and it is the reason the write paths are split.
|
|
|
|
|
+ // This route REPLACES the whole list, so an admin editing a path_style
|
|
|
|
|
+ // sends no static block - and if that silently dropped it, a live site
|
|
|
|
|
+ // would go dark on an unrelated edit.
|
|
|
|
|
+ auto edited = admin().Put(hbase.c_str(), nlohmann::json{{"hostnames",
|
|
|
|
|
+ {{{"host", "a.example.com"}, {"path_style", "full"}}, {{"host", "b.example.com"}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(edited); ASSERT_EQ(edited->status, 200) << edited->body;
|
|
|
|
|
+ // Reported read-only here, because a hostname list that hid which names
|
|
|
|
|
+ // publish files would conceal the most consequential thing about them.
|
|
|
|
|
+ const auto hview = nlohmann::json::parse(edited->body);
|
|
|
|
|
+ bool sawBlock = false;
|
|
|
|
|
+ for (const auto& h : hview.at("hostnames"))
|
|
|
|
|
+ if (h.at("host") == "a.example.com" && h.contains("static")) sawBlock = true;
|
|
|
|
|
+ EXPECT_TRUE(sawBlock) << "the hostnames route must SHOW the static block it refuses to write: "
|
|
|
|
|
+ << edited->body;
|
|
|
|
|
+ auto stillLive = onHost(port_, "a.example.com").Get("/live.txt");
|
|
|
|
|
+ ASSERT_TRUE(stillLive);
|
|
|
|
|
+ EXPECT_EQ(stillLive->status, 200)
|
|
|
|
|
+ << "editing a path_style unpublished a live site, which is exactly what splitting the "
|
|
|
|
|
+ "write paths is for: " << stillLive->body;
|
|
|
|
|
+
|
|
|
|
|
+ // REFUSED, NOT IGNORED. A silent ignore would let a caller believe they had
|
|
|
|
|
+ // configured something; the refusal names the route that honours it.
|
|
|
|
|
+ auto rejected = admin().Put(hbase.c_str(), nlohmann::json{{"hostnames",
|
|
|
|
|
+ {{{"host", "a.example.com"}, {"static", {{"enabled", true}}}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(rejected);
|
|
|
|
|
+ EXPECT_EQ(rejected->status, 422) << rejected->body;
|
|
|
|
|
+ EXPECT_NE(rejected->body.find("settings/static"), std::string::npos)
|
|
|
|
|
+ << "the refusal must say where the field IS honoured: " << rejected->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "live.txt").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AStaticBlockForAnUnregisteredHostnameIsRefusedAndARegisteredOneIsNot) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+
|
|
|
|
|
+ // A block on a name the project does not hold could never serve anything,
|
|
|
|
|
+ // and - worse - would read as though the name were published.
|
|
|
|
|
+ auto bad = admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"hostnames", {{{"host", "not-ours.example.com"}, {"enabled", true}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(bad);
|
|
|
|
|
+ EXPECT_EQ(bad->status, 422) << bad->body;
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: a registered name is accepted, so the refusal above is about
|
|
|
|
|
+ // ownership rather than about the route refusing every per-host block.
|
|
|
|
|
+ auto good = admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(good);
|
|
|
|
|
+ EXPECT_EQ(good->status, 200) << good->body;
|
|
|
|
|
+
|
|
|
|
|
+ // AND `enabled` IS REQUIRED in a per-host block: it is authoritative for
|
|
|
|
|
+ // that name and does not inherit, so a block that left it unsaid would
|
|
|
|
|
+ // silently unpublish a site on a PUT that only meant to set a root.
|
|
|
|
|
+ auto unsaid = admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"root", "/sites/a"}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(unsaid);
|
|
|
|
|
+ EXPECT_EQ(unsaid->status, 422) << unsaid->body;
|
|
|
|
|
+
|
|
|
|
|
+ // A root that no file could ever be stored under is refused too, with its
|
|
|
|
|
+ // own slug, because the remedy is to pick a different root rather than to
|
|
|
|
|
+ // fix a payload.
|
|
|
|
|
+ auto badRoot = admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}, {"root", "/api"}}}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(badRoot);
|
|
|
|
|
+ EXPECT_EQ(badRoot->status, 422) << badRoot->body;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AnAbsentHostnamesKeyLeavesPerHostBlocksALONEandAnEmptyArrayCLEARSThem) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string sbase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "p.txt", "P", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/p.txt"}})->status, 201);
|
|
|
|
|
+ twoTenantHosts(port_, adminKey_, project_);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(), nlohmann::json{
|
|
|
|
|
+ {"enabled", false}, {"mount", "/"},
|
|
|
|
|
+ {"hostnames", {{{"host", "a.example.com"}, {"enabled", true}}}},
|
|
|
|
|
+ }.dump(), "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // ABSENT LEAVES ALONE: a PUT of an unrelated field must not disturb which
|
|
|
|
|
+ // names publish, or every cache_control edit would be a publishing change.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"cache_control", "public, max-age=60"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ auto kept = onHost(port_, "a.example.com").Get("/p.txt");
|
|
|
|
|
+ ASSERT_TRUE(kept);
|
|
|
|
|
+ EXPECT_EQ(kept->status, 200) << kept->body;
|
|
|
|
|
+ EXPECT_EQ(kept->get_header_value("Cache-Control"), "public, max-age=60")
|
|
|
|
|
+ << "cache_control is PROJECT-WIDE: it lives in one place and applies to every name";
|
|
|
|
|
+
|
|
|
|
|
+ // PRESENT REPLACES THE WHOLE SET, which is the only way to express removal.
|
|
|
|
|
+ // The name goes back to following the project, and the project is off.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(sbase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", nlohmann::json::array()}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ auto cleared = onHost(port_, "a.example.com").Get("/p.txt");
|
|
|
|
|
+ ASSERT_TRUE(cleared);
|
|
|
|
|
+ EXPECT_EQ(cleared->status, 404) << "an empty hostnames array must clear the per-host blocks, "
|
|
|
|
|
+ "returning each name to the project's own switch: "
|
|
|
|
|
+ << cleared->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "p.txt").c_str());
|
|
|
|
|
+}
|