From 51450961db9f979e8d132704161e12b6d18ccde2 Mon Sep 17 00:00:00 2001 From: "Mark E. DeYoung" <1854350+mark-e-deyoung@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:42:38 +0100 Subject: [PATCH 1/3] R059: require exact WinInspect wire handshake and boolean status --- api/core/wininspect.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/api/core/wininspect.py b/api/core/wininspect.py index a09209a1..2c870842 100644 --- a/api/core/wininspect.py +++ b/api/core/wininspect.py @@ -14,6 +14,9 @@ DEFAULT_HOST = "127.0.0.1" DEFAULT_PORT = 1985 +# Wire protocol and product release are independent. Never infer either from +# the other; the actual provider artifact is a separate qualification gate. +WININSPECT_PROTOCOL_VERSION = "0.4.0" DEFAULT_TIMEOUT_SECONDS = 3.0 MAX_MESSAGE_SIZE = 10 * 1024 * 1024 READ_ONLY_METHODS = { @@ -278,6 +281,10 @@ def _write_frame(sock: socket.socket, payload: dict[str, Any]) -> None: def request(method: str, params: dict[str, Any] | None = None) -> dict[str, Any]: if method not in READ_ONLY_METHODS and method not in MUTATION_METHODS: raise WinInspectError(f"WinInspect method is not allowed by WineBot: {method}") + # Deny a caller-supplied protocol override before process or socket effects. + if params is not None and "protocol_version" in params: + if params["protocol_version"] != WININSPECT_PROTOCOL_VERSION: + raise WinInspectError("WinInspect request protocol mismatch") state = ensure_daemon(start=True) if not state.get("running"): raise WinInspectError(str(state.get("error") or "WinInspect daemon is not running")) @@ -287,20 +294,26 @@ def request(method: str, params: dict[str, Any] | None = None) -> dict[str, Any] "id": request_id, "method": method, "params": { - "protocol_version": "0.4.0", **(params or {}), + "protocol_version": WININSPECT_PROTOCOL_VERSION, }, } with socket.create_connection((host(), port()), timeout=timeout_seconds()) as sock: sock.settimeout(timeout_seconds()) hello = _read_frame(sock) + if (hello.get("type") != "hello" + or hello.get("version") != WININSPECT_PROTOCOL_VERSION): + # Do not expose untrusted version/identity data or send RPC bytes. + raise WinInspectError("WinInspect hello protocol mismatch") _write_frame(sock, payload) response = _read_frame(sock) if response.get("id") != request_id: # Never expose untrusted daemon hello identity in API-visible errors. raise WinInspectError("WinInspect response correlation failed") - if not response.get("ok"): + if type(response.get("ok")) is not bool: + raise WinInspectError("WinInspect response status invalid") + if response["ok"] is False: error = response.get("error") or {} if isinstance(error, dict): code = error.get("code") From 8d3daa92e66399154afbc60a1842ec2192af5cf0 Mon Sep 17 00:00:00 2001 From: "Mark E. DeYoung" <1854350+mark-e-deyoung@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:42:41 +0100 Subject: [PATCH 2/3] R059: prove wrong protocol refuses before effects --- tests/test_wininspect_error_redaction.py | 82 +++++++++++++++++++++--- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/tests/test_wininspect_error_redaction.py b/tests/test_wininspect_error_redaction.py index 76649b55..9999656b 100644 --- a/tests/test_wininspect_error_redaction.py +++ b/tests/test_wininspect_error_redaction.py @@ -10,13 +10,15 @@ class Socket: - def __init__(self, response): - hello = { - "type": "hello", "version": "0.3.0", - "hostname": "SYNTHETIC_HOST_NEVER_ECHO", - "name": "SYNTHETIC_PERSON_NEVER_ECHO", - "uuid": "SYNTHETIC_UUID_NEVER_ECHO", - } + def __init__(self, response, hello=None): + if hello is None: + hello = { + "type": "hello", "version": "0.4.0", + "hostname": "SYNTHETIC_HOST_NEVER_ECHO", + "name": "SYNTHETIC_PERSON_NEVER_ECHO", + "uuid": "SYNTHETIC_UUID_NEVER_ECHO", + } + self.sent = bytearray() self.incoming = bytearray() for item in (hello, response): raw = json.dumps(item).encode() @@ -31,7 +33,7 @@ def settimeout(self, timeout): pass def sendall(self, value): - pass + self.sent.extend(value) def __enter__(self): return self @@ -57,3 +59,67 @@ def test_mismatched_correlation_refuses_without_hello_disclosure(bad_reply): for token in ("SYNTHETIC_HOST", "SYNTHETIC_PERSON", "SYNTHETIC_UUID", "different-request"): assert token not in str(error.value) + + + +@pytest.mark.parametrize("hello", [ + {}, + {"type": "not-hello", "version": "0.4.0"}, + {"type": "hello", "version": "0.3.0"}, + {"type": "hello", "version": "SYNTHETIC_VERSION_NEVER_ECHO"}, + {"type": "hello", "version": ["0.4.0"]}, + {"type": "hello"}, +]) +def test_wrong_hello_refuses_before_any_rpc(hello): + sock = Socket({}, hello=hello) + with ( + patch.object(wi, "ensure_daemon", return_value={"running": True}), + patch.object(wi.socket, "create_connection", return_value=sock), + ): + with pytest.raises(wi.WinInspectError) as error: + wi.request("daemon.capabilities") + assert str(error.value) == "WinInspect hello protocol mismatch" + assert not sock.sent + + +@pytest.mark.parametrize("version", ["0.3.0", "0.5.0", None, True, ["0.4.0"]]) +def test_protocol_override_refuses_before_process_or_socket(version): + with ( + patch.object(wi, "ensure_daemon") as ensure, + patch.object(wi.socket, "create_connection") as connect, + ): + with pytest.raises(wi.WinInspectError) as error: + wi.request("daemon.capabilities", {"protocol_version": version}) + assert str(error.value) == "WinInspect request protocol mismatch" + ensure.assert_not_called() + connect.assert_not_called() + + +def test_exact_protocol_and_correlation_are_required(): + sock = Socket({"id": "winebot-abc123456789", "ok": True, "result": {}}) + with ( + patch.object(wi, "ensure_daemon", return_value={"running": True}), + patch.object(wi.socket, "create_connection", return_value=sock), + patch.object(wi.uuid, "uuid4") as mock_uuid, + ): + mock_uuid.return_value.hex = "abc1234567890" + result = wi.request("daemon.capabilities", {"protocol_version": "0.4.0"}) + assert result["result"] == {} + size = struct.unpack("!I", sock.sent[:4])[0] + assert size == len(sock.sent) - 4 + request = json.loads(sock.sent[4:]) + assert request["params"]["protocol_version"] == "0.4.0" + + +@pytest.mark.parametrize("status", [None, 0, 1, "true", {}, []]) +def test_non_boolean_reply_status_refuses(status): + sock = Socket({"id": "winebot-abc123456789", "ok": status, "result": {}}) + with ( + patch.object(wi, "ensure_daemon", return_value={"running": True}), + patch.object(wi.socket, "create_connection", return_value=sock), + patch.object(wi.uuid, "uuid4") as mock_uuid, + ): + mock_uuid.return_value.hex = "abc1234567890" + with pytest.raises(wi.WinInspectError) as error: + wi.request("daemon.capabilities") + assert str(error.value) == "WinInspect response status invalid" From 4d101f138febe8f9cf3ef562bdf93bcc46c1a72f Mon Sep 17 00:00:00 2001 From: "Mark E. DeYoung" <1854350+mark-e-deyoung@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:42:44 +0100 Subject: [PATCH 3/3] R059: align observer test with explicit wire contract --- tests/test_wininspect_observer_contract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_wininspect_observer_contract.py b/tests/test_wininspect_observer_contract.py index 62a7c2c6..c34a1396 100644 --- a/tests/test_wininspect_observer_contract.py +++ b/tests/test_wininspect_observer_contract.py @@ -119,7 +119,7 @@ def test_malformed_event_envelope_fails_closed(monkeypatch, payload): class Socket: def __init__(self, response): self.data = bytearray() - for message in ({"type": "hello", "name": "NEVER_RETURN_IDENTITY"}, response): + for message in ({"type": "hello", "version": "0.4.0", "name": "NEVER_RETURN_IDENTITY"}, response): raw = json.dumps(message).encode("utf-8") self.data += struct.pack("!I", len(raw)) + raw