From 3a6f9fdbc4f599b974a5a7fba6b631c977f745a1 Mon Sep 17 00:00:00 2001 From: m1kados <75936762+m1kados@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:55:57 +0100 Subject: [PATCH 1/2] Fix: Raise GvmError when the remote closed the connection on send Sending a command over a connection that has been closed by the remote server, for example by gvmd after an error response, raised a raw BrokenPipeError/ConnectionResetError from the socket. Reading from a closed connection already results in a GvmError. Convert connection errors raised while sending or reading into a GvmError too, so callers can handle a closed connection consistently and re-connect. The original exception is kept as the cause. Closes #11 Co-Authored-By: Claude Fable 5.2 --- gvm/connections/_connection.py | 21 +++++++++-- tests/connections/test_gvm_connection.py | 44 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/gvm/connections/_connection.py b/gvm/connections/_connection.py index e4b3bba91..009fd8e21 100644 --- a/gvm/connections/_connection.py +++ b/gvm/connections/_connection.py @@ -82,11 +82,22 @@ def send(self, data: bytes) -> None: Args: data: Data to be send to the server as bytes. + + Raises: + GvmError: If the socket is not connected or the remote server + has closed the connection. In the latter case the connection + needs to be re-established via :py:meth:`connect`. """ if self._socket is None: raise GvmError("Socket is not connected") - self._socket.sendall(data) + try: + self._socket.sendall(data) + except ConnectionError as e: + # Connection was closed by server, e.g. after an error response + raise GvmError( + f"Remote closed the connection. Error was {e}" + ) from e def read(self) -> bytes: """Read data from the remote server @@ -98,7 +109,13 @@ def read(self) -> bytes: time() + self._timeout if self._timeout is not None else None ) - data = self._read() + try: + data = self._read() + except ConnectionError as e: + # Connection was reset by server + raise GvmError( + f"Remote closed the connection. Error was {e}" + ) from e if not data: # Connection was closed by server diff --git a/tests/connections/test_gvm_connection.py b/tests/connections/test_gvm_connection.py index b3aecfe9d..92b1e67a4 100644 --- a/tests/connections/test_gvm_connection.py +++ b/tests/connections/test_gvm_connection.py @@ -43,6 +43,50 @@ def test_read_no_data(self): with self.assertRaises(GvmError, msg="Remote closed the connection"): connection.read() + def test_read_connection_reset(self): + read_mock = MagicMock() + read_mock.side_effect = ConnectionResetError("Connection reset") + + connection = DummyConnection() + connection._read = read_mock + + with self.assertRaisesRegex( + GvmError, "^Remote closed the connection. Error was " + ): + connection.read() + + def test_send_not_connected(self): + connection = DummyConnection() + + with self.assertRaisesRegex(GvmError, "^Socket is not connected$"): + connection.send(b"") + + def test_send_remote_closed(self): + for error in ( + BrokenPipeError("Broken pipe"), + ConnectionResetError("Connection reset"), + ConnectionAbortedError("Connection aborted"), + ): + with self.subTest(error=type(error).__name__): + connection = DummyConnection() + connection._socket = MagicMock() + connection._socket.sendall.side_effect = error + + with self.assertRaisesRegex( + GvmError, "^Remote closed the connection. Error was " + ) as cm: + connection.send(b"") + + self.assertIs(cm.exception.__cause__, error) + + def test_send_other_os_error_is_not_wrapped(self): + connection = DummyConnection() + connection._socket = MagicMock() + connection._socket.sendall.side_effect = TimeoutError("timed out") + + with self.assertRaises(TimeoutError): + connection.send(b"") + def test_read_trigger_timeout(self): # mocking the response into two parts, so we run into the timeout # check in the loop From 8958c462baf81ca2c04c69f6861de55a6c9efa60 Mon Sep 17 00:00:00 2001 From: m1kados <75936762+m1kados@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:28:08 +0100 Subject: [PATCH 2/2] Fix: Handle TLS close errors when the remote closed the connection TLS sockets raise ssl.SSLEOFError or ssl.SSLZeroReturnError if the remote server has closed the connection. Both aren't ConnectionErrors and therefore were not converted into a GvmError for a TLSConnection. Handle them the same way as connection errors when sending and reading. Other SSL errors are still passed through unchanged. Co-Authored-By: Claude Fable 5.2 --- gvm/connections/_connection.py | 13 ++++++- tests/connections/test_gvm_connection.py | 47 +++++++++++++++++++----- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/gvm/connections/_connection.py b/gvm/connections/_connection.py index 009fd8e21..c092b30f7 100644 --- a/gvm/connections/_connection.py +++ b/gvm/connections/_connection.py @@ -4,6 +4,7 @@ import logging import socket as socketlib +import ssl from abc import ABC, abstractmethod from time import time from typing import Protocol, runtime_checkable @@ -16,6 +17,14 @@ logger = logging.getLogger(__name__) +# Errors raised by a socket if the remote server has closed the connection. +# The SSL errors are raised by TLS sockets and aren't ConnectionErrors. +REMOTE_CLOSED_ERRORS = ( + ConnectionError, + ssl.SSLEOFError, + ssl.SSLZeroReturnError, +) + @runtime_checkable class GvmConnection(Protocol): @@ -93,7 +102,7 @@ def send(self, data: bytes) -> None: try: self._socket.sendall(data) - except ConnectionError as e: + except REMOTE_CLOSED_ERRORS as e: # Connection was closed by server, e.g. after an error response raise GvmError( f"Remote closed the connection. Error was {e}" @@ -111,7 +120,7 @@ def read(self) -> bytes: try: data = self._read() - except ConnectionError as e: + except REMOTE_CLOSED_ERRORS as e: # Connection was reset by server raise GvmError( f"Remote closed the connection. Error was {e}" diff --git a/tests/connections/test_gvm_connection.py b/tests/connections/test_gvm_connection.py index 92b1e67a4..091442d3e 100644 --- a/tests/connections/test_gvm_connection.py +++ b/tests/connections/test_gvm_connection.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later # +import ssl import unittest from unittest.mock import MagicMock @@ -13,6 +14,14 @@ from gvm.connections._connection import AbstractGvmConnection from gvm.errors import GvmError +REMOTE_CLOSED_ERRORS = ( + BrokenPipeError("Broken pipe"), + ConnectionResetError("Connection reset"), + ConnectionAbortedError("Connection aborted"), + ssl.SSLEOFError("EOF occurred in violation of protocol"), + ssl.SSLZeroReturnError("TLS/SSL connection has been closed (EOF)"), +) + class DummyConnection(AbstractGvmConnection): def connect(self) -> None: @@ -43,16 +52,30 @@ def test_read_no_data(self): with self.assertRaises(GvmError, msg="Remote closed the connection"): connection.read() - def test_read_connection_reset(self): + def test_read_remote_closed(self): + for error in REMOTE_CLOSED_ERRORS: + with self.subTest(error=type(error).__name__): + read_mock = MagicMock() + read_mock.side_effect = error + + connection = DummyConnection() + connection._read = read_mock + + with self.assertRaisesRegex( + GvmError, "^Remote closed the connection. Error was " + ) as cm: + connection.read() + + self.assertIs(cm.exception.__cause__, error) + + def test_read_other_ssl_error_is_not_wrapped(self): read_mock = MagicMock() - read_mock.side_effect = ConnectionResetError("Connection reset") + read_mock.side_effect = ssl.SSLError("bad record mac") connection = DummyConnection() connection._read = read_mock - with self.assertRaisesRegex( - GvmError, "^Remote closed the connection. Error was " - ): + with self.assertRaises(ssl.SSLError): connection.read() def test_send_not_connected(self): @@ -62,11 +85,7 @@ def test_send_not_connected(self): connection.send(b"") def test_send_remote_closed(self): - for error in ( - BrokenPipeError("Broken pipe"), - ConnectionResetError("Connection reset"), - ConnectionAbortedError("Connection aborted"), - ): + for error in REMOTE_CLOSED_ERRORS: with self.subTest(error=type(error).__name__): connection = DummyConnection() connection._socket = MagicMock() @@ -79,6 +98,14 @@ def test_send_remote_closed(self): self.assertIs(cm.exception.__cause__, error) + def test_send_other_ssl_error_is_not_wrapped(self): + connection = DummyConnection() + connection._socket = MagicMock() + connection._socket.sendall.side_effect = ssl.SSLError("bad record mac") + + with self.assertRaises(ssl.SSLError): + connection.send(b"") + def test_send_other_os_error_is_not_wrapped(self): connection = DummyConnection() connection._socket = MagicMock()