T09 · Insecure Skill Coding Practices
- Location
scripts/package_skill.py:34- Finding
Path Containment Bypass Allows Files Outside the Skill Directory to Be Packaged
- Content
View full analysis
Vulnerability Details
File Location:
scripts/package_skill.py, lines 34-38 and 57-70
Vulnerability Type: Improper path containment validation
Risk Level: MediumVulnerable Code
python def is_safe_path(path: str, base_path: str) -> bool: """检查路径安全性,防止符号链接逃逸""" real_path = Path(path).resolve() real_base = Path(base_path).resolve() return str(real_path).startswith(str(real_base))python with zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) as zf: for root, dirs, files in os.walk(skill_path): # 排除隐藏目录和临时文件 dirs[:] = [d for d in dirs if not d.startswith('.') and d != '__pycache__'] files = [f for f in files if not f.startswith('.') and not f.endswith('.pyc')] for file in files: file_path = Path(root) / file arcname = file_path.relative_to(skill_path) # 安全检查 if not is_safe_path(file_path, skill_path): print(f"跳过不安全的路径: {file_path}") continue zf.write(file_path, arcname)Technical Analysis
The
is_safe_path()function compares resolved paths usingstr.startswith(). String-prefix equality does not verify that one path is a filesystem descendant of another.For example, if the package root is
/tmp/skill, the external path/tmp/skill-private/secret.txtstarts with the string/tmp/skilleven though it is not located beneath that directory. A symbolic link inside/tmp/skillthat resolves to this external path therefore passes the check.Path.resolve()follows the symbolic link, but the subsequent prefix comparison incorrectly accepts the resolved target.zipfile.ZipFile.write()then reads the target using the privileges of the packaging process and stores its contents under the symbolic link's archive name.Exploitation requires the attacker to place or influence a symbolic link within the directory ...[truncated 1799 chars]
- Remediation
View remediation
Remediation Suggestions
Replace string-prefix comparison with component-aware path containment validation and reject symbolic links explicitly:
python def is_safe_path(path: Path, base_path: Path) -> bool: if path.is_symlink(): return False try: resolved_base = base_path.resolve(strict=True) resolved_path = path.resolve(strict=True) resolved_path.relative_to(resolved_base) return True except (ValueError, FileNotFoundError, RuntimeError): return FalseApply the following additional hardening measures:
- Reject symbolic links for both files and directories unless following them is an explicit requirement.
- Validate every traversed directory with
Path.relative_to()against the resolved package root. - Use
os.path.commonpath()as an alternative component-aware check, ensuring the result exactly equals the resolved base path. - Open files defensively where supported, such as with no-follow semantics, to reduce time-of-check/time-of-use risks caused by replacing a checked file with a symbolic link.
- Treat packaging input as untrusted and run the script with minimal filesystem privileges.
- Add regression tests covering:
- Ordinary files beneath the package root.
- Symbolic links to unrelated external paths.
- Prefix-colliding paths such as
/tmp/skilland/tmp/skill-private. - Broken symbolic links.
- Symbolic-link replacement between validation and archive creation.
