Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Tolerate unsolicited HTTP responses. #1084

Merged
merged 2 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions Sources/NIOHTTP1/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private class BetterHTTPParser {
private var httpParserOffset = 0
private var rawBytesView: UnsafeRawBufferPointer = .init(start: UnsafeRawPointer(bitPattern: 0xcafbabe), count: 0)
private var httpErrno: http_errno? = nil
private var richerError: Error? = nil
private let kind: HTTPDecoderKind
var requestHeads = CircularBuffer<HTTPRequestHead>(initialCapacity: 1)

Expand Down Expand Up @@ -295,9 +296,14 @@ private class BetterHTTPParser {
// does not meet the requirement of RFC 7230. This is an outstanding http_parser issue:
// https://github.com/nodejs/http-parser/issues/251. As a result, we check for these status
// codes and override http_parser's handling as well.
let method = self.requestHeads.removeFirst().method
if method == .HEAD || method == .CONNECT {
return .skipBody
if self.requestHeads.count > 0 {
let method = self.requestHeads.removeFirst().method
if method == .HEAD || method == .CONNECT {
return .skipBody
}
} else {
self.richerError = NIOHTTPDecoderError.unsolicitedResponse
return .error(HPE_UNKNOWN)
}

if (statusCode / 100 == 1 || // 1XX codes
Expand Down Expand Up @@ -361,8 +367,14 @@ private class BetterHTTPParser {
// if we chose to abort (eg. wrong HTTP version) the error will be in self.httpErrno, otherwise http_parser
// will tell us...
// self.parser must be non-nil here because we can't be re-entered here (ByteToMessageDecoder guarantee)
let err = http_errno(rawValue: self.httpErrno?.rawValue ?? parserErrno)
throw HTTPParserError.httpError(fromCHTTPParserErrno: err)!
// If we have a richer error than the errno code, and the errno is unknown, we'll use it. Otherwise, we use the
// error from http_parser.
let err = self.httpErrno ?? http_errno(rawValue: parserErrno)
if err == HPE_UNKNOWN, let richerError = self.richerError {
throw richerError
} else {
throw HTTPParserError.httpError(fromCHTTPParserErrno: err)!
}
}
if let firstNonDiscardableOffset = self.firstNonDiscardableOffset {
self.httpParserOffset += parserConsumed - firstNonDiscardableOffset
Expand Down Expand Up @@ -783,3 +795,30 @@ extension HTTPMethod {
}
}
}


/// Errors thrown by `HTTPRequestDecoder` and `HTTPResponseDecoder` in addition to
/// `HTTPParserError`.
public struct NIOHTTPDecoderError: Error {
private enum BaseError: Hashable {
case unsolicitedResponse
}

private let baseError: BaseError
}


extension NIOHTTPDecoderError {
/// A response was received from a server without an associated request having been sent.
public static let unsolicitedResponse: NIOHTTPDecoderError = .init(baseError: .unsolicitedResponse)
}


extension NIOHTTPDecoderError: Hashable { }


extension NIOHTTPDecoderError: CustomDebugStringConvertible {
public var debugDescription: String {
return String(describing: self.baseError)
}
}
2 changes: 2 additions & 0 deletions Tests/NIOHTTP1Tests/HTTPDecoderTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ extension HTTPDecoderTest {
("testBytesCanBeForwardedWhenHandlerRemoved", testBytesCanBeForwardedWhenHandlerRemoved),
("testBytesCanBeFiredAsErrorWhenHandlerRemoved", testBytesCanBeFiredAsErrorWhenHandlerRemoved),
("testBytesCanBeDroppedWhenHandlerRemoved", testBytesCanBeDroppedWhenHandlerRemoved),
("testAppropriateErrorWhenReceivingUnsolicitedResponse", testAppropriateErrorWhenReceivingUnsolicitedResponse),
("testAppropriateErrorWhenReceivingUnsolicitedResponseDoesNotRecover", testAppropriateErrorWhenReceivingUnsolicitedResponseDoesNotRecover),
]
}
}
Expand Down
40 changes: 40 additions & 0 deletions Tests/NIOHTTP1Tests/HTTPDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,44 @@ class HTTPDecoderTest: XCTestCase {
XCTAssertNoThrow(XCTAssert(try channel.finish().isClean))
}

func testAppropriateErrorWhenReceivingUnsolicitedResponse() throws {
let channel = EmbeddedChannel()
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("HTTP/1.1 200 OK\r\nServer: a-bad-server/1.0.0\r\n\r\n")

let decoder = ByteToMessageHandler(HTTPResponseDecoder(leftOverBytesStrategy: .dropBytes))
XCTAssertNoThrow(try channel.pipeline.addHandler(decoder).wait())

XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(error as? NIOHTTPDecoderError, .unsolicitedResponse)
}
}

func testAppropriateErrorWhenReceivingUnsolicitedResponseDoesNotRecover() throws {
let channel = EmbeddedChannel()
var buffer = channel.allocator.buffer(capacity: 64)
buffer.writeStaticString("HTTP/1.1 200 OK\r\nServer: a-bad-server/1.0.0\r\n\r\n")

let decoder = ByteToMessageHandler(HTTPResponseDecoder(leftOverBytesStrategy: .dropBytes))
XCTAssertNoThrow(try channel.pipeline.addHandler(decoder).wait())

XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
XCTAssertEqual(error as? NIOHTTPDecoderError, .unsolicitedResponse)
}

// Write a request.
let request = HTTPClientRequestPart.head(.init(version: .init(major: 1, minor: 1), method: .GET, uri: "/"))
XCTAssertNoThrow(try channel.writeOutbound(request))

// The server sending another response should lead to another error.
XCTAssertThrowsError(try channel.writeInbound(buffer)) { error in
guard case .some(.dataReceivedInErrorState(let baseError, _)) = error as? ByteToMessageDecoderError else {
XCTFail("Unexpected error type: \(error)")
return
}

XCTAssertEqual(baseError as? NIOHTTPDecoderError, .unsolicitedResponse)
}
}

}