| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #pragma once
- #include <memory>
- #include <string>
- #include <string_view>
- #include <unordered_map>
- #include <vector>
- /// Which deployment surface a request's `Host` names, and which project it is
- /// about.
- ///
- /// THIS IS A TENANCY BOUNDARY, not routing. A hostname that resolves to the
- /// wrong project hands one customer another customer's data, which is the worst
- /// failure this service has - so everything here is pure, exactly-matched, and
- /// refuses rather than guesses. The shape to keep in mind is the one this
- /// codebase has met before: `_files` is a pseudo-collection because a scope rule
- /// answers "may this principal touch that data", and a collection name is
- /// project-qualified because the separator decides whose row it is. A hostname
- /// is the same kind of thing wearing routing's clothes.
- namespace svapi {
- /// What a hostname is allowed to serve.
- enum class HostKind {
- /// Not registered. 404, and NEVER a fallback to a project: an unknown host
- /// resolving to "some project" is the hole this whole file exists to close.
- Unknown,
- /// The operator surface: every route, every project, plus the web UI and
- /// the docs page.
- Admin,
- /// Every project's API and functions, project named in the path. What the
- /// single customer-facing hostname does today.
- Global,
- /// Exactly one project's API and functions. No web UI, no docs.
- Project,
- };
- /// Whether a project hostname expects the project in the path.
- enum class PathStyle {
- /// `/api/v1/collections/...` - the project comes from the hostname.
- /// The default, because a customer-facing hostname saying its own tenant's
- /// name back to it is noise.
- Short,
- /// `/api/v1/projects/<p>/collections/...`, and `<p>` must be this
- /// hostname's project.
- Full,
- };
- struct HostBinding {
- HostKind kind = HostKind::Unknown;
- /// Set only when `kind == Project`.
- std::string project;
- /// Meaningful only when `kind == Project`.
- PathStyle pathStyle = PathStyle::Short;
- };
- /// One registered hostname, as stored in settings.
- struct HostEntry {
- std::string host;
- PathStyle pathStyle = PathStyle::Short;
- };
- /// Lowercase, strip the port, strip one trailing dot.
- ///
- /// The `Host` header is caller-controlled, so every normalisation here is a
- /// place a mismatch could be engineered:
- /// - CASE: DNS is case-insensitive, so `NT.KB.example` and `nt.kb.example`
- /// are one name. Comparing raw would let a caller miss a registry entry
- /// (harmless) or, with a sloppier comparison elsewhere, hit the wrong one.
- /// - PORT: `example:8443` is the same host as `example`. A deployment serving
- /// a non-443 port would otherwise register one spelling and receive the
- /// other.
- /// - IPv6: `[::1]:8080` keeps its brackets and loses only the port - the
- /// colons inside the brackets are part of the address, so a naive
- /// "truncate at the first colon" turns `[::1]` into `[`.
- /// - TRAILING DOT: `example.` is the fully-qualified spelling of `example`
- /// and resolves identically, so a client using it must not miss.
- std::string normaliseHost(std::string_view hostHeader);
- /// Host -> binding. Immutable once built; swapped as a whole under a
- /// `shared_ptr`, the same discipline SettingsStore and KeyStore use, so a
- /// reload cannot be observed half-applied by a request in flight.
- class HostRegistry {
- public:
- HostRegistry() = default;
- /// Build from the deployment's own names plus every project's.
- ///
- /// PRECEDENCE IS FIXED AND ADMIN WINS. If a project claims a hostname the
- /// deployment already uses, the deployment keeps it and the project's claim
- /// is dropped. That can only happen if a collision slipped past the
- /// write-time check (an older row, a hand-edited document), and the safe
- /// resolution is the one that cannot hand an operator surface to a tenant.
- /// The admin/global names are checked first for the same reason.
- ///
- /// A hostname claimed by two PROJECTS is dropped from BOTH rather than
- /// given to either. Last-writer-wins would silently move a tenancy
- /// boundary; refusing both makes the misconfiguration visible as a 404 on a
- /// name nobody can reach, which is recoverable, where serving the wrong
- /// tenant is not.
- static HostRegistry build(const std::string& adminHost,
- const std::vector<std::string>& globalHosts,
- const std::unordered_map<std::string, std::vector<HostEntry>>& byProject,
- std::vector<std::string>* droppedOut = nullptr);
- /// The binding for a `Host` header value. Normalises first.
- HostBinding resolve(std::string_view hostHeader) const;
- /// True when nothing is registered at all - the ordinary case for a
- /// deployment behind someone else's proxy, and the signal to skip host
- /// gating entirely rather than 404 every request.
- bool empty() const { return map_.empty(); }
- std::size_t size() const { return map_.size(); }
- private:
- std::unordered_map<std::string, HostBinding> map_;
- };
- /// Is this a hostname a project may register?
- ///
- /// Deliberately strict, and WILDCARDS ARE REFUSED. A project registering
- /// `*.example.com` would catch every subdomain nobody had claimed yet,
- /// including ones a later project expects to own - a tenancy boundary that
- /// moves when an unrelated DNS record appears. Wildcards belong in CERTIFICATE
- /// selection, where matching the wrong one fails the handshake, not in the
- /// registry, where matching the wrong one serves data.
- bool validHostname(std::string_view host);
- } // namespace svapi
|