Files
swift-nio-http2/Tests/NIOHTTP2Tests/CompoundOutboundBufferTest.swift
T
Cory Benfield 86ce1dcd0d Merge pull request from GHSA-jchv-x857-q8fq
* Forbid excessive sending of empty DATA frames

Motivation:

One vector for DoS can be sending a large number of essentially "empty"
frames, such as empty DATA frames without END_STREAM set. We should
forbid peers from doing so excessively, but we want to tolerate
implementations that occasionally emit such a frame for buggy reasons.

Modifications:

- Added verification that too many empty DATA frames are not sent.

Result:

Better resistance to DoS attacks.

* Improved buffering in CompoundOutboundBuffer

Motivation:

In HTTP/2 it is possible for remote peers to submit work in a way that
cause the NIOHTTP2Handler to generate a substantial number of control
frames. If the peer is not consuming these frames, we can eventually run
out of memory and die, which is not exactly ideal.

It's hard for us to know how the buffering is going if we always submit
all frames directly to the Channel, so we need to pay attention to
whether the Channel is actually writable. If it isn't, rather than issue
further writes we need to delay those writes so we can keep track of how
big that buffer is getting. If the buffer gets too large we need a
circuit breaker to drop the connection, as we're clearly not able to get
work done.

This patch adds a new buffer for outbound control frames. This new
buffer is the last step in the buffering sequence, and the first to be
drained when a Channel becomes writable.

Additionally, enhancements need to be made to ensure that other buffers
behave correctly in the face of this new model. The
ConcurrentStreamsBuffer needs to be more aggressive about storing
HEADERS frames that cannot be emitted to the wire because it does not
tolerate emitting a HEADERS frame that cannot be immediately written to
the wire. Additionally, the buffers must not be polled when a Channel is
unwritable.

This is a large patch, but it does substantially change the buffering
logic, and protects NIO against a wide range of denial of service
attacks, while providing a clear and coherent defense surface.

Note that one thing this patch does not do is constrain the size of the
buffers in the ConcurrentStreamsBuffer. This is on purpose: the size of
this buffer is controlled entirely by user action. Users that do not
wish to buffer excessively in this buffer can avoid doing so simply by
respecting the peer's value of SETTINGS_MAX_CONCURRENT_STREAMS. As the
entire purpose of this buffer is to tolerate users that do not wish to
abide by this setting, it would be unreasonable to provide an upper
bound on the value used in this setting.

Modifications:

- Moved the various buffers into a directory.
- Built and implemented ControlFrameBuffer.
- Integrated ControlFrameBuffer into CompoundOutboundBuffer.
- Added awareness around channel writability to HTTP2Handler
- Enhanced ConcurrentStreamsBuffer to be writability-aware.

Result:

We'll have somewhere to put control frames.

* Cleanup API for merged security fixes.

Motivation:

After merging the security fixes we ended up with a slightly awkward
API: the argument names differed slightly for no good reason, and it
became impossible to override only one of the two security settings.

Modifications:

- Renamed `max` to `maximum`.
- Gave default values for the security settings.

Result:

Better user experience.

* Prevent DoS with large header lists.

Motivation:

There are two ways malicious peers could attempt to DoS a SwiftNIO
HTTP/2 implementation with header lists. The first would be to attempt
to send an enormous header list, ideally with an amplification factor.
This approach was possible because SwiftNIO never actually enforced the
value of the max header list size it advertised.

In principle the peer can still get a good amplification attack effect
by sending a long header list that contains zero-length headers. We may
as well close this door as well: zero length header field names are
unacceptable, and so we should forbid them.

Modifications:

- Added max header list enforcement.
- Forbade zero-length header field names.

Result:

Peers cannot DoS a SwiftNIO HTTP/2 implementation using large header
lists.
2019-08-13 18:00:41 +01:00

