diff --git a/Sources/MWBCore/Session.swift b/Sources/MWBCore/Session.swift index 9725a16..8fc42ec 100644 --- a/Sources/MWBCore/Session.swift +++ b/Sources/MWBCore/Session.swift @@ -104,7 +104,7 @@ public final class Session { public static let slowHeartbeat: TimeInterval = 300 /// Untrusted packets tolerated before the key is declared wrong - /// (`SocketStuff.cs:1390`). + /// (`SocketStuff.cs:1389`). public static let untrustedPacketLimit = 10 private let config: SessionConfig @@ -117,7 +117,7 @@ public final class Session { private var pending: [SessionEvent] = [] /// -1 once the peer is trusted, as `packageCount` is on Windows - /// (`SocketStuff.cs:1441`). + /// (`SocketStuff.cs:1439`). private var untrustedCount = 0 private var expectedNonce: (UInt32, UInt32, UInt32, UInt32) = (0, 0, 0, 0) private var receiveErrors = 0 @@ -311,7 +311,7 @@ public final class Session { countUntrusted(packet) } - /// The untrusted branch of `MainTCPRoutine` (`SocketStuff.cs:1386-1500`). + /// The untrusted branch of `MainTCPRoutine` (`SocketStuff.cs:1387-1529`). private func countUntrusted(_ packet: Packet) { untrustedCount += 1 if untrustedCount >= Session.untrustedPacketLimit { @@ -354,7 +354,7 @@ public final class Session { return } - untrustedCount = -1 // Trusted (SocketStuff.cs:1441) + untrustedCount = -1 // Trusted (SocketStuff.cs:1439) peerName = name peerID = packet.src report(.connected) diff --git a/docs/features/session-matrix/README.md b/docs/features/session-matrix/README.md index c19324a..989e9b6 100644 --- a/docs/features/session-matrix/README.md +++ b/docs/features/session-matrix/README.md @@ -479,6 +479,100 @@ No macOS permission was requested, no `CGEvent` was posted, no pasteboard was touched. Nothing in these files logs, so no key material can leak through one. +### 2026-09-16 — dev (Opus), rework round + +`tools/gates.sh` green. One commit per item; no wire-core file, no +`docs/topo.md`, no `TODO.md` section but §2 was touched. + +**QA finding 1 — read errors must spend the untrusted budget.** Confirmed +and ported exactly. The mechanism QA identified is real and is worse than +"a separate counter": `TcpReceiveData` never signals a bad read out of +band, it *overwrites the type byte of the buffer it was filling* — +`PackageType.Error` when the read returned 0 bytes, `PackageType.Invalid` +when it returned a short count, for both the 32-byte and the 64-byte half +(`SocketStuff.cs:574-578, 588-593`). The result is an ordinary package and +`MainTCPRoutine` runs it through the whole loop body. So three things had +to change, not one: + +- `errCount` counts only the `Error` (0-byte) kind and closes with + `SocketStatus.Error` on the sixth in a row (`:1332-1364`); **any** other + type resets it, including a short read's `Invalid` (`:1366-1368`). +- Whatever `errCount` did, the package then falls through + `Type == Handshake` into the untrusted branch (`:1370-1529`) and spends + a `packageCount`, so five malformed reads raise `.invalidKey` and ten + send the ten `Invalid` packets and close. +- The trusted branch is guarded by `receivedCount > 0` (`:1531`), so a + 0-byte read never reaches `Receiver.ProcessPackage` at all. + +`Session` now has one private `receive(_:bytesReceived:)` that *is* the +loop body; `feed` and the renamed `noteReceiveError(bytesReceived:)` both +go through it, the latter by building the same synthetic `.error` / +`.invalid` packet Windows builds. The public signature changed (it takes +the byte count now) — flagged to control-channel in Handshakes, and the +caller must pass the real count rather than collapsing a short read into +a 0-byte one. Written up as protocol §10.2 note 10. Two consequences +worth stating plainly because they are counter-intuitive: a run of +*short* reads ends in `.invalidKey`, while a run of six *empty* reads +still ends in `.error` (the `errCount` limit is reached first) — both are +Windows behaviour and both now fall out of the same code. + +**QA finding 2 — `learn` persisted on every heartbeat.** Confirmed, and +the citation is exact: `AddToMachinePool` returns inside the +`TryFindMachineByName` branch at `MachineStuff.cs:737`, and the +`UpdateMachinePoolStringSetting()` at `:755` sits after the whole +if/else, reachable only by falling through the new-machine branch. The +known-machine branch now returns `[]`. It still refreshes the id and +timestamp in memory and still re-points `desMachineID` when the machine +being controlled changed id (`:731-735`) — that part is not persistence +and Windows does it there. The "5th machine" refusal already returned +`[]` and is unchanged. + +**QA finding 3 — `parkCursor` did not carry the gate.** Confirmed; the +consequence QA described (the local cursor jerking to the top of the +screen every time control comes home, e.g. on every `ByeBye` via +`Common.cs:622-625`) is exactly what a naive consumer would have built. +Took the stronger of the two remedies QA offered: the action's shape +changed rather than only its doc comment, to +`.parkCursor(destinationIsRemote: Bool, byHideMouseMessage: Bool)`. Both +halves of `Common.cs:1488` are now pre-evaluated by `Switcher` (new +public `destinationIsRemote`, `desMachineID != localID && != ALL`), read +*after* the switch is applied because `SwitchToMachine` calls +`HideMouseCursor(false)` after setting `DesMachineID` +(`MachineStuff.cs:1025`). The rule "record the position always, move the +OS cursor only when `destinationIsRemote || byHideMouseMessage`" is now a +consumer rule in Handshakes, so `input-inject` can implement +`HideMouseCursor` without opening the Windows source. + +**Contract C1 — the API cannot send an unstamped application packet.** +`Session.send(_:)` is gone. The per-connection stamp survives only as +`private func originate(_:)`, used by exactly the four things Windows +also sends per connection (`Handshake`/`HandshakeAck` via `sendDirect`, +the heartbeat on `tick` and the one written on a fresh connection at +`SocketStuff.cs:1451`, `Hi`, `ByeBye`). Application traffic has one way +in — `forward(_:)`, which fills `Src` if it is `NONE` and frames, and +never touches `Id`. This is now enforced by the type rather than by a +README sentence. Added protocol §10.2 note 11 recording *why*: `SkSend` +increments `Package.PackageID` once, before the socket loop +(`Common.cs:914`), and hands the same `DATA` to every matching socket +(`:919-929`), which is the whole reason the receiver's 50-entry replay +ring collapses a peer pair's duplicate delivery. A per-connection stamp +would give the duplicate a fresh `Id` and defeat the ring — so this was +a latent wire-behaviour bug, not only an API-shape preference. + +**Compliance re-read of all seven files.** No force-unwrap, `try!`, +`fatalError` or `precondition` anywhere (the risky constructs are +`try? packet.setMachineName(...)`, deliberate: an over-long or non-ASCII +name is dropped from its field rather than failing the send, README D7). +Longest file is `Session.swift` at 422 lines, longest function +`countUntrusted` at 26 — both well inside the CLAUDE.md targets. Nothing +logs. Four stale `SocketStuff.cs` line citations were off by one or two +against the reference checkout (`:1390`→`:1389`, `:1441`→`:1439`, +the untrusted branch `:1386-1500`→`:1387-1529`) and were corrected; +`xcrun swift-format format -i` made no further changes. + +No macOS permission was requested, no `CGEvent` posted, no pasteboard +touched. + ## QA log ### 2026-09-16 — QA (Sonnet)