T09 · Insecure Skill Coding Practices
- Location
scripts/imagegen.py:350- Finding
Path Traversal Through Server-Controlled Download Filenames
- Content
View full analysis
Vulnerability Details
File Location:
scripts/imagegen.py:350-358
Vulnerability Type: Path traversal and arbitrary file overwrite
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)Technical Analysis
The
taskTypevalue is obtained from an API response and incorporated directly into a local output path. It is not restricted to a safe filename character set, normalized, or checked to ensure that the resulting path remains underout_dir.A malicious value containing traversal components such as
../../can escape the intended output directory. A value beginning with an absolute path can also cause Python'spathlibpath composition to disregardout_dir. The subsequentdownload_filecall opens the resulting path inwbmode, truncating an existing file if one is present.Exploitation requires control over, or compromise of, the relevant AI Hive API response. The final filename is partially constrained by the appended task identifier, sequence number, and extension, but this does not prevent writes outside the configured directory.
Attack Path
- A user submits an image-generation request.
- The client polls the remote API for task results.
- A compromised or malicious API response supplies a crafted
taskTypecontaining traversal components or an absolute path. - The client concatenates that value with
task_id, the result index, and an extension. out_dir / filenameresolves to a location outside the intended download directory.- Content retrieved from
resultUrlis opened inwbmode and written to the attacker-selected path.
Impact As
...[truncated 408 chars]
- Remediation
View remediation
Remediation Suggestions
- Do not use API-controlled metadata as a local filename.
- Generate filenames locally from a random identifier or a strictly validated task identifier.
- If
taskTypemust be retained, allow only a narrow character set such as ASCII letters, digits, underscores, and hyphens. - Resolve the output directory and destination path before writing, then verify that the destination remains a child of the output directory.
- Open newly created files with exclusive-create semantics where overwriting is unnecessary.
- Reject absolute paths, path separators,
.components, and..components.
Example containment check:
python import re safe_task_type = re.sub(r"[^A-Za-z0-9_-]", "_", task_type) base = out_dir.resolve() destination = (base / f"{safe_task_type}_{task_id}_{i+1}{ext}").resolve() if destination.parent != base: raise SystemExit("Unsafe output path")
