T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/utils.py:35- Finding
Path Validation Allows Unrestricted Access to Arbitrary Files and Directories
- Content
View full analysis
bool: """防止路径遍历攻击(Path Traversal)""" try: target = os.path.realpath(os.path.abspath(path)) if base: return os.path.commonprefix([target, os.path.realpath(base)]) == os.path.realpath(base) return True except Exception: return False ``` A representative caller is: ```python def run(action: str, file_path: str, **kwargs) -> str: """执行动作并返回标准化 JSON 字符串""" if not is_safe_path(file_path): return error_output(f"路径不合法(可能含遍历): {file_path}", file_path) encoding = kwargs.get("encoding", "utf-8") backup = kwargs.get("backup", True) try: if action == "read": result = do_read(file_path, encoding) elif action == "create": content = kwargs.get("content", "") overwrite = kwargs.get("overwrite", False) result = do_create(file_path, content, encoding, overwrite, backup) elif action == "update": content = kwargs.get("content", "") mode = kwargs.get("mode", "replace") line = kwargs.get("line", None) result = do_update(file_path, content, encoding, mode, line, backup) elif action == "delete": result = do_delete(file_path, backup) ``` ### Technical Analysis `is_safe_path()` only applies a containment check when a `base` argument is supplied. All audited CRUD callers invoke it without a base directory. Consequently, every path that can be normalized is considered safe, including: - Absolute paths outside the intended workspace. - Relative paths containing `..`. - Paths to configuration files, credentials, or ...[truncated 1707 chars]- Remediation
View remediation
bool: if not base: return False target = os.path.realpath(os.path.abspath(path)) root = os.path.realpath(os.path.abspath(base)) try: return os.path.commonpath([target, root]) == root except ValueError: return False ``` 4. Reject missing base-directory configuration instead of defaulting to unrestricted access. 5. Check each existing path component for symlinks where symlink traversal is not required. 6. Explicitly deny sensitive locations such as Agent configuration, credential stores, system directories, and startup files. 7. Require explicit authorization for deletion, overwrite, recursive operations, and backup bypass. 8. Add tests covering absolute paths, `..`, sibling-prefix paths, symlinks, and cross-drive paths on Windows. ]]>
