T09 · Insecure Skill Coding Practices
- Location
generalrecognition.py:20- Finding
Symlink Bypass Allows Unauthorized Local File Disclosure to External OCR Service
- 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} def _build_pic_base64(req: Dict[str, Any]) -> Dict[str, Any]: """ 从请求中获取 base64 图片内容: - 若提供 pic 字段(base64 字符串),直接使用; - 若提供 path/image/file,则从本地文件读取并转为 base64。 """ pic = req.get("pic") if pic: return {"pic": str(pic), "error": None} path_raw = req.get("path") or req.get("image") or req.get("file") if not path_raw: return { "pic": None, "error": "Either 'pic' (base64) or 'path/image/file' is required", } safe = _normalize_local_path(str(path_raw).strip(), "path") if safe["error"]: return {"pic": None, "error": safe["message"]} path = safe["path"] if not os.path.isfile(path): return {"pic": None, "error": f"File not found: {safe['relative']}"} try: with open(path, "rb") as f: raw = f.read() except Exception as e: return {"pic": None, "error": f"Failed to read file: {e}"} try: ...[truncated 3744 chars]- Remediation
View remediation
