From 9a5bb9cbba495fe79762bad53e4bef99a0bb4872 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:52:57 -0600 Subject: [PATCH] fix(deploy): tighten registry validation --- lib/default.nix | 90 +++++++++++++++++++++++++++++++++++++++---- pkgs/deploy/deploy.py | 35 ++++++++++++++--- 2 files changed, 111 insertions(+), 14 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index d8f777dd30..bcf538be0d 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1206,6 +1206,60 @@ let }; } ); + validationService = { + package = "test-package"; + entryPoint = "bin/service"; + args = [ ]; + environment = { }; + secrets = [ ]; + user = "nobody"; + ports = [ ]; + restart = "always"; + healthCheck = null; + targets = [ "laptop" ]; + class = "systemd"; + }; + # Keep each registry valid except for the exact issue #6383 shape. + newlineNameRegistry = pkgs.writeText "deploy-targets-newline-name.json" '' + {"svc\n": ${builtins.toJSON validationService}} + ''; + duplicateSecretsRegistry = pkgs.writeText "deploy-targets-duplicate-secrets.json" ( + builtins.toJSON { + svc = validationService // { + secrets = [ + "secret.name" + "secret.name" + ]; + }; + } + ); + duplicateTargetsRegistry = pkgs.writeText "deploy-targets-duplicate-targets.json" ( + builtins.toJSON { + svc = validationService // { + targets = [ + "laptop" + "laptop" + ]; + }; + } + ); + hostlessHealthCheckRegistry = pkgs.writeText "deploy-targets-hostless-health-check.json" ( + builtins.toJSON { + svc = validationService // { + healthCheck = "http://"; + }; + } + ); + duplicatePortsRegistry = pkgs.writeText "deploy-targets-duplicate-ports.json" ( + builtins.toJSON { + svc = validationService // { + ports = [ + 8080 + 8080 + ]; + }; + } + ); # Duplicate keys cannot be expressed as a Nix attrset, so these # registries are raw JSON text (issue #6384): load_registry must # reject them naming the duplicated key. @@ -1244,15 +1298,30 @@ let mkdir -p $out cp list.txt $out/list.txt ''; - deploy-dupkeys-property = pkgs.runCommandLocal "check-deploy-dupkeys-property" - { - nativeBuildInputs = [ pkgs.python3 ]; - } - '' - python3 ${../pkgs/deploy/test_dupkeys_property.py} ${../pkgs/deploy/deploy.py} - mkdir -p $out - ''; + deploy-dupkeys-property = + pkgs.runCommandLocal "check-deploy-dupkeys-property" + { + nativeBuildInputs = [ pkgs.python3 ]; + } + '' + python3 ${../pkgs/deploy/test_dupkeys_property.py} ${../pkgs/deploy/deploy.py} + mkdir -p $out + ''; deploy-list-malformed = pkgs.runCommandLocal "check-deploy-list-malformed" { } '' + expect_invalid() { + registry="$1" + expected="$2" + if ${deployPkgs.deploy}/bin/deploy list --registry "$registry" > out.txt 2> err.txt; then + echo "deploy list accepted a malformed registry: $registry" >&2 + exit 1 + fi + if ! grep -Fq "$expected" err.txt; then + echo "deploy list did not report '$expected' for $registry" >&2 + cat err.txt >&2 + exit 1 + fi + test ! -s out.txt + } if ${deployPkgs.deploy}/bin/deploy list --registry ${badRegistry} > out.txt 2> err.txt; then echo "deploy list accepted a malformed registry" >&2 exit 1 @@ -1284,6 +1353,11 @@ let exit 1 fi test ! -s unhash-out.txt + expect_invalid ${newlineNameRegistry} "name must match" + expect_invalid ${duplicateSecretsRegistry} "secrets must not repeat a secret name" + expect_invalid ${duplicateTargetsRegistry} "targets must not repeat a target" + expect_invalid ${hostlessHealthCheckRegistry} "healthCheck must be an http(s) URL with a host" + expect_invalid ${duplicatePortsRegistry} "ports must not repeat a port" mkdir -p $out cp err.txt $out/errors.txt ''; diff --git a/pkgs/deploy/deploy.py b/pkgs/deploy/deploy.py index b81ccfd2f2..8c85f137d8 100644 --- a/pkgs/deploy/deploy.py +++ b/pkgs/deploy/deploy.py @@ -25,6 +25,7 @@ import os import re import sys +from urllib.parse import urlsplit CLASSES = {"systemd", "launchd", "cloudflare", "sandbox", "container"} RESTART_POLICIES = { @@ -61,7 +62,7 @@ def validate(registry): return ["registry: no service definitions"] for name, defn in sorted(registry.items()): where = f"service {name!r}" - if not NAME_RE.match(name): + if not NAME_RE.fullmatch(name): errors.append(f"{where}: name must match {NAME_RE.pattern}") if not isinstance(defn, dict): errors.append(f"{where}: definition must be an object") @@ -108,6 +109,11 @@ def validate(registry): errors.append( f"{where}: secrets must be a list of secret names (names only, never values)" ) + elif ( + "secrets" in defn + and len(set(defn["secrets"])) != len(defn["secrets"]) + ): + errors.append(f"{where}: secrets must not repeat a secret name") if "user" in defn and not (isinstance(defn["user"], str) and defn["user"]): errors.append(f"{where}: user must be a non-empty string") if "ports" in defn: @@ -130,11 +136,23 @@ def validate(registry): ) if "healthCheck" in defn and defn["healthCheck"] is not None: hc = defn["healthCheck"] - if not ( - isinstance(hc, str) - and (hc.startswith("http://") or hc.startswith("https://")) - ): - errors.append(f"{where}: healthCheck must be an http(s) URL or null") + try: + parsed = urlsplit(hc) if isinstance(hc, str) else None + hostname = parsed.hostname if parsed is not None else None + if parsed is not None: + # Accessing .port also rejects malformed/out-of-range ports. + parsed.port + valid_url = ( + parsed is not None + and parsed.scheme in {"http", "https"} + and bool(hostname) + ) + except ValueError: + valid_url = False + if not valid_url: + errors.append( + f"{where}: healthCheck must be an http(s) URL with a host or null" + ) if "targets" in defn and not ( isinstance(defn["targets"], list) and defn["targets"] @@ -143,6 +161,11 @@ def validate(registry): errors.append( f"{where}: targets must be a non-empty list of host/class names" ) + elif ( + "targets" in defn + and len(set(defn["targets"])) != len(defn["targets"]) + ): + errors.append(f"{where}: targets must not repeat a target") if "class" in defn and not ( isinstance(defn["class"], str) and defn["class"] in CLASSES ):