T09 · Insecure Skill Coding Practices
- Location
scripts/save_file.py:17- Finding
Unrestricted Arbitrary File Write
- Content
View full analysis
Vulnerability Details
File Location:
scripts/save_file.py, lines 17–21 and 29–35
Vulnerability Type: Unrestricted file path handling and arbitrary file overwrite
Risk Level: HighVulnerable Code
python # create dir if not exist os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w", encoding="utf-8") as f: f.write(content)The destination and content are supplied directly through command-line arguments:
python parser.add_argument("--path", type=str, required=True, help="Path to save the file") parser.add_argument( "--content", type=str, required=True, help="Content to write into the file", )Technical Analysis
The script accepts an unrestricted caller-controlled path and writes caller-controlled content to it. It does not:
- Restrict output to a designated artifact directory.
- Reject absolute paths or
..traversal components. - Canonicalize the destination before validating it.
- detect symlink-based escapes.
- Prevent unintended replacement of existing files.
The use of
open(file_path, "w")truncates an existing destination before writing. AlthoughSKILL.mddescribes saving generated files under an Agent-specific directory, that intended boundary is not enforced by the executable code.Exploitation requires the attacker to influence the
--pathand potentially--contentarguments passed to the script. This may occur through direct invocation or manipulation of an Agent workflow that derives the output path from untrusted input.Attack Path
- The Skill completes Agent code generation and invokes
save_file.py. - An attacker influences the path argument directly or through manipulated Agent naming or output-path content.
- The attacker supplies an absolute path, a path containing traversal components, or a path that resolves through a symlink.
os.makedirs()creates missing paren ...[truncated 1054 chars]
- Remediation
View remediation
Remediation Suggestions
- Define a fixed, trusted artifact root and permit writes only beneath it.
- Parse the requested destination as a relative path and reject absolute paths and traversal components.
- Resolve both the artifact root and destination with
pathlib.Path.resolve(), then verify that the destination is a descendant of the trusted root. - Validate generated Agent names using a strict allowlist, such as ASCII letters, digits, and underscores.
- Reject symlink destinations and inspect existing parent path components for symlinks where the threat model requires it.
- Avoid overwriting existing files by default. Use exclusive creation mode (
"x") or require explicit authorization before replacement. - Apply restrictive file permissions where generated artifacts may contain sensitive content.
- Prefer passing content through standard input or a protected temporary file if command-line exposure is a concern.
- Add tests covering absolute paths,
../traversal, nested traversal, symlink escapes, existing-file replacement, and valid paths under the artifact root.
Example containment check:
python from pathlib import Path OUTPUT_ROOT = Path("generated_agents").resolve() def safe_destination(relative_path: str) -> Path: requested = Path(relative_path) if requested.is_absolute(): raise ValueError("Absolute paths are not permitted") destination = (OUTPUT_ROOT / requested).resolve() if destination == OUTPUT_ROOT or OUTPUT_ROOT not in destination.parents: raise ValueError("Destination escapes the artifact directory") return destination
