T09 · Insecure Skill Coding Practices
- Location
scripts/imagegen.py:390- Finding
API-Controlled Path Traversal Allows Arbitrary File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
scripts/imagegen.py:390-408
Vulnerability Type: Path traversal and arbitrary file write
Risk Level: HighVulnerable Code
python 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) # Tail frame 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 local destination filename is constructed from
task_typeandtask_id. These values originate from API responses and are not validated or normalized before being joined with the configured output directory.Python's
pathlibjoining behavior does not guarantee containment beneathout_dir. A value containing parent-directory sequences such as../../can escape the intended directory. An absolute path component can also cause the preceding output directory to be discarded.The resulting path is passed to
download_file(), which opens the destination in binary write mode. Consequently, an attacker who controls or compromises the API response can select both the downloaded content and, through crafted path components, its destination.Exploitation requires control over, or compromise of, the configured API service. That condition is plausible because the base URL can be changed through the
--base-urlargument, theAI_HIVE_BASE_URLenvironment variable, or the local configuration file.Attack Path
- The victim invokes image generation while connected to a malicious or compromised API endpoint.
- The endpoint returns a craft ...[truncated 1254 chars]
- Remediation
View remediation
Remediation Suggestions
-
Do not use remote response fields directly in local filenames. Generate destination names from trusted constants and locally generated identifiers.
-
Enforce a strict allowlist for identifiers, such as ASCII letters, digits, underscores, and hyphens.
-
Explicitly reject
/,\,.., control characters, null bytes, and absolute path components. -
Resolve and verify the final path before writing:
python output_root = Path(output_dir).resolve() destination = (output_root / safe_filename).resolve() if output_root not in destination.parents: raise SystemExit("Unsafe output path") -
Consider using
Path.nameonly as a secondary defense; it should not replace strict input validation. -
Avoid silently overwriting existing files. Use exclusive creation mode, generate collision-resistant local names, or require explicit overwrite confirmation.
-
Validate
taskIdimmediately when received and validatetaskTypebefore any filesystem operation. -
Restrict custom API endpoints to HTTPS and warn users before sending credentials or trusting file metadata from a non-default endpoint.
-
