| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // Hostname resolution: which surface a `Host` names and whose project it is.
- //
- // This is a TENANCY BOUNDARY, so almost every test below is about what must NOT
- // resolve. A test suite for this that only checked the happy path would pass
- // just as well against a registry that answered "the first project it could
- // find" for every unknown name - which is the single failure that matters.
- #include <gtest/gtest.h>
- #include "hostnames.hpp"
- using svapi::HostBinding;
- using svapi::HostEntry;
- using svapi::HostKind;
- using svapi::HostRegistry;
- using svapi::normaliseHost;
- using svapi::PathStyle;
- using svapi::validHostname;
- namespace {
- HostRegistry twoTenants(std::vector<std::string>* dropped = nullptr) {
- return HostRegistry::build(
- "admin.smartbotics.ai", {"global-api-01.smartbotics.ai"},
- {{"novitax", {{"nt.kb.smartbotics.ai", PathStyle::Short}}},
- {"justice-pa", {{"pa.smartbotics.ai", PathStyle::Full},
- {"assistant.smartbotics.ai", PathStyle::Short}}}},
- dropped);
- }
- } // namespace
- // --- normalisation: every arm is a spelling a caller controls ----------------
- TEST(NormaliseHost, LowercasesBecauseDnsIsCaseInsensitive) {
- EXPECT_EQ(normaliseHost("NT.KB.Smartbotics.AI"), "nt.kb.smartbotics.ai");
- }
- TEST(NormaliseHost, StripsThePort) {
- EXPECT_EQ(normaliseHost("example.com:8443"), "example.com");
- EXPECT_EQ(normaliseHost("example.com:443"), "example.com");
- }
- TEST(NormaliseHost, AnIPv6LiteralKeepsItsAddressAndLosesOnlyThePort) {
- // The trap: truncating at the FIRST colon turns `[::1]:8080` into `[`,
- // which then matches nothing - or worse, matches a registry entry somebody
- // created by pasting a bracket.
- EXPECT_EQ(normaliseHost("[::1]:8080"), "[::1]");
- EXPECT_EQ(normaliseHost("[2001:db8::5]"), "[2001:db8::5]");
- EXPECT_EQ(normaliseHost("[2001:DB8::5]:443"), "[2001:db8::5]");
- }
- TEST(NormaliseHost, StripsExactlyOneTrailingDot) {
- // `example.com.` is the fully-qualified spelling and resolves identically,
- // so a client using it must not miss.
- EXPECT_EQ(normaliseHost("example.com."), "example.com");
- // But only one: `example.com..` is not a name anybody registered, and
- // normalising it would invent a match.
- EXPECT_EQ(normaliseHost("example.com.."), "example.com.");
- }
- TEST(NormaliseHost, TrimsTheOptionalWhitespaceAHeaderMayCarry) {
- EXPECT_EQ(normaliseHost(" example.com\t"), "example.com");
- }
- // --- what a project may register --------------------------------------------
- TEST(ValidHostname, RefusesAWildcardOutright) {
- // THE one that matters. `*.smartbotics.ai` would catch every subdomain
- // nobody had claimed YET, so a tenancy boundary would move whenever an
- // unrelated DNS record appeared. Wildcards belong in certificate selection,
- // where the wrong match fails a handshake rather than serving data.
- EXPECT_FALSE(validHostname("*.smartbotics.ai"));
- EXPECT_FALSE(validHostname("*"));
- EXPECT_FALSE(validHostname("nt.*.smartbotics.ai"));
- }
- TEST(ValidHostname, RefusesAnythingThatIsNotJustAName) {
- EXPECT_FALSE(validHostname("https://nt.kb.smartbotics.ai"));
- EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai/admin"));
- EXPECT_FALSE(validHostname("nt.kb.smartbotics.ai:8443"));
- EXPECT_FALSE(validHostname("user@nt.kb.smartbotics.ai"));
- EXPECT_FALSE(validHostname("nt kb.smartbotics.ai"));
- }
- TEST(ValidHostname, RefusesASingleLabelSoAnIntranetAliasCannotBecomeATenant) {
- EXPECT_FALSE(validHostname("localhost"));
- EXPECT_FALSE(validHostname("api"));
- EXPECT_TRUE(validHostname("api.example.com"));
- }
- TEST(ValidHostname, RefusesMalformedLabels) {
- EXPECT_FALSE(validHostname(""));
- EXPECT_FALSE(validHostname(".example.com"));
- EXPECT_FALSE(validHostname("example.com.")); // normalise first, then validate
- EXPECT_FALSE(validHostname("a..b.com"));
- EXPECT_FALSE(validHostname("-bad.example.com"));
- EXPECT_FALSE(validHostname("bad-.example.com"));
- EXPECT_FALSE(validHostname(std::string(254, 'a') + ".com"));
- }
- // --- resolution -------------------------------------------------------------
- TEST(HostRegistry, ResolvesEachKind) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin);
- EXPECT_EQ(r.resolve("global-api-01.smartbotics.ai").kind, HostKind::Global);
- const HostBinding nt = r.resolve("nt.kb.smartbotics.ai");
- EXPECT_EQ(nt.kind, HostKind::Project);
- EXPECT_EQ(nt.project, "novitax");
- EXPECT_EQ(nt.pathStyle, PathStyle::Short) << "short is the default shape";
- const HostBinding pa = r.resolve("pa.smartbotics.ai");
- EXPECT_EQ(pa.project, "justice-pa");
- EXPECT_EQ(pa.pathStyle, PathStyle::Full) << "the per-hostname setting is honoured";
- }
- TEST(HostRegistry, OneProjectMayHoldSeveralHostnames) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("pa.smartbotics.ai").project, "justice-pa");
- EXPECT_EQ(r.resolve("assistant.smartbotics.ai").project, "justice-pa");
- }
- TEST(HostRegistry, AnUnknownHostIsUNKNOWNAndNEVERAProject) {
- // The failure this file exists to prevent. Note the second and third cases:
- // a SUFFIX of a registered name and a PARENT of one must both miss, because
- // a closest-parent match would hand every unclaimed subdomain to whoever
- // owns something above it.
- const HostRegistry r = twoTenants();
- for (const char* h : {"nobody.example.com", "kb.smartbotics.ai", "smartbotics.ai",
- "evil.nt.kb.smartbotics.ai", "nt.kb.smartbotics.ai.evil.com", ""}) {
- const HostBinding b = r.resolve(h);
- EXPECT_EQ(b.kind, HostKind::Unknown) << h;
- EXPECT_TRUE(b.project.empty()) << h << " resolved to project '" << b.project << "'";
- }
- }
- TEST(HostRegistry, ResolutionIsCaseAndPortInsensitive) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.resolve("NT.KB.SMARTBOTICS.AI:8443").project, "novitax");
- EXPECT_EQ(r.resolve("nt.kb.smartbotics.ai.").project, "novitax");
- }
- // --- collisions: the two directions, and they resolve differently -----------
- TEST(HostRegistry, ADeploymentNameCANNOTBeTakenByAProject) {
- // A project claiming the admin hostname would hand it the web UI and the
- // docs catalogue. The deployment keeps its name; the claim is dropped.
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.smartbotics.ai", {"global.smartbotics.ai"},
- {{"evil", {{"admin.smartbotics.ai", PathStyle::Short},
- {"global.smartbotics.ai", PathStyle::Short}}}},
- &dropped);
- EXPECT_EQ(r.resolve("admin.smartbotics.ai").kind, HostKind::Admin);
- EXPECT_EQ(r.resolve("global.smartbotics.ai").kind, HostKind::Global);
- EXPECT_TRUE(r.resolve("admin.smartbotics.ai").project.empty());
- EXPECT_EQ(dropped.size(), 2u) << "both claims reported, so the operator can see them";
- }
- TEST(HostRegistry, AHostClaimedByTWOProjectsGoesToNEITHER) {
- // Last-writer-wins would move a tenancy boundary silently. Refusing both
- // makes it a 404 on a name nobody can reach - visible, and recoverable.
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"shared.example.com", PathStyle::Short}}},
- {"beta", {{"shared.example.com", PathStyle::Short}}}},
- &dropped);
- const HostBinding b = r.resolve("shared.example.com");
- EXPECT_EQ(b.kind, HostKind::Unknown);
- EXPECT_TRUE(b.project.empty()) << "contested host served '" << b.project << "'";
- EXPECT_EQ(dropped.size(), 1u);
- }
- TEST(HostRegistry, OneProjectListingTheSameHostTwiceIsHarmless) {
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"a.example.com", PathStyle::Short}, {"A.example.com.", PathStyle::Full}}}},
- nullptr);
- // Kept, and the FIRST entry's style wins - stated by a test so a later
- // change to "last wins" is a deliberate decision rather than a surprise.
- const HostBinding b = r.resolve("a.example.com");
- EXPECT_EQ(b.project, "alpha");
- EXPECT_EQ(b.pathStyle, PathStyle::Short);
- }
- TEST(HostRegistry, AnInvalidRegisteredHostIsDroppedRatherThanStored) {
- std::vector<std::string> dropped;
- const HostRegistry r = HostRegistry::build(
- "admin.example.com", {},
- {{"alpha", {{"*.example.com", PathStyle::Short}, {"localhost", PathStyle::Short}}}},
- &dropped);
- EXPECT_EQ(r.resolve("*.example.com").kind, HostKind::Unknown);
- EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown)
- << "a stored wildcard must not have become a live wildcard";
- EXPECT_EQ(dropped.size(), 2u);
- }
- // --- empty: the ordinary case for a deployment behind someone else's proxy ---
- TEST(HostRegistry, NoHostnamesConfiguredMeansEmptyNotEverythingRefused) {
- // The signal to skip host gating entirely. Without this, adding the feature
- // would 404 every request on every existing deployment - an outage caused
- // by shipping a control, which this codebase has done once already
- // (network_egress defaulting to deny would have done exactly that).
- const HostRegistry r = HostRegistry::build("", {}, {}, nullptr);
- EXPECT_TRUE(r.empty());
- EXPECT_EQ(r.resolve("anything.example.com").kind, HostKind::Unknown);
- }
- TEST(HostRegistry, SizeCountsEveryRegisteredName) {
- const HostRegistry r = twoTenants();
- EXPECT_EQ(r.size(), 5u) << "admin + global + 3 project hostnames";
- EXPECT_FALSE(r.empty());
- }
- // --- serialisation: a hostname has to survive the document round trip -------
- //
- // These live here rather than in test_stores.cpp because they are pure: the
- // store's job is tested there, and what a STORED document means is tested here.
- #include "project_settings.hpp"
- using svapi::ProjectSettings;
- TEST(ProjectSettingsHostnames, RoundTripsHostAndStyle) {
- ProjectSettings p;
- p.project = "novitax";
- p.hostnames = {{"nt.kb.smartbotics.ai", PathStyle::Short},
- {"portal.novitax.example", PathStyle::Full}};
- const ProjectSettings back = ProjectSettings::fromJson(p.toJson());
- ASSERT_EQ(back.hostnames.size(), 2u);
- EXPECT_EQ(back.hostnames[0].host, "nt.kb.smartbotics.ai");
- EXPECT_EQ(back.hostnames[0].pathStyle, PathStyle::Short);
- EXPECT_EQ(back.hostnames[1].host, "portal.novitax.example");
- EXPECT_EQ(back.hostnames[1].pathStyle, PathStyle::Full)
- << "the per-hostname style must survive storage, or every hostname silently "
- "becomes the default shape after a restart";
- }
- TEST(ProjectSettingsHostnames, EmittedAsAnArrayEvenWhenEmpty) {
- // "No hostnames" and "a server that does not know about hostnames" must not
- // look the same to a reader - this file's most repeated trap.
- const nlohmann::json j = ProjectSettings{}.toJson();
- ASSERT_TRUE(j.contains("hostnames"));
- EXPECT_TRUE(j.at("hostnames").is_array());
- EXPECT_TRUE(j.at("hostnames").empty());
- }
- TEST(ProjectSettingsHostnames, AcceptsTheBareStringSpellingAndDefaultsToShort) {
- // So a hand-written document stays readable.
- const nlohmann::json j = {{"project", "p"}, {"hostnames", {"a.example.com"}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].host, "a.example.com");
- EXPECT_EQ(p.hostnames[0].pathStyle, PathStyle::Short);
- }
- TEST(ProjectSettingsHostnames, AnUnknownStyleIsShortRatherThanAnError) {
- // A document written by a NEWER server must not take a project offline -
- // the same forward-compatibility rule the function descriptor follows for
- // an unknown key.
- const nlohmann::json j = {{"project", "p"},
- {"hostnames", {{{"host", "a.example.com"}, {"path_style", "sideways"}}}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].pathStyle, PathStyle::Short);
- }
- TEST(ProjectSettingsHostnames, AMalformedEntryIsSkippedWithoutLosingTheRest) {
- // A typo in one hostname must not cost the project its embedding overrides.
- const nlohmann::json j = {{"project", "p"},
- {"default_embedding_model", "m"},
- {"hostnames", {7, nlohmann::json::object(), "ok.example.com"}}};
- const ProjectSettings p = ProjectSettings::fromJson(j);
- EXPECT_EQ(p.defaultEmbeddingModel, "m") << "the rest of the document survived";
- ASSERT_EQ(p.hostnames.size(), 1u);
- EXPECT_EQ(p.hostnames[0].host, "ok.example.com");
- }
|