418 lines
20 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
import NIOHPACK
@testable import NIOHTTP2
final class CompoundOutboundBufferTest: XCTestCase {
var loop: EmbeddedEventLoop!
override func setUp() {
self.loop = EmbeddedEventLoop()
}
override func tearDown() {
XCTAssertNoThrow(try self.loop.syncShutdownGracefully())
self.loop = nil
}
func testSimpleControlFramePassthrough() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 100, maxBufferedControlFrames: 1000)
let frames: [HTTP2Frame] = [
HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 0), ack: true)),
HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: 0, errorCode: .protocolError, opaqueData: nil)),
HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([]))),
HTTP2Frame(streamID: .rootStream, payload: .alternativeService(origin: nil, field: nil)),
HTTP2Frame(streamID: .rootStream, payload: .origin([])),
HTTP2Frame(streamID: 1, payload: .priority(.init(exclusive: true, dependency: 0, weight: 200))),
HTTP2Frame(streamID: 1, payload: .rstStream(.protocolError)),
]
for frame in frames {
XCTAssertNoThrow(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertForward(), "Threw on \(frame)")
}
}
func testSimpleOutboundStreamBuffering() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
// Let's create 100 new outbound streams.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrames: [HTTP2Frame] = stride(from: 3, to: 201, by: 2).map { streamID in
HTTP2Frame(streamID: HTTP2StreamID(streamID), payload: .headers(.init(headers: HPACKHeaders([]), endStream: true)))
}
// Write the first frame, and all the subsequent frames, and flush them. This will lead to one flushed frame.
XCTAssertNoThrow(try buffer.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 65535)
for frame in subsequentFrames {
XCTAssertNoThrow(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing())
}
buffer.flushReceived()
buffer.nextFlushedWritableFrame(channelWritable: true).assertNoFrame()
// Ok, now we're going to tell the buffer that the first stream is closed, and then spin over the buffer getting
// new frames and closing them until there are no more to get.
var newFrames: [HTTP2Frame] = Array()
XCTAssertEqual(buffer.streamClosed(1).count, 0)
while case .frame(let write) = buffer.nextFlushedWritableFrame(channelWritable: true) {
newFrames.append(write.0)
XCTAssertNil(write.1)
buffer.streamCreated(write.0.streamID, initialWindowSize: 65535)
XCTAssertEqual(buffer.streamClosed(write.0.streamID).count, 0)
}
subsequentFrames.assertFramesMatch(newFrames)
}
func testSimpleFlowControlBuffering() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
let streamOne = HTTP2StreamID(1)
buffer.streamCreated(streamOne, initialWindowSize: 15)
var frame = self.createDataFrame(streamOne, byteBufferSize: 50)
XCTAssertNoThrow(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing())
buffer.flushReceived()
buffer.receivedFrames().assertFramesMatch([frame.sliceDataFrame(length: 15)])
// Update the window.
buffer.updateStreamWindow(streamOne, newSize: 20)
buffer.receivedFrames().assertFramesMatch([frame.sliceDataFrame(length: 20)])
// And again, we get the rest.
buffer.updateStreamWindow(streamOne, newSize: 20)
buffer.receivedFrames().assertFramesMatch([frame.sliceDataFrame(length: 15)])
}
func testConcurrentStreamErrorThrows() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
// We're going to create stream 1 and stream 11. Stream 1 will be passed through, stream 11 will have to wait.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let elevenFrame = HTTP2Frame(streamID: 11, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try buffer.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(elevenFrame, promise: nil, channelWritable: true).assertNothing())
// Now we're going to try to write a frame for stream 5. This will fail.
let fiveFrame = HTTP2Frame(streamID: 5, payload: .headers(.init(headers: HPACKHeaders([]))))
do {
_ = try buffer.processOutboundFrame(fiveFrame, promise: nil, channelWritable: true)
XCTFail("Did not throw")
} catch is NIOHTTP2Errors.StreamIDTooSmall {
// Ok
} catch {
XCTFail("Unexpected error: \(error)")
}
}
func testFlowControlStreamThrows() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
let streamOne = HTTP2StreamID(1)
let frame = self.createDataFrame(streamOne, byteBufferSize: 50)
XCTAssertThrowsError(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true)) {
guard let err = $0 as? NIOHTTP2Errors.NoSuchStream else {
XCTFail("Got unexpected error: \($0)")
return
}
XCTAssertEqual(err.streamID, streamOne)
}
}
func testDelayedFlowControlStreamErrors() {
// This is like testFlowControlStreamThrows, but we delay this behind a concurrentStreams buffer such that
// it appears during nextFlushedWritableFrame.
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
// Create two streams. The first proceeds, the second does not. The second stream is actually created with a data
// frame, which is a violation of what is required by the flow control buffer.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let dataFrame = self.createDataFrame(3, byteBufferSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing())
buffer.flushReceived()
buffer.nextFlushedWritableFrame(channelWritable: true).assertNoFrame()
// Ok, now we complete stream 1. This makes the next frame elegible for emission.
XCTAssertEqual(buffer.streamClosed(1).count, 0)
buffer.nextFlushedWritableFrame(channelWritable: true).assertError(NIOHTTP2Errors.NoSuchStream(streamID: 3))
}
func testBufferedFrameDrops() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
var results: [Bool?] = Array(repeating: nil as Bool?, count: 4)
var futures: [EventLoopFuture<Void>] = []
// Write in a bunch of frames, which will get buffered.
for streamID in [HTTP2StreamID(1), HTTP2StreamID(3)] {
let startFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: HPACKHeaders([]))))
let dataFrame = self.createDataFrame(streamID, byteBufferSize: 50)
let startPromise = self.loop.makePromise(of: Void.self)
let dataPromise = self.loop.makePromise(of: Void.self)
if streamID == 1 {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertForward())
startPromise.succeed(())
buffer.streamCreated(streamID, initialWindowSize: 65535)
} else {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertNothing())
}
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing())
futures.append(contentsOf: [startPromise.futureResult, dataPromise.futureResult])
}
for (idx, future) in futures.enumerated() {
future.map {
results[idx] = true
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.StreamClosed(streamID: 3, errorCode: .protocolError))
results[idx] = false
}
}
// Ok, mark the flush point.
buffer.flushReceived()
XCTAssertEqual(results, [true, nil, nil, nil])
// Now, fail stream one.
for promise in buffer.streamClosed(1) {
promise?.fail(NIOHTTP2Errors.StreamClosed(streamID: 3, errorCode: .protocolError))
}
XCTAssertEqual(results, [true, false, nil, nil])
// Now fail stream three.
for promise in buffer.streamClosed(3) {
promise?.fail(NIOHTTP2Errors.StreamClosed(streamID: 3, errorCode: .protocolError))
}
XCTAssertEqual(results, [true, false, false, false])
}
func testRejectsPrioritySelfDependency() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
XCTAssertThrowsError(try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: false, dependency: 1, weight: 36))) { error in
XCTAssertEqual(error as? NIOHTTP2Errors.PriorityCycle, NIOHTTP2Errors.PriorityCycle(streamID: 1))
}
XCTAssertThrowsError(try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: true, dependency: 1, weight: 36))) { error in
XCTAssertEqual(error as? NIOHTTP2Errors.PriorityCycle, NIOHTTP2Errors.PriorityCycle(streamID: 1))
}
}
func testBufferedFlushedFrameBufferingBehindFlowControl() {
// This is a test that hits an edge case occasionally spotted under high load.
// Specifically, this test catches the case where we have a flushed DATA frame buffered in the concurrent streams
// buffer that gets unbuffered from there, and rebuffered into the flow control buffer. In this instance, the
// buggy effect will be for this frame to get "stuck", as we were failing to propgate the fact that the frame
// is flushed.
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
// Shrink the connection window size.
buffer.connectionWindowSize = 5
// Now, prepare our four frames. We'll open a stream and consume the connection window, then
// attempt to open another stream and send a data frame. The second two frames must buffer.
let firstHeaders = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let firstData = self.createDataFrame(1, byteBufferSize: 5)
let secondHeaders = HTTP2Frame(streamID: 3, payload: .headers(.init(headers: HPACKHeaders([]))))
let secondData = self.createDataFrame(3, byteBufferSize: 5)
XCTAssertNoThrow(try buffer.processOutboundFrame(firstHeaders, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 65535)
XCTAssertNoThrow(try buffer.processOutboundFrame(firstData, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(try buffer.processOutboundFrame(secondHeaders, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(try buffer.processOutboundFrame(secondData, promise: nil, channelWritable: true).assertNothing())
// Now mark them flushed.
buffer.flushReceived()
// Now grab that first data frame and consume the connection window.
guard case .frame(let firstUnbufferedFrame, _) = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Did not unbuffer a data frame")
return
}
buffer.connectionWindowSize = 0
firstUnbufferedFrame.assertFrameMatches(this: firstData)
// Now complete the first stream.
XCTAssertEqual(buffer.streamClosed(1).count, 0)
// Ask for the next frame, which should be the headers.
guard case .frame(let secondUnbufferedFrame, _) = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Did not unbuffer a headers frame")
return
}
buffer.streamCreated(3, initialWindowSize: 65535)
secondUnbufferedFrame.assertFrameMatches(this: secondHeaders)
// The second frame must not unbuffer, as there is no room in the connection window.
guard case .noFrame = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Emitted a frame unexpectedly")
return
}
// Now increase the connection window size. This should free up the frame.
buffer.connectionWindowSize = 5
guard case .frame(let thirdUnbufferedFrame, _) = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Did not unbuffer a data frame")
return
}
thirdUnbufferedFrame.assertFrameMatches(this: secondData)
}
func testFailingAllPromisesOnClose() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
var results: [Bool?] = Array(repeating: nil as Bool?, count: 5)
var futures: [EventLoopFuture<Void>] = []
// Write in a bunch of frames, which will get buffered.
for streamID in [HTTP2StreamID(1), HTTP2StreamID(3)] {
let startFrame = HTTP2Frame(streamID: streamID, payload: .headers(.init(headers: HPACKHeaders([]))))
let dataFrame = self.createDataFrame(streamID, byteBufferSize: 50)
let startPromise = self.loop.makePromise(of: Void.self)
let dataPromise = self.loop.makePromise(of: Void.self)
if streamID == 1 {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertForward())
startPromise.succeed(())
buffer.streamCreated(streamID, initialWindowSize: 65535)
} else {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertNothing())
}
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing())
futures.append(contentsOf: [startPromise.futureResult, dataPromise.futureResult])
}
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 0), ack: false))
let pingPromise = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try buffer.processOutboundFrame(pingFrame, promise: pingPromise, channelWritable: false).assertNothing())
futures.append(pingPromise.futureResult)
for (idx, future) in futures.enumerated() {
future.map {
results[idx] = false
}.whenFailure { error in
XCTAssertEqual(error as? ChannelError, ChannelError.ioOnClosedChannel)
results[idx] = true
}
}
XCTAssertEqual(results, [false, nil, nil, nil, nil])
// Now, invalidate the buffer.
buffer.invalidateBuffer()
XCTAssertEqual(results, [false, true, true, true, true])
}
func testBufferedControlFramesAreEmittedPreferentiallyToOtherFrames() {
// We're going to buffera DATA frame and a control frame, then attempt to emit them and confirm they
// come out in the right order.
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
// Send a HEADERS frame through to open up a space in the flow control buffer.
let startFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders())))
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 65535)
// Send a DATA frame. This is buffered.
let dataFrame = self.createDataFrame(1, byteBufferSize: 50)
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing())
// Send in a PING frame which will be buffered.
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 9), ack: false))
XCTAssertNoThrow(try buffer.processOutboundFrame(pingFrame, promise: nil, channelWritable: false).assertNothing())
buffer.flushReceived()
// Now attempt to unbuffer the frames.
guard case .frame(let firstFrame) = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Didn't get expected frame")
return
}
firstFrame.0.assertFrameMatches(this: pingFrame)
guard case .frame(let secondFrame) = buffer.nextFlushedWritableFrame(channelWritable: true) else {
XCTFail("Didn't get expected frame")
return
}
secondFrame.0.assertFrameMatches(this: dataFrame)
}
}
extension CompoundOutboundBufferTest {
func createDataFrame(_ streamID: HTTP2StreamID, byteBufferSize: Int) -> HTTP2Frame {
var buffer = ByteBufferAllocator().buffer(capacity: byteBufferSize)
buffer.writeBytes(repeatElement(UInt8(0xff), count: byteBufferSize))
return HTTP2Frame(streamID: streamID, payload: .data(.init(data: .byteBuffer(buffer))))
}
}
extension CompoundOutboundBuffer {
fileprivate mutating func receivedFrames(file: StaticString = #file, line: UInt = #line) -> [HTTP2Frame] {
var receivedFrames: [HTTP2Frame] = Array()
loop: while true {
switch self.nextFlushedWritableFrame(channelWritable: true) {
case .noFrame:
break loop
case .frame(let bufferedFrame, let promise):
receivedFrames.append(bufferedFrame)
promise?.succeed(())
case .error(let promise, let error):
promise?.fail(error)
XCTFail("Caught error: \(error)", file: file, line: line)
}
}
return receivedFrames
}
}
extension CompoundOutboundBuffer.FlushedWritableFrameResult {
internal func assertNoFrame(file: StaticString = #file, line: UInt = #line) {
guard case .noFrame = self else {
XCTFail("Expected .noFrame, got \(self)", file: file, line: line)
return
}
}
internal func assertError<ErrorType: Error & Equatable>(_ error: ErrorType, file: StaticString = #file, line: UInt = #line) {
guard case .error(let promise, let thrownError) = self else {
XCTFail("Expected .error, got \(self)", file: file, line: line)
return
}
guard let castError = thrownError as? ErrorType, castError == error else {
XCTFail("Expected \(error), got \(thrownError)", file: file, line: line)
return
}
promise?.fail(error)
}
}