1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(console): a VNC console whose access check fails is closed, not left unchecked

novncd checks every 10 seconds that an open VNC console's user may still
open it. An error in that check, such as a locked database, ended the
checking thread and left the session open for good, unchecked: a user
removed from the VM afterwards kept its console. The session now ends
with a warning in the log, as a serial console's worker does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 18:58:55 +03:00
parent 16ad625cda
commit 417be11616
2 changed files with 22 additions and 6 deletions

View file

@ -108,13 +108,19 @@ def end_when_refused(instance, user_id, tsock, ended):
"""Close the session's VNC socket, which ends it, once its user may no
longer open the console (as the serial console does)."""
while not ended.wait(ACCESS_CHECK):
if not may_still_open(user_id, instance):
try:
if may_still_open(user_id, instance):
continue
logging.info("VNC console of VM %s closed: its user may no longer open it", instance.name)
try:
tsock.shutdown(socket.SHUT_RDWR)
except OSError:
pass # the session ended meanwhile
return
except Exception as e:
# unchecked, the session would go on whoever loses the VM meanwhile
logging.warning("VNC console of VM %s closed: its user's access could not be checked: %s",
instance.name, safe_text(e))
try:
tsock.shutdown(socket.SHUT_RDWR)
except OSError:
pass # the session ended meanwhile
return
class ConsoleClient:

View file

@ -16,6 +16,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.signing import TimestampSigner
from django.conf import settings
from django.db import OperationalError
from django.test import TestCase, override_settings
from instances.models import Instance
from libvirt import libvirtError
@ -298,6 +299,15 @@ class NovncdDaemonLogicTestCase(TestCase):
may_still_open.assert_called_with(7, vm)
tsock.shutdown.assert_called_once_with(novncd_mod.socket.SHUT_RDWR)
def test_a_vnc_session_whose_access_cannot_be_checked_ends(self):
tsock, ended = MagicMock(), MagicMock()
ended.wait.return_value = False
with patch.object(novncd_mod, "may_still_open", side_effect=OperationalError("database is locked")), \
self.assertLogs(level="WARNING") as logs:
novncd_mod.end_when_refused(MagicMock(), 7, tsock, ended)
tsock.shutdown.assert_called_once_with(novncd_mod.socket.SHUT_RDWR)
self.assertIn("database is locked", logs.output[0])
def test_a_token_of_a_serial_console_does_not_open_a_vnc_console(self):
user = get_user_model().objects.create_user("vnc-owner", password="pw")
compute = Compute.objects.create(name="novnc-type-compute", hostname="127.0.0.1", login="root", password="", type=1)