Meilenstein 2: Verbindungs-API und Dashboard
- CRUD-API /api/connections mit Filter nach Sichtbarkeit und Typ - Sichtbarkeit: privat nur für Besitzer (sonst 404), team für alle bearbeitbar; nur der Besitzer darf team auf privat setzen - Strenge serverseitige Prüfung von Ziel, Port, Benutzer und URL - Dashboard mit Kacheln nach Typ, Filter Alle/Privat/Team, Suche, Dialog zum Anlegen/Bearbeiten, Links itrdp:// und itssh:// - Tests für Validierung und Sichtbarkeitslogik, SETUP.md ergänzt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
293367f33e
commit
2e89c4ad59
10 files changed
+816
-30
No files matched your search
@@ -0,0 +1,73 @@
|
||||
"""Strenge Prüfung von Zielen, Ports und Benutzernamen (Sicherheitsregel 3).
|
||||
|
||||
Erlaubt sind nur Buchstaben, Ziffern, Punkte, Bindestriche, Doppelpunkte bei IPv6
|
||||
und Unterstriche bei Benutzernamen. Keine Shell-Metazeichen.
|
||||
"""
|
||||
|
||||
import ipaddress
|
||||
import re
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
# Ein Label eines Hostnamens: 1–63 Zeichen, Buchstaben/Ziffern/Bindestrich, nicht mit '-' am Rand
|
||||
_LABEL_RE = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$")
|
||||
BENUTZER_RE = re.compile(r"^[A-Za-z0-9._-]{1,64}$")
|
||||
_STEUERZEICHEN_RE = re.compile(r"[\x00-\x1f\x7f]")
|
||||
|
||||
STANDARD_PORTS = {"rdp": 3389, "ssh": 22}
|
||||
|
||||
|
||||
def ist_hostname(wert: str) -> bool:
|
||||
if not wert or len(wert) > 253:
|
||||
return False
|
||||
labels = wert.rstrip(".").split(".") if wert != "." else []
|
||||
return bool(labels) and all(_LABEL_RE.fullmatch(label) for label in labels)
|
||||
|
||||
|
||||
def ist_ip(wert: str) -> bool:
|
||||
# Nur reine Adressen, keine Zonen-IDs (%eth0) und keine Klammern
|
||||
if not re.fullmatch(r"[0-9A-Fa-f.:]+", wert):
|
||||
return False
|
||||
try:
|
||||
ipaddress.ip_address(wert)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def host_pruefen(wert: str) -> str:
|
||||
wert = wert.strip()
|
||||
if ist_ip(wert) or ist_hostname(wert):
|
||||
return wert
|
||||
raise ValueError("Ungültiges Ziel. Erlaubt sind IP-Adressen und Hostnamen (Buchstaben, Ziffern, Punkte, Bindestriche).")
|
||||
|
||||
|
||||
def url_pruefen(wert: str) -> str:
|
||||
wert = wert.strip()
|
||||
if len(wert) > 2048 or _STEUERZEICHEN_RE.search(wert) or " " in wert:
|
||||
raise ValueError("Ungültige URL.")
|
||||
teile = urlsplit(wert)
|
||||
if teile.scheme.lower() not in ("http", "https"):
|
||||
raise ValueError("Die URL muss mit http:// oder https:// beginnen.")
|
||||
host = teile.hostname or ""
|
||||
if not (ist_hostname(host) or ist_ip(host)):
|
||||
raise ValueError("Die URL enthält keinen gültigen Hostnamen.")
|
||||
try:
|
||||
teile.port # löst bei ungültigem Port ValueError aus
|
||||
except ValueError:
|
||||
raise ValueError("Die URL enthält einen ungültigen Port.") from None
|
||||
return wert
|
||||
|
||||
|
||||
def benutzer_pruefen(wert: str) -> str:
|
||||
wert = wert.strip()
|
||||
if not BENUTZER_RE.fullmatch(wert):
|
||||
raise ValueError("Ungültiger Benutzername. Erlaubt sind Buchstaben, Ziffern, Punkte, Bindestriche und Unterstriche.")
|
||||
return wert
|
||||
|
||||
|
||||
def text_pruefen(wert: str, feld: str, *, mehrzeilig: bool = False) -> str:
|
||||
wert = wert.strip()
|
||||
erlaubt = wert.replace("\n", "").replace("\r", "").replace("\t", "") if mehrzeilig else wert
|
||||
if _STEUERZEICHEN_RE.search(erlaubt):
|
||||
raise ValueError(f"{feld} enthält unzulässige Steuerzeichen.")
|
||||
return wert
|
||||
Reference in new issue
Block a user