From ca9acc113c4fbbe73fd13b9b4887a7f1fcda1737 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 27 Sep 2018 11:52:44 +0100 Subject: [PATCH 1/2] Fix: respond with correct reason code --- .../KituraWebSocket/WebSocketConnection.swift | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Sources/KituraWebSocket/WebSocketConnection.swift b/Sources/KituraWebSocket/WebSocketConnection.swift index a91aaf1..3a59d92 100644 --- a/Sources/KituraWebSocket/WebSocketConnection.swift +++ b/Sources/KituraWebSocket/WebSocketConnection.swift @@ -254,7 +254,8 @@ public class WebSocketConnection { return } } else if frame.payload.length == 0 { - reasonCode = .noReasonCodeSent + connectionClosed(reason: .noReasonCodeSent, reasonToSendBack: .normal) + return } else { connectionClosed(reason: .protocolError, description: "Close frames, which contain a payload, must be between 2 and 125 octets inclusive") return @@ -286,15 +287,17 @@ public class WebSocketConnection { } case .ping: - guard frame.payload.length < 126 else { - connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets") - return - } - guard frame.finalFrame else { - connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented") - return + if active { + guard frame.payload.length < 126 else { + connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets") + return + } + guard frame.finalFrame else { + connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented") + return + } + sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length) } - sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length) case .pong: break @@ -380,7 +383,7 @@ public class WebSocketConnection { } if abs(lastFrameReceivedAt.timeIntervalSinceNow) > (Double(connectionTimeout) * 0.4) { if abs(lastFrameReceivedAt.timeIntervalSinceNow) > (Double(connectionTimeout)) { - strongSelf.connectionClosed(reason: .closedAbnormally) + strongSelf.connectionClosed(reason: .closedAbnormally, description: "Client failed to respond to a heartbeat ping with a pong", reasonToSendBack: .protocolError) return } strongSelf.ping() From b5e0218ac98d766f876d290df64ebdac2f59c455 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 27 Sep 2018 13:09:45 +0100 Subject: [PATCH 2/2] added guard for active check --- .../KituraWebSocket/WebSocketConnection.swift | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/KituraWebSocket/WebSocketConnection.swift b/Sources/KituraWebSocket/WebSocketConnection.swift index 3a59d92..092ed1a 100644 --- a/Sources/KituraWebSocket/WebSocketConnection.swift +++ b/Sources/KituraWebSocket/WebSocketConnection.swift @@ -287,17 +287,17 @@ public class WebSocketConnection { } case .ping: - if active { - guard frame.payload.length < 126 else { - connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets") - return - } - guard frame.finalFrame else { - connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented") - return - } - sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length) + guard active else { break } + guard frame.payload.length < 126 else { + connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets") + return + } + guard frame.finalFrame else { + connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented") + return } + sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length) + case .pong: break