"""Explicit HTTP Host aliases; never URLs, wildcards, or client IP access rules.""" import ipaddress import re def normalize_host(value): if not isinstance(value, str) or not value or len(value) > 300 or re.search(r'[\s/@?#\\]', value): raise ValueError('Hostnamen ohne Protokoll, Pfad oder Leerzeichen eingeben.') port = None if value.startswith('['): match = re.fullmatch(r'\[([^]]+)\](?::([0-9]+))?', value) if not match: raise ValueError('Ungültiger IPv6-Host.') host = '[' + str(ipaddress.IPv6Address(match[1])) + ']' port = match[2] else: parts = value.split(':') if len(parts) > 2: raise ValueError('IPv6-Adressen in eckigen Klammern eingeben.') if len(parts) == 2: port = parts[1] try: host = parts[0].encode('idna').decode('ascii').lower() except UnicodeError: raise ValueError('Ungültiger Hostname.') from None if len(host) > 253 or not all(re.fullmatch(r'[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?', label) for label in host.split('.')): raise ValueError('Ungültiger Hostname; Platzhalter sind nicht erlaubt.') if port is not None: if not port.isascii() or not port.isdigit() or not 1 <= int(port) <= 65535: raise ValueError('Ungültiger Port.') port = int(port) # Standard HTTP(S) ports identify the same browser-facing host. return host + (':' + str(port) if port not in (None, 80, 443) else '') def validate_hosts(values): if not isinstance(values, list) or len(values) > 64: raise ValueError('Höchstens 64 erlaubte Hostnamen als Liste eingeben.') return list(dict.fromkeys(normalize_host(value) for value in values)) def host_allowed(value, expected, aliases): if value == expected: return True try: return normalize_host(value) in aliases except ValueError: return False