diff --git a/Sources/MWBCore/ClipboardCodec.swift b/Sources/MWBCore/ClipboardCodec.swift index 2e6b8cf..c9d2744 100644 --- a/Sources/MWBCore/ClipboardCodec.swift +++ b/Sources/MWBCore/ClipboardCodec.swift @@ -345,6 +345,11 @@ public final class Reassembler { /// one chunk of zero padding. public static let sizeLimit = ClipboardCodec.inBandLimit + Packet.clipboardChunkSize + /// How many chunk ids are remembered to collapse a peer pair's second + /// delivery. The same size as the session replay ring (`Receiver.cs:31`), + /// and for the same reason. + public static let idWindow = ReplayFilter.windowSize + /// True when the payload grew past `sizeLimit` and was thrown away. public private(set) var abandoned = false /// Whether the chunks seen so far are image bytes. @@ -353,6 +358,7 @@ public final class Reassembler { public private(set) var lastChunk: TimeInterval private var payload = Data() + private var recentIDs: [Int32] = [] /// An empty reassembly, started at `now`. public init(now: TimeInterval) { @@ -368,7 +374,7 @@ public final class Reassembler { switch packet.type { case .clipboardText, .clipboardImage: isImage = packet.type == .clipboardImage - guard !abandoned else { return nil } + guard !abandoned, !isRepeat(packet.id) else { return nil } payload.append(packet.payload48) if payload.count > Reassembler.sizeLimit { abandoned = true @@ -382,4 +388,23 @@ public final class Reassembler { return nil } } + + /// True when this chunk has already been appended. + /// + /// A machine pair keeps **two** control connections (docs/protocol.md §10.3 + /// note 4) and the session replay ring deliberately exempts + /// `ClipboardText` and `ClipboardImage` (`Receiver.cs:40-82`), so every + /// chunk is delivered twice. Windows never notices: its receive loop blocks + /// on the one socket the first chunk arrived on and reassembles from that + /// socket alone (`Clipboard.cs:308-373`), so the second copy becomes a + /// separate, parallel reassembly. Ours is keyed by sending machine, which + /// is what makes the interleaving visible — and the sender stamps one `Id` + /// per chunk for both deliveries (contracts C1), so the `Id` tells the copy + /// from the original. + private func isRepeat(_ id: Int32) -> Bool { + if recentIDs.contains(id) { return true } + recentIDs.append(id) + if recentIDs.count > Reassembler.idWindow { recentIDs.removeFirst() } + return false + } } diff --git a/docs/features/clipboard/README.md b/docs/features/clipboard/README.md index 86b112a..e212ce5 100644 --- a/docs/features/clipboard/README.md +++ b/docs/features/clipboard/README.md @@ -33,7 +33,11 @@ Confirmed or corrected while porting; every correction is written up in `ClipboardDataEnd (76)`, last chunk zero-padded with no length field (`Clipboard.cs:253-306`). They are app-stamped and forwarded (C1). The receiver keeps one `Reassembler` per sender; trailing zeros are - part of the DEFLATE stream and the decoder tolerates them. The + part of the DEFLATE stream and the decoder tolerates them. **Each chunk + arrives twice** — a machine pair has two connections and the replay + ring exempts these two types — so `Reassembler` keeps its own ring of + chunk `Id`s; keying reassembly by sender rather than by socket is what + makes the duplicate visible (§10.7 note 11). The "abort after 100 interleaved foreign packets" rule is **not** ported — it is a property of the Windows blocking read loop, which we do not have; a size cap and a 30 s idle timeout take its place (§10.7 note 9). @@ -155,7 +159,7 @@ public enum ClipboardCodec { public static func string(fromUTF16LE:) throws -> String } public final class Reassembler { // one per sending machine - public static let sizeLimit: Int + public static let sizeLimit, idWindow: Int public init(now: TimeInterval) public func feed(_ packet: Packet, now: TimeInterval) -> Data? // payload on DataEnd public private(set) var abandoned, isImage: Bool @@ -409,6 +413,13 @@ was not needed. - The "100 interleaved foreign packets" abort became a size cap plus an idle timeout: we have no blocking read loop for foreign packets to interleave with (§10.7 note 9). +- `Reassembler` de-duplicates by chunk `Id`. Found while re-reading the + first pass: a peer pair has two control connections, `SkSend` writes + the same chunk to both, and `PreProcess` exempts `ClipboardText` and + `ClipboardImage` from the replay ring — so a reassembly keyed by + sending machine, as ours is, interleaves the two copies and corrupts + the payload where Windows's socket-keyed loop merely does the work + twice (§10.7 note 11). - The download folder is created on the first received file, not at startup, so an app that never receives one leaves the Desktop alone. @@ -431,8 +442,9 @@ clipboard, no listener outside a few seconds on 127.0.0.1:51599): prefix; the 1024-byte header round trip including a Windows path, the `0*` error form and `..\..` traversal reduced to its last component; padding block-aligned for 0, 1, 15, 16, 31, 32, 119 and - 1 048 577; the `Reassembler` size cap; an untagged blob read as plain - text. All pass. + 1 048 577; the `Reassembler` size cap; the same chunk stream delivered + twice, interleaved, collapsing to one payload; an untagged blob read as + plain text. 170 checks, all pass. - a two-channel loopback harness on 127.0.0.1:51599, up for six seconds: a text payload pulled and decoded correctly; a 5 000-byte file pushed and staged with the right size and name; a 3 MB image pulled across diff --git a/docs/protocol.md b/docs/protocol.md index eed1e4e..220d567 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -1051,3 +1051,20 @@ sees them. One subsection per lane, appended, never rewritten. 24 000 packets of it. The helper refuses to read text over 20 MB or an image over 50 MB off the clipboard in the first place (`FormHelper.cs:28-29, 206, 287`). +11. **Every in-band clipboard chunk is delivered twice, and only the Windows + receive loop's shape hides it.** A machine pair keeps two control + connections (§10.3 note 4) and `SkSend` writes the same bytes with the same + `Id` to both (§10.2 note 11), while `Receiver.PreProcess` exempts + `ClipboardText` and `ClipboardImage` from the 50-entry replay ring + (`Receiver.cs:40-82`). On Windows the first chunk to arrive hands its + *socket* to `ReceiveClipboardDataUsingTCP`, which then reads the rest of the + transfer from that socket alone (`Clipboard.cs:308-373`) — so the second + copy becomes a separate, parallel reassembly on the other socket's thread + and ends as a second, identical clipboard write. A port whose reassembly is + keyed by *sending machine* rather than by socket instead sees the two copies + interleave into one buffer and corrupt it. `Reassembler` therefore keeps its + own 50-entry ring of chunk `Id`s — which works precisely because contracts + C1 stamps one `Id` per chunk for both deliveries. `ClipboardDataEnd (76)` is + **not** exempt from the replay ring, so exactly one terminator arrives; a + copy that arrives wholly after the terminator starts a reassembly that can + never finish and is dropped by the idle timeout.