T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/kubernetes_devops_toolkit.py:406
- Finding
- Helm operations ignore the caller-specified kubeconfig and may target an unintended cluster<![CDATA[ ## Vulnerability Details **File Location**: `scripts/kubernetes_devops_toolkit.py:406-439` **Additional Affected Locations**: `scripts/kubernetes_devops_toolkit.py:459-477`, `scripts/kubernetes_devops_toolkit.py:495-500`, `scripts/kubernetes_devops_toolkit.py:516-527` **Vulnerability Type**: Cluster-target configuration mismatch **Risk Level**: High ### Vulnerable Code ```python def __init__(self, kubeconfig_path: Optional[str] = None): self.kubeconfig_path = kubeconfig_path or "~/.kube/config" def install(self, release_name: str, chart: str, namespace: str = "default", values: Optional[Dict[str, Any]] = None, version: Optional[str] = None) -> bool: """ Install a Helm chart. Args: release_name: Name for the release chart: Chart reference (repo/chart or path) namespace: Target namespace values: Values to override version: Chart version Returns: bool: True if installation successful """ # Implementation would use helm CLI or pyhelm import subprocess cmd = ["helm", "install", release_name, chart, "-n", namespace] if values: for key, val in values.items(): cmd.extend(["--set", f"{key}={val}"]) if version: cmd.extend(["--version", version]) try: result = subprocess.run(cmd, capture_output=True, text=True, check=True) return True except subprocess.CalledProcessError as e: print(f"Helm install failed: {e.stderr}") return False ``` The same omission is present in the `upgrade`, `rollback`, and `list_releases` command construction. ### Technical Analysis `HelmManager` accepts and stores a `kubeconfig_path`, creating the reasonable expectation that Helm operations will use that configuration. However, none of the generated Helm commands includes `--kubeconfig`, and the subprocess environment is not configured with an equivalent `KUBECONFIG` value. C ...[truncated 1948 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Pass the selected kubeconfig explicitly to every Helm invocation: ```python from pathlib import Path kubeconfig = str(Path(self.kubeconfig_path).expanduser().resolve()) cmd = [ "helm", "install", release_name, chart, "--namespace", namespace, "--kubeconfig", kubeconfig, ] ``` 2. Apply the same correction to `install`, `upgrade`, `rollback`, and `list_releases`. 3. Validate that the kubeconfig exists, is a regular file, and has appropriately restrictive permissions before invoking Helm. 4. Consider accepting an explicit context and passing `--kube-context` to prevent reliance on the kubeconfig's current context. 5. Before mutating a cluster, retrieve and display or verify the selected context and cluster endpoint. 6. Require explicit confirmation for high-impact operations when the selected context appears to be production. 7. Add mocked subprocess tests asserting that every generated Helm command includes the expected `--kubeconfig` and, when configured, `--kube-context` arguments. ]]>
