Skip to content

fix(backup): carry live credentials only onto the same account - #5394

Merged
Quick104 merged 5 commits into
mainfrom
fix/restore-carry-same-account
Nov 27, 2026
Merged

Quick104 merged 5 commits into
mainfrom
fix/restore-carry-same-account

Conversation

@Quick104

@Quick104 Quick104 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: none
Other clients: N/A (no client-visible change)

A full restore could carry a live account's credentials onto a different account that had come to share its id. That covers its API keys, external sign-in links, notification destinations and watch-provider connections. It took two full restores, the second from a backup older than an account made between them, plus an account in that backup that had never been used. This change carries a live credential only onto the same account, and stops a full restore from handing out account ids the live server already used.

A full restore keeps the live server's credentials, less those whose account is not in the restored data or has more authority there (carriedOwners). It found "the account" by id alone. But the restored data brings back the backup's account id sequence, so a new account on the live server can get an id that another account holds in a later backup. The authority comparison refused most such pairs, because a profile only the backup has counts as wider. An account that never had a profile slipped through. The stricter-switch carry matched an account by id and name, so a live "disabled" could land on another account with the same name.

Approach

  • A live account and a restored one are the same account when both the id and the creation time (users.created_at) agree. carriedOwners keeps only those, so every carried table follows. The condition is one SQL fragment, sameAccount, over a same_accounts table the carry builds, in the new internal/backup/credentials_accounts.go.
  • The creation time is a timestamptz that only its column default writes, and pg_dump copies it at full precision. The test checks that it comes back unchanged. Accounts made by invitation, sign-in provider or admin all take the default.
  • The stricter account switches (accountKey) also match on the creation time, written in UTC.
  • After the carry, the restored account id sequence follows the live server's sequence, so it moves past every id the live server handed out, not only the ids of its surviving accounts. This runs only when the restore carries from a live server of the same deployment. For a backup of another deployment nothing is carried, and the sequence stays as the backup has it.
  • A live credential whose account is a different account in the backup is dropped and listed with the other dropped credentials. That message now also says "is another account there with the same id".

Validation

  • New Postgres test, TestFullRestoreCarriesCredentialsOnlyOntoTheSameAccountDB, which runs real backups and full restores in three cases: an account never used in the backup, one with a profile, and a disabled live account that shares the name. On main it fails in 5 of 5 runs: the live API key and sign-in link land on the backup's account, the disabled switch carries to it, and the next account reuses a live id. On this branch it passes in 5 of 5 runs. A second test covers the sequence on its own: after a full restore, the next account takes an id above every one the live server's sequence handed out, including when that account is deleted or the same backup is restored again.
  • Whole internal/backup package, run alone against a migrated database with pg_dump and pg_restore available: all 164 top-level tests pass and none skip. Three existing carry tests that set up one account on two databases now give both rows the same creation time.
  • gofmt, go build ./..., go vet, go mod tidy -diff, make lint-changed, make lint-router-recovery, make verify-local-paths and make verify-size-ratchet all pass.
  • An independent review ran these, in its own checkout:
    • At the head (532193f42a): both new tests; the sequence case staged by hand, with the newest live account deleted and either no account or one older account left; and an ordinary restore onto the same server with the live session in three time zones, which carried the API key, the sign-in link and the disabled switch each time.
    • At the commit before the sequence change (a69771ad0f) only, not again at the head: the whole internal/backup package, and a backup taken by the first build that had backups, restored by this build after the live database was migrated forward. That restore carried the key, the link and the disabled switch, with the creation time unchanged to the microsecond.
    • Also at a69771ad0f: removing the sequence step, the same-account condition in carriedOwners, or the creation time in accountKey each fails a case of the first test.
  • CI on the head commit is the deciding run for the package, lint and contract checks.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/pr-5394/

Steps, build and trimmed test output for main and for this branch. Nothing visible changes.

Risks

A live credential whose account is a different account in the backup is now dropped where it used to be carried. The owner sees it in the restore output's "Dropped … whose owner is not in the backup, is another account there with the same id, …" line. That person signs in again and creates the key or link again, on the account the restore actually gives them. On a full restore from the same deployment every account in both databases is the same row, so nothing else changes. An account whose creation time differs between the two would be treated as a different account and lose its carried credentials. Nothing in Silo writes that column after an account is created.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • Each test this pull request adds meets "Decide what to test" in CONTRIBUTING.md.
  • The Evidence section shows every change a user can see, or how I checked a change nobody sees.

