Class BrowserRequestOriginPolicy

java.lang.Object
com.revetsec.BrowserRequestOriginPolicy

@Immutable @CheckReturnValue public final class BrowserRequestOriginPolicy extends Object
Checks browser request metadata against exact configured origins.

Configure this immutable policy once, then pass it the HTTP method and every raw field occurrence for Sec-Fetch-Site and Origin. Safe methods are admitted. For other methods, Sec-Fetch-Site: same-origin and Sec-Fetch-Site: none are sufficient when Origin is absent, opaque or exactly configured, including when a no-referrer page sends Origin: null. A contradictory concrete origin rejects. Same-site, cross-site, absent and future syntactically valid fetch-site values require an exact configured origin. Duplicate or malformed fields reject.

This is a request-origin admission layer, not a complete CSRF defense. Keep application state or synchronizer tokens where the workflow requires them, validate the request's host at a trusted edge, and do not derive allowed origins from Host or Forwarded.

Since:
1.0.0
  • Method Details

    • withAllowedOrigins

      Starts a policy with an exact, complete allowed-origin set.

      Origins must use their canonical ASCII serialization: lower-case https scheme and host, no user information, path, query or fragment, and no explicit default port. Plain http is rejected unless the development option is enabled, and then is limited to a loopback literal or exactly localhost.

      Parameters:
      allowedOrigins - exact allowed origins
      Returns:
      a new builder
      Throws:
      NullPointerException - if the set or an entry is null
      Since:
      1.0.0
    • checkResult

      public @NonNull BrowserRequestOriginResult checkResult(@NonNull String method, @NonNull List<@NonNull String> secFetchSiteHeaderValues, @NonNull List<@NonNull String> originHeaderValues)
      Checks a request without retaining its metadata.

      The two lists must preserve separate field occurrences. A list with more than one element rejects even if the values are identical. Safe methods (GET, HEAD and OPTIONS) are admitted before request metadata is inspected. Frameworks are responsible for supplying a valid HTTP method token.

      Parameters:
      method - HTTP method
      secFetchSiteHeaderValues - every raw Sec-Fetch-Site field occurrence
      originHeaderValues - every raw Origin field occurrence
      Returns:
      closed admission outcome
      Throws:
      NullPointerException - if an input, list entry or method is null
      IllegalArgumentException - if method is not a valid HTTP token
      Since:
      1.0.0
    • toString

      Redacts the configured origins and options.
      Overrides:
      toString in class Object
      Returns:
      a fixed description
      Since:
      1.0.0