T09 · Insecure Skill Coding Practices
- Location
__init__.py:81- Finding
Path Traversal and Unintended File Overwrite Through Unvalidated Workflow Identifier
- Content
View full analysis
Vulnerability Details
File Location:
__init__.py, lines 58–59 and 81–83
Vulnerability Type: Path traversal leading to an arbitrary file write
Risk Level: Mediumpython if not (output_path.exists() and output_path.is_dir()): raise ValueError(f"输出路径无效:{output_path} 必须是绝对且存在的目录") # 4. 保存规格(终结一致性) skill_id = f"skill-{workflow_id.split('-')[1]}-{workflow_id.split('-')[2]}" saved_specs = output_path / f"{skill_id}_specs.json" content = { "skill_id": skill_id, "checksums": {"findings": find_ck, "decisions": dec_ck}, "locked_thresh": locked_thresh, "exec_copy": exec_copy, "vlm_layers": multimodal_layers, "check_status": check_status } saved_specs.write_text(str(content))Technical Analysis
The documented format of
workflow_idiswrk-YYYYMMDD-XXXX, but the implementation does not validate that format. Instead, it splits the caller-controlled value and directly incorporates its second and third components into a filesystem path.A third component containing path separators and traversal sequences such as
../can cause the resultingsaved_specspath to escape the intended output directory. For example, a value shaped likewrk-a-../../victimproduces a path resembling:text <output_path>/skill-a-/../../victim_specs.jsonExploitation requires the intermediate
skill-a-directory to exist, but this may be feasible when the attacker can write within or otherwise influence the output directory. In addition,Path.write_text()follows symbolic links, so predictable destination names can permit a symlink-based overwrite if the output directory is attacker-writable.The validation at lines 58–59 only checks that
output_pathexists and is a directory. Despite the error message stating that the path must be absolute, the implementation does not calloutput_path.is_absolute(). It also does not resolve the final destination and verify that ...[truncated 1434 chars]- Remediation
View remediation
Remediation Suggestions
-
Strictly validate
workflow_idbefore using it:python import re if not re.fullmatch(r"wrk-\d{8}-[A-Za-z0-9]+", workflow_id): raise ValueError("Invalid workflow_id format") -
Require and normalize an absolute output directory:
python if not output_path.is_absolute(): raise ValueError("output_path must be absolute") output_root = output_path.resolve(strict=True) if not output_root.is_dir(): raise ValueError("output_path must be an existing directory") -
Resolve and constrain the destination before writing:
python destination = (output_root / f"{skill_id}_specs.json").resolve(strict=False) if destination.parent != output_root: raise ValueError("Destination escapes the approved output directory") -
Reject symbolic-link destinations and avoid silently overwriting existing files. Use exclusive creation where replacement is not required:
python if destination.is_symlink(): raise ValueError("Symbolic-link destinations are not allowed") with destination.open("x", encoding="utf-8") as file: file.write(serialized_content) -
Where stronger protection against symlink races is required, use directory-file-descriptor-based operations and platform facilities such as
O_NOFOLLOWandO_EXCL. -
Add tests covering malformed identifiers, absolute path components, traversal sequences, existing symlinks, duplicate filenames, relative output paths, and destinations outside the approved output directory.
-
