Class SecurityHeaders

java.lang.Object
com.jsgalactic.axiom.security.SecurityHeaders
All Implemented Interfaces:
Middleware

public final class SecurityHeaders extends Object implements Middleware
Middleware that adds secure default response headers for an HTTP API.

 app.use(SecurityHeaders.defaults());
 app.use(SecurityHeaders.defaults().with("Strict-Transport-Security", "max-age=31536000"));
 

defaults() sets:

Default headers
HeaderValueEffect
X-Content-Type-Optionsnosniffbrowsers keep the declared media type
X-Frame-OptionsDENYno framing (older browsers)
Content-Security-Policydefault-src 'none'; frame-ancestors 'none' a response rendered by a browser loads nothing and cannot be framed
Referrer-Policyno-referrerno URL leaks through Referer
Cross-Origin-Resource-Policysame-originother origins cannot embed responses
Strict-Transport-Security is not a default: HSTS is only meaningful, and safe to enable, where the deployment serves HTTPS for the whole host and keeps doing so. Add it with with(String, String) on a TLS listener or behind a TLS-terminating proxy.

A header the response already carries is left unchanged, so a handler can deliberately loosen one (for example a CSP for an HTML page). Registered globally, the middleware also decorates the router's own 404, 405, automatic OPTIONS and 501 answers. It also decorates responses mapped from exceptions (problem responses, including the 401 and 403 of Security policies, and error handler responses) through Middleware.afterError(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.http.Response).

Immutable and thread-safe; one instance can serve every request.

  • Method Details

    • defaults

      public static SecurityHeaders defaults()
      Returns the default header set described above.
      Returns:
      default security headers
    • with

      public SecurityHeaders with(String name, String value)
      Returns a copy that also sets, or replaces, one header.
      Parameters:
      name - header name, an HTTP token
      value - header value without control characters
      Returns:
      a copy with the header
      Throws:
      IllegalArgumentException - for an invalid name or value
    • without

      public SecurityHeaders without(String name)
      Returns a copy that no longer sets one header.
      Parameters:
      name - header name, matched case-insensitively
      Returns:
      a copy without the header
    • headers

      public Map<String,String> headers()
      Returns the headers this middleware sets.
      Returns:
      immutable, case-insensitive header map
    • handle

      public Response handle(Context context, Middleware.Next next) throws Exception
      Description copied from interface: Middleware
      Handles one request, usually by calling next.run().
      Specified by:
      handle in interface Middleware
      Parameters:
      context - the request-scoped context shared with the handler; status settings made here apply to the handler's result mapping
      next - the rest of the chain
      Returns:
      the response to use; never null
      Throws:
      Exception - to fail the request as a handler exception would
    • afterError

      public Response afterError(Context context, Response response)
      Description copied from interface: Middleware
      Decorates a response that was produced from an exception: a problem response for an AxiomException, the response of an error handler, the generic 500, or the 406 for an unacceptable response type. The default returns the response unchanged.

      It is called once per such response for each middleware that was entered when the failure happened (a middleware an outer one short-circuited before was never entered), innermost first, after error mapping and before the response leaves app.handle. It is not called for responses that come back through Middleware.Next.run(), including responses a middleware or handler builds itself, nor for the router's 404, 405 and 501 answers (those flow through global middleware as ordinary responses), nor for failures before routing (413, CONNECT) and listener errors. An exception that nothing maps, and a request that was cancelled or timed out, never reach it.

      Like Middleware.handle(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.context.Middleware.Next), it runs on the request's thread under the request deadline and must be thread-safe. It must not throw: an exception from it is not mapped again. An AxiomException answers with its own undecorated problem response and anything else fails the request as an unexpected exception.

      Specified by:
      afterError in interface Middleware
      Parameters:
      context - the failed request's context
      response - the response produced from the exception, never null
      Returns:
      the response to pass outwards; never null