AI Disclosure

  • Harness: T3 Code (Claude Code)
  • Tool(s): Claude Code, GitHub CLI
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated; human verification is pending before merge
  • Adversarial review: an independent review ran before merge; what it ran is under Validation

🤖 Generated with Claude Code

Note

Restrict credential carry to same-account matches and advance restored account ID sequence

  • Credential carry in PGLoader.CarryCredentials now only carries live credentials onto an account whose restored ID and created_at both match the live account, classified via a same_accounts temporary table (credentials.go, credentials_accounts.go)
  • Stricter-flag account matching in accountKey now includes the UTC creation timestamp at microsecond precision, not just the username-or-email fallback (credentials_stricter.go)
  • After a carry, advanceAccountIDs moves the restored account ID sequence past the highest live ID used, while keeping a higher restored position or restored user ID. This prevents new accounts from reusing an ID previously held by a different live account
  • Adds PostgreSQL tests for full restores where a live account reuses a backup account's ID, plus sequence checks across deleted accounts and repeated restores (carry_same_account_postgres_test.go); both tests are added to db-contracts.txt
  • Behavioral Change: credentials belonging to a live account that only matches by ID (not creation time) are no longer carried and are reported as dropped; check carriedOwners, sameAccounts, and Dropped field semantics in credentials.go if you depend on prior carry behavior

Macroscope summarized 532193f.

Quick104 and others added 4 commits November 26, 2026 05:19
…account

Two accounts can hold one id once a full restore has moved the account
id sequence back. This test builds that history through real backups and
full restores and checks that the live account's API key, sign-in link
and disabled switch stay off the backup's account, and that a new
account does not take an id the live server used. It fails on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A full restore kept a live account's credentials wherever the restored
data had an account with its id. A full restore also puts the account id
sequence back, so an account created on the live server since could hold
an id that another account has in a later backup, and its API keys,
sign-in links and destinations were carried onto that account.

The carry now pairs a live account with a restored one only when the id
and the creation time agree (same_accounts, sameAccount); a live
credential of any other account is dropped and reported. The stricter
account switches match on the creation time as well. After the carry
the account id sequence moves past every id the live server used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
These tests stand up one account on a live and a restored database. The
carry now also compares when the account was created, so both rows get
one creation time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves sameAccount and the live account reads into credentials_accounts.go,
keeps credentials.go under the size limit, and splits the restore test's
checks into a helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@silo-triage silo-triage Bot added the size/L 200–499 added + deleted lines (all files); change volume, not priority. label Nov 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T06:21:30.009182Z 532193f New commits
🔒 Security Review ✅ Completed 2026-11-26T06:24:39.951551Z 532193f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@silo-kody

silo-kody Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌
⚠️ 4 Kody Rule(s) were not evaluated

These rules declare that they need repository context beyond this diff, and the review could not retrieve it, so they were not judged on this pull request:

  • Preserve optional store capabilities through production wrappers
  • Detach mandatory cleanup from request cancellation and bound its lifetime
  • Preserve durable media identity and share identity-evidence precedence
  • Bound decoder throughput trades and share direct-play eligibility

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code impact: security Auth, privacy, or permission boundary priority: P0 Fix/merge first: data-loss, security, or widespread breakage labels Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The restore flow matches live and restored accounts by ID and creation time before carrying credentials. It also advances the restored account ID sequence and adds PostgreSQL coverage for full restores.

Changes: Credential Carry

Layer / File(s) Summary
Account matching inputs
internal/backup/credentials_accounts.go, internal/backup/credentials_stricter.go, internal/backup/credentials.go
Live account data includes IDs and creation times. Account keys include creation time, and a temporary table records live and restored accounts that match on both fields.
Credential carry and ID sequence
internal/backup/credentials.go, internal/backup/credentials_accounts.go
Credential carry filters live account IDs through the matching-account set. The dropped-credentials report includes ID collisions with different restored accounts. The restore also advances the account ID sequence.
Full-restore regression coverage
internal/backup/carry_same_account_postgres_test.go, internal/backup/backup_postgres_test.go, internal/backup/carry_swapped_names_db_test.go, internal/backup/selective_postgres_test.go, scripts/ci/db-contracts.txt
PostgreSQL tests cover full-restore credential carry, account state, and subsequent ID allocation. Existing test fixtures now use matching creation times, and the new tests are listed as database contracts.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: blurbery, rhainland

