HSTS

Http4s provides a Middleware giving support for HTTP Strict Transport Security (HSTS). The middleware is called HSTS and simply adds a header to enable a HSTS security policy. Though it is not enforced, HSTS only makes sense for an https service.

Examples in this document have the following dependencies.

libraryDependencies ++= Seq(
  "org.http4s" %% "http4s-dsl" % http4sVersion,
  "org.http4s" %% "http4s-server" % http4sVersion
)

And we need some imports.

import org.http4s._
import org.http4s.dsl.io._
import org.http4s.implicits._
import cats.effect.IO

Let’s make a simple service that will be exposed and wrapped with HSTS.

val service = HttpRoutes.of[IO] {
  case _ =>
    Ok("ok")
}
// service: org.http4s.HttpRoutes[cats.effect.IO] = Kleisli(org.http4s.HttpRoutes$$$Lambda$32692/1970119254@1c2b9225)

val request = Request[IO](Method.GET, Uri.uri("/"))
// request: org.http4s.Request[cats.effect.IO] = Request(method=GET, uri=/, headers=Headers())

// Do not call 'unsafeRunSync' in your code
val response = service.orNotFound(request).unsafeRunSync
// response: org.http4s.Response[cats.effect.IO] = Response(status=200, headers=Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2))

response.headers
// res1: org.http4s.Headers = Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2)

If we were to wrap this on the HSTS middleware.

import org.http4s.server.middleware._
val hstsService = HSTS(service)
// hstsService: cats.data.Kleisli[[A]cats.data.OptionT[cats.effect.IO,A],org.http4s.Request[cats.effect.IO],org.http4s.Response[cats.effect.IO]] = Kleisli(org.http4s.server.middleware.HSTS$$$Lambda$32853/2004448009@ba052a8)

// Do not call 'unsafeRunSync' in your code
val response = hstsService.orNotFound(request).unsafeRunSync
// response: org.http4s.Response[cats.effect.IO] = Response(status=200, headers=Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2, Strict-Transport-Security: max-age=31536000; includeSubDomains))

response.headers
// res3: org.http4s.Headers = Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2, Strict-Transport-Security: max-age=31536000; includeSubDomains)

Now the response has the Strict-Transport-Security header which will mandate browsers supporting HSTS to always connect using https.

As described in Middleware, services and middleware can be composed though HSTS is something you may want enabled across all your routes.

Configuration

By default HSTS is configured to indicate that all requests during 1 year should be done over https and it will contain the includeSubDomains directive by default.

If you want to preload or change other default values you can pass a custom header, e.g.

import org.http4s.headers._
import scala.concurrent.duration._
val hstsHeader = `Strict-Transport-Security`.unsafeFromDuration(30.days, includeSubDomains = true, preload = true)
// hstsHeader: org.http4s.headers.Strict-Transport-Security = Strict-Transport-Security: max-age=2592000; includeSubDomains; preload

val hstsService = HSTS(service, hstsHeader)
// hstsService: cats.data.Kleisli[[A]cats.data.OptionT[cats.effect.IO,A],org.http4s.Request[cats.effect.IO],org.http4s.Response[cats.effect.IO]] = Kleisli(org.http4s.server.middleware.HSTS$$$Lambda$32853/2004448009@81108e4)

// Do not call 'unsafeRunSync' in your code
val response = hstsService.orNotFound(request).unsafeRunSync
// response: org.http4s.Response[cats.effect.IO] = Response(status=200, headers=Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2, Strict-Transport-Security: max-age=2592000; includeSubDomains; preload))

response.headers
// res5: org.http4s.Headers = Headers(Content-Type: text/plain; charset=UTF-8, Content-Length: 2, Strict-Transport-Security: max-age=2592000; includeSubDomains; preload)

References