From 42defd9d1e31a459f885159f80b18924c27f5909 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 03:05:05 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[LOW]=20nig?= =?UTF-8?q?htly=20backup=20drops=20the=20Jellyfin=20webhook=20secret?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backup's promise is 'no tokens inside' (it is what makes the 0640 BACKUP_GROUP_READABLE mode safe to offer), but _strip_secrets only blanked token columns; the webhook's shared secret is an app_state row and was copied verbatim. Delete that row in the copy (secure_delete is already on); a restore leaves the route off until the admin's next look at the setup card makes a new secret. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014GBDVBSiv59ZWfET13xFYp --- src/services/scheduler.py | 12 +++++++++--- tests/test_backup_perms.py | 14 +++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/services/scheduler.py b/src/services/scheduler.py index a7e3ad0..f45a74e 100644 --- a/src/services/scheduler.py +++ b/src/services/scheduler.py @@ -1512,12 +1512,16 @@ def _snapshot_sqlite(db_path: str, dest: str) -> bool: # server's own token lives in .env, never in the database. Plex keeps its # token out of its database backups the same way. _SECRET_COLUMNS = (("users", "plex_token"), ("sessions", "access_token"), ("sessions", "refresh_token")) +# app_state rows that are credentials. The Jellyfin webhook secret: without +# it the route is off, and the admin's next look at the setup card makes a +# new one to paste into the plugin. +_SECRET_STATE_KEYS = ("jellyfin_hook_secret",) def _strip_secrets(dest: str) -> bool: - """Blank the token columns in a backup copy, overwriting the old bytes - (secure_delete), and leave it one file (out of WAL mode). True when the - copy still checks.""" + """Blank the token columns and drop the secret app_state rows in a + backup copy, overwriting the old bytes (secure_delete), and leave it one + file (out of WAL mode). True when the copy still checks.""" con = sqlite_connect(dest) try: con.execute("PRAGMA journal_mode=DELETE") @@ -1526,6 +1530,8 @@ def _strip_secrets(dest: str) -> bool: for table, col in _SECRET_COLUMNS: if table in tables and col in {r[1] for r in con.execute(f'PRAGMA table_info("{table}")')}: # nosec B608 - literals above con.execute(f'UPDATE "{table}" SET "{col}" = NULL WHERE "{col}" IS NOT NULL') # nosec B608 - literals above + if "app_state" in tables: + con.executemany("DELETE FROM app_state WHERE key = ?", [(k,) for k in _SECRET_STATE_KEYS]) con.commit() return (con.execute("PRAGMA quick_check(1)").fetchone() or ["?"])[0] == "ok" finally: diff --git a/tests/test_backup_perms.py b/tests/test_backup_perms.py index 7165d4f..5e4f200 100644 --- a/tests/test_backup_perms.py +++ b/tests/test_backup_perms.py @@ -2,7 +2,8 @@ * No tokens: users' Plex tokens are blanked in the backup copy, and their bytes overwritten (secure_delete) - a restore asks each member to sign in - once, as Plex keeps its own token out of its database backups. + once, as Plex keeps its own token out of its database backups. The + Jellyfin webhook secret (an app_state row) goes too. * 0600 like the live database, unless the operator sets BACKUP_GROUP_READABLE (0640, folders 0750) to copy backups off a NAS share. The start-up pass used to force every backup to 0600 whatever was @@ -30,6 +31,7 @@ POSIX = os.name == "posix" TOKEN = "zQ7plexTokenAbc123xy" +HOOK_SECRET = "hK9jellyfinHookSecret42" def mode(p): @@ -43,6 +45,9 @@ def mode(p): c.execute("create table users (id integer primary key, plex_username text, plex_token text)") c.execute("create table sessions (id integer primary key, access_token text, refresh_token text)") c.execute("insert into users (plex_username, plex_token) values ('owner', ?), ('member', ?)", (TOKEN, TOKEN[::-1])) +c.execute("create table app_state (key text primary key, value text, updated_at text)") +c.execute("insert into app_state (key, value) values ('jellyfin_hook_secret', ?), ('last_sync_at', '2026-11-17')", + (HOOK_SECRET,)) c.commit() c.close() dest = TMP / "backup.db" @@ -56,6 +61,13 @@ def mode(p): blob = dest.read_bytes() check("the token's bytes are gone from the file, not just unlinked (secure_delete)", TOKEN.encode() not in blob and TOKEN[::-1].encode() not in blob) +c = sqlite3.connect(f"file:{dest}?mode=ro", uri=True) +check("the Jellyfin webhook secret is not in the backup; the rest of app_state stays", + c.execute("select key from app_state").fetchall() == [("last_sync_at",)]) +c.close() +check("...nor its bytes", HOOK_SECRET.encode() not in blob) +from src.services.media_server import jellyfin_hook # noqa: E402 +check("the stripped key is the one the webhook route reads", jellyfin_hook.SECRET_KEY in sched._SECRET_STATE_KEYS) check("the backup is one file (no -wal beside it)", not (TMP / "backup.db-wal").exists()) c = sqlite3.connect(f"file:{src_db}?mode=ro", uri=True) check("the live database keeps its tokens", c.execute("select count(*) from users where plex_token is not null").fetchone()[0] == 2)