T09 · Insecure Skill Coding Practices
- Location
qrcode.py:10- Finding
Symlink-Based Escape from the Intended Filesystem Boundary
- Content
View full analysis
Dict[str, Any]: """ 规范化并限制本地文件路径,只允许在当前工作目录及其子目录内读写。 禁止绝对路径和目录穿越(包含 ..)。 """ if not user_path: return { "error": "invalid_param", "message": f"field '{field}' is empty", } # 禁止绝对路径 if os.path.isabs(user_path): return { "error": "invalid_path", "message": f"Absolute path is not allowed for '{field}'", } # 规范化并检查目录穿越 norm = os.path.normpath(user_path) if norm.startswith("..") or norm == "..": return { "error": "invalid_path", "message": f"Path traversal is not allowed for '{field}'", } base = os.getcwd() full = os.path.join(base, norm) return {"error": None, "path": full, "relative": norm} ``` The resulting path is used for output as follows: ```python safe = _normalize_local_path(out_raw, "out") if safe["error"]: return safe out = safe["path"] out_dir = os.path.dirname(out) if out_dir: os.makedirs(out_dir, exist_ok=True) try: img.save(out) except Exception as e: return {"error": "save_failed", "message": str(e), "path": out} ``` It is also used for input as follows: ```python safe = _normalize_local_path(path_raw, "path") if safe["error"]: return safe path = safe["path"] if not os.path.isfile(path): return {"error": "file_not_found", "message": f"File not found: {safe['relative']}"} img = cv2.imread(path) ``` ### Technical Analysis The path validation is lexical. It rejects absolute paths and normalized strings beginning with `..`, but it does not resolve symbolic links before deciding whether a path remains inside the current working directory. ...[truncated 2133 chars]- Remediation
View remediation
