ContentLengthVerifier init throws on negative values (#361)

Motivation:

We discovered that this code path can result in an arithmetic overflow
in the case where the content-length header has INT_MIN value. Since
negative content-lengths don't have any meaning anyway we may as well
throw if we encounter one so the decoding can be aborted.

Modifications:

* ContentLengthVerifier init throws a `ContentLengthHeaderNegative` error
when the decoded value is < 0.
* ContentLengthVerifier init throws a `ContentLengthHeaderMalformedValue` error
when the value cannot be decoded, e.g. it is out of bounds for Int or is
an invalid string.

Result:

We will no longer panic for INT_MIN content-lengths and will fail
decoding if one of various invalid values is encountered.
This commit is contained in:
Rick Newton-Rogers authored and GitHub committed 2022-11-25 13:41:46 +00:00
1 parent b0b64afb0a
commit a6d0d37e2e
4 files changed
+108 -2

No files matched your search

@@ -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.
+51
View File
@@ -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 {
@@ -30,6 +30,10 @@ extension ContentLengthVerifierTests {
("testDuplicatedLengthHeadersPermitted", testDuplicatedLengthHeadersPermitted),
("testDuplicatedConflictingLengthHeadersThrow", testDuplicatedConflictingLengthHeadersThrow),
("testNumericallyEquivalentButConflictingLengthHeadersThrow", testNumericallyEquivalentButConflictingLengthHeadersThrow),
("testNegativeLengthHeaderThrows", testNegativeLengthHeaderThrows),
("testMinIntLengthHeaderDoesntPanic", testMinIntLengthHeaderDoesntPanic),
("testMaxIntLengthHeaderDoesntPanic", testMaxIntLengthHeaderDoesntPanic),
("testInvalidLengthHeaderValuesThrow", testInvalidLengthHeaderValuesThrow),
]
}
}
@@ -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)
}
}
}