Safer connection handling

This commit is contained in:
Leon Böttger authored and Leon Böttger committed 2026-03-08 15:18:07 +01:00
1 parent 3ed93e6079
commit e0407d9398
2 files changed
+28 -2

No files matched your search

@@ -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
+13 -1
View File
@@ -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)