T09 · Insecure Skill Coding Practices
- Location
scripts/videogen.py:397- Finding
Untrusted API Response Fields Allow Output Path Traversal
- Content
View full analysis
Vulnerability Details
File Location:
scripts/videogen.py:397-414
Vulnerability Type: Path traversal and arbitrary file write
Risk Level: Mediumpython task_type = task.get("taskType", "task") for i, item in enumerate(succeeded): result_url = item.get("resultUrl") if not result_url: continue ext = ".mp4" if "video" in task_type.lower() else ".png" filename = f"{task_type}_{task_id}_{i+1}{ext}" out_path = out_dir / filename download_file(result_url, out_path) # 尾帧 for item in succeeded: last_frame = item.get("lastFrameUrl") if last_frame: out_path = out_dir / f"{task_type}_{task_id}_lastframe.png" download_file(last_frame, out_path)Technical Analysis
The code constructs output paths using
taskTypeandtaskIdwithout validating or sanitizing either value. These identifiers can originate from API responses, particularly when a generation request returns itstaskIdand a subsequent polling response suppliestaskType.If either value contains an absolute path or traversal components such as
../, the expressionout_dir / filenamedoes not guarantee that the resulting file remains beneathout_dir. For example, an absolutetaskTypecan causepathlibto discard the preceding output directory. Traversal components can similarly resolve outside that directory.The destination is then passed to
download_file(), which opens it in write mode:python with open(str(out_path), "wb") as f:Existing files at a resolved destination can therefore be truncated and replaced. Exploitation requires control of, or the ability to compromise, the configured API endpoint or its responses.
Attack Path
- An attacker controls or compromises the API server used by the client. This can also occur if the user is induced to configure an attacker-controlled
--base-url. - The malicious server ...[truncated 1069 chars]
- An attacker controls or compromises the API server used by the client. This can also occur if the user is induced to configure an attacker-controlled
- Remediation
View remediation
Remediation Suggestions
-
Validate
taskIdandtaskTypeagainst a strict allowlist, such as^[A-Za-z0-9_-]+$. -
Do not use server-provided descriptive fields directly in local filenames. Prefer locally generated identifiers, such as a UUID.
-
Resolve and verify every destination before opening it:
python out_dir = Path(output_dir).expanduser().resolve() out_dir.mkdir(parents=True, exist_ok=True) safe_task_type = sanitize_identifier(task.get("taskType", "task")) safe_task_id = sanitize_identifier(task_id) destination = ( out_dir / f"{safe_task_type}_{safe_task_id}_{i + 1}{ext}" ).resolve() if destination.parent != out_dir: raise SystemExit("Unsafe output path returned by API") -
Use exclusive creation mode (
"xb") when overwriting existing files is unnecessary. -
Consider assigning fixed local extensions and filenames independently of all remote metadata.
-
Add tests covering absolute paths,
../traversal, path separators, empty identifiers, and platform-specific path syntax.
-
