hostnames.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include <memory>
  3. #include <string>
  4. #include <string_view>
  5. #include <unordered_map>
  6. #include <vector>
  7. /// Which deployment surface a request's `Host` names, and which project it is
  8. /// about.
  9. ///
  10. /// THIS IS A TENANCY BOUNDARY, not routing. A hostname that resolves to the
  11. /// wrong project hands one customer another customer's data, which is the worst
  12. /// failure this service has - so everything here is pure, exactly-matched, and
  13. /// refuses rather than guesses. The shape to keep in mind is the one this
  14. /// codebase has met before: `_files` is a pseudo-collection because a scope rule
  15. /// answers "may this principal touch that data", and a collection name is
  16. /// project-qualified because the separator decides whose row it is. A hostname
  17. /// is the same kind of thing wearing routing's clothes.
  18. namespace svapi {
  19. /// What a hostname is allowed to serve.
  20. enum class HostKind {
  21. /// Not registered. 404, and NEVER a fallback to a project: an unknown host
  22. /// resolving to "some project" is the hole this whole file exists to close.
  23. Unknown,
  24. /// The operator surface: every route, every project, plus the web UI and
  25. /// the docs page.
  26. Admin,
  27. /// Every project's API and functions, project named in the path. What the
  28. /// single customer-facing hostname does today.
  29. Global,
  30. /// Exactly one project's API and functions. No web UI, no docs.
  31. Project,
  32. };
  33. /// Whether a project hostname expects the project in the path.
  34. enum class PathStyle {
  35. /// `/api/v1/collections/...` - the project comes from the hostname.
  36. /// The default, because a customer-facing hostname saying its own tenant's
  37. /// name back to it is noise.
  38. Short,
  39. /// `/api/v1/projects/<p>/collections/...`, and `<p>` must be this
  40. /// hostname's project.
  41. Full,
  42. };
  43. struct HostBinding {
  44. HostKind kind = HostKind::Unknown;
  45. /// Set only when `kind == Project`.
  46. std::string project;
  47. /// Meaningful only when `kind == Project`.
  48. PathStyle pathStyle = PathStyle::Short;
  49. };
  50. /// One registered hostname, as stored in settings.
  51. struct HostEntry {
  52. std::string host;
  53. PathStyle pathStyle = PathStyle::Short;
  54. };
  55. /// Lowercase, strip the port, strip one trailing dot.
  56. ///
  57. /// The `Host` header is caller-controlled, so every normalisation here is a
  58. /// place a mismatch could be engineered:
  59. /// - CASE: DNS is case-insensitive, so `NT.KB.example` and `nt.kb.example`
  60. /// are one name. Comparing raw would let a caller miss a registry entry
  61. /// (harmless) or, with a sloppier comparison elsewhere, hit the wrong one.
  62. /// - PORT: `example:8443` is the same host as `example`. A deployment serving
  63. /// a non-443 port would otherwise register one spelling and receive the
  64. /// other.
  65. /// - IPv6: `[::1]:8080` keeps its brackets and loses only the port - the
  66. /// colons inside the brackets are part of the address, so a naive
  67. /// "truncate at the first colon" turns `[::1]` into `[`.
  68. /// - TRAILING DOT: `example.` is the fully-qualified spelling of `example`
  69. /// and resolves identically, so a client using it must not miss.
  70. std::string normaliseHost(std::string_view hostHeader);
  71. /// Host -> binding. Immutable once built; swapped as a whole under a
  72. /// `shared_ptr`, the same discipline SettingsStore and KeyStore use, so a
  73. /// reload cannot be observed half-applied by a request in flight.
  74. class HostRegistry {
  75. public:
  76. HostRegistry() = default;
  77. /// Build from the deployment's own names plus every project's.
  78. ///
  79. /// PRECEDENCE IS FIXED AND ADMIN WINS. If a project claims a hostname the
  80. /// deployment already uses, the deployment keeps it and the project's claim
  81. /// is dropped. That can only happen if a collision slipped past the
  82. /// write-time check (an older row, a hand-edited document), and the safe
  83. /// resolution is the one that cannot hand an operator surface to a tenant.
  84. /// The admin/global names are checked first for the same reason.
  85. ///
  86. /// A hostname claimed by two PROJECTS is dropped from BOTH rather than
  87. /// given to either. Last-writer-wins would silently move a tenancy
  88. /// boundary; refusing both makes the misconfiguration visible as a 404 on a
  89. /// name nobody can reach, which is recoverable, where serving the wrong
  90. /// tenant is not.
  91. static HostRegistry build(const std::string& adminHost,
  92. const std::vector<std::string>& globalHosts,
  93. const std::unordered_map<std::string, std::vector<HostEntry>>& byProject,
  94. std::vector<std::string>* droppedOut = nullptr);
  95. /// The binding for a `Host` header value. Normalises first.
  96. HostBinding resolve(std::string_view hostHeader) const;
  97. /// True when nothing is registered at all - the ordinary case for a
  98. /// deployment behind someone else's proxy, and the signal to skip host
  99. /// gating entirely rather than 404 every request.
  100. bool empty() const { return map_.empty(); }
  101. std::size_t size() const { return map_.size(); }
  102. private:
  103. std::unordered_map<std::string, HostBinding> map_;
  104. };
  105. /// Is this a hostname a project may register?
  106. ///
  107. /// Deliberately strict, and WILDCARDS ARE REFUSED. A project registering
  108. /// `*.example.com` would catch every subdomain nobody had claimed yet,
  109. /// including ones a later project expects to own - a tenancy boundary that
  110. /// moves when an unrelated DNS record appears. Wildcards belong in CERTIFICATE
  111. /// selection, where matching the wrong one fails the handshake, not in the
  112. /// registry, where matching the wrong one serves data.
  113. bool validHostname(std::string_view host);
  114. } // namespace svapi