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

+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 {