T09 · Insecure Skill Coding Practices
- Location
scripts/read_file.py:27- Finding
Path Containment Bypass Enables Unauthorized Local File Disclosure
- Content
View full analysis
Vulnerability Details
File Location:
scripts/read_file.py, lines 27–42
Vulnerability Type: Improper path containment validation
Risk Level: MediumVulnerable Code:
python def validate_file_path(file_path): abs_path = os.path.abspath(file_path) script_dir = os.path.dirname(os.path.abspath(__file__)) skill_dir = os.path.dirname(script_dir) project_root = os.path.dirname(skill_dir) if not abs_path.startswith(project_root): raise ValueError("错误:文件路径超出允许范围") if not os.path.exists(abs_path): raise FileNotFoundError("错误:文件不存在") if os.path.isdir(abs_path): raise ValueError("错误:路径是目录而非文件") file_size = os.path.getsize(abs_path) if file_size > MAX_FILE_SIZE: raise ValueError("错误:文件大小超过限制") return abs_pathTechnical Analysis
The function attempts to restrict input files to
project_rootusing a string-prefix comparison:python abs_path.startswith(project_root)String prefixes do not represent filesystem containment boundaries. For example, if the permitted root is
/tmp/project, a sibling path such as/tmp/project-backup/secret.txtalso begins with/tmp/projectand passes this check.In addition,
os.path.abspath()normalizes relative path components but does not resolve symbolic links. A symbolic link located under an accepted path can therefore reference a file outside the intended root while still passing the lexical prefix check. The subsequent calls toexists(),getsize(), andopen()follow that link.Once validation succeeds, files with an accepted extension are read and returned in full. In particular,
.md,.txt,.json, and.csvfiles are passed directly toread_text_file().Attack Path
- The attacker identifies the value of the parent directory treated as
project_root. - The attacker supplies either:
- A file in a sibling directory whose path shares ...[truncated 974 chars]
- The attacker identifies the value of the parent directory treated as
- Remediation
View remediation
Remediation Suggestions
Resolve canonical filesystem paths and compare path components rather than string prefixes:
python from pathlib import Path def validate_file_path(file_path): allowed_root = Path(__file__).resolve().parents[2] candidate = Path(file_path).resolve(strict=True) if not candidate.is_relative_to(allowed_root): raise ValueError("File path is outside the permitted directory") if not candidate.is_file(): raise ValueError("Path is not a regular file") if candidate.stat().st_size > MAX_FILE_SIZE: raise ValueError("File exceeds the size limit") return str(candidate)Additional hardening measures should include:
- Define the permitted upload directory explicitly instead of deriving an unnecessarily broad parent directory.
- Reject symbolic links with
Path.is_symlink()if links are not required. - Open the file defensively to reduce time-of-check/time-of-use race conditions.
- Run the Skill under a least-privileged account without access to unrelated secrets.
- Validate the actual file type rather than relying exclusively on the filename extension.
- Add tests covering sibling prefix collisions,
..traversal, symbolic links, broken links, and paths outside the allowed root.
