diff --git a/NearbyShare/Connection/InboundNearbyConnection.swift b/NearbyShare/Connection/InboundNearbyConnection.swift index c7434fd..e22d61b 100644 --- a/NearbyShare/Connection/InboundNearbyConnection.swift +++ b/NearbyShare/Connection/InboundNearbyConnection.swift @@ -127,7 +127,21 @@ class InboundNearbyConnection: NearbyConnection { case .sentPairedKeyResult: try processPairedKeyResultFrame(frame) case .receivedPairedKeyResult: - try processIntroductionFrame(frame) + if frame.hasV1, frame.v1.hasIntroduction { + try processIntroductionFrame(frame) + return + } + if frame.hasV1, frame.v1.hasType { + if frame.v1.type == .pairedKeyResult || frame.v1.type == .pairedKeyEncryption || frame.v1.type == .progressUpdate { + log("[InboundNearbyConnection \(self.id)] Ignoring duplicate/intermediate transfer setup frame \(frame.v1.type) while waiting for introduction.") + return + } + if frame.v1.type == .response, case .accept = frame.v1.connectionResponse.status { + log("[InboundNearbyConnection \(self.id)] Ignoring accept response while waiting for introduction.") + return + } + } + throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") default: if frame.hasV1, frame.v1.hasType, frame.v1.type == .progressUpdate { // ignore progress updates diff --git a/NearbyShare/Connection/NearbyConnection.swift b/NearbyShare/Connection/NearbyConnection.swift index 97fff66..b04d26d 100644 --- a/NearbyShare/Connection/NearbyConnection.swift +++ b/NearbyShare/Connection/NearbyConnection.swift @@ -206,7 +206,7 @@ class NearbyConnection { private func receiveFrameAsync(length: UInt32) { - connection.receive(minimumIncompleteLength: Int(length), maximumLength: Int(length)) { [self] content, _, isComplete, _ in + connection.receive(minimumIncompleteLength: Int(length), maximumLength: Int(length)) { [self] content, _, isComplete, error in if self.connectionClosed { log("[NearbyConnection \(self.id)] Received FIN from peer, connection closed.") connection.cancel() @@ -237,11 +237,23 @@ class NearbyConnection { return } + if let error = error { + log("[NearbyConnection \(self.id)] Error during receiveFrameAsync(length:): \(error)") + self.lastError = error + self.protocolError() + return + } guard let content = content else { log("[NearbyConnection \(self.id)] Received nil content during receiveFrameAsync(length:). IsComplete: \(isComplete)") self.protocolError() return } + guard content.count == Int(length) else { + log("[NearbyConnection \(self.id)] Dropping partial frame body. Expected \(length) bytes, got \(content.count)") + self.lastError = NearbyError.protocolError("Unexpected frame body length") + self.protocolError() + return + } self.startAndResetHeartbeatTimer() self.processReceivedFrame(frameData: content)