Motivation:
As part of the work in #214, HTTP2StreamID will need to be able to
tolerate not knowing its stream ID. For now this is not a problem, but
we should think about everywhere we use the stream ID and make sure we
have a solid principle in mind. This lays the groundwork for future
enhancements.
Modifications:
- Update HTTP2StreamChannel to hold an optional stream ID.
- Update interface to HTTP2StreamMultiplexer to tolerate closing
channels without stream IDs (we'll need it later).
- Move the "get a new stream ID" functionality to a method on
HTTP2StreamMultiplexer.
- Validate we behave correctly with optional stream IDs in the channel.
Result:
We can tolerate stream IDs being not present.
* 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.
Motivation:
RFC 7540 requires that compliant specifications emit errors if content-length has been incorrectly
set. We should have this behaviour by default, though we should allow it to be disabled if necessary.
Modifications:
- Added content-length checking.
- Added flag to disable content-length checking.
Result:
Better RFC 7540 compliance.
Motivation:
RFC 7540 forbids the use of connection-specific header fields, except for TE sent
with the value "trailers". We should police that.
Modifications:
- Added extra policing around regular header fields.
Result:
Better validation
Motivation:
RFC 7540 does not allow empty :path pseudo-headers. We should police this.
Modifications:
- Added a check that :path isn't empty.
Result:
Better RFC 7540 validation.
Motivation:
HTTP/2 header blocks have a certain set of pseudo-headers that are mandatory
and allowed on them. We currently don't enforce that, which makes it possible to send
malformed requests or responses, as well as to have to handle them. That's not great.
Modifications:
- Added enforcement of the allowed pseudo-header fields.
Result:
Better pseudo-header field enforcement.
Motivation:
RFC 7540 requires that HTTP/2 pseudo-headers must be first in a HTTP/2
header block, and must occur before any regular headers. We should enforce
that requirement.
Modifications:
- Added checking that pseudo-headers are first in a header block.
Result:
Better RFC 7540 validation.
Motivation:
RFC 7540 forbids uppercase characters in header field names, but we
currently allow them. We shouldn't.
This is the first in a series of patches that will validate header blocks
when reasonable. As a result, it seems larger than necessary, as it adds
a bunch of scaffolding for extending with further validation code.
Modifications:
- Added header field validation scaffolding.
- Validates that header field names do not contain uppercase chars.
- Allows sending RST_STREAM frame before stream setup.
Result:
Better header field validation.
Motivation:
RFC 7540 requires that the END_STREAM bit be set on a HEADERS frame that
sends trailers. We should enforce that.
Modifications:
- Require that the END_STREAM bit is set on trailers.
Result:
We reject more incorrect flows.
Motivation:
In the future we'll want to use PRIORITY frames to provide feedback on
how we emit data. Additionally, RFC 7540 forbids certain kinds of
PRIORITY frame, which right now we tolerate receiving. We can design a
minor PRIORITY shim to ensure that we have a code path for PRIORITY data
to be passed around, and then use this shim to enforce RFC 7540's
requirements.
Modifications:
- Added support for PRIORITY frame data inbound.
- Enforce that frames may not establish self-dependency.
Result:
Better policing of RFC 7540 constraints.
Motivation:
Receiving a CONTINUATION frame at the wrong stage of a connection is
a PROTOCOL_ERROR, but out of an abundance of caution we were categorising
it as an INTERNAL_ERROR. We should be more confident about our error
categories.
Modifications:
- Changed INTERNAL_ERROR to PROTOCOL_ERROR.
- Added a test for this error case.
Result:
More confidence, more style, more pizazz.
Motivation:
When we spiked out these codecs we were missing a number of error handling
paths, which meant that erroneous input would lead to crashes. While we
don't expect these handlers to have to face that kind of input, we should
still aim to tolerate it where doing so is reasonable.
Modifications:
- Replaced all force unwraps and preconditions with throwing.
Result:
Fewer crashes on bad input.
* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.
* Add pure-Swift HTTP/2 protocol implementation.
Motivation:
Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.
This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.
Modifications:
- Add pure Swift protocol implementations.
- Test them.
Results:
Another step towards protocol independence.
* Initial basic parser code.
* Adding unit tests.
* Add NIOHTTP2FlowControlHandler
Motivation:
One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.
Modifications:
- Added NIOHTTP2FlowControlHandler.
Result:
Users will have the same feature set as they did with nghttp2.
* Generate Linux test information.
* Documentation comment.
* Updated new tests from master to use new payload types.
* Updated following review.
Notable changes:
- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.
* Updated Linux tests.
* Headers placed in relevant files.
* Add pure-Swift HTTP/2 protocol implementation.
Motivation:
Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.
This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.
Modifications:
- Add pure Swift protocol implementations.
- Test them.
Results:
Another step towards protocol independence.
* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.
* Updated Linux tests.
* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.
* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)
By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.
(cherry picked from commit 88bd6d369a)
* Minor tweaks to get the tests passing
* Initial integration here
* Minor clearing up of the handler
* Get the tests compiling
* Fix header issues.
We now pass the frame parser to the various setting operations to let it
update the state. We also fixed some issues with specific settings.
* Remove ngthttp2-based impl, clean up stream ID
* Tests with new HTTP2StreamID
* Simple test cleanup
* Get remaining non-core tests passing
* Allow GOAWAY even when fully quiesced
* We don't buffer outbound frames any more
* Emit StreamClosed events on GoAway frame
* Notify stream creation and closure
* This test started failing
* Goodbye, C code! 🎉
* Send PING ACK automatically
* Updated based on PR feedback.
* Correctly handle 1XX responses
* Add SETTINGS_MAX_FRAME_SIZE support to flow controller
* Correctly account for flow control
* Define ConcurrentStreamsHandler
* Prepare for signalling connection state changes
* Better state change message propagation
* Refactor to use result with effect everywhere
* Don't lose flow control info on stream error
* Add basic flow control management to stream multiplexer
* Better pipeline in sample server
* Clean up some reentrancy concerns
* Move to Swift 5 and NIO 2
* Move to new Swift 5 for CI
* Allow wider range of choices of years
* Catch up to NIO 2 again
Motivation:
We shouldn't just crash if the client sends bad magic.
Changes:
Send an error through the pipeline instead of crashing on bad client
magic.
Result:
fewer crashes
Motivation
The most common mechanism for negotiating HTTP/2 is to use ALPN.
We should support that.
Modifications:
Added a `ChannelPipeline` extension to configure a pipeline for
ALPN-based upgrade.
Added a property that indicates the ALPN tokens for HTTP/2 and
HTTP/1.1 use.
Result:
Should be possible to configure a channel pipeline for HTTP/2 work.