mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-17 01:39:10 +00:00
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.
This commit is contained in:
1 parent
be2653bdb7
commit
93f58caf7a
2 files changed
+62
-1
No files matched your search
+1
-1
@@ -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"}
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user