Repository navigation
fix(passwordreset): recheck admin authority when storing a reset link - #3313
Conversation
IssuePasswordReset checked the Owner rules and the scoped-key rule against an account read before the link was stored. If the Owner promoted the account in between, the promotion deleted the account's reset links first and a non-Owner admin, or a scoped key, still received a working link for the new admin. Repository.Issue now locks the issuer (FOR KEY SHARE) and the account (FOR NO KEY UPDATE) in id order, as ownership transfers do, reruns auth.CheckOwnerTarget and the scoped-key rule on the locked rows, and stores the link in the same transaction. A promotion or ownership move that commits first is refused; a promotion that commits later deletes the link as before. The issuer's weaker lock still waits for ownership moves but not for access-group updates, which lock rows in no fixed order. The handler keeps its early checks and maps the locked refusals to the same 403 problems. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Next included review available in 7 minutes. View limit details
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
Problem
Related issue: N/A
Validation tasks: none
A non-Owner administrator could end up with a working password reset link for another administrator's account. Only the server Owner may reset another admin's password, and a scoped API key may not reset any admin's. The reset endpoint checked those rules against the account as it was read at the start of the request, then stored the link later in a separate statement. If the Owner promoted the account to admin in between, the promotion deleted the account's reset links first, and the link stored afterwards still worked. Whoever held it could set the new admin's password.
The same gap let a scoped API key get a link for an account promoted mid-request, and let an Owner who handed ownership away mid-request finish issuing a link for another admin.
Approach
Repository.Issuenow runs in one transaction. It locks the issuing account and the target account in id order, the order ownership transfers use. It rerunsauth.CheckOwnerTargetand the scoped-key rule on the locked rows, then stores the link. If a promotion or ownership move commits first, issuance sees it and refuses. If a promotion commits afterwards, it deletes the new link, as it already did.FOR NO KEY UPDATEas before. The issuer is lockedFOR KEY SHARE, which waits for ownership moves (they takeFOR UPDATE) but not for access-group updates, which lock many accounts in no fixed order. Review reproduced a deadlock with access-group updates when the issuer was locked as strongly as the target.owner_protected(permission_deniedon v2) for the Owner rules, andinsufficient_scopefor a scoped key.IssueUnlessRecent), its one-hour TTL, five-minute cooldown, and the protection of admin-issued links are untouched.docs/architecture/password-resets.mdrecords the invariant.Surfaces: only
POST /api/v2/admin/users/{id}/password-resetchanges behavior, and only when an account changes role or ownership during the request. No/api/v1route issues reset links, and jellycompat has no reset-link surface. The wire shape and error codes do not change, so the web, Apple and Android clients need no change. The user manual already says only the Owner can reset another admin, so it needs no update.#2306 also turns
Repository.Issueinto a transaction, to write audit rows. The two will conflict textually; the audit insert belongs after this change's authority check, inside the same transaction.Validated cases: User management (#1196, #1197) and API keys C2 (#1190) cover admin authority and key scopes. They passed on builds made before reset links and the Owner rules existed, so those passes never exercised this path and stay valid.
Validation
MutateAdminAccount), or an ownership move, with the account locked, wait until issuance is blocked on it, then let it commit. Results:Issue, the non-Owner admin and scoped-key cases fail: issuance returns no error and the link resolves.-racewith-count=3.go build ./...,gofmt -l .,go vet ./...,make lint-changed(0 issues),make lint-router-recovery: pass.make test-db-contracts: all five password-reset contracts pass, including the two new ones. Two unrelated contracts fail locally:TestScanIndexesFileWithNonUTF8Name(the macOS filesystem rejects non-UTF-8 names) andTestNotificationInboxCutoffReplayAndTies, which appears to hit the same database-versus-host clock skew.make test-db-pins: one unrelated failure,TestCollectionCollageReadsKeepActiveVariantsDB. It compares the database clock with the host clock, and the local Docker VM clock ran about 55 ms behind.make test-go: failures only in packages that do not import the changed code (internal/jellycompat,internal/metadata/nfo,internal/playback,internal/scanner,internal/subtitles/subsync). They are case-variant filename, non-UTF-8 filename and real-FFmpeg tests that depend on the local macOS filesystem and media tooling.internal/passwordreset,internal/api/handlersandinternal/apiv2pass.make test-webhas 30 failures in 5 files (auth refresh, request deadline, player context, admin collections); this branch changes no web code.verify-*target and the committed apiv2 artifact test pass, except two.verify-route-inventorycould not type-check the cgo files on macOS (could not import C), so it did not compare routes; this branch changes no routes.verify-prometheus-rulesdid not run because pullingprom/prometheus:v3.5.0failed with a Docker DNS timeout.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3313/
No screen changes. The only behavior change is the refusal in the race window described above.
Risks
Checklist
AI Disclosure
FOR NO KEY UPDATEcould deadlock with access-group updates (reproduced; fixed by theFOR KEY SHAREissuer lock), that no test covered the issuer lock (added the ownership-move test), and one inaccurate comment (fixed). It also noted two older gaps outside this change: a deleted or demoted issuer is not refused under the lock, andmoveOwnershipkeeps reset links the former Owner issued.🤖 Generated with Claude Code
Note
Recheck admin authority transactionally when storing password reset links
Repository issuance now re-verifies owner-target and scoped-key/admin authorization inside the same transaction that stores the link, using locked account state, so concurrent promotions or ownership moves are judged after the lock is acquired.
passwordreset.IssuerandErrScopedKeyAdmin;Repository.Issuelocks the target (FOR NO KEY UPDATE) and issuer (FOR KEY SHARE) in sorted ID order, then inserts or replaces the link only if the target still matches.IssueInputand mapsErrScopedKeyAdminto the existinginsufficient_scopeAPI error in password_resets.go.Repository.Issue's issuer argument changed from an*account.IDpointer toIssuer; out-of-tree implementations of thepasswordreset.repositoryinterface must be updated.Macroscope summarized 2ac0a30.