From 22757ac305f3d44d2b99ba541193ff1d64e77d00 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 6 Feb 2023 10:20:36 +0000 Subject: [PATCH] Content-Length errors shouldn't crash (#371) Motivation: When we added new content length verification and new errors, we didn't update the places that were handling the old error. This code updates that logic to ensure that we don't crash. Modifications: Add new internal protocol to which the content length validation errors conform. Catch that protocol instead. Result: No crashes on invalid content-length --- Sources/NIOHTTP2/HTTP2Error.swift | 11 +++-- Sources/NIOHTTP2/StreamStateMachine.swift | 4 +- .../ConnectionStateMachineTests+XCTest.swift | 3 ++ .../ConnectionStateMachineTests.swift | 45 +++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Sources/NIOHTTP2/HTTP2Error.swift b/Sources/NIOHTTP2/HTTP2Error.swift index 3e5428c..aea009c 100644 --- a/Sources/NIOHTTP2/HTTP2Error.swift +++ b/Sources/NIOHTTP2/HTTP2Error.swift @@ -18,6 +18,9 @@ import NIOHPACK /// Users are recommended not to implement this protocol with their own types. public protocol NIOHTTP2Error: Equatable, Error { } +/// An internal marker-protocol for errors to do with content-length. +protocol InvalidContentLengthError: NIOHTTP2Error { } + /// Errors that ``NIOHTTP2`` raises when handling HTTP/2 connections. public enum NIOHTTP2Errors { /// Creates an ``ExcessiveOutboundFrameBuffering`` error with appropriate source context. @@ -1396,7 +1399,7 @@ public enum NIOHTTP2Errors { } /// A request or response has violated the expected content length, either exceeding or falling beneath it. - public struct ContentLengthViolated: NIOHTTP2Error { + public struct ContentLengthViolated: NIOHTTP2Error, InvalidContentLengthError { private let file: String private let line: UInt @@ -1421,7 +1424,7 @@ public enum NIOHTTP2Errors { } /// A request header block contains multiple content length headers with disagreeing values - public struct ContentLengthHeadersMismatch: NIOHTTP2Error { + public struct ContentLengthHeadersMismatch: NIOHTTP2Error, InvalidContentLengthError { private let file: String private let line: UInt @@ -1441,7 +1444,7 @@ public enum NIOHTTP2Errors { } /// A request header block contains a content length header with a negative value - public struct ContentLengthHeaderNegative: NIOHTTP2Error { + public struct ContentLengthHeaderNegative: NIOHTTP2Error, InvalidContentLengthError { private let file: String private let line: UInt @@ -1462,7 +1465,7 @@ public enum NIOHTTP2Errors { /// A request header block contains a content length header with a malformed value /// e.g. an unparsable string or an integer which cannot be represented by an Int - public struct ContentLengthHeaderMalformedValue: NIOHTTP2Error { + public struct ContentLengthHeaderMalformedValue: NIOHTTP2Error, InvalidContentLengthError { private let file: String private let line: UInt diff --git a/Sources/NIOHTTP2/StreamStateMachine.swift b/Sources/NIOHTTP2/StreamStateMachine.swift index 696c7d9..809b6db 100644 --- a/Sources/NIOHTTP2/StreamStateMachine.swift +++ b/Sources/NIOHTTP2/StreamStateMachine.swift @@ -389,7 +389,7 @@ extension HTTP2StreamStateMachine { case .reservedRemote, .halfClosedLocalPeerIdle, .halfClosedLocalPeerActive: return .init(result: .streamError(streamID: self.streamID, underlyingError: NIOHTTP2Errors.badStreamStateTransition(from: NIOHTTP2StreamState.get(self.state)), type: .protocolError), effect: nil) } - } catch let error where error is NIOHTTP2Errors.ContentLengthViolated { + } catch let error where error is InvalidContentLengthError { return .init(result: .streamError(streamID: self.streamID, underlyingError: error, type: .protocolError), effect: nil) } catch { preconditionFailure("Unexpected error: \(error)") @@ -521,7 +521,7 @@ extension HTTP2StreamStateMachine { case .reservedLocal, .halfClosedRemoteLocalIdle, .halfClosedRemoteLocalActive: return .init(result: .streamError(streamID: self.streamID, underlyingError: NIOHTTP2Errors.badStreamStateTransition(from: NIOHTTP2StreamState.get(self.state)), type: .protocolError), effect: nil) } - } catch let error where error is NIOHTTP2Errors.ContentLengthViolated { + } catch let error where error is InvalidContentLengthError { return .init(result: .streamError(streamID: self.streamID, underlyingError: error, type: .protocolError), effect: nil) } catch { preconditionFailure("Unexpected error: \(error)") diff --git a/Tests/NIOHTTP2Tests/ConnectionStateMachineTests+XCTest.swift b/Tests/NIOHTTP2Tests/ConnectionStateMachineTests+XCTest.swift index 17ca96d..3e17d80 100644 --- a/Tests/NIOHTTP2Tests/ConnectionStateMachineTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/ConnectionStateMachineTests+XCTest.swift @@ -151,6 +151,9 @@ extension ConnectionStateMachineTests { ("testContentLengthForHeadFailure", testContentLengthForHeadFailure), ("testPushHeadRequestFailure", testPushHeadRequestFailure), ("testPushHeadRequest", testPushHeadRequest), + ("testNegativeContentLengthHeader", testNegativeContentLengthHeader), + ("testInvalidContentLengthHeader", testInvalidContentLengthHeader), + ("testContentLengthHeadersMismatch", testContentLengthHeadersMismatch), ] } } diff --git a/Tests/NIOHTTP2Tests/ConnectionStateMachineTests.swift b/Tests/NIOHTTP2Tests/ConnectionStateMachineTests.swift index 138db19..0d11b4d 100644 --- a/Tests/NIOHTTP2Tests/ConnectionStateMachineTests.swift +++ b/Tests/NIOHTTP2Tests/ConnectionStateMachineTests.swift @@ -3144,6 +3144,51 @@ class ConnectionStateMachineTests: XCTestCase { // Send in 0 bytes over one frame assertSucceeds(self.client.receiveData(streamID: streamTwo, contentLength: 0, flowControlledBytes: 0, isEndStreamSet: true)) } + + func testNegativeContentLengthHeader() { + let streamOne = HTTP2StreamID(1) + + self.exchangePreamble() + + let responseHeaders = HPACKHeaders([(":status", "200"), ("content-length", "-25")]) + + // Set up the connection + assertSucceeds(self.client.sendHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + assertSucceeds(self.server.receiveHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + + assertStreamError(type: .protocolError, self.server.sendHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + assertStreamError(type: .protocolError, self.client.receiveHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + } + + func testInvalidContentLengthHeader() { + let streamOne = HTTP2StreamID(1) + + self.exchangePreamble() + + let responseHeaders = HPACKHeaders([(":status", "200"), ("content-length", "0xFF")]) + + // Set up the connection + assertSucceeds(self.client.sendHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + assertSucceeds(self.server.receiveHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + + assertStreamError(type: .protocolError, self.server.sendHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + assertStreamError(type: .protocolError, self.client.receiveHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + } + + func testContentLengthHeadersMismatch() { + let streamOne = HTTP2StreamID(1) + + self.exchangePreamble() + + let responseHeaders = HPACKHeaders([(":status", "200"), ("content-length", "1384"), ("content-length", "4831")]) + + // Set up the connection + assertSucceeds(self.client.sendHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + assertSucceeds(self.server.receiveHeaders(streamID: streamOne, headers: Self.requestHeaders, isEndStreamSet: true)) + + assertStreamError(type: .protocolError, self.server.sendHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + assertStreamError(type: .protocolError, self.client.receiveHeaders(streamID: streamOne, headers: responseHeaders, isEndStreamSet: false)) + } }