From 2373863f548556ee5dffbc45a1ddf1954f2f6851 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 12 Dec 2017 11:31:05 +0000 Subject: [PATCH] No fatal error on unexpected non-ciphertext data --- Sources/NIO/Embedded.swift | 2 +- Sources/NIOOpenSSL/OpenSSLHandler.swift | 3 +- Sources/NIOOpenSSL/SSLErrors.swift | 1 + .../OpenSSLIntegrationTest+XCTest.swift | 1 + .../OpenSSLIntegrationTest.swift | 43 +++++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 89dba7fdb..21a22d6fc 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -230,7 +230,7 @@ public class EmbeddedChannel : Channel { return !channelcore.outboundBuffer.isEmpty } - private func throwIfErrorCaught() throws { + public func throwIfErrorCaught() throws { if let error = channelcore.error { channelcore.error = nil throw error diff --git a/Sources/NIOOpenSSL/OpenSSLHandler.swift b/Sources/NIOOpenSSL/OpenSSLHandler.swift index 2a774148d..71b98b8b0 100644 --- a/Sources/NIOOpenSSL/OpenSSLHandler.swift +++ b/Sources/NIOOpenSSL/OpenSSLHandler.swift @@ -109,7 +109,8 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { case .closing: doShutdownStep(ctx: ctx) default: - fatalError("Read during invalid TLS state") + ctx.fireErrorCaught(error: NIOOpenSSLError.readInInvalidTLSState) + channelClose(ctx: ctx) } } diff --git a/Sources/NIOOpenSSL/SSLErrors.swift b/Sources/NIOOpenSSL/SSLErrors.swift index 754051eb7..ab5554533 100644 --- a/Sources/NIOOpenSSL/SSLErrors.swift +++ b/Sources/NIOOpenSSL/SSLErrors.swift @@ -61,6 +61,7 @@ public enum NIOOpenSSLError: Error { case noCertificateToValidate case unableToValidateCertificate case cannotFindPeerIP + case readInInvalidTLSState } extension NIOOpenSSLError: Equatable { diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift index 6955bfc30..4289e5173 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift @@ -39,6 +39,7 @@ extension OpenSSLIntegrationTest { ("testDontLoseClosePromises", testDontLoseClosePromises), ("testTrustStoreOnDisk", testTrustStoreOnDisk), ("testChecksTrustStoreOnDisk", testChecksTrustStoreOnDisk), + ("testReadAfterCloseNotifyDoesntKillProcess", testReadAfterCloseNotifyDoesntKillProcess), ] } } diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift index 53df9236d..171eff26c 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift @@ -802,4 +802,47 @@ class OpenSSLIntegrationTest: XCTestCase { XCTAssertEqual(actualErrors.count, 1) try clientChannel.closeFuture.wait() } + + func testReadAfterCloseNotifyDoesntKillProcess() throws { + let serverChannel = EmbeddedChannel() + let clientChannel = EmbeddedChannel() + + let ctx = try configuredSSLContext() + + try serverChannel.pipeline.add(handler: try OpenSSLServerHandler(context: ctx)).wait() + try clientChannel.pipeline.add(handler: try OpenSSLClientHandler(context: ctx)).wait() + + let addr: SocketAddress = try .unixDomainSocketAddress(path: "/tmp/whatever2") + let connectFuture = clientChannel.connect(to: addr) + serverChannel.pipeline.fireChannelActive() + try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel) + try connectFuture.wait() + + // Ok, we're connected. Now we want to close the server, and have that trigger a client CLOSE_NOTIFY. + // However, when we deliver that CLOSE_NOTIFY we're then going to immediately send another chunk of + // data. We can get away with doing this because the Embedded channel fires any promise for close() + // before it fires channelInactive, which will allow us to fire channelRead from within the callback. + let closePromise = serverChannel.close() + closePromise.whenComplete { _ in + var buffer = serverChannel.allocator.buffer(capacity: 5) + buffer.write(staticString: "hello") + serverChannel.pipeline.fireChannelRead(data: NIOAny(buffer)) + serverChannel.pipeline.fireChannelReadComplete() + } + + do { + try serverChannel.throwIfErrorCaught() + } catch { + XCTFail("Already has error: \(error)") + } + + do { + try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel) + XCTFail("Did not cause error") + } catch NIOOpenSSLError.readInInvalidTLSState { + // Nothing to do here. + } catch { + XCTFail("Encountered unexpected error: \(error)") + } + } }