1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(instances): a VNC password the browser console can send, kept as set

The console settings accepted any password of at most 8 bytes, so "éééé"
was saved, but noVNC derives its key from the characters while QEMU
compares the UTF-8 bytes: the browser console could not log in. A new
password now takes up to 8 printable ASCII characters, which also keeps
control characters out of the definition.

The .vv file wrote the password and the VM name as they are; virt-viewer
reads it as a GLib key file, so a backslash or a leading space changed
the password. Both are escaped as a key file expects.

A new or removed password also drops a passwdValidTo set elsewhere: an
expired one left the new password unusable.
This commit is contained in:
catborise 2026-11-13 22:26:02 +03:00
parent 275bf11aed
commit cd3edf4dff
11 changed files with 257 additions and 222 deletions

View file

@ -87,9 +87,9 @@ class ConsoleForm(forms.Form):
password = data.get("password", "")
if not password:
self.add_error("password", _("Enter the new password."))
# VNC uses 8 bytes; libvirt refuses a longer password
elif len(password.encode()) > 8:
self.add_error("password", _("A VNC password takes at most 8 bytes."))
# VNC takes 8 bytes, and the browser console sends only ASCII as typed
elif len(password) > 8 or not all(" " <= c <= "~" for c in password):
self.add_error("password", _("A VNC password takes up to 8 letters, digits or symbols, without accents."))
# A console that listens beyond localhost (set by an administrator)
# keeps its password unless an administrator removes it.
if action == "none" and not self.is_superuser and not self.instance.proxy.console_listens_locally():

View file

@ -745,7 +745,7 @@
<div class="console-new-password ms-4 mt-1 mb-2">
<label class="form-label small" for="{{ console_form.password.id_for_label }}">{{ console_form.password.label }}</label>
{{ console_form.password }}
<p class="small text-body-secondary mt-1 mb-0">{% trans "Up to 8 characters; an accented letter counts as two." %}</p>
<p class="small text-body-secondary mt-1 mb-0">{% trans "Up to 8 letters, digits or symbols, without accents." %}</p>
</div>
{% endif %}
{% endfor %}

View file

@ -71,9 +71,10 @@ class CanOpenConsoleTestCase(TestCase):
self.owner.user_permissions.add(Permission.objects.get(codename="passwordless_console"))
with patch("instances.models.wvmInstance") as wvm:
proxy = wvm.return_value.cached_reads.return_value.__enter__.return_value
proxy.get_console_passwd.return_value = "secret"
proxy.get_console_passwd.return_value = " a\\nb"
res = self.client.get(reverse("instances:getvvfile", args=[self.instance.id]))
self.assertIn(b"password=secret\n", res.content)
# as a key file reads it back: the leading space and the backslash stay
self.assertIn(b"password=\\sa\\\\nb\n", res.content)
def test_owner_can_get_vdi_url(self):
self.client.force_login(self.owner)

View file

@ -53,7 +53,7 @@ class UpdateConsoleTestCase(TestCase):
proxy.set_console_keymap.assert_not_called()
def test_the_password_changes_by_the_action_chosen(self):
for action, password in (("set", " a b c "), ("set", "éééé"), ("none", "")):
for action, password in (("set", " a b c "), ("set", "~a\\b{}!"), ("none", "")):
with self.subTest(action, password=password):
proxy, _ = self.post({**VNC, "password_action": action, "password": password})
proxy.set_console_passwd.assert_called_once_with(password)
@ -62,7 +62,8 @@ class UpdateConsoleTestCase(TestCase):
self.assertEqual(len(generated), 8)
def test_a_password_vnc_cannot_take_is_refused_and_nothing_is_saved(self):
for password in ("", "123456789", "ééééé"): # libvirt counts bytes: é is two
# the browser console sends the password's characters, QEMU expects its bytes
for password in ("", "123456789", "éé", "a\nb", "a\tb"):
with self.subTest(password=password):
proxy, messages = self.post({**VNC, "keymap": "de", "password_action": "set", "password": password})
proxy.set_console_passwd.assert_not_called()

View file

@ -2461,6 +2461,13 @@ def set_template(request, pk):
return _back(request, pk, "options")
def keyfile_value(value):
"""value as a GLib key file (the .vv file) reads it back: a backslash,
a line break or a leading space would change it."""
value = value.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")
return "\\s" + value[1:] if value.startswith(" ") else value
def getvvfile(request, pk):
instance = get_instance(request.user, pk)
# The .vv file contains the VNC password: same rule as the console.
@ -2480,10 +2487,10 @@ def getvvfile(request, pk):
response.writelines(f"type={proxy.get_console_type()}\n")
response.writelines(f"host={proxy.host if listen == '0.0.0.0' else listen}\n")
response.writelines(f"port={proxy.get_console_port()}\n")
response.writelines(f"title={proxy.instance.name()}\n")
response.writelines(f"title={keyfile_value(proxy.instance.name())}\n")
# as the console page: without the permission the viewer asks for the password
if request.user.has_perm("instances.passwordless_console"):
response.writelines(f"password={proxy.get_console_passwd() or ''}\n")
response.writelines(f"password={keyfile_value(proxy.get_console_passwd() or '')}\n")
response.writelines("enable-usbredir=1\n")
response.writelines("disable-effects=all\n")
response.writelines("secure-attention=ctrl+alt+ins\n")