T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/python_env_tool.py:37
- Finding
- Arbitrary Recursive Directory Deletion via User-Controlled Virtual Environment Path## Vulnerability Details **File Location**: `scripts/python_env_tool.py`, lines 37–40 **Vulnerability Type**: Unrestricted recursive deletion of a caller-controlled path **Risk Level**: High **Vulnerable Code**: ```python def cmd_bootstrap(args: argparse.Namespace) -> int: venv = Path(args.venv) if args.recreate and venv.exists(): shutil.rmtree(venv) if not venv.exists(): _run([sys.executable, "-m", "venv", str(venv)]) ``` The affected path is supplied through the unrestricted `--venv` argument at lines 91–92: ```python bootstrap.add_argument("--venv", default=".venv", help="venv path (default: .venv)") bootstrap.add_argument("--recreate", action="store_true", help="delete and recreate venv") ``` ### Technical Analysis The `bootstrap` command converts the caller-provided `--venv` value directly into a `Path`. If `--recreate` is present and that path exists, the program passes it to `shutil.rmtree()` without validating that: - The path is a virtual environment. - The path is inside the current project. - The resolved path is not the project root, user home, or another sensitive directory. - The path contains a virtual-environment marker such as `pyvenv.cfg`. - The user has explicitly confirmed the resolved deletion target. This creates a destructive path-manipulation vulnerability. An accidental argument or attacker-influenced Agent instruction can select any writable directory for recursive deletion. Argument-list execution in `_run()` prevents shell injection, but it does not mitigate the unsafe filesystem operation. ### Attack Path 1. An attacker-controlled task, prompt, automation input, or operator mistake influences the value passed to `--venv`. 2. The helper is invoked with both `--recreate` and a path to a valuable writable directory, for example: ```bash scripts/python_env_tool.py bootstrap --recreate --venv /path/to/writable/data ``` 3. `Path ...[truncated 833 chars]
- Remediation
- ## Remediation Suggestions 1. Resolve and normalize both the project root and requested virtual-environment path before any deletion. 2. Require the deletion target to be a strict descendant of the project root; reject the project root itself and all paths outside it. 3. Reject filesystem roots, the current user's home directory, empty paths, `.` and other sensitive targets. 4. Before deletion, require a recognized virtual-environment marker such as `pyvenv.cfg` and verify the expected interpreter layout. 5. Consider restricting `--recreate` to a fixed project-local directory such as `.venv` rather than accepting arbitrary paths. 6. Display the resolved deletion target and require explicit confirmation for destructive operations, with a separate carefully named noninteractive override for trusted automation. 7. Refuse deletion when validation is ambiguous, including unexpected symlink or path-resolution conditions. 8. Add automated tests covering absolute paths, parent traversal, project-root selection, home-directory selection, filesystem roots, symlinks, and directories that are not virtual environments.
