Headline
CVE-2021-21294: Merge pull request from GHSA-xhv5-w9c5-2r2w · http4s/http4s@987d658
Http4s (http4s-blaze-server) is a minimal, idiomatic Scala interface for HTTP services. Http4s before versions 0.21.17, 0.22.0-M2, and 1.0.0-M14 have a vulnerability which can lead to a denial-of-service. Blaze-core, a library underlying http4s-blaze-server, accepts connections unboundedly on its selector pool. This has the net effect of amplifying degradation in services that are unable to handle their current request load, since incoming connections are still accepted and added to an unbounded queue. Each connection allocates a socket handle, which drains a scarce OS resource. This can also confound higher level circuit breakers which work based on detecting failed connections. http4s provides a general “MaxActiveRequests” middleware mechanism for limiting open connections, but it is enforced inside the Blaze accept loop, after the connection is accepted and the socket opened. Thus, the limit only prevents the number of connections which can be simultaneously processed, not the number of connections which can be held open. In 0.21.17, 0.22.0-M2, and 1.0.0-M14, a new “maxConnections” property, with a default value of 1024, has been added to the BlazeServerBuilder
. Setting the value to a negative number restores unbounded behavior, but is strongly disrecommended. The NIO2 backend does not respect maxConnections
. Its use is now deprecated in http4s-0.21, and the option is removed altogether starting in http4s-0.22. There are several possible workarounds described in the refrenced GitHub Advisory GHSA-xhv5-w9c5-2r2w.
@@ -38,7 +38,6 @@ import org.http4s.blaze.channel.{ SocketConnection } import org.http4s.blaze.channel.nio1.NIO1SocketServerGroup import org.http4s.blaze.channel.nio2.NIO2SocketServerGroup import org.http4s.blaze.http.http2.server.ALPNServerSelector import org.http4s.blaze.pipeline.LeafBuilder import org.http4s.blaze.pipeline.stages.SSLStage @@ -86,13 +85,13 @@ import scodec.bits.ByteVector * this is necessary to recover totality from the error condition. * @param banner: Pretty log to display on server start. An empty sequence * such as Nil disables this * @param maxConnections: The maximum number of client connections that may be active at any time. */ class BlazeServerBuilder[F[_]]( class BlazeServerBuilder[F[_]] private ( socketAddress: InetSocketAddress, executionContext: ExecutionContext, responseHeaderTimeout: Duration, idleTimeout: Duration, isNio2: Boolean, connectorPoolSize: Int, bufferSize: Int, selectorThreadFactory: ThreadFactory, @@ -105,6 +104,7 @@ class BlazeServerBuilder[F[_]]( httpApp: HttpApp[F], serviceErrorHandler: ServiceErrorHandler[F], banner: immutable.Seq[String], maxConnections: Int, val channelOptions: ChannelOptions )(implicit protected val F: Async[F]) extends ServerBuilder[F] @@ -118,7 +118,6 @@ class BlazeServerBuilder[F[_]]( executionContext: ExecutionContext = executionContext, idleTimeout: Duration = idleTimeout, responseHeaderTimeout: Duration = responseHeaderTimeout, isNio2: Boolean = isNio2, connectorPoolSize: Int = connectorPoolSize, bufferSize: Int = bufferSize, selectorThreadFactory: ThreadFactory = selectorThreadFactory, @@ -131,14 +130,14 @@ class BlazeServerBuilder[F[_]]( httpApp: HttpApp[F] = httpApp, serviceErrorHandler: ServiceErrorHandler[F] = serviceErrorHandler, banner: immutable.Seq[String] = banner, maxConnections: Int = maxConnections, channelOptions: ChannelOptions = channelOptions ): Self = new BlazeServerBuilder( socketAddress, executionContext, responseHeaderTimeout, idleTimeout, isNio2, connectorPoolSize, bufferSize, selectorThreadFactory, @@ -151,6 +150,7 @@ class BlazeServerBuilder[F[_]]( httpApp, serviceErrorHandler, banner, maxConnections, channelOptions )
@@ -219,8 +219,6 @@ class BlazeServerBuilder[F[_]]( def withSelectorThreadFactory(selectorThreadFactory: ThreadFactory): Self = copy(selectorThreadFactory = selectorThreadFactory)
def withNio2(isNio2: Boolean): Self = copy(isNio2 = isNio2)
def withWebSockets(enableWebsockets: Boolean): Self = copy(enableWebSockets = enableWebsockets)
@@ -247,6 +245,9 @@ class BlazeServerBuilder[F[_]]( def withChunkBufferMaxSize(chunkBufferMaxSize: Int): BlazeServerBuilder[F] = copy(chunkBufferMaxSize = chunkBufferMaxSize)
def withMaxConnections(maxConnections: Int): BlazeServerBuilder[F] = copy(maxConnections = maxConnections)
private def pipelineFactory( scheduler: TickWheelExecutor, engineConfig: Option[(SSLContext, SSLEngine => Unit)], @@ -343,12 +344,8 @@ class BlazeServerBuilder[F[_]]( else address
val mkFactory: Resource[F, ServerChannelGroup] = Resource.make(F.delay { if (isNio2) NIO2SocketServerGroup .fixedGroup(connectorPoolSize, bufferSize, channelOptions, selectorThreadFactory) else NIO1SocketServerGroup .fixedGroup(connectorPoolSize, bufferSize, channelOptions, selectorThreadFactory) NIO1SocketServerGroup .fixed(connectorPoolSize, bufferSize, channelOptions, selectorThreadFactory, maxConnections) })(factory => F.delay(factory.closeGroup()))
def mkServerChannel( @@ -415,7 +412,6 @@ object BlazeServerBuilder { executionContext = executionContext, responseHeaderTimeout = defaults.ResponseTimeout, idleTimeout = defaults.IdleTimeout, isNio2 = false, connectorPoolSize = DefaultPoolSize, bufferSize = 64 * 1024, selectorThreadFactory = defaultThreadSelectorFactory, @@ -428,6 +424,7 @@ object BlazeServerBuilder { httpApp = defaultApp[F], serviceErrorHandler = DefaultServiceErrorHandler[F], banner = defaults.Banner, maxConnections = defaults.MaxConnections, channelOptions = ChannelOptions(Vector.empty) )