mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-25 23:31:25 +00:00
fix: pass the calibre title as an argv item, not shell syntax
Co-Authored-By: Shengcun <business@rs-systems-web.com>
This commit is contained in:
1 parent
2526bd7287
commit
c641b1faa7
2 files changed
+59
-2
No files matched your search
@@ -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()]
|
||||
|
||||
@@ -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"
|
||||
Reference in new issue
Block a user