T09 · Insecure Skill Coding Practices
- Location
scripts/script_generator.py:265- Finding
Caller-Controlled Path Traversal in Script File Saving
- Content
View full analysis
Vulnerability Details
File Location:
scripts/script_generator.py, lines 265–269
Vulnerability Type: Path traversal leading to an arbitrary relative-path file write
Risk Level: MediumVulnerable Code
python def save_script(self, script: str, filename: str) -> str: """保存脚本到文件""" filepath = f"scripts/{filename}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt" with open(filepath, 'w', encoding='utf-8') as f: f.write(script) return filepathTechnical Analysis
The
save_script()method directly interpolates the caller-controlledfilenamevalue into a filesystem path. It does not reject path separators, absolute path components, or parent-directory sequences such as../.Consequently, an attacker who can control
filenamecan cause the resolved destination to escape the intendedscripts/directory. Thescriptargument supplies the file contents, so a caller controlling both arguments can create attacker-selected content at an unintended writable location.The timestamp and
.txtsuffix make replacement of a precisely named existing file less straightforward, but they do not prevent directory traversal or unauthorized file creation. Exploitation remains subject to the operating system permissions of the process running the Skill.Attack Path
-
An attacker gains control over the
filenameargument passed tosave_script(). -
The attacker supplies a traversal value such as
../../shared/payload. -
The method constructs a path resembling:
text scripts/../../shared/payload_YYYYMMDD_HHMMSS.txt -
The operating system normalizes the parent-directory components, placing the file outside the intended
scripts/directory. -
The function writes the supplied
scriptcontent to that unintended location. -
If the destination is later trusted, processed, published, or executed by another component, the una ...[truncated 822 chars]
-
- Remediation
View remediation
Remediation Suggestions
- Restrict
filenameto a simple basename using a strict allowlist, such as letters, digits, underscores, and hyphens. - Reject empty names, absolute paths,
..,/,\, null bytes, and platform-specific path separators. - Build paths with
pathlib.Pathrather than string concatenation. - Resolve the output directory and candidate destination, then verify that the destination remains inside the approved directory.
- Create the output directory explicitly and use restrictive filesystem permissions where appropriate.
- Consider generating the complete server-side filename rather than accepting a path-like caller value.
Example hardening:
python import re from pathlib import Path from datetime import datetime def save_script(self, script: str, filename: str) -> str: if not re.fullmatch(r"[A-Za-z0-9_-]{1,80}", filename): raise ValueError("Invalid filename") output_dir = Path("scripts").resolve() output_dir.mkdir(parents=True, exist_ok=True) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") destination = (output_dir / f"{filename}_{timestamp}.txt").resolve() if destination.parent != output_dir: raise ValueError("Destination escapes the output directory") destination.write_text(script, encoding="utf-8") return str(destination)Add tests covering
../, absolute paths, Windows separators, nested paths, encoded traversal forms, empty names, and excessively long names.- Restrict