Merge Risk: Minimal · up to 53219

Restores now carry live credentials only onto the same account (matched by ID and creation time) and keep the account ID sequence ahead of IDs already issued. No concrete merge-blocking risk was found. Owners may need to sign in again after a restore that drops credentials.

Security architecture risk: Low · up to 53219

The change reduces the risk of attaching live credentials to the wrong account. A restore can now stop before cutover if its database role cannot read the live account ID sequence. The live database remains in place, but recovery may be delayed until that access is corrected.

Retained concerns

  • Low · reliability · inferred: Same-deployment full restore now requires the restore role to read the live users sequence directly. A role that passed the prior restore checks and could read account and credential rows, but lacks SELECT on that sequence, can pass preflight and fail only after the backup has been loaded into staging. The failure stops cutover and preserves the live database, but can delay recovery.
Security review details

Security Blast Radius

  • observed — The ownership change applies to live account-owned credentials carried into the staged replacement database, including API keys and external sign-in links. The restore still withholds the live account-bound carry source when the backup is from another deployment.

Trust Boundaries and Controls

  • observed — Full restore takes the restore lock and checks for other live database clients before carry and again after closing the live database to new connections. The checks support the requirement to stop live writers; the restore lock does not itself exclude normal database clients.

Hardening Proposals

  • proposed — Check the live sequence read privilege before loading staging for a same-deployment restore. If access is missing, report the specific permission needed so operators can correct it before retrying without broadening the restore role unnecessarily.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(backup): carry live credentials only onto the same account" matches the main change: the restore now carries live credentials only onto an account whose ID and creation time both match.…
Description check Passed The description explains the problem (credentials carried onto a different account sharing an ID), the approach (same-account matching by ID and users.created_at, sequence advancement, stricter accoun…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(backup): carry live credentials only onto the same account" matches the main change: the restore now carries live credentials only onto an account whose ID and creation time both match. It is specific and concise, and a teammate scanning history would understand the primary change.

Full details: Description check

Explanation

The description explains the problem (credentials carried onto a different account sharing an ID), the approach (same-account matching by ID and users.created_at, sequence advancement, stricter account key change), validation, and risks. It lines up with the changeset files and objectives.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 51ec33b8-1fe7-4839-9421-2fc3715d2f76
Commits

Reviewing files that changed from the base of the PR and between a69771a and 532193f.

Files selected for processing: 3

  • internal/backup/carry_same_account_postgres_test.go
  • internal/backup/credentials_accounts.go
  • scripts/ci/db-contracts.txt

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 63157ddf-c44a-4068-ac35-2ce64ee1a72b
Commits

Reviewing files that changed from the base of the PR and between 0ad67a0 and a69771a.

Files selected for processing: 8

  • internal/backup/backup_postgres_test.go
  • internal/backup/carry_same_account_postgres_test.go
  • internal/backup/carry_swapped_names_db_test.go
  • internal/backup/credentials.go
  • internal/backup/credentials_accounts.go
  • internal/backup/credentials_stricter.go
  • internal/backup/selective_postgres_test.go
  • scripts/ci/db-contracts.txt

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Comment thread internal/backup/credentials_accounts.go Resolved
advanceAccountIDs moved the restored sequence past the highest live
account that still existed. An id handed to an account deleted since, or
the position an earlier restore of the same backup reached, could be
given out again. It now reads the live sequence's position in the live
snapshot and moves the restored sequence past the greatest of that, the
highest live account, the restored accounts and the restored sequence,
also when the live server has no accounts left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@silo-kody

silo-kody Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌
⚠️ 4 Kody Rule(s) were not evaluated

These rules declare that they need repository context beyond this diff, and the review could not retrieve it, so they were not judged on this pull request:

  • Preserve optional store capabilities through production wrappers
  • Detach mandatory cleanup from request cancellation and bound its lifetime
  • Preserve durable media identity and share identity-evidence precedence
  • Bound decoder throughput trades and share direct-play eligibility

@Quick104
Quick104 merged commit 9120fd3 into main Nov 27, 2026
19 checks passed
@Quick104
Quick104 deleted the fix/restore-carry-same-account branch November 27, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code impact: security Auth, privacy, or permission boundary priority: P0 Fix/merge first: data-loss, security, or widespread breakage size/L 200–499 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant