T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/read_file.py:25- Finding
Path Containment Bypass Enables Unauthorized Local File Disclosure
- Content
View full analysis
MAX_FILE_SIZE: raise ValueError("错误:文件大小超过限制") return abs_path ``` ### Technical Analysis The function attempts to restrict input files to `project_root` by applying `os.path.abspath()` and then performing a string-prefix comparison: ```python if not abs_path.startswith(project_root): ``` A string-prefix check does not establish that the candidate is a descendant of the permitted directory. For example, if the permitted root is `/tmp/project`, a file under `/tmp/project-secret/records.txt` also starts with `/tmp/project` and therefore passes validation. In addition, `os.path.abspath()` normalizes a lexical path but does not resolve symbolic links. A path that appears to be inside the permitted tree can consequently be a symbolic link to a file outside it. The later calls to `os.path.exists()`, `os.path.getsize()`, and `open()` follow symbolic links. The extension is checked using the accepted path after validation rather than the resolved target. Therefore, a symbolic link named with an accepted extension such as `.txt` can point to a target whose real path and extension would otherwise be rejected. ### Attack Path 1. The attacker supplies a path to a file in a sibling directory whose name share ...[truncated 1562 chars]- Remediation
View remediation
MAX_FILE_SIZE: raise ValueError("File exceeds the size limit") return str(candidate) ``` 2. Alternatively, use `os.path.realpath()` together with `os.path.commonpath()`: ```python root = os.path.realpath(project_root) candidate = os.path.realpath(file_path) if os.path.commonpath([root, candidate]) != root: raise ValueError("File path is outside the permitted directory") ``` 3. Restrict file access to a dedicated upload directory rather than the parent directory of the Skill package. 4. Reject symbolic links explicitly when they are not required. Where race conditions are relevant, open files with platform-supported no-follow semantics such as `O_NOFOLLOW`, then validate the opened file descriptor. 5. Apply the extension policy to the canonical target and, where possible, verify the actual file format rather than relying solely on its filename. 6. Add regression tests covering sibling-prefix paths, `..` traversal, absolute external paths, relative and absolute symbolic links, broken links, nested links, and files exceeding the configured size limit. ]]>
