Skip to content

fix(audiobooks): refuse Audiobookshelf tokens minted during a sign-out everywhere - #2919

Merged
Quick104 merged 1 commit into
mainfrom
fix/issue-2907-abs-token-revocation-race
Oct 31, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/issue-2907-abs-token-revocation-race

Conversation

@Quick104

@Quick104 Quick104 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2907
Related issue: N/A
Validation tasks: none

An Audiobookshelf-compatible app could stay signed in after its account was signed out everywhere (a password reset, an admin setting the password, an admin's Sign out everywhere, disabling the account). If the sign-out committed while the app was signing in, refreshing its token, or calling /authorize, the tokens that request stored afterwards kept working, for up to 30 days in the case of a refresh token. This change makes those requests store their tokens only while the credential they started from is still valid, and refuses them with 401 otherwise.

Every sign-out everywhere locks the account's users row and then revokes its abs_sessions rows in one statement. The Audiobookshelf handlers stored new rows in separate statements without that lock: refresh revoked the old token, then inserted the new pair; sign-in checked the password (opening a login session), then inserted; /authorize checked the presented token, then inserted. A revocation that committed in between could not see rows inserted after it.

Approach

TokenStore.InsertToken and RevokeTokenIfActive are replaced by IssueTokens, which stores tokens in one transaction that:

  • takes the account's users row FOR SHARE, so it waits for a sign-out in progress, and a sign-out that starts later waits for it and then revokes what it stored;
  • refuses a deleted or disabled account;
  • rechecks what the tokens come from: the login session the sign-in's credential check opened (now carried on abs.Principal.LoginSessionID), the refresh token being rotated (revoked in the same transaction, which keeps one-winner rotation), or the token /authorize was called with.

When that check fails the store returns ErrNotFound and the handler answers 401, which these apps already treat as "sign in again". handleRefresh now reads the old token's row before minting and leaves the revocation to IssueTokens, so a failed rotation rolls back and the old refresh token still works for a retry. A refresh token that fails the user, profile, type, or expiry checks is no longer revoked as a side effect; it fails those checks on every use anyway.

The issuance rule is added to docs/architecture/password-resets.md. Jellyfin-compatible sessions need no change: they already end with the login session they came from. No API, client, or schema change.

Validation

  • Postgres tests: the new handler test fails on main for all three paths (sign-in, refresh, /authorize each returned 200 and left live tokens after the sign-out) and passes with the fix. A second test holds each side of the account lock in turn and fails in both orders if FOR SHARE is removed. Both are listed in scripts/ci/db-contracts.txt and pass under the dbpins runner.
  • go build ./..., gofmt -l ., go vet ./..., make lint-changed, make lint-router-recovery, make verify-local-paths: clean. go test -race on the touched packages: pass.
  • make test-go: one failure, internal/mediasample TestRunStatsWithRealFFmpeg, which fails the same way on main with the local ffmpeg build.
  • Not reproduced live; the window is a few milliseconds and no sandbox was available for this run.

Evidence

No user-visible change. The page holds the test runs on main and on this branch.

Evidence: https://evidence.siloserver.org/r/silo-server/issue-2907/

Risks

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 says there is none.

AI Disclosure

🤖 Generated with Claude Code

Note

Reject Audiobookshelf tokens minted during sign-out everywhere

  • Replaces InsertToken and RevokeTokenIfActive on the token store with a single IssueTokens operation in abs_session_store.go. It locks the account row FOR SHARE, rejects missing or disabled accounts, validates the source credential (login session or JTI), and inserts all tokens in one transaction.
  • Sign-in, refresh, and /authorize in login.go now issue tokens through IssueTokens. If the source credential was revoked (sign-out everywhere), the handler returns HTTP 401 without storing tokens.
  • Refresh rotation now revokes the source JTI and stores both replacement tokens atomically. A failed or concurrent rotation leaves the old token live; concurrent rotations return 401 for the loser.
  • Adds database tests for both lock orderings between sign-out everywhere and token issuance, plus a docs section in password-resets.md.
  • Risk: TokenSource now requires a login-session ID at sign-in; the in-memory test store checks the source descriptor but not a session store, so out-of-tree TokenStore implementations must move to IssueTokens.

Macroscope summarized 7765800.

@silo-triage silo-triage Bot added the size/XL 500–999 added + deleted lines (all files); change volume, not priority. label Oct 22, 2026
@silo-kody

silo-kody Bot commented Oct 22, 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 ❌

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

  • Ask an admin to enable usage-based reviews

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

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: 43e19570-6220-4183-ade7-0da53a64b714
Commits

Reviewing files that changed from the base of the PR and between 271a4c8 and 7765800.

Files selected for processing: 11
  • docs/architecture/password-resets.md
  • internal/audiobooks/abs/handler.go
  • internal/audiobooks/abs/login.go
  • internal/audiobooks/abs/login_refresh_race_test.go
  • internal/audiobooks/abs/login_refresh_test.go
  • internal/audiobooks/abs/profile_pin_test.go
  • internal/audiobooks/abs_session_store.go
  • internal/audiobooks/abs_session_store_db_test.go
  • internal/audiobooks/abs_signout_db_test.go
  • internal/audiobooks/cred_validator.go
  • scripts/ci/db-contracts.txt
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@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 Oct 22, 2026

@Quick104 Quick104 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewed 88ef6476b464c416c355e065ec80b0f1649c1583. No actionable defects found.

The two new database tests pass against an isolated Postgres instance with the head migrations: sign-in, refresh, and /authorize refuse issuance after account-wide revocation, and both account-lock orderings preserve revocation. The handler test fails against the merge-base implementation: all three requests return 200 and leave new live tokens. Removing FOR SHARE in a scratch overlay also makes both lock-order tests fail. Focused refresh, authorize, and PIN tests pass.

The transaction holds the account lock through source validation and token writes. Refresh revocation and both replacement tokens commit together, so a failed write preserves the old token for retry. No live client was exercised; the evidence verifies handler and database behavior, not recovery UX.

Validated features: none of the 117 inspected passed result cells require revalidation. The native session/admin and Jellyfin paths remain unchanged; ABS cases #1247 C1–C3 are still Not run on the deferred beta surface. Validation tasks: none is appropriate. Full project enumeration was unavailable because the token lacks read:project; live issue bodies and comment-only results supplied the evidence. No validation records were changed.

The credentials-plugin sign-in race (#2920) and ordinary ABS profile-logout race (#2921) remain pre-existing, separately tracked limits. They are accurately disclosed. Native API and first-party client contracts are unchanged. The manual already says password resets sign devices out, so no manual correction was identified.

Fit: 10/10 · Readiness: 9/10 — ready (assuming CI is green).

Reviewed with Codex and gpt-6.1-sol at xhigh, using independent finder, validation, and sweep passes. No product code was changed.

…t everywhere

Audiobookshelf-compatible sign-in, refresh, and /authorize stored new
tokens in separate statements outside the account lock that every
sign-out everywhere takes, so a sign-out committing between the
credential check and the insert left the new tokens working.

Tokens are now stored by TokenStore.IssueTokens in one transaction that
holds the account's users row FOR SHARE, refuses a missing or disabled
account, and rechecks what the tokens come from: the login session the
sign-in's credential check opened, the refresh token being rotated
(revoked in the same transaction), or the token /authorize was called
with. The request gets 401 when that credential was revoked.

Closes #2907

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Quick104
Quick104 force-pushed the fix/issue-2907-abs-token-revocation-race branch from 88ef647 to 7765800 Compare October 31, 2026 04:02
@silo-kody

silo-kody Bot commented Oct 31, 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 ❌

@Quick104
Quick104 merged commit 7e48237 into main Oct 31, 2026
14 checks passed
@Quick104
Quick104 deleted the fix/issue-2907-abs-token-revocation-race branch October 31, 2026 04:09
Quick104 added a commit that referenced this pull request Oct 31, 2026
…3302)

#2919 made the token store refuse tokens issued with no source credential,
and #2793 added a login test whose principal carries none. Each passed CI
alone; together they fail `go test` on main.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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/XL 500–999 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] An Audiobookshelf token refreshed during a sign-out-everywhere stays valid

1 participant