|
@@ -101,7 +101,19 @@ bool installSniHook(void* ctx) {
|
|
|
std::string projectFor(ServerDeps& d, const httplib::Request& req, size_t group) {
|
|
std::string projectFor(ServerDeps& d, const httplib::Request& req, size_t group) {
|
|
|
const auto reg = d.settings.hostRegistry();
|
|
const auto reg = d.settings.hostRegistry();
|
|
|
const bool gatingOn = reg && reg->gating();
|
|
const bool gatingOn = reg && reg->gating();
|
|
|
- const HostBinding host = reg ? reg->resolve(req.get_header_value("Host")) : HostBinding{};
|
|
|
|
|
|
|
+ HostBinding host = reg ? reg->resolve(req.get_header_value("Host")) : HostBinding{};
|
|
|
|
|
+ // LOOPBACK RESCUES ONLY AN UNKNOWN HOST. It must not override a name that
|
|
|
|
|
+ // resolves: a request on loopback carrying a registered PROJECT hostname is
|
|
|
|
|
+ // still that project's, because the caller told us which tenant they meant
|
|
|
|
|
+ // and discarding that would make the local surface behave differently from
|
|
|
|
|
+ // the public one - the two would then need separate reasoning, which is how
|
|
|
|
|
+ // a tenancy rule gets tested in a shape nobody ships.
|
|
|
|
|
+ //
|
|
|
|
|
+ // What it rescues is the case with no usable name at all: the operator
|
|
|
|
|
+ // connecting by address after the deployment named itself. Keyed on the
|
|
|
|
|
+ // INTERFACE, not the header, because the header is caller-controlled.
|
|
|
|
|
+ if (gatingOn && host.kind == HostKind::Unknown && isLoopbackAddress(req.local_addr))
|
|
|
|
|
+ host = HostBinding{HostKind::Admin, {}, PathStyle::Full};
|
|
|
|
|
|
|
|
// An absent group is the short form. `req.matches` is sized by the pattern,
|
|
// An absent group is the short form. `req.matches` is sized by the pattern,
|
|
|
// so a route whose optional project group did not participate yields an
|
|
// so a route whose optional project group did not participate yields an
|
|
@@ -363,7 +375,9 @@ void ApiServer::registerRoutes() {
|
|
|
// behind someone else's proxy all send a Host nobody registered, and
|
|
// behind someone else's proxy all send a Host nobody registered, and
|
|
|
// refusing those would be an outage caused by ADDING a control.
|
|
// refusing those would be an outage caused by ADDING a control.
|
|
|
if (const auto reg = d_.settings.hostRegistry(); reg && reg->gating()) {
|
|
if (const auto reg = d_.settings.hostRegistry(); reg && reg->gating()) {
|
|
|
- const HostBinding host = reg->resolve(req.get_header_value("Host"));
|
|
|
|
|
|
|
+ HostBinding host = reg->resolve(req.get_header_value("Host"));
|
|
|
|
|
+ if (host.kind == HostKind::Unknown && isLoopbackAddress(req.local_addr))
|
|
|
|
|
+ host = HostBinding{HostKind::Admin, {}, PathStyle::Full};
|
|
|
if (host.kind == HostKind::Unknown) {
|
|
if (host.kind == HostKind::Unknown) {
|
|
|
// 404, never a redirect and never a fallback to a default
|
|
// 404, never a redirect and never a fallback to a default
|
|
|
// host: an unknown name is not a mistake we can correct for the
|
|
// host: an unknown name is not a mistake we can correct for the
|
|
@@ -371,14 +385,45 @@ void ApiServer::registerRoutes() {
|
|
|
sendJson(res, 404, errorBody("not_found", "no such host"));
|
|
sendJson(res, 404, errorBody("not_found", "no such host"));
|
|
|
return httplib::Server::HandlerResponse::Handled;
|
|
return httplib::Server::HandlerResponse::Handled;
|
|
|
}
|
|
}
|
|
|
- // A project hostname serves the API and functions only. `/healthz`
|
|
|
|
|
- // stays reachable everywhere so an outside monitor needs no
|
|
|
|
|
- // credential and no special name.
|
|
|
|
|
- if (host.kind == HostKind::Project && req.method != "OPTIONS" &&
|
|
|
|
|
- req.path.rfind("/api/", 0) != 0 && req.path.rfind("/fn/", 0) != 0 &&
|
|
|
|
|
- req.path != "/healthz") {
|
|
|
|
|
- sendJson(res, 404, errorBody("not_found", "not served on this host"));
|
|
|
|
|
- return httplib::Server::HandlerResponse::Handled;
|
|
|
|
|
|
|
+ // A PROJECT HOSTNAME SERVES THAT PROJECT AND NOTHING ELSE.
|
|
|
|
|
+ //
|
|
|
|
|
+ // Not just "no web UI": the GLOBAL admin routes are under /api/
|
|
|
|
|
+ // too, and an earlier version of this gate let them through.
|
|
|
|
|
+ // Measured by the test beside it - `/api/v1/keys` answered 200 on a
|
|
|
|
|
+ // tenant's hostname, handing the key-management surface to a name
|
|
|
|
|
+ // whose whole purpose is to serve one project's data. They are
|
|
|
|
|
+ // admin-key-gated, so it was not an open door, but the hostname is
|
|
|
|
|
+ // meant to be the narrower boundary and was not.
|
|
|
|
|
+ //
|
|
|
|
|
+ // A WHITELIST, not a deny-list of the global routes, and that is
|
|
|
|
|
+ // the load-bearing choice: a deny-list would admit any route added
|
|
|
|
|
+ // in a later release the moment the package upgraded, with nobody
|
|
|
|
|
+ // noticing - the same reasoning that made the customer-facing nginx
|
|
|
|
|
+ // mount a whitelist. The cost is that a genuinely project-scoped
|
|
|
|
|
+ // resource added later must be added here deliberately, which is
|
|
|
|
|
+ // the point.
|
|
|
|
|
+ if (host.kind == HostKind::Project && req.method != "OPTIONS") {
|
|
|
|
|
+ static const char* kProjectResources[] = {
|
|
|
|
|
+ "collections", "files", "functions", "relations", "secrets", "settings",
|
|
|
|
|
+ };
|
|
|
|
|
+ bool allowed = req.path == "/healthz" || req.path.rfind("/fn/", 0) == 0;
|
|
|
|
|
+ if (!allowed && req.path.rfind("/api/v1/projects/", 0) == 0) {
|
|
|
|
|
+ // The full form. WHICH project it names is not decided
|
|
|
|
|
+ // here - projectFor() refuses a mismatch per route, so this
|
|
|
|
|
+ // gate stays about the SURFACE and the tenancy check stays
|
|
|
|
|
+ // in one place.
|
|
|
|
|
+ allowed = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!allowed && req.path.rfind("/api/v1/", 0) == 0) {
|
|
|
|
|
+ const std::string rest = req.path.substr(8);
|
|
|
|
|
+ const std::string first = rest.substr(0, rest.find('/'));
|
|
|
|
|
+ for (const char* r : kProjectResources)
|
|
|
|
|
+ if (first == r) { allowed = true; break; }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!allowed) {
|
|
|
|
|
+ sendJson(res, 404, errorBody("not_found", "not served on this host"));
|
|
|
|
|
+ return httplib::Server::HandlerResponse::Handled;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|