Class HubOrigins

java.lang.Object
com.iizix.server.hub.HubOrigins

public final class HubOrigins extends Object
The Origin allow-list of HUB-DESIGN.md section 3.6 (R-17), applied by ServerShell once for both WebSocket upgrade paths - Jetty through WSCreator, NIO through WebSocketCommNIO - before any end point constructor is asked. Before B-7b the NIO path tested only that an Origin was present and the Jetty path tested nothing (survey row 65).

What is allowed: an Origin whose scheme, host and port match one of

  1. the server's own domains: the host the request came to (the Host header, which WSCreator has already checked against the URL), and every environment's allowedDomains, matched by AllowedDomains (exact names and *. wildcards), plus the configured external host name;
  2. the members' public URLs from the hub definition (a Browser page loaded from A connects to B, section 7.5);
  3. the iiziRun WebView origins ServerConstants.IIZIRUN_WEBVIEW_ORIGINS, the ones the upload endpoint's CORS filter established;
  4. the definition's [origins] list, with *. wildcards;
  5. the $allowedOrigins server setting (ServerSettings.ALLOWED_ORIGINS): exact origins, no wildcards, for a standalone server that has no definition file to name them in (CC-FIX-SB-CORS).
An environment that allows every domain contributes nothing beyond the Host rule: it says the server answers on any name, not that any page may open a WebSocket here.

No Origin (ruled H-10): a Browser always sends one, the Java WebSocket client sends none, so members and LicenseComm arrive without one: allowed, and authenticated by what the end point requires. Development (ruled H-10): a refusal is logged at WARNING and not enforced. A literal null origin (a sandboxed page, a data: or file: URL) matches nothing and is refused.

CORS (CC-FIX-SB-CORS, IIZI-JETTY-CORS-CREDENTIALS-ANY-ORIGIN): the same list decides which cross-origin page may read the server's responses, through isAllowedForCORS(String, String): a granted origin is granted, a development server grants the loopback origins as well, and a refusal is enforced on every server, unlike the upgrade of H-10: a CORS refusal is visible at once in the Browser's console.

Author:
Christopher Mindus
  • Field Details

  • Constructor Details

    • HubOrigins

      public HubOrigins(Supplier<ServerConfigEnvProps[]> environments, Supplier<String> externalHostName, Supplier<HubDefinition> definition, BooleanSupplier development)
      Creates the allow-list over live suppliers: nothing is cached, so a reload of the settings or the definition applies at the next upgrade. No $allowedOrigins setting.
      Parameters:
      environments - The server's environments, never null (may answer empty).
      externalHostName - The external host name URL as configured, or null.
      definition - The hub definition, null or standalone for none.
      development - true for a development server.
    • HubOrigins

      public HubOrigins(Supplier<ServerConfigEnvProps[]> environments, Supplier<String> externalHostName, Supplier<HubDefinition> definition, BooleanSupplier development, Supplier<String> allowedOrigins)
      Creates the allow-list over live suppliers: nothing is cached, so a reload of the settings or the definition applies at the next upgrade.
      Parameters:
      environments - The server's environments, never null (may answer empty).
      externalHostName - The external host name URL as configured, or null.
      definition - The hub definition, null or standalone for none.
      development - true for a development server.
      allowedOrigins - The $allowedOrigins server setting as written, null for none (CC-FIX-SB-CORS).
  • Method Details

    • originOf

      public static String originOf(Map<String, List<String>> headerFields)
      Gets the Origin of an upgrade request from its header fields, whatever the case of the field name: the NIO path lower-cases its names, the Jetty path keeps them.
      Parameters:
      headerFields - The header fields, may be null.
      Returns:
      The first Origin value, stripped, or null for none.
    • parse

      public static HubOrigins.Origin parse(String origin)
      Takes an origin or an absolute URL apart.
      Parameters:
      origin - The origin as sent, or a URL; may be null.
      Returns:
      The parts, or null when it is not http(s)://host[:port]....
    • originOfURL

      public static HubOrigins.Origin originOfURL(String url)
      The origin of a URL, e.g. a member's public base URL or the external host name.
      Parameters:
      url - The URL, may be null.
      Returns:
      The parts, or null when invalid.
    • check

      public HubOrigins.Verdict check(String origin, String requestHost)
      Decides on an origin.
      Parameters:
      origin - The Origin header value, null for none.
      requestHost - The host the request came to (comm.getDomain()), may be null.
      Returns:
      The verdict.
    • isAllowedForCORS

      public boolean isAllowedForCORS(String origin, String requestHost)
      Decides for CORS whether a cross-origin page may read the server's responses (CC-FIX-SB-CORS, IIZI-JETTY-CORS-CREDENTIALS-ANY-ORIGIN): the allow-list of check(String, String), and on a development server the loopback origins as well (WSHelper.isLoopbackOrigin(String)), so that the pages one development server serves on a developer's machine may call another on another port of it. Unlike isAccepted(String, String, String, Map), a refusal is enforced on a development server too. Asked by CORSFilter through IExternalBrowserPages.isOriginAllowed, for an http(s) origin that is neither an iiziRun WebView origin nor the host the request came to, which the filter grants by itself.
      Parameters:
      origin - The Origin header value, null for none.
      requestHost - The host the request came to, may be null.
      Returns:
      true when granted; false for no origin, a foreign origin or the literal null.
    • isAccepted

      public boolean isAccepted(String who, String uri, String requestHost, Map<String, List<String>> headerFields)
      Decides on an upgrade request and logs a refusal: in production the request is refused, in development it is logged at WARNING and accepted (ruled H-10).
      Parameters:
      who - Who the request is from, for the log line, e.g. addr / host.
      uri - The request URI, for the log line.
      requestHost - The host the request came to, may be null.
      headerFields - The header fields, may be null.
      Returns:
      true to go on with the upgrade, false to refuse it.