"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(calibre): harden Zip Slip check and replace rglob with calibredb
ChengYed6fb8cc52b8a
diff --git a/calibre/agent-harness/cli_anything/calibre/core/export.py b/calibre/agent-harness/cli_anything/calibre/core/export.pyindex 9b52b7fae..e4b27659c 100644--- a/calibre/agent-harness/cli_anything/calibre/core/export.py+++ b/calibre/agent-harness/cli_anything/calibre/core/export.py@@ -301,11 +301,14 @@ def export_chapters_pdf(# Step 2: extract EPUB and parse chapter listepub_dir = Path(tmpdir) / "epub_content"epub_dir.mkdir()+ resolved_epub_dir = epub_dir.resolve()with zipfile.ZipFile(epub_files[0]) as zf:- # Validate each member path to prevent Zip Slip directory traversal attacks+ # Validate each member path to prevent Zip Slip directory traversal attacks.+ # Use Path.is_relative_to (Py3.10+) instead of str.startswith to avoid+ # sibling-prefix bypasses (e.g. /tmp/epub vs /tmp/epub_evil).for member in zf.infolist():- member_path = (epub_dir / member.filename).resolve()- if not str(member_path).startswith(str(epub_dir.resolve())):+ member_path = (resolved_epub_dir / member.filename).resolve()+ if not member_path.is_relative_to(resolved_epub_dir):raise ValueError(f"Unsafe EPUB entry rejected: {member.filename}")zf.extract(member, epub_dir)diff --git a/calibre/agent-harness/cli_anything/calibre/core/library.py b/calibre/agent-harness/cli_anything/calibre/core/library.pyindex 3ef051047..363b19b95 100644--- a/calibre/agent-harness/cli_anything/calibre/core/library.py+++ b/calibre/agent-harness/cli_anything/calibre/core/library.py@@ -1,9 +1,7 @@"""Library operations — wraps calibredb list, search, add, remove, export."""import json-import osfrom pathlib import Path-from typing import Anyfrom cli_anything.calibre.utils.calibre_backend import (run_calibredb,@@ -27,16 +25,44 @@ def library_info(library_path: str) -> dict:else:book_count = 0- # Count format types by scanning library directory for book files+ # Derive format counts from calibredb metadata instead of recursively scanning+ # the library directory, which is slow and I/O-heavy on large libraries.format_counts: dict[str, int] = {}- lib = Path(library_path)- skip_exts = {".db", ".jpg", ".png", ".opf", ".sqlite", ""}- for p in lib.rglob("*"):- if p.is_file():- ext = p.suffix.upper().lstrip(".")- if ext and ext not in {"DB", "JPG", "PNG", "OPF", "SQLITE", "WEBP", "GIF"}:- format_counts[ext] = format_counts.get(ext, 0) + 1+ formats_result = run_calibredb(+ ["list", "--fields=formats", "--for-machine"],+ library_path=library_path,+ )+ formats_output = formats_result["stdout"].strip()+ if formats_output:+ try:+ rows = json.loads(formats_output)+ except json.JSONDecodeError:+ rows = []++ if isinstance(rows, dict):+ rows = list(rows.values())+ elif not isinstance(rows, list):+ rows = []++ for row in rows:+ if not isinstance(row, dict):+ continue+ value = row.get("formats")+ if not value:+ continue+ if isinstance(value, str):+ fmts = [f.strip() for f in value.split(",") if f.strip()]+ elif isinstance(value, list):+ fmts = [str(f).strip() for f in value if str(f).strip()]+ else:+ continue+ for fmt in fmts:+ # calibredb returns paths like "/.../book.epub" or bare "EPUB"+ ext = Path(fmt).suffix.lstrip(".").upper() or fmt.upper()+ if ext and ext not in {"JPG", "PNG", "OPF", "SQLITE", "WEBP", "GIF", "DB"}:+ format_counts[ext] = format_counts.get(ext, 0) + 1+ lib = Path(library_path)db_path = lib / "metadata.db"db_size = db_path.stat().st_size if db_path.exists() else 0