zim: address PR review — config race, YAML null, path traversal, pinned build

Triage of Greptile findings on the default-Zim / configurable-directory PR:

- Stale config on restart (P1): settings update() only queues an async
  background_save, and the restart switch has no ordering guarantee (the switch
  worker pops LIFO), so start_kiwix_serve.sh could read the old zims_destination.
  Save the config synchronously before activating the restart switch.
- YAML null (P1): read_config_value.sh now treats an unquoted null/~ as unset
  (returns the default), matching PyYAML/the Python config layer.  A quoted
  "null" remains the literal string.
- Path traversal (P1/security): the settings API now rejects a zims_destination
  containing "..", and start_kiwix_serve.sh defends in depth by falling back to
  `zims` for any absolute/traversal/empty value, so Kiwix can never scan or
  rewrite library.xml outside the media root.
- Reproducible build (P2): pin the zim-tools image to 3.7.0 instead of :latest.

Tests added for null handling, traversal rejection (API + script).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-20 18:44:19 -06:00
parent 76aafc5da9
commit aeebe8d833
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
7 changed files with 68 additions and 1 deletions

View file

@ -14,6 +14,14 @@ DEFAULT_ZIM="${DEFAULT_ZIM:-/opt/wrolpi-blobs/default.zim}"
# Honor the configured Zim directory (wrolpi.yaml `zims_destination`, always
# relative to the media directory), defaulting to `zims`.
ZIMS_DEST="$(MEDIA_DIRECTORY="${MEDIA_DIRECTORY}" "${PROJECT_DIR}/wrolpi/scripts/read_config_value.sh" zims_destination zims)"
# Defense in depth: never let an absolute path, traversal, or empty value escape
# the media root (the settings API also rejects these). Fall back to `zims`.
case "${ZIMS_DEST}" in
''|/*|*..*)
echo "Ignoring unsafe zims_destination '${ZIMS_DEST}'; using 'zims'" >&2
ZIMS_DEST=zims
;;
esac
ZIM_DIRECTORY="${MEDIA_DIRECTORY}/${ZIMS_DEST}"
LIBRARY="${ZIM_DIRECTORY}/library.xml"