T09 · Insecure Skill Coding Practices
- Location
scripts/imagegen.py:340- Finding
Server-Controlled Path Traversal in Result Downloads
- Content
View full analysis
Vulnerability Details
File Location:
scripts/imagegen.py, lines 340-361
Vulnerability Type: Path traversal leading to arbitrary file overwrite
Risk Level: Mediumpython # Download results out_dir = Path(output_dir) out_dir.mkdir(parents=True, exist_ok=True) 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) # Last 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 application constructs output paths using
taskType, which is taken directly from the remote task response. The value is not restricted to a safe filename character set, reduced to a basename, or checked for path separators and traversal sequences.When a value containing components such as
../is joined without_dir, Python'sPathobject preserves those components. The resulting path can therefore resolve outside the user-selected output directory. The subsequentdownload_file()operation opens that path inwbmode, allowing an existing accessible file to be overwritten with downloaded content.The fixed API endpoint reduces exposure under normal operation, but the client still treats remotely supplied task metadata as trusted filesystem input. Exploitation would require a malicious or compromised API service, manipulation of the API response in a trusted network path, or another condition that lets an attacker control task metadata.
Attack Path
- The user submits or queries an image-generation task.
- A malicious or compromised API ...[truncated 1504 chars]
- Remediation
View remediation
Remediation Suggestions
- Do not use remotely supplied task metadata as a filesystem path component.
- Generate filenames from locally controlled identifiers and fixed prefixes.
- If
taskTypemust be retained, normalize it to a strict allowlist such as[A-Za-z0-9_-]and reject all path separators,.components, control characters, and empty values. - Resolve both the output directory and destination path, then verify containment before opening the file:
python import re safe_task_type = re.sub(r"[^A-Za-z0-9_-]", "_", task_type) safe_task_id = re.sub(r"[^A-Za-z0-9_-]", "_", str(task_id)) base_dir = Path(output_dir).expanduser().resolve() base_dir.mkdir(parents=True, exist_ok=True) filename = f"{safe_task_type}_{safe_task_id}_{i + 1}{ext}" destination = (base_dir / filename).resolve() if destination.parent != base_dir: raise SystemExit("Unsafe output path returned by task service")- Apply the same validation to regular result files and last-frame files.
- Consider exclusive file creation or explicit overwrite confirmation to prevent unintended replacement of existing files.
- Add automated tests covering absolute paths,
../, nested traversal, platform-specific separators, control characters, and malicious task identifiers.
