fix(gitops): wire app_name and harden compose validator for env_file and volumes
- Pass app_name=canonical_app_name to validate_compose_security in execute_deployment - Block env_file path escapes outside workload root to prevent secret exfiltration - Normalize string security_opt directives before capability validation - Reject ambiguous volume paths not declared in top-level volumes - Add unit tests verifying env_file containment, string security_opt, and volume safety
This commit is contained in:
1 parent
66c5e4280a
commit
ea0ce0b1d6
2 files changed
+121
-3
No files matched your search
@@ -370,11 +370,41 @@ def validate_compose_security(
|
|||||||
return False, f"Service '{svc_name}' specifies prohibited host device mount: {devices}."
|
return False, f"Service '{svc_name}' specifies prohibited host device mount: {devices}."
|
||||||
|
|
||||||
sec_opts = svc.get("security_opt", [])
|
sec_opts = svc.get("security_opt", [])
|
||||||
|
if isinstance(sec_opts, str):
|
||||||
|
sec_opts = [sec_opts]
|
||||||
if isinstance(sec_opts, list):
|
if isinstance(sec_opts, list):
|
||||||
for opt in sec_opts:
|
for opt in sec_opts:
|
||||||
if any(bad in str(opt).lower() for bad in ("unconfined", "disable")):
|
if any(bad in str(opt).lower() for bad in ("unconfined", "disable")):
|
||||||
return False, f"Service '{svc_name}' specifies prohibited security_opt: {opt}."
|
return False, f"Service '{svc_name}' specifies prohibited security_opt: {opt}."
|
||||||
|
|
||||||
|
raw_env_files = svc.get("env_file")
|
||||||
|
if raw_env_files:
|
||||||
|
env_file_list: list[str] = []
|
||||||
|
if isinstance(raw_env_files, str):
|
||||||
|
env_file_list = [raw_env_files]
|
||||||
|
elif isinstance(raw_env_files, list):
|
||||||
|
for item in raw_env_files:
|
||||||
|
if isinstance(item, str):
|
||||||
|
env_file_list.append(item)
|
||||||
|
elif isinstance(item, dict) and "path" in item:
|
||||||
|
env_file_list.append(str(item["path"]))
|
||||||
|
elif isinstance(raw_env_files, dict) and "path" in raw_env_files:
|
||||||
|
env_file_list = [str(raw_env_files["path"])]
|
||||||
|
|
||||||
|
for env_path in env_file_list:
|
||||||
|
clean_env = env_path.strip()
|
||||||
|
if not clean_env:
|
||||||
|
continue
|
||||||
|
if clean_env.startswith("~"):
|
||||||
|
resolved_env = Path(clean_env).expanduser().resolve()
|
||||||
|
elif Path(clean_env).is_absolute():
|
||||||
|
resolved_env = Path(clean_env).resolve()
|
||||||
|
else:
|
||||||
|
resolved_env = (allowed_root / clean_env).resolve()
|
||||||
|
|
||||||
|
if not (allowed_root.is_dir() and resolved_env.is_relative_to(canonical_allowed_root)):
|
||||||
|
return False, f"Service '{svc_name}' specifies prohibited env_file escaping workload root: '{clean_env}'."
|
||||||
|
|
||||||
for ns_key in ("network_mode", "pid", "ipc", "uts", "userns_mode"):
|
for ns_key in ("network_mode", "pid", "ipc", "uts", "userns_mode"):
|
||||||
val = str(svc.get(ns_key, "")).strip().lower()
|
val = str(svc.get(ns_key, "")).strip().lower()
|
||||||
if val in PROHIBITED_HOST_NAMESPACES:
|
if val in PROHIBITED_HOST_NAMESPACES:
|
||||||
@@ -403,6 +433,10 @@ def validate_compose_security(
|
|||||||
if host_path in top_volumes and not (host_path.startswith((".", "/", "~")) or "/" in host_path or "\\" in host_path):
|
if host_path in top_volumes and not (host_path.startswith((".", "/", "~")) or "/" in host_path or "\\" in host_path):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Undeclared named volumes or bare identifiers not starting with ., /, ~ or containing / are prohibited
|
||||||
|
if not (host_path.startswith((".", "/", "~")) or "/" in host_path or "\\" in host_path):
|
||||||
|
return False, f"Service '{svc_name}' specifies undeclared named volume or ambiguous path: '{host_path}'."
|
||||||
|
|
||||||
# Host bind mount evaluation
|
# Host bind mount evaluation
|
||||||
if host_path.startswith("~"):
|
if host_path.startswith("~"):
|
||||||
expanded = Path(host_path).expanduser().resolve()
|
expanded = Path(host_path).expanduser().resolve()
|
||||||
@@ -465,6 +499,14 @@ def execute_deployment(target_dir: Path, deployment_config: dict, branch: str) -
|
|||||||
logger.info(f"Skipping deployment: pushed branch '{branch}' != target branch '{expected_branch}'")
|
logger.info(f"Skipping deployment: pushed branch '{branch}' != target branch '{expected_branch}'")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
canonical_app_name = target_dir.name.removeprefix("homelab-")
|
||||||
|
manifest_file = target_dir / "app.yaml"
|
||||||
|
if manifest_file.is_file():
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
pre_manifest = (yaml.safe_load(manifest_file.read_text(encoding="utf-8")) if yaml else parse_yaml_simple(manifest_file.read_text(encoding="utf-8"))) or {}
|
||||||
|
if isinstance(pre_manifest, dict) and pre_manifest.get("name"):
|
||||||
|
canonical_app_name = str(pre_manifest["name"]).strip()
|
||||||
|
|
||||||
logger.info(f"🚀 Deploying '{target_dir.name}' at {target_dir} (branch: {expected_branch})...")
|
logger.info(f"🚀 Deploying '{target_dir.name}' at {target_dir} (branch: {expected_branch})...")
|
||||||
actions = deployment_config.get("actions", ["git_pull"])
|
actions = deployment_config.get("actions", ["git_pull"])
|
||||||
|
|
||||||
@@ -495,6 +537,9 @@ def execute_deployment(target_dir: Path, deployment_config: dict, branch: str) -
|
|||||||
logger.error(f"❌ Post-pull manifest validation failed for '{target_dir.name}': {err_msg}")
|
logger.error(f"❌ Post-pull manifest validation failed for '{target_dir.name}': {err_msg}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if isinstance(fresh_manifest, dict) and fresh_manifest.get("name"):
|
||||||
|
canonical_app_name = str(fresh_manifest["name"]).strip()
|
||||||
|
|
||||||
fresh_config = fresh_manifest.get("deployment")
|
fresh_config = fresh_manifest.get("deployment")
|
||||||
if isinstance(fresh_config, dict):
|
if isinstance(fresh_config, dict):
|
||||||
fresh_branch = fresh_config.get("branch", "main")
|
fresh_branch = fresh_config.get("branch", "main")
|
||||||
@@ -514,7 +559,7 @@ def execute_deployment(target_dir: Path, deployment_config: dict, branch: str) -
|
|||||||
if not compose_file.is_file():
|
if not compose_file.is_file():
|
||||||
compose_file = target_dir / "docker-compose.yaml"
|
compose_file = target_dir / "docker-compose.yaml"
|
||||||
if compose_file.is_file():
|
if compose_file.is_file():
|
||||||
valid_sec, sec_err = validate_compose_security(compose_file, target_dir)
|
valid_sec, sec_err = validate_compose_security(compose_file, target_dir, app_name=canonical_app_name)
|
||||||
if not valid_sec:
|
if not valid_sec:
|
||||||
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
||||||
return False
|
return False
|
||||||
@@ -528,7 +573,7 @@ def execute_deployment(target_dir: Path, deployment_config: dict, branch: str) -
|
|||||||
if not compose_file.is_file():
|
if not compose_file.is_file():
|
||||||
compose_file = target_dir / "docker-compose.yaml"
|
compose_file = target_dir / "docker-compose.yaml"
|
||||||
if compose_file.is_file():
|
if compose_file.is_file():
|
||||||
valid_sec, sec_err = validate_compose_security(compose_file, target_dir)
|
valid_sec, sec_err = validate_compose_security(compose_file, target_dir, app_name=canonical_app_name)
|
||||||
if not valid_sec:
|
if not valid_sec:
|
||||||
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
||||||
return False
|
return False
|
||||||
@@ -546,7 +591,7 @@ def execute_deployment(target_dir: Path, deployment_config: dict, branch: str) -
|
|||||||
if not compose_file.is_file():
|
if not compose_file.is_file():
|
||||||
compose_file = target_dir / "docker-compose.yaml"
|
compose_file = target_dir / "docker-compose.yaml"
|
||||||
if compose_file.is_file():
|
if compose_file.is_file():
|
||||||
valid_sec, sec_err = validate_compose_security(compose_file, target_dir)
|
valid_sec, sec_err = validate_compose_security(compose_file, target_dir, app_name=canonical_app_name)
|
||||||
if not valid_sec:
|
if not valid_sec:
|
||||||
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
logger.error(f"❌ Compose security validation failed for '{target_dir.name}': {sec_err}")
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -896,6 +896,79 @@ def test_validate_compose_security_rejects_symlink_escape(tmp_path: Path):
|
|||||||
assert err is not None and "symlink" in err.lower()
|
assert err is not None and "symlink" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_rejects_escaping_env_file(tmp_path: Path):
|
||||||
|
"""Ensure services cannot use env_file to read secrets outside the workload root."""
|
||||||
|
app_dir = tmp_path / "my-app"
|
||||||
|
app_dir.mkdir(parents=True)
|
||||||
|
(app_dir / "valid.env").write_text("A=1\n", encoding="utf-8")
|
||||||
|
|
||||||
|
# 1. Escaping absolute path
|
||||||
|
compose_file = app_dir / "docker-compose.yml"
|
||||||
|
compose_file.write_text("""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
env_file: /run/secrets/homeserver.env
|
||||||
|
""", encoding="utf-8")
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(compose_file, app_dir, app_name="my-app")
|
||||||
|
assert valid is False
|
||||||
|
assert err is not None and "env_file" in err.lower()
|
||||||
|
|
||||||
|
# 2. Escaping relative path in list
|
||||||
|
compose_file.write_text("""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
env_file:
|
||||||
|
- valid.env
|
||||||
|
- ../../Core/.env
|
||||||
|
""", encoding="utf-8")
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(compose_file, app_dir, app_name="my-app")
|
||||||
|
assert valid is False
|
||||||
|
assert err is not None and "env_file" in err.lower()
|
||||||
|
|
||||||
|
# 3. Valid local env_file
|
||||||
|
compose_file.write_text("""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
env_file: valid.env
|
||||||
|
""", encoding="utf-8")
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(compose_file, app_dir, app_name="my-app")
|
||||||
|
assert valid is True
|
||||||
|
assert err is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_rejects_undeclared_named_volumes(tmp_path: Path):
|
||||||
|
"""Ensure ambiguous volume paths not declared in top-level volumes are rejected."""
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text("""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
volumes:
|
||||||
|
- rogue_named_volume:/data
|
||||||
|
""", encoding="utf-8")
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(compose_file, tmp_path, app_name="my-app")
|
||||||
|
assert valid is False
|
||||||
|
assert err is not None and "undeclared" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_handles_string_security_opt(tmp_path: Path):
|
||||||
|
"""Ensure security_opt specified as string instead of list is strictly checked."""
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text("""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
security_opt: "apparmor:unconfined"
|
||||||
|
""", encoding="utf-8")
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(compose_file, tmp_path, app_name="my-app")
|
||||||
|
assert valid is False
|
||||||
|
assert err is not None and "security_opt" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user