|
@@ -5449,3 +5449,514 @@ TEST_F(ApiFixture, TheGlobalStatsRouteSurvivesTheProjectRouteWideningBesideIt) {
|
|
|
ASSERT_TRUE(p); ASSERT_EQ(p->status, 200) << p->body;
|
|
ASSERT_TRUE(p); ASSERT_EQ(p->status, 200) << p->body;
|
|
|
EXPECT_EQ(nlohmann::json::parse(p->body).at("project"), project_);
|
|
EXPECT_EQ(nlohmann::json::parse(p->body).at("project"), project_);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// ===========================================================================
|
|
|
|
|
+// STATIC FILE SERVING ON A PROJECT HOSTNAME.
|
|
|
|
|
+//
|
|
|
|
|
+// THE ONLY DELIBERATELY PUBLIC READ PATH in this service besides /fn/ and
|
|
|
|
|
+// /healthz, so every test here is a PAIRING: an arm that proves the refusal
|
|
|
|
|
+// and an arm that proves the refusal is SELECTIVE. A suite that only asserted
|
|
|
|
|
+// the 404s would pass just as well against a build that served nothing at all,
|
|
|
|
|
+// which is a strictly worse defect than the one being prevented.
|
|
|
|
|
+//
|
|
|
|
|
+// Two conditions must BOTH hold before a byte is served - a project hostname,
|
|
|
|
|
+// and that project having enabled serving - and neither implies the other, so
|
|
|
|
|
+// each is pinned by flipping it alone with everything else held constant.
|
|
|
|
|
+// ===========================================================================
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+
|
|
|
|
|
+/// RAII: put the project's static block back to DISABLED however the test
|
|
|
|
|
+/// leaves. Host gating has its own guard for the same reason; this one is
|
|
|
|
|
+/// separate because the two settings are written by different routes and a
|
|
|
|
|
+/// test may use either alone.
|
|
|
|
|
+struct StaticServingGuard {
|
|
|
|
|
+ int port;
|
|
|
|
|
+ std::string key;
|
|
|
|
|
+ std::string project;
|
|
|
|
|
+ ~StaticServingGuard() {
|
|
|
|
|
+ httplib::Client c("127.0.0.1", port);
|
|
|
|
|
+ c.set_default_headers({{"Authorization", "Bearer " + key}});
|
|
|
|
|
+ (void)c.Put(("/api/v1/projects/" + project + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", false}, {"mount", "/"}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheStaticBlockSURVIVESThePutThatWroteIt) {
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ auto put = admin().Put(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "site/"}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(put); ASSERT_EQ(put->status, 200) << put->body;
|
|
|
|
|
+ auto j = nlohmann::json::parse(put->body);
|
|
|
|
|
+ EXPECT_TRUE(j.at("enabled").get<bool>());
|
|
|
|
|
+ EXPECT_EQ(j.at("mount"), "/site") << "the mount was not normalised on write";
|
|
|
|
|
+ EXPECT_TRUE(j.at("configured").get<bool>());
|
|
|
|
|
+ EXPECT_FALSE(j.at("served").get<bool>())
|
|
|
|
|
+ << "no hostname and no gating, so nothing can actually be reached yet";
|
|
|
|
|
+
|
|
|
|
|
+ // THE REGRESSION: hasAnyOverrides() has to count the static block, or the
|
|
|
|
|
+ // PUT's own removeProject() deletes the document it just wrote. It is the
|
|
|
|
|
+ // second instance of this trap - hostnames hit it first.
|
|
|
|
|
+ auto get = admin().Get(base.c_str());
|
|
|
|
|
+ ASSERT_TRUE(get); ASSERT_EQ(get->status, 200);
|
|
|
|
|
+ auto g = nlohmann::json::parse(get->body);
|
|
|
|
|
+ EXPECT_TRUE(g.at("configured").get<bool>())
|
|
|
|
|
+ << "the static block did not survive the write - hasAnyOverrides() is probably "
|
|
|
|
|
+ "ignoring it: " << get->body;
|
|
|
|
|
+ EXPECT_EQ(g.at("mount"), "/site");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AMountUnderTheServicesOwnPrefixIsREFUSEDWithItsOwnSlug) {
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ for (const char* bad : {"/api", "/api/v1", "/fn", "/healthz"}) {
|
|
|
|
|
+ auto r = admin().Put(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", bad}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(r); EXPECT_EQ(r->status, 422) << bad << ": " << r->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(r->body).at("error").at("code"), "invalid_mount")
|
|
|
|
|
+ << bad << " was refused with the wrong slug: " << r->body;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: an ordinary mount in the same breath is accepted, so the refusal
|
|
|
|
|
+ // is about the prefix and not about the route being broken.
|
|
|
|
|
+ auto ok = admin().Put(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/apidocs"}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(ok); EXPECT_EQ(ok->status, 200) << ok->body;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AStaticPUTIsADMINOnly) {
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ auto mk = admin().Post("/api/v1/keys",
|
|
|
|
|
+ nlohmann::json{{"label","mgr"},{"projects",{project_}},{"admin",false}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_EQ(mk->status, 201);
|
|
|
|
|
+ auto mkJson = nlohmann::json::parse(mk->body);
|
|
|
|
|
+ const std::string manageKey = mkJson["key"], manageId = mkJson["id"];
|
|
|
|
|
+
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+ // A project-manage key must not be able to publish its own project's files
|
|
|
|
|
+ // to the world - this route decides what the world can read with no
|
|
|
|
|
+ // credential at all.
|
|
|
|
|
+ auto denied = http(manageKey).Put(base.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(denied); EXPECT_EQ(denied->status, 403) << denied->body;
|
|
|
|
|
+ auto deniedGet = http(manageKey).Get(base.c_str());
|
|
|
|
|
+ ASSERT_TRUE(deniedGet); EXPECT_EQ(deniedGet->status, 403) << deniedGet->body;
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: the admin key reaches the same route, so the 403 is about the
|
|
|
|
|
+ // key rather than about the route refusing everyone.
|
|
|
|
|
+ EXPECT_EQ(admin().Get(base.c_str())->status, 200);
|
|
|
|
|
+ admin().Delete(("/api/v1/keys/" + manageId).c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AFileCarriesItsServedPathAsMETADATAAndWithoutOneItIsPrivate) {
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ // WITH a path.
|
|
|
|
|
+ auto withPath = request("PUT", files + "withpath.png", "PNGBYTES", "image/png", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/img/logo.png"}});
|
|
|
|
|
+ ASSERT_TRUE(withPath); ASSERT_EQ(withPath->status, 201) << withPath->body;
|
|
|
|
|
+ auto wp = nlohmann::json::parse(withPath->body);
|
|
|
|
|
+ ASSERT_TRUE(wp.contains("metadata")) << "the metadata map is not carried through";
|
|
|
|
|
+ EXPECT_EQ(wp.at("metadata").at("path"), "/img/logo.png");
|
|
|
|
|
+
|
|
|
|
|
+ // It SURVIVES a read back, which is the half that proves the database
|
|
|
|
|
+ // stores and returns it rather than our own response echoing the request.
|
|
|
|
|
+ auto info = admin().Get((files + "withpath.png/info").c_str());
|
|
|
|
|
+ ASSERT_TRUE(info); ASSERT_EQ(info->status, 200) << info->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(info->body).at("metadata").at("path"), "/img/logo.png")
|
|
|
|
|
+ << "the path metadata did not round-trip through the database: " << info->body;
|
|
|
|
|
+
|
|
|
|
|
+ // WITHOUT a path. The PAIRING: the upload still works and the file is still
|
|
|
|
|
+ // readable by name, so "no path" means private rather than broken.
|
|
|
|
|
+ auto noPath = request("PUT", files + "nopath.png", "PNGBYTES2", "image/png", adminKey_, {});
|
|
|
|
|
+ ASSERT_TRUE(noPath); ASSERT_EQ(noPath->status, 201) << noPath->body;
|
|
|
|
|
+ auto np = nlohmann::json::parse(noPath->body);
|
|
|
|
|
+ ASSERT_TRUE(np.contains("metadata"))
|
|
|
|
|
+ << "the key must be present even when empty, or its absence would mean two things";
|
|
|
|
|
+ EXPECT_TRUE(np.at("metadata").empty());
|
|
|
|
|
+ EXPECT_EQ(admin().Get((files + "nopath.png").c_str())->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // A path that could never be served is refused at write time rather than
|
|
|
|
|
+ // stored and 404ed on forever.
|
|
|
|
|
+ auto badPath = request("PUT", files + "bad.png", "X", "image/png", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/api/v1/version"}});
|
|
|
|
|
+ ASSERT_TRUE(badPath); EXPECT_EQ(badPath->status, 422) << badPath->body;
|
|
|
|
|
+ auto traversal = request("PUT", files + "bad2.png", "X", "image/png", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/a/../b.png"}});
|
|
|
|
|
+ ASSERT_TRUE(traversal); EXPECT_EQ(traversal->status, 422) << traversal->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "withpath.png").c_str());
|
|
|
|
|
+ admin().Delete((files + "nopath.png").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AStaticFileIsServedOnTheProjectHostnameAndIsNOTOnTheAdminOne) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "logo.png", "PNGBYTES", "image/png", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/img/logo.png"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/"}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {{{"host","tenant.example.com"},{"path_style","short"}}}}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // UNAUTHENTICATED, which is the whole point: a browser fetching an image
|
|
|
|
|
+ // carries no credential and cannot be given one.
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+ auto served = tenant.Get("/img/logo.png");
|
|
|
|
|
+ ASSERT_TRUE(served);
|
|
|
|
|
+ ASSERT_EQ(served->status, 200) << served->body;
|
|
|
|
|
+ EXPECT_EQ(served->body, "PNGBYTES");
|
|
|
|
|
+ EXPECT_EQ(served->get_header_value("Content-Type"), "image/png")
|
|
|
|
|
+ << "the content type must be the STORED one, never sniffed from the extension";
|
|
|
|
|
+ EXPECT_FALSE(served->get_header_value("ETag").empty());
|
|
|
|
|
+ EXPECT_EQ(served->get_header_value("Cache-Control"), "public, max-age=300");
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: the SAME file on the ADMIN hostname is 404. A tenant's HTML
|
|
|
|
|
+ // rendered there would be same-origin with the admin UI.
|
|
|
|
|
+ httplib::Client adminHost("127.0.0.1", port_);
|
|
|
|
|
+ adminHost.set_default_headers({{"Host", "admin.example.com"}});
|
|
|
|
|
+ auto onAdmin = adminHost.Get("/img/logo.png");
|
|
|
|
|
+ ASSERT_TRUE(onAdmin);
|
|
|
|
|
+ EXPECT_NE(onAdmin->status, 200)
|
|
|
|
|
+ << "a project's static tree was served on the admin hostname: " << onAdmin->body;
|
|
|
|
|
+
|
|
|
|
|
+ // A path that matches no file is the SAME 404 whatever the project holds -
|
|
|
|
|
+ // there is no directory listing and no enumeration, ever. (index is
|
|
|
|
|
+ // disabled first, so this is the plain-404 arm rather than the fallback.)
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"index", ""}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ auto missing = tenant.Get("/img/nothing-here.png");
|
|
|
|
|
+ ASSERT_TRUE(missing); EXPECT_EQ(missing->status, 404);
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "logo.png").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, IfNoneMatchWithTheRightEtagIs304AndWithAWrongOneIs200) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "app.js", "console.log(1)", "application/javascript",
|
|
|
|
|
+ adminKey_, {{"X-File-Path", "/assets/app.js"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/"}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+ auto first = tenant.Get("/assets/app.js");
|
|
|
|
|
+ ASSERT_TRUE(first); ASSERT_EQ(first->status, 200) << first->body;
|
|
|
|
|
+ const std::string etag = first->get_header_value("ETag");
|
|
|
|
|
+ ASSERT_FALSE(etag.empty());
|
|
|
|
|
+
|
|
|
|
|
+ // THE RIGHT ETag: 304, carrying the ETag and Cache-Control and NO body.
|
|
|
|
|
+ httplib::Client c304("127.0.0.1", port_);
|
|
|
|
|
+ c304.set_default_headers({{"Host", "tenant.example.com"}, {"If-None-Match", etag}});
|
|
|
|
|
+ auto notModified = c304.Get("/assets/app.js");
|
|
|
|
|
+ ASSERT_TRUE(notModified);
|
|
|
|
|
+ EXPECT_EQ(notModified->status, 304) << notModified->body;
|
|
|
|
|
+ EXPECT_EQ(notModified->get_header_value("ETag"), etag);
|
|
|
|
|
+ EXPECT_EQ(notModified->get_header_value("Cache-Control"), "public, max-age=300");
|
|
|
|
|
+ EXPECT_TRUE(notModified->body.empty());
|
|
|
|
|
+
|
|
|
|
|
+ // THE PAIRING, and it is the half that matters: a WRONG ETag must be 200
|
|
|
|
|
+ // with the content. A handler that answered 304 unconditionally would pass
|
|
|
|
|
+ // the arm above on its own.
|
|
|
|
|
+ httplib::Client cWrong("127.0.0.1", port_);
|
|
|
|
|
+ cWrong.set_default_headers({{"Host", "tenant.example.com"},
|
|
|
|
|
+ {"If-None-Match", "\"definitely-not-the-checksum\""}});
|
|
|
|
|
+ auto stale = cWrong.Get("/assets/app.js");
|
|
|
|
|
+ ASSERT_TRUE(stale);
|
|
|
|
|
+ EXPECT_EQ(stale->status, 200) << "a wrong If-None-Match was answered 304";
|
|
|
|
|
+ EXPECT_EQ(stale->body, "console.log(1)");
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "app.js").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheEnabledSwitchIsWhatDecidesWhetherAFileIsPublic) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string staticBase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "s.css", "body{}", "text/css", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/s.css"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+
|
|
|
|
|
+ // OFF: everything else is already in place - the hostname, the file, the
|
|
|
|
|
+ // path metadata - so this isolates the switch.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(staticBase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", false}, {"mount", "/"}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ auto off = tenant.Get("/s.css");
|
|
|
|
|
+ ASSERT_TRUE(off); EXPECT_EQ(off->status, 404) << off->body;
|
|
|
|
|
+
|
|
|
|
|
+ // ON: the same request, nothing else changed.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(staticBase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ auto on = tenant.Get("/s.css");
|
|
|
|
|
+ ASSERT_TRUE(on);
|
|
|
|
|
+ EXPECT_EQ(on->status, 200) << "flipping enabled did not publish the file: " << on->body;
|
|
|
|
|
+ EXPECT_EQ(on->body, "body{}");
|
|
|
|
|
+
|
|
|
|
|
+ // And the file is still readable by its authenticated route either way, so
|
|
|
|
|
+ // the switch governs the PUBLIC path only.
|
|
|
|
|
+ EXPECT_EQ(admin().Get((files + "s.css").c_str())->status, 200);
|
|
|
|
|
+ admin().Delete((files + "s.css").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheServicesOwnSurfacesSurviveAStaticMountAtTheROOT) {
|
|
|
|
|
+ // THE SHADOWING REGRESSION. With mount "/", a naive handler swallows the
|
|
|
|
|
+ // API. Note which paths are checked and why: /api/v1/version is NOT served
|
|
|
|
|
+ // on a project hostname at all (the host gate's whitelist is about project
|
|
|
|
|
+ // resources), so it cannot distinguish "shadowed by static" from "refused
|
|
|
|
|
+ // by the gate". The project-scoped API routes CAN.
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "idx.html", "<html>ROOTFILE</html>", "text/html", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/index.html"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/"}, {"index", "index.html"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {{{"host","tenant.example.com"},{"path_style","short"}}}}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // (1) A PROJECT API ROUTE still answers on the project hostname, with the
|
|
|
|
|
+ // API's own JSON rather than the tenant's index.html.
|
|
|
|
|
+ auto api = request("GET", "/api/v1/collections", "", "", adminKey_,
|
|
|
|
|
+ {{"Host", "tenant.example.com"}});
|
|
|
|
|
+ ASSERT_TRUE(api);
|
|
|
|
|
+ ASSERT_EQ(api->status, 200)
|
|
|
|
|
+ << "a static mount at '/' swallowed the API on a project hostname: " << api->body;
|
|
|
|
|
+ EXPECT_TRUE(nlohmann::json::parse(api->body).contains("collections"))
|
|
|
|
|
+ << "the API route answered with something other than the API: " << api->body;
|
|
|
|
|
+ EXPECT_EQ(api->body.find("ROOTFILE"), std::string::npos)
|
|
|
|
|
+ << "the tenant's index.html was served in place of the API response";
|
|
|
|
|
+
|
|
|
|
|
+ // (2) The FULL form too, which is the other spelling that must survive.
|
|
|
|
|
+ auto apiFull = request("GET", "/api/v1/projects/" + project_ + "/collections", "", "",
|
|
|
|
|
+ adminKey_, {{"Host", "tenant.example.com"}});
|
|
|
|
|
+ ASSERT_TRUE(apiFull); EXPECT_EQ(apiFull->status, 200) << apiFull->body;
|
|
|
|
|
+
|
|
|
|
|
+ // (3) UNAUTHENTICATED /api/ still answers 401, not the index file. The
|
|
|
|
|
+ // static handler runs BEFORE the auth check, so this is the arm that
|
|
|
|
|
+ // proves it declined to claim the path rather than claimed it and missed.
|
|
|
|
|
+ httplib::Client anon("127.0.0.1", port_);
|
|
|
|
|
+ anon.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+ auto unauth = anon.Get("/api/v1/collections");
|
|
|
|
|
+ ASSERT_TRUE(unauth);
|
|
|
|
|
+ EXPECT_EQ(unauth->status, 401)
|
|
|
|
|
+ << "an unauthenticated /api/ request was answered by the static tree: " << unauth->body;
|
|
|
|
|
+
|
|
|
|
|
+ // (4) /healthz is still the health route, not a static 404 and not the
|
|
|
|
|
+ // index file - a monitor must need no credential anywhere.
|
|
|
|
|
+ auto health = anon.Get("/healthz");
|
|
|
|
|
+ ASSERT_TRUE(health); EXPECT_EQ(health->status, 200) << health->body;
|
|
|
|
|
+ EXPECT_EQ(health->body.find("ROOTFILE"), std::string::npos);
|
|
|
|
|
+
|
|
|
|
|
+ // (5) /api/v1/version on a project hostname is refused BY THE HOST GATE,
|
|
|
|
|
+ // and its message says so - which is how this distinguishes "the gate
|
|
|
|
|
+ // refused it" from "static claimed it and found nothing".
|
|
|
|
|
+ auto version = request("GET", "/api/v1/version", "", "", adminKey_,
|
|
|
|
|
+ {{"Host", "tenant.example.com"}});
|
|
|
|
|
+ ASSERT_TRUE(version); ASSERT_EQ(version->status, 404) << version->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(version->body).at("error").at("message"),
|
|
|
|
|
+ "not served on this host")
|
|
|
|
|
+ << "the static tree claimed a service-owned path: " << version->body;
|
|
|
|
|
+
|
|
|
|
|
+ // (6) THE PAIRING for all of the above: the mount really is at '/' and
|
|
|
|
|
+ // really is serving, so none of these passed because static was inert.
|
|
|
|
|
+ auto root = anon.Get("/");
|
|
|
|
|
+ ASSERT_TRUE(root);
|
|
|
|
|
+ EXPECT_EQ(root->status, 200) << "the root mount is not serving at all: " << root->body;
|
|
|
|
|
+ EXPECT_EQ(root->body, "<html>ROOTFILE</html>");
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "idx.html").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheIndexFallbackServesAnUnmatchedPathAndAnEmptyIndexDisablesIt) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ const std::string staticBase = "/api/v1/projects/" + project_ + "/settings/static";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "spa.html", "<html>SPA</html>", "text/html", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/index.html"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(staticBase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/"}, {"index", "index.html"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+
|
|
|
|
|
+ // A client-side route matches no stored file and gets the SPA shell.
|
|
|
|
|
+ auto deep = tenant.Get("/settings/profile");
|
|
|
|
|
+ ASSERT_TRUE(deep); ASSERT_EQ(deep->status, 200) << deep->body;
|
|
|
|
|
+ EXPECT_EQ(deep->body, "<html>SPA</html>");
|
|
|
|
|
+ EXPECT_EQ(deep->get_header_value("Content-Type"), "text/html");
|
|
|
|
|
+
|
|
|
|
|
+ // THE PAIRING: an EMPTY index disables it, and the same request is then a
|
|
|
|
|
+ // plain 404. Without this arm the test could not tell the fallback from a
|
|
|
|
|
+ // handler that serves index.html for everything unconditionally.
|
|
|
|
|
+ ASSERT_EQ(admin().Put(staticBase.c_str(),
|
|
|
|
|
+ nlohmann::json{{"index", ""}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ auto noFallback = tenant.Get("/settings/profile");
|
|
|
|
|
+ ASSERT_TRUE(noFallback);
|
|
|
|
|
+ EXPECT_EQ(noFallback->status, 404)
|
|
|
|
|
+ << "an empty index did not disable the SPA fallback: " << noFallback->body;
|
|
|
|
|
+
|
|
|
|
|
+ // And the real file is still served either way, so disabling the fallback
|
|
|
|
|
+ // did not disable serving.
|
|
|
|
|
+ auto real = tenant.Get("/index.html");
|
|
|
|
|
+ ASSERT_TRUE(real); EXPECT_EQ(real->status, 200) << real->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "spa.html").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AFileWithNoPathMetadataIsNOTReachableOverTheMount) {
|
|
|
|
|
+ // ABSENCE MEANS PRIVATE. It never falls back to serving by name, which is
|
|
|
|
|
+ // the rule that keeps every file uploaded before this feature existed
|
|
|
|
|
+ // exactly as private as it was.
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "secret.txt", "PRIVATE", "text/plain", adminKey_, {})->status,
|
|
|
|
|
+ 201);
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "public.txt", "PUBLIC", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/public.txt"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/"}, {"index", ""}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+
|
|
|
|
|
+ // By NAME: not reachable, because the lookup is on the `path` metadata and
|
|
|
|
|
+ // nothing else.
|
|
|
|
|
+ auto byName = tenant.Get("/secret.txt");
|
|
|
|
|
+ ASSERT_TRUE(byName);
|
|
|
|
|
+ EXPECT_EQ(byName->status, 404) << "a file with no path metadata was served by NAME: "
|
|
|
|
|
+ << byName->body;
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: the file that DOES carry a path is served from the same mount in
|
|
|
|
|
+ // the same breath, so the 404 above is about the metadata and not about the
|
|
|
|
|
+ // mount being broken.
|
|
|
|
|
+ auto withPath = tenant.Get("/public.txt");
|
|
|
|
|
+ ASSERT_TRUE(withPath); EXPECT_EQ(withPath->status, 200) << withPath->body;
|
|
|
|
|
+ EXPECT_EQ(withPath->body, "PUBLIC");
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "secret.txt").c_str());
|
|
|
|
|
+ admin().Delete((files + "public.txt").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, ANonRootMountStripsItselfAndClaimsNothingOutsideItself) {
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ StaticServingGuard sguard{port_, adminKey_, project_};
|
|
|
|
|
+ const std::string files = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+
|
|
|
|
|
+ ASSERT_EQ(request("PUT", files + "m.txt", "MOUNTED", "text/plain", adminKey_,
|
|
|
|
|
+ {{"X-File-Path", "/deep/m.txt"}})->status, 201);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/static").c_str(),
|
|
|
|
|
+ nlohmann::json{{"enabled", true}, {"mount", "/site"}, {"index", ""}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+
|
|
|
|
|
+ auto mounted = tenant.Get("/site/deep/m.txt");
|
|
|
|
|
+ ASSERT_TRUE(mounted); EXPECT_EQ(mounted->status, 200) << mounted->body;
|
|
|
|
|
+ EXPECT_EQ(mounted->body, "MOUNTED");
|
|
|
|
|
+
|
|
|
|
|
+ // PAIRING: outside the mount the gate refuses rather than the static tree
|
|
|
|
|
+ // answering - a prefix match would have handed /sitemap.xml to this mount
|
|
|
|
|
+ // and stripped four characters out of the middle of the name.
|
|
|
|
|
+ auto outside = tenant.Get("/deep/m.txt");
|
|
|
|
|
+ ASSERT_TRUE(outside); EXPECT_EQ(outside->status, 404) << outside->body;
|
|
|
|
|
+ auto prefixTrap = tenant.Get("/sitemap.xml");
|
|
|
|
|
+ ASSERT_TRUE(prefixTrap); EXPECT_EQ(prefixTrap->status, 404) << prefixTrap->body;
|
|
|
|
|
+
|
|
|
|
|
+ admin().Delete((files + "m.txt").c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, TheAdminSPAShellIsNOTServedOnAProjectHostname) {
|
|
|
|
|
+ // A DEFECT THE STATIC TESTS FOUND, and it predates static serving: the SPA
|
|
|
|
|
+ // history fallback lives in set_error_handler, which runs for EVERY 404
|
|
|
|
|
+ // however it was produced - so any extensionless 404 on a tenant's
|
|
|
|
|
+ // hostname was answered with the OPERATOR's web UI at status 200. That is
|
|
|
|
|
+ // the surface withdrawn from the customer-facing name in 0.15.0, handed
|
|
|
|
|
+ // back by a handler nobody thinks of as a route.
|
|
|
|
|
+ //
|
|
|
|
|
+ // Static serving is deliberately left OFF here, so this isolates the error
|
|
|
|
|
+ // handler rather than testing the static 404 path.
|
|
|
|
|
+ HostGatingGuard guard{port_, adminKey_, project_};
|
|
|
|
|
+ ASSERT_EQ(admin().Put(("/api/v1/projects/" + project_ + "/settings/hostnames").c_str(),
|
|
|
|
|
+ nlohmann::json{{"hostnames", {"tenant.example.com"}}}.dump(), "application/json")->status, 200);
|
|
|
|
|
+ ASSERT_EQ(admin().Put("/api/v1/settings",
|
|
|
|
|
+ nlohmann::json{{"admin_hostname", "admin.example.com"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ httplib::Client tenant("127.0.0.1", port_);
|
|
|
|
|
+ tenant.set_default_headers({{"Host", "tenant.example.com"}});
|
|
|
|
|
+ auto onProject = tenant.Get("/some/client/route");
|
|
|
|
|
+ ASSERT_TRUE(onProject);
|
|
|
|
|
+ EXPECT_NE(onProject->status, 200) << onProject->body;
|
|
|
|
|
+ EXPECT_EQ(onProject->body.find("SVAPI_SPA_OK"), std::string::npos)
|
|
|
|
|
+ << "the admin web UI shell was served on a project hostname: " << onProject->body;
|
|
|
|
|
+
|
|
|
|
|
+ // THE PAIRING: the admin hostname still gets the shell, so the fix narrowed
|
|
|
|
|
+ // the fallback rather than removing it. Without this arm the assertion
|
|
|
|
|
+ // above would pass against a build where the SPA fallback was simply gone.
|
|
|
|
|
+ httplib::Client adminHost("127.0.0.1", port_);
|
|
|
|
|
+ adminHost.set_default_headers({{"Host", "admin.example.com"}});
|
|
|
|
|
+ auto onAdmin = adminHost.Get("/some/client/route");
|
|
|
|
|
+ ASSERT_TRUE(onAdmin);
|
|
|
|
|
+ EXPECT_EQ(onAdmin->status, 200) << onAdmin->body;
|
|
|
|
|
+ EXPECT_NE(onAdmin->body.find("SVAPI_SPA_OK"), std::string::npos)
|
|
|
|
|
+ << "the SPA history fallback stopped working on the admin hostname: " << onAdmin->body;
|
|
|
|
|
+}
|