mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 09:26:17 -05:00
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:
parent
76aafc5da9
commit
aeebe8d833
7 changed files with 68 additions and 1 deletions
|
|
@ -57,6 +57,21 @@ def test_empty_value_returns_default(media_with_config):
|
|||
assert _read(media, 'zims_destination', 'zims') == 'zims'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('null_repr', ['null', 'Null', 'NULL', '~'])
|
||||
def test_yaml_null_returns_default(media_with_config, null_repr):
|
||||
"""An unquoted YAML null means "unset" -> default, matching the Python config layer."""
|
||||
media, write = media_with_config
|
||||
write(f'zims_destination: {null_repr}\n')
|
||||
assert _read(media, 'zims_destination', 'zims') == 'zims'
|
||||
|
||||
|
||||
def test_quoted_null_is_literal_string(media_with_config):
|
||||
"""A quoted "null" is the string null, not YAML null."""
|
||||
media, write = media_with_config
|
||||
write('zims_destination: "null"\n')
|
||||
assert _read(media, 'zims_destination', 'zims') == 'null'
|
||||
|
||||
|
||||
def test_missing_config_file_returns_default(tmp_path):
|
||||
# No config directory/file at all (fresh install) -> default, no error.
|
||||
assert _read(tmp_path / 'media', 'zims_destination', 'zims') == 'zims'
|
||||
|
|
|
|||
|
|
@ -159,6 +159,21 @@ def test_honors_configured_zims_destination(kiwix_env):
|
|||
assert 'mycustomzims/wikipedia.zim' in library.read_text()
|
||||
|
||||
|
||||
def test_traversal_zims_destination_is_rejected(kiwix_env):
|
||||
"""A traversal zims_destination must not let Kiwix escape the media root; it falls back to `zims`."""
|
||||
kiwix_env['write_config']('zims_destination: ../../escape\n')
|
||||
_write_zim(kiwix_env['zims'] / 'wikipedia.zim') # a valid zim in the safe default dir
|
||||
|
||||
result = kiwix_env['run']()
|
||||
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert kiwix_env['started_marker'].exists(), 'kiwix-serve was not started'
|
||||
# Served from the safe `zims` directory, and no library.xml was created outside media.
|
||||
assert kiwix_env['library'].exists()
|
||||
assert 'wikipedia.zim' in kiwix_env['library'].read_text()
|
||||
assert not (kiwix_env['media'].parent / 'escape').exists()
|
||||
|
||||
|
||||
def test_falls_back_to_default_zim_when_no_usable_zim(kiwix_env):
|
||||
"""When no functioning zim is present, the default Zim is served."""
|
||||
# No zims in the directory; create the default Zim the script should fall back to.
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -245,6 +245,9 @@ async def update_settings(_: Request, body: schema.SettingsRequest):
|
|||
|
||||
if body.zims_destination and pathlib.Path(body.zims_destination).is_absolute():
|
||||
raise InvalidConfig('Zims directory must be relative to media directory')
|
||||
elif body.zims_destination and '..' in pathlib.Path(body.zims_destination).parts:
|
||||
# Kiwix serves and rebuilds library.xml in this directory; traversal must not escape media.
|
||||
raise InvalidConfig('Zims directory must not contain ".."')
|
||||
elif not body.zims_destination:
|
||||
new_config['zims_destination'] = wrolpi_config.default_config['zims_destination']
|
||||
|
||||
|
|
@ -305,6 +308,15 @@ async def update_settings(_: Request, body: schema.SettingsRequest):
|
|||
# without waiting for a reboot. Local import avoids a circular import
|
||||
# (modules depend on wrolpi, not the reverse).
|
||||
if wrolpi_config.zims_destination != old_zims_destination:
|
||||
# Persist synchronously first: update() only queues an async background_save,
|
||||
# and that switch has no ordering guarantee against the restart switch below.
|
||||
# start_kiwix_serve.sh reads zims_destination from disk, so the file must be
|
||||
# current before Kiwix restarts. A save failure is non-fatal (background_save
|
||||
# still runs); log and continue.
|
||||
try:
|
||||
wrolpi_config.save()
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not synchronously save config before restarting Kiwix: {e}')
|
||||
from modules.zim.lib import restart_kiwix_handler
|
||||
restart_kiwix_handler.activate_switch()
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,9 @@ set -euo pipefail
|
|||
|
||||
HERE="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &>/dev/null && pwd )"
|
||||
BLOBS="$( cd -- "${HERE}/../../blobs" &>/dev/null && pwd )"
|
||||
IMAGE="ghcr.io/openzim/zim-tools:latest"
|
||||
# Pinned (not :latest) so the committed default.zim is reproducible: identical
|
||||
# source assets always build with the same zimwriterfs release.
|
||||
IMAGE="ghcr.io/openzim/zim-tools:3.7.0"
|
||||
|
||||
echo "Building default.zim from ${HERE}/src -> ${BLOBS}/default.zim ..."
|
||||
rm -f "${BLOBS}/default.zim" # zimwriterfs refuses to overwrite an existing file.
|
||||
|
|
|
|||
|
|
@ -35,6 +35,12 @@ if [ -f "${CONFIG}" ]; then
|
|||
sub(/^[[:space:]]+/, "", v) # leading whitespace
|
||||
sub(/[[:space:]]+#.*$/, "", v) # trailing inline comment
|
||||
sub(/[[:space:]]+$/, "", v) # trailing whitespace
|
||||
# Unquoted YAML null means "unset"; return empty so the caller uses its
|
||||
# default, matching how PyYAML/the Python config layer treat null.
|
||||
if (v == "null" || v == "Null" || v == "NULL" || v == "~") {
|
||||
print ""
|
||||
exit
|
||||
}
|
||||
# Strip a matching pair of surrounding quotes.
|
||||
if (v ~ /^".*"$/ || v ~ /^'\''.*'\''$/)
|
||||
v = substr(v, 2, length(v) - 2)
|
||||
|
|
|
|||
|
|
@ -104,6 +104,15 @@ async def test_changing_zims_destination_restarts_kiwix(test_session, async_clie
|
|||
mock_activate.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zims_destination_rejects_traversal(test_session, async_client):
|
||||
"""zims_destination must be relative and must not contain traversal ('..') or be absolute."""
|
||||
for bad in ('../../escape', 'a/../../b', '/abs/path'):
|
||||
request, response = await async_client.patch('/api/settings',
|
||||
content=json.dumps({'zims_destination': bad}))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, f'{bad!r} should be rejected'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_log_level(async_client):
|
||||
"""Log level must be between 0 and 40."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue