From 93f58caf7ade130f43e7ee248d16d5149c1b7c6f Mon Sep 17 00:00:00 2001 From: splitsec2 <35583321+splitsec2@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:59:10 -0600 Subject: [PATCH] fix(debug): keep SHELFMARK_API_KEY_READONLY out of the debug bundle (#1432) Kept small on purpose so it's easy to review. `genDebug.sh` filters secrets out of `environment.txt` by matching `_KEY=`, `_SECRET=`, `_PASSWORD=` and `_TOKEN=`, which only catches names that end in those words. `SHELFMARK_API_KEY_READONLY` ends in `_READONLY`, so its value lands in the file, and people attach these bundles to issues. That key only reads counters, so the impact is small, but any variable like `DB_PASSWORD_FILE` would leak the same way. The pattern now matches the secret word anywhere in the variable name. The new test runs the real filter line from the script against a made-up environment, and it fails on the old pattern. The one red check is the existing clock-dependent test `test_a_queued_torrent_asks_for_a_grace_once`, which fails on any runner that booted recently. It is fixed in #1426 and is not caused by this change. --- genDebug.sh | 2 +- tests/core/test_gen_debug_env_filter.py | 61 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/core/test_gen_debug_env_filter.py diff --git a/genDebug.sh b/genDebug.sh index 47d34911..d4f6f372 100755 --- a/genDebug.sh +++ b/genDebug.sh @@ -134,7 +134,7 @@ else fi # Add environment variables (redacting sensitive info) -env | grep -v -E "(AA_DONATOR_KEY|HARDCOVER_API_KEY|_KEY=|_SECRET=|_PASSWORD=|_TOKEN=)" | sort > "$LOG_DIR/environment.txt" +env | grep -v -E "^[^=]*(_KEY|_SECRET|_PASSWORD|_TOKEN)[^=]*=" | sort > "$LOG_DIR/environment.txt" # Add configuration files (redacting sensitive values) CONFIG_DIR=${CONFIG_DIR:-"/config"} diff --git a/tests/core/test_gen_debug_env_filter.py b/tests/core/test_gen_debug_env_filter.py new file mode 100644 index 00000000..cdc72109 --- /dev/null +++ b/tests/core/test_gen_debug_env_filter.py @@ -0,0 +1,61 @@ +"""genDebug.sh must keep secret-looking variables out of environment.txt. + +People attach the debug bundle to public issues, so the filter is tested by running the +real pipeline line from the script against a controlled environment. +""" + +import re +import subprocess +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parents[2] / "genDebug.sh" + + +def _filtered_env(env: dict[str, str]) -> set[str]: + line = next( + line for line in SCRIPT.read_text().splitlines() if line.startswith("env | grep -v") + ) + pipeline = re.sub(r'\s*>\s*"\$LOG_DIR/environment\.txt"\s*$', "", line) + out = subprocess.run( + ["bash", "-c", pipeline], + env=env, + capture_output=True, + text=True, + check=True, + ).stdout + return {entry.split("=", 1)[0] for entry in out.splitlines()} + + +def test_secret_variables_are_left_out() -> None: + names = _filtered_env( + { + "PATH": "/usr/bin:/bin", + "SHELFMARK_API_KEY": "a", + "SHELFMARK_API_KEY_READONLY": "b", + "HARDCOVER_API_KEY": "c", + "AA_DONATOR_KEY": "d", + "SOME_SECRET": "e", + "DB_PASSWORD_FILE": "f", + "SERVICE_TOKEN_VALUE": "g", + } + ) + + assert names.isdisjoint( + { + "SHELFMARK_API_KEY", + "SHELFMARK_API_KEY_READONLY", + "HARDCOVER_API_KEY", + "AA_DONATOR_KEY", + "SOME_SECRET", + "DB_PASSWORD_FILE", + "SERVICE_TOKEN_VALUE", + } + ) + + +def test_ordinary_variables_are_kept() -> None: + names = _filtered_env( + {"PATH": "/usr/bin:/bin", "TZ": "UTC", "PUID": "1000", "FLASK_PORT": "8084"} + ) + + assert {"TZ", "PUID", "FLASK_PORT"} <= names