From 4738cc044a15f6153132de25a71c896e63412c4c Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Thu, 26 Nov 2026 13:27:14 -0700 Subject: [PATCH] fix(remote): skip a malformed server log record or channel message instead of ending the session channel --- tests/remote/unit/test_malformed_input.py | 65 +++++++++++++++++++++-- unshackle/core/remote_channel.py | 14 +++-- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/tests/remote/unit/test_malformed_input.py b/tests/remote/unit/test_malformed_input.py index 1f5428a8..9929e31f 100644 --- a/tests/remote/unit/test_malformed_input.py +++ b/tests/remote/unit/test_malformed_input.py @@ -3,20 +3,25 @@ from __future__ import annotations import base64 +import json import logging +from collections.abc import AsyncIterator from types import SimpleNamespace -from typing import Any +from typing import Any, cast +from unittest.mock import Mock import pytest import requests -from aiohttp import web +from aiohttp import WSMsgType, web from click.testing import CliRunner +from tests.remote.unit.test_session_reopen import make_service from unshackle.core import remote_service from unshackle.core.api import handlers from unshackle.core.api.errors import APIError, APIErrorCode from unshackle.core.api.routes import setup_routes -from unshackle.core.remote_channel import PollingChannel, ServerMessages +from unshackle.core.remote_channel import PollingChannel, ServerMessages, WebSocketChannel +from unshackle.core.remote_protocol import AuthStatus from unshackle.core.remote_service import RemoteClient, RemoteError, RemoteService pytestmark = pytest.mark.unit @@ -263,3 +268,57 @@ def test_a_relayed_qr_code_too_long_to_draw_still_logs_its_line(monkeypatch) -> messages = ServerMessages(service) messages.show_logs([{"seq": 1, "level": "INFO", "message": "Scan", "qr": "\u20ac" * 1500}]) assert seen == ["Scan"] and messages.log_seq == 1 + + +async def feed(channel: WebSocketChannel, *messages: dict[str, Any]) -> None: + """Read the given messages on the session channel, then end.""" + + async def socket() -> AsyncIterator[SimpleNamespace]: + for message in messages: + yield SimpleNamespace(type=WSMsgType.TEXT, data=json.dumps(message)) + + await channel.read(cast(Any, socket())) + + +def channel_logging_to(seen: list[str]) -> WebSocketChannel: + service = make_service() + service.log = SimpleNamespace(log=lambda level, message: seen.append(message)) # type: ignore[assignment] + messages = ServerMessages(service) + return WebSocketChannel(messages, PollingChannel(messages)) + + +@pytest.mark.parametrize("seq", ["x", "2", None, [1], {"a": 1}, float("inf")]) +async def test_a_log_record_with_a_malformed_seq_is_skipped(seq: Any) -> None: + seen: list[str] = [] + channel = channel_logging_to(seen) + await feed(channel, {"type": "log", "seq": seq, "message": "bad"}, {"type": "log", "seq": 1, "message": "good"}) + assert seen == ["good"] + assert channel.messages.log_seq == 1 + + +@pytest.mark.parametrize( + ("logs", "shown"), [(["x", {"seq": 1, "message": "good"}], ["good"]), (None, []), (5, []), ("ab", [])] +) +def test_a_malformed_polled_log_reply_is_skipped(logs: Any, shown: list[str]) -> None: + seen: list[str] = [] + channel = channel_logging_to(seen) + channel.service.client = SimpleNamespace(get_optional=lambda _: {"logs": logs}) # type: ignore[assignment] + channel.fallback.drain_logs() + assert seen == shown + + +async def test_a_channel_message_that_fails_is_skipped(monkeypatch: pytest.MonkeyPatch) -> None: + channel = channel_logging_to([]) + show_logs = Mock(side_effect=[RuntimeError("bad"), None]) + monkeypatch.setattr(channel.messages, "show_logs", show_logs) + await feed(channel, {"type": "log", "seq": 1}, {"type": "log", "seq": 2}) + assert show_logs.call_count == 2 + + +async def test_a_failed_login_status_shows_the_server_error_without_control_characters() -> None: + channel = channel_logging_to([]) + await feed(channel, {"type": "status", "status": AuthStatus.FAILED, "error": "\x1b[2Jbad\r\nforged"}) + channel.connected.set() + with pytest.raises(RemoteError) as caught: + channel.auth_step() + assert caught.value.message == "Remote auth failed: [2Jbadforged" diff --git a/unshackle/core/remote_channel.py b/unshackle/core/remote_channel.py index 9d37683a..da2d7e11 100644 --- a/unshackle/core/remote_channel.py +++ b/unshackle/core/remote_channel.py @@ -86,7 +86,10 @@ class ServerMessages: with self.log_lock: for record in records: - seq = int(record.get("seq", 0)) + seq = record.get("seq") if isinstance(record, dict) else None + if not isinstance(seq, int): + log.debug(f"Skipped a malformed server log record: {record!r:.200}") + continue if seq <= self.log_seq: continue message = sanitize_log(record.get("message", ""), keep_lines=True) @@ -285,10 +288,10 @@ class PollingChannel: with quiet(): endpoint = self.messages.route(LOGS_ROUTE) + f"?since={self.messages.log_seq}" resp = cast(LogsReply, self.service.client.get_optional(endpoint)) + if resp: + self.messages.show_logs(resp.get("logs", [])) except (Exception, SystemExit): return - if resp: - self.messages.show_logs(resp.get("logs", [])) def draining(self) -> bool: """Whether the server announced a restart.""" @@ -395,7 +398,10 @@ class WebSocketChannel: except ValueError: continue if isinstance(data, dict): - await self.receive(ws, data) + try: + await self.receive(ws, data) + except Exception as e: + log.debug(f"Skipped a session channel message that failed: {e!r}") async def receive(self, ws: aiohttp.ClientWebSocketResponse, data: dict[str, Any]) -> None: kind = data.get("type")