From 86877eab84bc33cbc2a2fa4b522e3a2474b28cec Mon Sep 17 00:00:00 2001 From: Daniel Alm Date: Tue, 5 Mar 2019 13:19:57 +0100 Subject: [PATCH] [nghttp2] Work around two possible crashes. (#50) Context: Our services received (invalid) HTTP2 streams with a disallowed `connection` header. This caused a `nghttp2 error: Invalid HTTP header field was received: frame type: 1, stream: 23, name: [connection], value: [keep-alive]` to be logged, but also caused our serving process to crash subsequently in `onStreamCloseCallback`. After that was worked around, `doOneWrite` was called with a negative length. This patch does _not_ fix the underlying issue, it only prevents a hard crash of the process until we can migrate to the non-`nghttp2` implementation. --- Sources/NIOHTTP2/NGHTTP2Session.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/NIOHTTP2/NGHTTP2Session.swift b/Sources/NIOHTTP2/NGHTTP2Session.swift index 1ecc518..96644a5 100644 --- a/Sources/NIOHTTP2/NGHTTP2Session.swift +++ b/Sources/NIOHTTP2/NGHTTP2Session.swift @@ -490,7 +490,9 @@ class NGHTTP2Session { /// Called when the stream `streamID` is closed. fileprivate func onStreamCloseCallback(streamID: Int32, errorCode: UInt32) throws { - let streamData = self.streamIDManager.getStreamData(for: streamID)! + guard let streamData = self.streamIDManager.getStreamData(for: streamID) else { + throw NIOHTTP2Errors.NoSuchStream(streamID: HTTP2StreamID(knownID: streamID)) + } // If we have a data provider, pull it out. let error = NIOHTTP2Errors.StreamClosed(streamID: streamData.streamID, @@ -648,7 +650,7 @@ class NGHTTP2Session { let length = nghttp2_session_mem_send(self.session, &data) // TODO(cory): I think this mishandles DATA frames: they'll say 0, but there may be more frames to // send. Must investigate. - guard length != 0 else { + guard length > 0 else { return .noWrite } var buffer = self.allocator.buffer(capacity: length)