fix(compose): enforce host containment on dictionary volumes and reject variable interpolation
- Default dictionary volume declarations omitting 'type: bind' to host containment checks when 'source' is specified (CR-DI-01)
- Reject volume host paths containing dynamic variable interpolation (${...}) to prevent runtime boundary escapes (CR-DI-02)
- Add security regression tests in tests/security/test_gitops_security.py
This commit is contained in:
1 parent
03c8fdcf8e
commit
178a0c8990
2 files changed
+106
-5
No files matched your search
@@ -110,7 +110,9 @@ def execute_deployment_via_daemon(
|
|||||||
timeout=330.0,
|
timeout=330.0,
|
||||||
)
|
)
|
||||||
if resp.success:
|
if resp.success:
|
||||||
logger.info(f"✨ Deployment of '{target}' via daemon completed successfully.")
|
logger.info(
|
||||||
|
f"✨ Deployment of '{target}' via daemon completed successfully."
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
err_msg = resp.error.message if resp.error else "Unknown daemon error"
|
err_msg = resp.error.message if resp.error else "Unknown daemon error"
|
||||||
logger.error(f"❌ Deployment of '{target}' via daemon failed: {err_msg}")
|
logger.error(f"❌ Deployment of '{target}' via daemon failed: {err_msg}")
|
||||||
@@ -286,7 +288,7 @@ def get_trusted_repository_mapping() -> dict[str, Path]:
|
|||||||
if not raw_path:
|
if not raw_path:
|
||||||
continue
|
continue
|
||||||
if raw_path.startswith("~/Sites/") or raw_path == "~/Sites":
|
if raw_path.startswith("~/Sites/") or raw_path == "~/Sites":
|
||||||
sub = raw_path[len("~/Sites"):].lstrip("/")
|
sub = raw_path[len("~/Sites") :].lstrip("/")
|
||||||
p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve()
|
p = (SITES_DIR / sub).resolve() if sub else SITES_DIR.resolve()
|
||||||
else:
|
else:
|
||||||
p = Path(raw_path).expanduser().resolve()
|
p = Path(raw_path).expanduser().resolve()
|
||||||
@@ -407,8 +409,6 @@ def resolve_target(repo_name: str) -> tuple[Path | None, dict]:
|
|||||||
return target_dir, deployment_config
|
return target_dir, deployment_config
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PROHIBITED_HOST_NAMESPACES = {"host"}
|
PROHIBITED_HOST_NAMESPACES = {"host"}
|
||||||
DANGEROUS_SECURITY_OPTS = {"unconfined", "apparmor:unconfined", "seccomp:unconfined"}
|
DANGEROUS_SECURITY_OPTS = {"unconfined", "apparmor:unconfined", "seccomp:unconfined"}
|
||||||
|
|
||||||
@@ -607,6 +607,8 @@ def validate_compose_security(
|
|||||||
)
|
)
|
||||||
if ":" in vol:
|
if ":" in vol:
|
||||||
host_path = vol.split(":")[0].strip()
|
host_path = vol.split(":")[0].strip()
|
||||||
|
else:
|
||||||
|
host_path = vol.strip()
|
||||||
elif isinstance(vol, dict):
|
elif isinstance(vol, dict):
|
||||||
src = str(vol.get("source", ""))
|
src = str(vol.get("source", ""))
|
||||||
if "/var/run/docker.sock" in src:
|
if "/var/run/docker.sock" in src:
|
||||||
@@ -614,12 +616,19 @@ def validate_compose_security(
|
|||||||
False,
|
False,
|
||||||
f"Service '{svc_name}' directly mounts Docker socket: {src}.",
|
f"Service '{svc_name}' directly mounts Docker socket: {src}.",
|
||||||
)
|
)
|
||||||
if vol.get("type") == "bind":
|
vol_type = vol.get("type")
|
||||||
|
if vol_type in ("bind", "volume") or (vol_type is None and src):
|
||||||
host_path = src
|
host_path = src
|
||||||
|
|
||||||
if not host_path:
|
if not host_path:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if "${" in host_path or "}" in host_path:
|
||||||
|
return (
|
||||||
|
False,
|
||||||
|
f"Service '{svc_name}' specifies prohibited variable interpolation in volume host path: '{host_path}'.",
|
||||||
|
)
|
||||||
|
|
||||||
# Declared top-level named volume without path separators
|
# Declared top-level named volume without path separators
|
||||||
if host_path in top_volumes and not (
|
if host_path in top_volumes and not (
|
||||||
host_path.startswith((".", "/", "~"))
|
host_path.startswith((".", "/", "~"))
|
||||||
|
|||||||
@@ -1068,3 +1068,95 @@ services:
|
|||||||
)
|
)
|
||||||
assert valid is False
|
assert valid is False
|
||||||
assert err is not None and "security_opt" in err.lower()
|
assert err is not None and "security_opt" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_rejects_dict_volume_without_type_bind(
|
||||||
|
tmp_path: Path,
|
||||||
|
):
|
||||||
|
"""Ensure dictionary volume declarations without explicit type: bind enforce host containment (CR-DI-01)."""
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text(
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
volumes:
|
||||||
|
- source: /etc/shadow
|
||||||
|
target: /app/shadow
|
||||||
|
""",
|
||||||
|
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 "prohibited host bind mount" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_rejects_variable_interpolation_in_volume_host_path(
|
||||||
|
tmp_path: Path,
|
||||||
|
):
|
||||||
|
"""Ensure volume host paths containing variable interpolation (${...}) fail closed (CR-DI-02)."""
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text(
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
volumes:
|
||||||
|
- "${HOST_DIR}/data:/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 "variable interpolation" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_rejects_variable_interpolation_in_dict_volume_source(
|
||||||
|
tmp_path: Path,
|
||||||
|
):
|
||||||
|
"""Ensure dictionary volume source containing variable interpolation (${...}) fails closed (CR-DI-02)."""
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text(
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
volumes:
|
||||||
|
- source: "${HOST_DIR}/data"
|
||||||
|
target: /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 "variable interpolation" in err.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_compose_security_allows_valid_dict_volume_within_workload(
|
||||||
|
tmp_path: Path,
|
||||||
|
):
|
||||||
|
"""Ensure valid dictionary volume within workload root is accepted even without type: bind."""
|
||||||
|
(tmp_path / "data").mkdir()
|
||||||
|
compose_file = tmp_path / "docker-compose.yml"
|
||||||
|
compose_file.write_text(
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: alpine
|
||||||
|
volumes:
|
||||||
|
- source: ./data
|
||||||
|
target: /data
|
||||||
|
""",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
valid, err = gitops_dispatcher.validate_compose_security(
|
||||||
|
compose_file, tmp_path, app_name="my-app"
|
||||||
|
)
|
||||||
|
assert valid is True
|
||||||
|
assert err is None
|
||||||
Reference in new issue
Block a user