Throw on invalid psuedoheaders when converting between http/2 (#316)

Motivation:

The HTTPType to HTTP2 conversion functions can trip up on invalid psuedo
header fields. Prior to this change they'll loop indefinitely, instead
they should be rejected per RFC 9113 § 8.3.

Modifications:

- Throw on unrecognised psuedo header fields
- Add tests

Result:

- More spec compliant
This commit is contained in:
George Barnett authored and GitHub committed 2026-07-02 14:31:30 +00:00
1 parent 92a6e73338
commit cff75b3d21
2 files changed
+85 -5

No files matched your search

@@ -32,6 +32,18 @@ private final class InboundRecorder<Frame>: ChannelInboundHandler {
}
}
/// A handler that records errors fired down the pipeline via fireErrorCaught.
private final class ErrorRecorder: ChannelInboundHandler {
// InboundIn is a placeholder; this handler only captures errors.
typealias InboundIn = ByteBuffer
var caughtErrors: [Error] = []
func errorCaught(context: ChannelHandlerContext, error: Error) {
self.caughtErrors.append(error)
}
}
extension HTTPField.Name {
static let xFoo = Self("X-Foo")!
}
@@ -162,4 +174,71 @@ final class NIOHTTPTypesHTTP2Tests: XCTestCase {
XCTAssertTrue(try self.channel.finish().isClean)
}
func testServerCodecFiresErrorOnUnrecognizedPseudoHeaderInRequest() throws {
// A HEADERS frame containing ":status" must be rejected for a
// request and should result in an error being thrown.
let malformedHeaders: HPACKHeaders = [
":method": "GET",
":scheme": "https",
":path": "/",
":status": "200",
]
let errorRecorder = ErrorRecorder()
try self.channel.pipeline.syncOperations.addHandlers(HTTP2FramePayloadToHTTPServerCodec(), errorRecorder)
try self.channel.writeInbound(HTTP2Frame.FramePayload(headers: malformedHeaders))
XCTAssertNil(try self.channel.readInbound(as: HTTPRequestPart.self))
XCTAssertEqual(errorRecorder.caughtErrors.count, 1)
XCTAssertEqual(errorRecorder.caughtErrors.first as? HTTP2TypeConversionError, .unknownPseudoField)
}
func testClientCodecFiresErrorOnUnrecognizedPseudoHeaderInResponse() throws {
// A HEADERS frame containing ":method" must be rejected for a
// response and should result in an error being thrown.
let malformedHeaders: HPACKHeaders = [
":status": "200",
":method": "GET",
]
let errorRecorder = ErrorRecorder()
try self.channel.pipeline.syncOperations.addHandlers(HTTP2FramePayloadToHTTPClientCodec(), errorRecorder)
// The client state machine requires a request to be sent before it will
// accept a response frame, so prime it with a request head.
try self.channel.writeOutbound(HTTPRequestPart.head(Self.request))
try self.channel.writeInbound(HTTP2Frame.FramePayload(headers: malformedHeaders))
XCTAssertNil(try self.channel.readInbound(as: HTTPResponsePart.self))
XCTAssertEqual(errorRecorder.caughtErrors.count, 1)
XCTAssertEqual(errorRecorder.caughtErrors.first as? HTTP2TypeConversionError, .unknownPseudoField)
}
func testHTTPRequestInitThrowsOnUnrecognizedPseudoHeader() throws {
// `HTTP2TypeConversionError.unknownPseudoField` should be thrown if an invalid psuedo header
// is included in the response.
let headers: HPACKHeaders = [
":method": "GET",
":scheme": "https",
":path": "/",
":status": "200", // invalid for requests
]
XCTAssertThrowsError(try HTTPRequest(headers)) { error in
XCTAssertEqual(error as? HTTP2TypeConversionError, .unknownPseudoField)
}
}
func testHTTPResponseInitThrowsOnUnrecognizedPseudoHeader() throws {
// `HTTP2TypeConversionError.unknownPseudoField` should be thrown if an invalid psuedo header
// is included in the response.
let headers: HPACKHeaders = [
":status": "200",
":method": "GET", // invalid for responses.
]
XCTAssertThrowsError(try HTTPResponse(headers)) { error in
XCTAssertEqual(error as? HTTP2TypeConversionError, .unknownPseudoField)
}
}
}