fix(remote): skip a malformed server log record or channel message instead of ending the session channel

This commit is contained in:
imSp4rky
2026-11-26 13:40:07 -07:00
parent 5575892395
commit 4738cc044a
2 changed files with 72 additions and 7 deletions
+62 -3
View File
@@ -3,20 +3,25 @@
from __future__ import annotations from __future__ import annotations
import base64 import base64
import json
import logging import logging
from collections.abc import AsyncIterator
from types import SimpleNamespace from types import SimpleNamespace
from typing import Any from typing import Any, cast
from unittest.mock import Mock
import pytest import pytest
import requests import requests
from aiohttp import web from aiohttp import WSMsgType, web
from click.testing import CliRunner from click.testing import CliRunner
from tests.remote.unit.test_session_reopen import make_service
from unshackle.core import remote_service from unshackle.core import remote_service
from unshackle.core.api import handlers from unshackle.core.api import handlers
from unshackle.core.api.errors import APIError, APIErrorCode from unshackle.core.api.errors import APIError, APIErrorCode
from unshackle.core.api.routes import setup_routes 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 from unshackle.core.remote_service import RemoteClient, RemoteError, RemoteService
pytestmark = pytest.mark.unit 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 = ServerMessages(service)
messages.show_logs([{"seq": 1, "level": "INFO", "message": "Scan", "qr": "\u20ac" * 1500}]) messages.show_logs([{"seq": 1, "level": "INFO", "message": "Scan", "qr": "\u20ac" * 1500}])
assert seen == ["Scan"] and messages.log_seq == 1 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"
+10 -4
View File
@@ -86,7 +86,10 @@ class ServerMessages:
with self.log_lock: with self.log_lock:
for record in records: 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: if seq <= self.log_seq:
continue continue
message = sanitize_log(record.get("message", ""), keep_lines=True) message = sanitize_log(record.get("message", ""), keep_lines=True)
@@ -285,10 +288,10 @@ class PollingChannel:
with quiet(): with quiet():
endpoint = self.messages.route(LOGS_ROUTE) + f"?since={self.messages.log_seq}" endpoint = self.messages.route(LOGS_ROUTE) + f"?since={self.messages.log_seq}"
resp = cast(LogsReply, self.service.client.get_optional(endpoint)) resp = cast(LogsReply, self.service.client.get_optional(endpoint))
if resp:
self.messages.show_logs(resp.get("logs", []))
except (Exception, SystemExit): except (Exception, SystemExit):
return return
if resp:
self.messages.show_logs(resp.get("logs", []))
def draining(self) -> bool: def draining(self) -> bool:
"""Whether the server announced a restart.""" """Whether the server announced a restart."""
@@ -395,7 +398,10 @@ class WebSocketChannel:
except ValueError: except ValueError:
continue continue
if isinstance(data, dict): 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: async def receive(self, ws: aiohttp.ClientWebSocketResponse, data: dict[str, Any]) -> None:
kind = data.get("type") kind = data.get("type")