diff --git a/skills/shelfmark/download_books.py b/skills/shelfmark/download_books.py index 0b8b02d0..3930f3a3 100755 --- a/skills/shelfmark/download_books.py +++ b/skills/shelfmark/download_books.py @@ -281,8 +281,8 @@ def check_calibre(title: str) -> bool: """Check if a book is already in the calibre database.""" import subprocess - cmd = f'calibredb list --search title:="{title}"' - result = subprocess.run(cmd, check=False, shell=True, capture_output=True, text=True) + cmd = ["calibredb", "list", "--search", f'title:="{title}"'] + result = subprocess.run(cmd, check=False, capture_output=True, text=True) output = result.stdout.strip() # calibredb output has header line "id title authors", count non-header lines lines = [l.strip() for l in output.split("\n") if l.strip()] diff --git a/tests/skills/test_check_calibre.py b/tests/skills/test_check_calibre.py new file mode 100644 index 00000000..dfb1b38b --- /dev/null +++ b/tests/skills/test_check_calibre.py @@ -0,0 +1,57 @@ +"""The calibre check must pass a title as data, never as shell syntax.""" + +import importlib.util +import subprocess +import sys +import types +from pathlib import Path +from unittest.mock import patch + +import pytest + +SCRIPT = Path(__file__).resolve().parents[2] / "skills" / "shelfmark" / "download_books.py" + + +def _load_module(): + # The script imports playwright at module level; checkout CI installs it, + # a bare test run may not. + if "playwright.sync_api" not in sys.modules: + try: + import playwright.sync_api # noqa: F401 + except ModuleNotFoundError: + playwright = types.ModuleType("playwright") + sync_api = types.ModuleType("playwright.sync_api") + sync_api.Error = type("Error", (Exception,), {}) + sync_api.sync_playwright = lambda: None + playwright.sync_api = sync_api + sys.modules["playwright"] = playwright + sys.modules["playwright.sync_api"] = sync_api + + spec = importlib.util.spec_from_file_location("shelfmark_download_books", SCRIPT) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize( + "title", + [ + "Mistborn", + 'Dune"; touch /tmp/pwned; echo "', + "Neuromancer$(id)", + "Snow Crash`whoami`", + ], +) +def test_title_is_an_argument_not_shell_syntax(title): + module = _load_module() + completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="id title authors\n", stderr="") + + with patch.object(subprocess, "run", return_value=completed) as run: + module.check_calibre(title) + + args, kwargs = run.call_args + command = args[0] + assert isinstance(command, list), "the command must be an argv list" + assert command[:3] == ["calibredb", "list", "--search"] + assert command[3] == f'title:="{title}"' + assert not kwargs.get("shell"), "a shell must not re-parse the title"