T09 · Insecure Skill Coding Practices
- Location
scripts/improve_art.py:12- Finding
Arbitrary File Overwrite Through Unrestricted Output Path
- Content
View full analysis
Vulnerability Details
File Location:
scripts/improve_art.py, lines 12–17 and 24–29
Vulnerability Type: Arbitrary file overwrite
Risk Level: MediumComplete Code Snippet:
python def generate_image(original_path, suggestions, output_path): # Placeholder – actual implementation should invoke an image-generation model # This demonstration uses copy as a placeholder from shutil import copyfile copyfile(original_path, output_path) return output_path if __name__ == "__main__": if len(sys.argv) < 4: print("Usage: improve_art.py <image_path> <suggestions_json> <output_path>") sys.exit(1) img_path = sys.argv[1] suggestions_json = sys.argv[2] out_path = sys.argv[3] suggestions = json.loads(suggestions_json) if suggestions_json.startswith('[') else [] if not os.path.isfile(img_path): print(f"File does not exist: {img_path}") sys.exit(1) result_path = generate_image(img_path, suggestions, out_path)Technical Analysis
The destination passed to
shutil.copyfile()is taken directly from the third command-line argument. The script does not canonicalize this path, require it to remain under the documented project output directory, reject symlinks, or prevent replacement of an existing file.Consequently, an attacker who can invoke the script can select any destination writable by the Agent process.
copyfile()opens the destination for writing and replaces its contents. No command injection or privilege escalation occurs directly; exploitation is limited by the operating-system permissions of the process.Attack Path
- Identify a file that is readable and can serve as
image_path. - Identify a security-sensitive or operationally important file writable by the Agent process.
- Invoke
scripts/improve_art.pywith the readable source file, a valid or ignored suggestions a ...[truncated 919 chars]
- Identify a file that is readable and can serve as
- Remediation
View remediation
Remediation Suggestions
- Use a fixed, application-controlled output directory rather than accepting an unrestricted destination path.
- Resolve both the output root and requested destination with
pathlib.Path.resolve(), then verify that the destination is a descendant of the approved output root. - Generate server-side filenames instead of trusting caller-provided paths.
- Reject absolute paths, traversal components, and destinations involving symbolic links.
- Refuse to overwrite existing files by default. Require explicit authorization if replacement is a legitimate feature.
- Create output files atomically with exclusive-creation semantics and restrictive permissions.
- Validate that the source is an allowed image type and that the destination uses an expected image extension.
- Run the Skill under a least-privileged account whose write access is restricted to its designated output directory.
Example containment pattern:
python from pathlib import Path from shutil import copyfile OUTPUT_ROOT = (Path(__file__).resolve().parent.parent / "output").resolve() def generate_image(original_path, suggestions, output_name): if Path(output_name).name != output_name: raise ValueError("Output must be a simple filename") destination = (OUTPUT_ROOT / output_name).resolve() if destination.parent != OUTPUT_ROOT: raise ValueError("Output path escapes the approved directory") if destination.exists() or destination.is_symlink(): raise FileExistsError("Refusing to overwrite an existing output") OUTPUT_ROOT.mkdir(parents=True, exist_ok=True) copyfile(original_path, destination) return str(destination)
