1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00
webvirtcloud/console/tokens.py
catborise 8eb619114c fix(console): a session that a password change ended opens no console
session_user_id read the user id out of the session row as it was, so a
session cookie that Django no longer accepts (the user's password changed,
or the user was deactivated) still matched a console token of its user in
novncd and socketiod. It now asks django.contrib.auth.get_user, which checks
the session's password hash and that the user may sign in.

The serial console tests built sessions by hand with only the user id;
they now sign in with force_login, as the panel does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
2026-11-08 20:47:27 +03:00

64 lines
2.9 KiB
Python

"""The console token: the console view signs "host:uuid:user_id:console type"
for the VM whose console it shows; the console daemons (novncd for vnc,
socketiod for pty) accept it for 120 s if that user may still open that VM's
console. novncd, whose token travels in the websocket URL (and so in logs),
also wants the session cookie of the token's user (session_user_id).
socketiod's token travels in the Socket.IO handshake, not the URL."""
from importlib import import_module
from types import SimpleNamespace
from django import db
from django.conf import settings
from django.contrib.auth import get_user, get_user_model
from django.core.signing import BadSignature, SignatureExpired, TimestampSigner
from django.http import parse_cookie
from instances.models import Instance
from instances.utils import can_open_console
SIGNER = TimestampSigner(salt="console.novnc")
MAX_AGE = 120
ACCESS_CHECK = 10 # seconds between checks that an open console's user may still use it
def sign(compute_id, uuid, user_id, console_type):
return SIGNER.sign(f"{compute_id}:{uuid}:{user_id}:{console_type}")
def authorize(token, console_type):
"""The Instance the token opens and the user it was given to;
PermissionError otherwise, also for a token of another console type."""
try:
host, uuid, user_id, signed_type = SIGNER.unsign(token, max_age=MAX_AGE).split(":")
host, user_id = int(host), int(user_id)
except (BadSignature, SignatureExpired, ValueError, TypeError):
raise PermissionError("Invalid or expired console token")
if signed_type != console_type:
raise PermissionError(f"The token is for a {signed_type} console")
instance = Instance.objects.filter(compute_id=host, uuid=uuid).select_related("compute").first()
user = get_user_model().objects.filter(id=user_id, is_active=True).first()
if instance is None or user is None or not can_open_console(user, instance):
raise PermissionError("No access to this console")
return instance, user
def may_still_open(user_id, instance):
"""The user still may open the console: an owner may have been removed or
the user deactivated since the session started."""
try:
user = get_user_model().objects.filter(pk=user_id).first()
return user is not None and can_open_console(user, instance)
finally:
db.connection.close() # a session may last for days: no idle connection meanwhile
def session_user_id(cookie_header):
"""The id (a string) of the user signed in with the session cookie of a
request's Cookie header, or None. Django's own check: a session that a
password change or a deactivation ended is no one's."""
session_key = parse_cookie(cookie_header or "").get(settings.SESSION_COOKIE_NAME)
if not session_key:
return None
user = get_user(SimpleNamespace(session=import_module(settings.SESSION_ENGINE).SessionStore(session_key)))
return str(user.pk) if user.is_authenticated else None