diff --git a/src/rockstor/system/tailscale.py b/src/rockstor/system/tailscale.py index b6aaa3fa7..60315a62c 100644 --- a/src/rockstor/system/tailscale.py +++ b/src/rockstor/system/tailscale.py @@ -104,32 +104,20 @@ def extract_param(param: str) -> str: def validate_ts_hostname(config: dict) -> dict: - """Ensure hostname is alphanumeric with hyphens only - No special character (including hyphens) is allowed as a custom hostname. - - "hostname": "rock-dev_@#~!$%^&*()+123" + """Applies a set of rules to validate hostname + Keep each character in hostname only if: + - not a special character + - not a unicode character + - is hyphen + - replace underscore with hyphen + + "hostname": "rock-dev_@#~!$%^&*()+123ü" should return "hostname": "rock-dev-123" """ if "hostname" in config: config["hostname"] = re.sub("_", "-", config["hostname"]) - to_exclude = [ - "@", - "#", - "~", - "!", - "$", - "%", - "^", - "&", - "*", - "(", - ")", - "+", - ] - config["hostname"] = "".join( - c for c in config["hostname"] if not c in to_exclude - ) + config["hostname"] = re.sub("[^a-zA-Z0-9-]", "", config["hostname"]) return config diff --git a/src/rockstor/system/tests/test_tailscale.py b/src/rockstor/system/tests/test_tailscale.py index f068e6f20..76abcf018 100644 --- a/src/rockstor/system/tests/test_tailscale.py +++ b/src/rockstor/system/tests/test_tailscale.py @@ -60,14 +60,14 @@ def test_tailscale_extract_param(self): ) def test_tailscale_validate_hostname(self): - """Ensure alphanumeric and no underscore in hostname""" + """Ensure alphanumeric, no underscore, and no unicode in hostname""" test_config = { "accept_routes": "yes", "advertise_exit_node": "yes", "advertise_routes": "192.168.1.0/24", "exit_node": "100.1.1.1", "exit_node_allow_lan_access": "true", - "hostname": "rock-dev_@#~!$%^&*()+123", + "hostname": "rock-dev_@#~!$%^&*()+123ü", "reset": "yes", "ssh": "yes", "custom_config": "--shields-up\n--accept-risk=all",