Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/services/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_backup_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -30,6 +31,7 @@

POSIX = os.name == "posix"
TOKEN = "zQ7plexTokenAbc123xy"
HOOK_SECRET = "hK9jellyfinHookSecret42"


def mode(p):
Expand All @@ -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"
Expand All @@ -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)
Expand Down
Loading