T09 · Insecure Skill Coding Practices
- Location
scripts/three_steps.py:18- Finding
Path Traversal Allows File Creation or Overwrite Outside the Project Directory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/three_steps.py, lines 18–35
Vulnerability Type: Path traversal and arbitrary JSON file overwrite
Risk Level: MediumVulnerable Code
python def create_project(project_name): """创建新项目""" ensure_dirs() project = { "name": project_name, "created_at": datetime.now().isoformat(), "status": "draft", "steps": { "step1": {"title": "核心课题", "content": "", "completed": False}, "step2": {"title": "解决方案", "content": "", "completed": False}, "step3": {"title": "传播规划", "content": "", "completed": False} } } project_file = PROJECTS_DIR / f"{project_name}.json" project_file.write_text(json.dumps(project, ensure_ascii=False, indent=2)) return projectTechnical Analysis
The
project_nameparameter originates from the command-line argumentsys.argv[2]and is inserted directly into a filesystem path. The code does not reject path separators, parent-directory components such as.., absolute paths, or other unsafe filename characters.Because
pathlib.Pathpreserves traversal components when joining paths, a project name such as../../targetproduces a destination equivalent to:text state/projects/../../target.jsonAfter filesystem path resolution, this destination is outside the intended
state/projectsdirectory. The use ofPath.write_text()also overwrites an existing destination by default. There is no containment check, filename allowlist, or exclusive-create protection.Exploitation is constrained to locations writable by the process, and the destination receives fixed JSON project data. The implementation also appends
.json, which restricts the target filename. These constraints reduce—but do not eliminate—the potential for destructive file overwrite.Attack Path
- An attacker gains ...[truncated 1134 chars]
- Remediation
View remediation
Remediation Suggestions
- Apply a strict allowlist to project names, permitting only a limited set of characters such as letters, digits, spaces, underscores, and hyphens.
- Explicitly reject absolute paths,
.and..path components, forward slashes, backslashes, and control characters. - Resolve the final destination and verify that it remains beneath the resolved project directory before writing.
- Use exclusive file creation if replacing existing projects is not intended.
- Specify UTF-8 explicitly and provide a controlled error when validation or creation fails.
Example hardening:
python import json import re from pathlib import Path SAFE_PROJECT_NAME = re.compile(r"^[A-Za-z0-9 _-]{1,100}$") def create_project(project_name): ensure_dirs() if not SAFE_PROJECT_NAME.fullmatch(project_name): raise ValueError("Project name contains unsupported characters") base_dir = PROJECTS_DIR.resolve() project_file = (base_dir / f"{project_name}.json").resolve() if project_file.parent != base_dir: raise ValueError("Project path escapes the project directory") project = { "name": project_name, "created_at": datetime.now().isoformat(), "status": "draft", "steps": { "step1": { "title": "Core Challenge", "content": "", "completed": False }, "step2": { "title": "Solution", "content": "", "completed": False }, "step3": { "title": "Communication Plan", "content": "", "completed": False } } } with project_file.open("x", encoding="utf-8") as output: json.dump(project, output, ensure_ascii=False, indent=2) return projectIf legitimate project replacement is r ...[truncated 143 chars]
