Files
swift-nio-extras/Sources/NIOHTTPCompression/HTTPDecompressor.swift
T
MichaelDRZandGeorge Barnett 88a51340f5 Make the HTTP decompressors safe against synchronous channelRead re-entry (#317)
### Motivation:

`NIOHTTPRequestDecompressor` / `NIOHTTPResponseDecompressor` keep their
state in a
stored `var decompressor` (a `struct`) and forwarded downstream
(`fireChannelRead` /
`fireErrorCaught`) from inside that struct's `mutating` `channelRead`,
i.e. while an
exclusive (modify) access to the stored property was still open. If
firing downstream
synchronously re-enters the handler's `channelRead`, a second exclusive
access is
opened and the program traps with `Fatal access conflict detected`
(SIGABRT).

This reproduces in production with AsyncHTTPClient over HTTP/2 and
`decompression = .enabled`: a compressed response is mid-decompress when
a
decompression error closes the stream, and the buffered read
re-delivered on the
stream-close path re-enters `channelRead` on the same decompressor.

### Modifications:

- The body decompression is now expressed as a
`NIOSingleStepByteToMessageDecoder`,
driven by a `NIOSingleStepByteToMessageProcessor`. The handler pulls one
decompressed
buffer at a time and forwards it only after `decode` has returned, so
the decoder
  relinquishes its exclusive access before anything is fired downstream.
- The shared driver is now a `class`, so the head/end forwarding paths
are re-entrancy
  safe for the same reason.
- The zlib stream is torn down in the decoder's `deinit`, which
additionally fixes a
  leak when a message ends without a final `.end`.
- No public API changes.

### Result:

Synchronous re-entry of `channelRead` no longer traps. Adds regression
tests covering
the forward and error re-entry paths on both decompressors; all existing
decompression
tests (limits, trailing data, truncation, multi-message reuse) continue
to pass.

Fixes #314

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-07-06 08:59:16 +00:00

213 lines
7.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019-2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import NIOHTTP1
/// Channel hander to decompress incoming HTTP data.
public final class NIOHTTPRequestDecompressor: ChannelDuplexHandler, RemovableChannelHandler {
/// Expect to receive `HTTPServerRequestPart` from the network
public typealias InboundIn = HTTPServerRequestPart
/// Pass `HTTPServerRequestPart` to the next pipeline state in an inbound direction.
public typealias InboundOut = HTTPServerRequestPart
/// Pass through `HTTPServerResponsePart` outbound.
public typealias OutboundIn = HTTPServerResponsePart
/// Pass through `HTTPServerResponsePart` outbound.
public typealias OutboundOut = HTTPServerResponsePart
private var decompressor: Decompressor<HTTPRequestHead>
/// Initialise
/// - Parameter limit: Limit on the amount of decompression allowed.
public init(limit: NIOHTTPDecompression.DecompressionLimit) {
self.decompressor = Decompressor(limit: limit)
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.decompressor.channelRead(context: context, part: Self.unwrapInboundIn(data))
}
}
@available(*, unavailable)
extension NIOHTTPRequestDecompressor: Sendable {}
/// Duplex channel handler which will accept deflate and gzip encoded responses and decompress them.
public final class NIOHTTPResponseDecompressor: ChannelDuplexHandler, RemovableChannelHandler {
/// Expect `HTTPClientResponsePart` inbound.
public typealias InboundIn = HTTPClientResponsePart
/// Sends `HTTPClientResponsePart` to the next pipeline stage inbound.
public typealias InboundOut = HTTPClientResponsePart
/// Expect `HTTPClientRequestPart` outbound.
public typealias OutboundIn = HTTPClientRequestPart
/// Send `HTTPClientRequestPart` to the next stage outbound.
public typealias OutboundOut = HTTPClientRequestPart
private var decompressor: Decompressor<HTTPResponseHead>
/// Initialise
/// - Parameter limit: Limit on the amount of decompression allowed.
public init(limit: NIOHTTPDecompression.DecompressionLimit) {
self.decompressor = Decompressor(limit: limit)
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let request = self.unwrapOutboundIn(data)
switch request {
case .head(var head):
if head.headers.contains(name: "Accept-Encoding") {
context.write(data, promise: promise)
} else {
head.headers.replaceOrAdd(name: "Accept-Encoding", value: "deflate, gzip")
context.write(self.wrapOutboundOut(.head(head)), promise: promise)
}
default:
context.write(data, promise: promise)
}
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.decompressor.channelRead(context: context, part: Self.unwrapInboundIn(data))
}
}
@available(*, unavailable)
extension NIOHTTPResponseDecompressor: Sendable {}
// MARK: - Shared implementation -
private protocol HTTPHead: Equatable {
var headers: HTTPHeaders { get }
}
extension HTTPRequestHead: HTTPHead {}
extension HTTPResponseHead: HTTPHead {}
/// This class encapsulates the state of a single http message decompression
private final class HTTPDecompressionDecoder: NIOSingleStepByteToMessageDecoder {
typealias InboundOut = ByteBuffer
private let allocator: ByteBufferAllocator
private var decompressor: NIOHTTPDecompression.Decompressor
/// The number of already consumed compressed bytes
private var compressedLength: Int
private var complete: Bool
init(limit: NIOHTTPDecompression.DecompressionLimit, allocator: ByteBufferAllocator) throws {
self.allocator = allocator
self.decompressor = NIOHTTPDecompression.Decompressor(limit: limit)
self.compressedLength = 0
self.complete = false
try self.decompressor.initializeDecoder()
}
deinit {
self.decompressor.deinitializeDecoder()
}
func decode(buffer: inout ByteBuffer) throws -> ByteBuffer? {
guard !self.complete, buffer.readableBytes > 0 else {
return nil
}
let compressedLength = self.compressedLength + buffer.readableBytes
var output = self.allocator.buffer(capacity: 16384)
let readableBytesBefore = buffer.readableBytes
let result = try self.decompressor.decompress(
part: &buffer,
buffer: &output,
compressedLength: compressedLength
)
self.compressedLength += readableBytesBefore - buffer.readableBytes
if result.complete {
self.complete = true
}
return output
}
func decodeLast(buffer: inout ByteBuffer, seenEOF: Bool) throws -> ByteBuffer? {
if let output = try self.decode(buffer: &buffer) {
return output
}
if !self.complete {
throw NIOHTTPDecompression.ExtraDecompressionError.truncatedData
}
return nil
}
}
private final class Decompressor<InboundHead: HTTPHead> {
typealias Inbound = HTTPPart<InboundHead, ByteBuffer>
private let limit: NIOHTTPDecompression.DecompressionLimit
private var processor: NIOSingleStepByteToMessageProcessor<HTTPDecompressionDecoder>?
init(limit: NIOHTTPDecompression.DecompressionLimit) {
self.limit = limit
self.processor = nil
}
func channelRead(context: ChannelHandlerContext, part: Inbound) {
switch part {
case .head(let head):
let contentType = head.headers[canonicalForm: "Content-Encoding"].first?.lowercased()
let algorithm = NIOHTTPDecompression.CompressionAlgorithm(header: contentType)
do {
if algorithm != nil {
let decoder = try HTTPDecompressionDecoder(
limit: self.limit,
allocator: context.channel.allocator
)
self.processor = NIOSingleStepByteToMessageProcessor(decoder)
}
context.fireChannelRead(NIOAny(part))
} catch {
context.fireErrorCaught(error)
}
case .body(let buffer):
guard let processor = self.processor else {
context.fireChannelRead(NIOAny(part))
return
}
do {
try processor.process(buffer: buffer) { output in
context.fireChannelRead(NIOAny(Inbound.body(output)))
}
// bytes left after the compressed body are invalid trailing data
if processor.unprocessedBytes > 0 {
context.fireErrorCaught(NIOHTTPDecompression.ExtraDecompressionError.invalidTrailingData)
}
} catch {
context.fireErrorCaught(error)
}
case .end:
if let processor = self.processor {
self.processor = nil
do {
try processor.finishProcessing(seenEOF: true) { output in
context.fireChannelRead(NIOAny(Inbound.body(output)))
}
} catch {
context.fireErrorCaught(error)
}
}
context.fireChannelRead(NIOAny(part))
}
}
}