Files
shelfmark/tests/skills
ShengcunandRoni Saguey 75b88f55e0 security: book title reaches the calibre check as shell syntax (#1718)
`check_calibre()` builds a shell command by interpolating a book title:

```python
cmd = f'calibredb list --search title:="{title}"'
subprocess.run(cmd, check=False, shell=True, ...)
```

Titles arrive from `--file` JSON, from positional CLI arguments, and
from metadata
pulled off the shelfmark page, so a title containing `"` escapes the
quoted search
term and the rest is re-parsed by `/bin/sh` — e.g. a title of
`Dune"; touch /tmp/pwned; echo "` runs the injected command as whoever
is running
the downloader.

Fix: pass the search term as a single argv item and drop `shell=True`.
Inside the
term, `\` and `"` are escaped, so the title reaches calibre as one value
instead of
ending the quoted term and turning the remainder into calibre query
syntax. The
match kind is untouched: it stays `title:"=..."`, an exact-title search,
so the
same books are found as before and none are re-downloaded.

A second commit 93378d1 handles a case the shell form hid: with the argv
call, a
missing or unexecutable `calibredb` raises instead of answering 127, so
it is caught
and read as "not in the library" — the same verdict the old code
produced — with a
warning on stdout rather than an aborted batch.

`tests/skills/test_check_calibre.py` asserts the command is an argv
list, that the
title is one element, that no shell is requested, that the search stays
an exact
match, and that an unusable binary reads as absent. It is parametrised
over a plain
title plus six shapes that used to break the quoting (embedded quotes,
`$(id)`,
backticks, a trailing backslash, a Windows path):

```
$ uv run pytest tests/skills/test_check_calibre.py -q
..........                                                               [100%]
```

`uv run pytest tests/skills tests/core -q` -> 3172 passed; `ruff check`
and
`ruff format --check` are clean.

---------

Co-authored-by: Roni Saguey <ronisaguey-ux@users.noreply.github.com>
2026-11-19 19:08:39 -05:00
..