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

fix(accounts): an SSH key is a whole key, and added once

The profile took an SSH key whose blob only named its type
("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5") or was cut short, and offered it
for a VM, where it lets no one in. A key is now parsed whole with
cryptography (which paramiko already brings), security keys
(sk-ssh-ed25519) included.

The same key with another comment was added a second time, and
deleting one left the other to be installed. Keys are now compared by
their type and key, without the comment.
This commit is contained in:
catborise 2026-11-12 16:33:09 +03:00
parent 2ab1bac0c7
commit f18be7c33e
4 changed files with 47 additions and 35 deletions

View file

@ -1,9 +1,8 @@
import base64
import binascii
import struct
from decimal import Decimal
import segno
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.primitives.serialization import load_ssh_public_key
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.core.signing import TimestampSigner
@ -38,25 +37,14 @@ def get_user_totp_device(user):
def validate_ssh_key(key):
# An OpenSSH public key is a type, a key and an optional comment, which may hold spaces
array = key.strip().encode().split(maxsplit=2)
if len(array) < 2:
"""An OpenSSH public key: its type, a whole key of that type (security
keys too) and an optional comment, which may hold spaces: one line, as
authorized_keys holds it."""
if "\n" in key.strip() or "\r" in key.strip():
return False
typeofkey = array[0]
string = array[1]
# must have only valid rsa-ssh key characters ie binascii characters
try:
data = base64.decodebytes(string)
except binascii.Error:
return False
# unpack the contents of data, from data[:4] , property of ssh key .
try:
str_len = struct.unpack(">I", data[:4])[0]
except struct.error:
return False
# data[4:str_len] must have string which matches with the typeofkey, another ssh key property.
if data[4 : 4 + str_len] != typeofkey:
load_ssh_public_key(" ".join(key.split(maxsplit=2)[:2]).encode())
except (ValueError, UnsupportedAlgorithm):
return False
return True