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

fix(instances): a console settings page opened before another change does not undo it

The console form compared what was posted with the settings saved at the
time of the save, so every field of a page opened earlier counted as a
change: an administrator who moved a console to localhost had it opened
to all interfaces again when another one, on an older page, only
generated a new password. The form now sends back a fingerprint of the
type, listen address and keymap it showed, and a save made after these
changed is refused with a message to check them and save again.

A refused save names the reasons without the field names.
This commit is contained in:
catborise 2026-11-13 22:31:08 +03:00
parent 7fe79463fc
commit 88e7e0fa90
10 changed files with 96 additions and 47 deletions

View file

@ -2,6 +2,7 @@ import re
from django import forms
from django.conf import settings
from django.utils.crypto import salted_hmac
from django.utils.translation import gettext_lazy as _
from .models import Flavor
@ -19,6 +20,12 @@ class FlavorForm(forms.ModelForm):
fields = "__all__"
def console_seen(console_type, listener, keymap, passwd):
"""The fingerprint of the console settings a page shows, which it sends
back: a page opened before another change would undo it."""
return salted_hmac("instances.console_seen", repr((console_type, listener, keymap, passwd)), algorithm="sha256").hexdigest()
class ConsoleForm(forms.Form):
console_type = forms.ChoiceField(
label=_("Console type"), required=False, widget=forms.Select(attrs={"class": "form-select"})
@ -39,6 +46,7 @@ class ConsoleForm(forms.Form):
required=False,
strip=False,
)
seen = forms.CharField(widget=forms.HiddenInput, required=False)
def __init__(self, *args, instance, is_superuser, **kwargs):
# the VM's settings as the initial values: a save changes only the fields edited
@ -51,6 +59,8 @@ class ConsoleForm(forms.Form):
"password_action": "keep",
"keymap": instance.config_console_keymap or "auto",
}
kwargs["initial"]["seen"] = console_seen(*(kwargs["initial"][k] for k in ("console_type", "listen_on", "keymap")),
instance.config_console_passwd)
super(ConsoleForm, self).__init__(*args, **kwargs)
types = [("vnc", "VNC")]
if settings.SERIAL_CONSOLE_ENABLED:
@ -85,6 +95,8 @@ class ConsoleForm(forms.Form):
def clean(self):
data = super().clean()
if data.get("seen") != self.initial["seen"]:
raise forms.ValidationError(_("The console settings were changed meanwhile; check them and save again."))
action = data.get("password_action")
if action == "set":
password = data.get("password", "")