T09 · Insecure Skill Coding Practices
- Location
tool_result_size_controller.py:31- Finding
Path Traversal Enables Writes Outside the Results Directory
- Content
View full analysis
Vulnerability Details
File Location:
tool_result_size_controller.py, lines 31-34 and 51-81
Vulnerability Type: Path traversal and unsafe file creation
Risk Level: HighVulnerable Code
python def get_result_path(tool_name: str, content_hash: str) -> Path: """生成结果文件路径""" date_str = datetime.now().strftime("%Y%m%d") return RESULTS_DIR / f"{tool_name}-{date_str}-{content_hash[:8]}.json.gz"The resulting path is subsequently opened for writing:
python def spill_to_disk(tool_name: str, content: Union[str, dict, list], compress: bool = True) -> dict: """ 将大结果写入磁盘 """ ensure_results_dir() # 序列化内容 if isinstance(content, str): content_str = content else: content_str = json.dumps(content, ensure_ascii=False, indent=2) # 计算hash和路径 content_hash = hashlib.md5(content_str.encode()).hexdigest() result_path = get_result_path(tool_name, content_hash) # 写入文件 if compress: with gzip.open(result_path, 'wt', encoding='utf-8') as f: f.write(content_str) else: result_path = result_path.with_suffix('.json') with open(result_path, 'w', encoding='utf-8') as f: f.write(content_str)Technical Analysis
The caller-controlled
tool_nameis inserted directly into a filesystem path without validating path separators, absolute paths, or parent-directory components such as../.Python's
pathlibpath composition does not guarantee that the resulting path remains underRESULTS_DIR. In particular, an absolute path in the right-hand operand can replace the intended base directory. Relative traversal sequences can similarly escape the directory when the path is resolved by the operating system.The code then opens the calculated path in write mode. This operation truncates an existing destination and follows symbolic links. No ca ...[truncated 1730 chars]
- Remediation
View remediation
Remediation Suggestions
-
Restrict
tool_nameto a conservative identifier format such as^[A-Za-z0-9_.-]+$. -
Explicitly reject absolute paths, path separators,
.components, and..components. -
Resolve the destination and verify that it remains beneath the canonical results directory before opening it:
python base = RESULTS_DIR.resolve() destination = (base / generated_name).resolve() if not destination.is_relative_to(base): raise ValueError("Result path escapes the configured results directory") -
Avoid using caller-controlled text as a filename. Generate an opaque random identifier and retain the tool name only as metadata.
-
Open new files atomically and exclusively so existing files cannot be silently truncated.
-
Use operating-system facilities that prevent following symbolic links where available.
-
Create the results directory with restrictive permissions and verify that it is not a symbolic link before use.
-
Add tests covering absolute paths, nested separators,
../traversal, symbolic links, and concurrent creation.
-
