T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/generate-project.py:124
- Finding
- Unrestricted Project Path Allows Arbitrary Recursive Directory Deletion## Vulnerability Details **File Location**: `scripts/generate-project.py`, lines 124–139 **Vulnerability Type**: Unvalidated path used in recursive deletion **Risk Level**: High ### Vulnerable Code ```python # 获取项目目录 if len(sys.argv) > 1: project_name = sys.argv[1] else: project_name = input("请输入项目名称: ").strip() if not project_name: print("❌ 项目名称不能为空") sys.exit(1) project_dir = Path.cwd() / project_name # 检查目录是否已存在 if project_dir.exists(): response = input(f"⚠️ 目录 {project_name} 已存在,是否覆盖?(y/N): ") if response.lower() != 'y': print("操作已取消") sys.exit(0) shutil.rmtree(project_dir) ``` ### Technical Analysis The script accepts `project_name` from a command-line argument or interactive input and uses it directly to construct `project_dir`. It does not require the value to be a simple project name or reject absolute paths, parent-directory traversal components such as `..`, or path separators. With `pathlib`, joining `Path.cwd()` to an absolute user-supplied path selects the absolute path. A relative value containing `..` can likewise resolve outside the current working directory. If that destination exists and the overwrite prompt is confirmed, `shutil.rmtree(project_dir)` recursively removes the selected directory. The confirmation prompt reduces the likelihood of accidental exploitation but does not establish a security boundary. It presents the unvalidated input rather than enforcing and clearly displaying a canonical path confined to an approved project root. The deletion behavior is therefore not limited to directories created or managed by this script. ### Attack Path 1. An attacker, unsafe wrapper, or copied command supplies a traversal or absolute path as the project name, for example: ```bash python scripts/generate-project.py ../valuable-directory ``` 2. The script constructs a destination outside the intended current-directory scope. 3. The external destination already exists, c ...[truncated 924 chars]
- Remediation
- ## Remediation Suggestions 1. Treat the input as a project name rather than an arbitrary path. Reject absolute paths, `.` and `..` components, path separators, empty names, and platform-specific drive or UNC path syntax. 2. Define an explicit, trusted output root and resolve both the root and candidate destination before performing filesystem operations. 3. Verify that the resolved destination is a direct child of the trusted root. Do not rely on string-prefix comparisons; use path-aware containment checks. 4. Refuse to delete filesystem roots, the current working directory, the trusted output root itself, home directories, or any destination outside the approved root. 5. Avoid recursive deletion by default. Prefer failing when a destination exists or require the user to select a new project name. 6. If overwrite support is necessary, display the fully resolved canonical path and require an explicit confirmation that cannot be bypassed by a generic affirmative response. 7. Consider only deleting a directory if it contains a trusted marker proving that it was previously generated by this tool. 8. Add automated tests covering absolute paths, parent traversal, nested paths, symbolic links, filesystem roots, and existing unrelated directories. A hardened validation pattern could resemble: ```python output_root = Path.cwd().resolve() project_name = project_name.strip() candidate_name = Path(project_name) if ( not project_name or candidate_name.is_absolute() or len(candidate_name.parts) != 1 or project_name in {".", ".."} ): raise ValueError("Project name must be a single safe directory name") project_dir = (output_root / project_name).resolve() if project_dir.parent != output_root: raise ValueError("Project directory must be a direct child of the output root") if project_dir.exists(): raise FileExistsError( f"Destination already exists; choose another project name: {project_dir}" ) ```
