diff --git a/Sources/NIOHTTP2/ContentLengthVerifier.swift b/Sources/NIOHTTP2/ContentLengthVerifier.swift index 43abe40..98aeea2 100644 --- a/Sources/NIOHTTP2/ContentLengthVerifier.swift +++ b/Sources/NIOHTTP2/ContentLengthVerifier.swift @@ -57,7 +57,15 @@ extension ContentLengthVerifier { guard contentLengths.dropFirst().allSatisfy({ $0 == first }) else { throw NIOHTTP2Errors.contentLengthHeadersMismatch() } + self.expectedContentLength = Int(first, radix: 10) + + guard let expectedLength = self.expectedContentLength else { + throw NIOHTTP2Errors.contentLengthHeaderMalformedValue() + } + if expectedLength < 0 { + throw NIOHTTP2Errors.contentLengthHeaderNegative() + } } /// The verifier for use when content length verification is disabled. diff --git a/Sources/NIOHTTP2/HTTP2Error.swift b/Sources/NIOHTTP2/HTTP2Error.swift index 09faf74..3e5428c 100644 --- a/Sources/NIOHTTP2/HTTP2Error.swift +++ b/Sources/NIOHTTP2/HTTP2Error.swift @@ -256,6 +256,16 @@ public enum NIOHTTP2Errors { return ContentLengthHeadersMismatch(file: file, line: line) } + /// Creates a ``ContentLengthHeaderNegative`` error with appropriate source context. + public static func contentLengthHeaderNegative(file: String = #fileID, line: UInt = #line) -> ContentLengthHeaderNegative { + return ContentLengthHeaderNegative(file: file, line: line) + } + + /// Creates a ``ContentLengthHeaderMalformedValue`` error with appropriate source context. + public static func contentLengthHeaderMalformedValue(file: String = #fileID, line: UInt = #line) -> ContentLengthHeaderMalformedValue { + return ContentLengthHeaderMalformedValue(file: file, line: line) + } + /// Creates a ``ExcessiveEmptyDataFrames`` error with appropriate source context. public static func excessiveEmptyDataFrames(file: String = #fileID, line: UInt = #line) -> ExcessiveEmptyDataFrames { return ExcessiveEmptyDataFrames(file: file, line: line) @@ -1430,6 +1440,47 @@ public enum NIOHTTP2Errors { } } + /// A request header block contains a content length header with a negative value + public struct ContentLengthHeaderNegative: NIOHTTP2Error { + private let file: String + private let line: UInt + + /// The location where the error was thrown. + public var location: String { + return _location(file: self.file, line: self.line) + } + + fileprivate init(file: String, line: UInt) { + self.file = file + self.line = line + } + + public static func ==(lhs: ContentLengthHeaderNegative, rhs: ContentLengthHeaderNegative) -> Bool { + return true + } + } + + /// 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 { + private let file: String + private let line: UInt + + /// The location where the error was thrown. + public var location: String { + return _location(file: self.file, line: self.line) + } + + fileprivate init(file: String, line: UInt) { + self.file = file + self.line = line + } + + public static func ==(lhs: ContentLengthHeaderMalformedValue, rhs: ContentLengthHeaderMalformedValue) -> Bool { + return true + } + } + /// The remote peer has sent an excessive number of empty `DATA` frames, which looks like a denial of service /// attempt, so the connection has been closed. public struct ExcessiveEmptyDataFrames: NIOHTTP2Error { diff --git a/Tests/NIOHTTP2Tests/ContentLengthVerifierTests+XCTest.swift b/Tests/NIOHTTP2Tests/ContentLengthVerifierTests+XCTest.swift index adfd775..5f33475 100644 --- a/Tests/NIOHTTP2Tests/ContentLengthVerifierTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/ContentLengthVerifierTests+XCTest.swift @@ -30,6 +30,10 @@ extension ContentLengthVerifierTests { ("testDuplicatedLengthHeadersPermitted", testDuplicatedLengthHeadersPermitted), ("testDuplicatedConflictingLengthHeadersThrow", testDuplicatedConflictingLengthHeadersThrow), ("testNumericallyEquivalentButConflictingLengthHeadersThrow", testNumericallyEquivalentButConflictingLengthHeadersThrow), + ("testNegativeLengthHeaderThrows", testNegativeLengthHeaderThrows), + ("testMinIntLengthHeaderDoesntPanic", testMinIntLengthHeaderDoesntPanic), + ("testMaxIntLengthHeaderDoesntPanic", testMaxIntLengthHeaderDoesntPanic), + ("testInvalidLengthHeaderValuesThrow", testInvalidLengthHeaderValuesThrow), ] } } diff --git a/Tests/NIOHTTP2Tests/ContentLengthVerifierTests.swift b/Tests/NIOHTTP2Tests/ContentLengthVerifierTests.swift index 6a75e84..44f600b 100644 --- a/Tests/NIOHTTP2Tests/ContentLengthVerifierTests.swift +++ b/Tests/NIOHTTP2Tests/ContentLengthVerifierTests.swift @@ -44,7 +44,9 @@ class ContentLengthVerifierTests: XCTestCase { XCTAssertEqual(1834, verifier.expectedContentLength) headers.add(contentsOf: [("Content-Length", "4381")]) - XCTAssertThrowsError(try ContentLengthVerifier(headers)) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeadersMismatch) + } } func testNumericallyEquivalentButConflictingLengthHeadersThrow() throws { @@ -53,6 +55,47 @@ class ContentLengthVerifierTests: XCTestCase { XCTAssertEqual(1834, verifier.expectedContentLength) headers.add(contentsOf: [("Content-Length", "01834")]) - XCTAssertThrowsError(try ContentLengthVerifier(headers)) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeadersMismatch) + } + } + + func testNegativeLengthHeaderThrows() throws { + let headers = HPACKHeaders([("Host", "apple.com"), ("content-length", "-1"), ("User-Agent", "myCoolClient/1.0")]) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeaderNegative) + } + } + + func testMinIntLengthHeaderDoesntPanic() throws { + let headers = HPACKHeaders([("Host", "apple.com"), ("content-length", String(Int.min)), ("User-Agent", "myCoolClient/1.0")]) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeaderNegative) + } + } + + func testMaxIntLengthHeaderDoesntPanic() throws { + let headers = HPACKHeaders([("Host", "apple.com"), ("content-length", String(Int.max)), ("User-Agent", "myCoolClient/1.0")]) + let verifier = try assertNoThrowWithValue(try ContentLengthVerifier(headers)) + XCTAssertEqual(Int.max, verifier.expectedContentLength) + } + + func testInvalidLengthHeaderValuesThrow() throws { + var headers = HPACKHeaders([("Host", "apple.com"), ("content-length", "0xFF"), ("User-Agent", "myCoolClient/1.0")]) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeaderMalformedValue) + } + + // Int.min - 1 + headers = HPACKHeaders([("Host", "apple.com"), ("content-length", "-9223372036854775809"), ("User-Agent", "myCoolClient/1.0")]) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeaderMalformedValue) + } + + // Int.max + 1 + headers = HPACKHeaders([("Host", "apple.com"), ("content-length", "9223372036854775809"), ("User-Agent", "myCoolClient/1.0")]) + XCTAssertThrowsError(try ContentLengthVerifier(headers)) { error in + XCTAssertTrue(error is NIOHTTP2Errors.ContentLengthHeaderMalformedValue) + } } }