* fix(sync): bind device tokens to their device; dial only known push-pull hosts
A client token paired to one device could read, reconfigure, delete, sync
saves for and record play sessions as any other device of the same user.
Device CRUD, sync negotiate/complete, push-pull trigger, the save device
resolver and play-session ingest now treat another device as not found for
a bound token, through a shared require_token_device helper. GET /api/devices
lists only the bound device.
Push-pull let any user with devices.write point ssh_host at any host and
port, and the connection error surfaced through the sync session, so the
worker could be used to probe the internal network. The SSH handler now
refuses a host with no trusted key or CA in the admin's known_hosts before
dialling it, which keeps LAN handhelds working while nothing else is
reached.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(device): revoke a deleted device's tokens; scope registration matches
Addresses Greptile review on #5460. Deleting a device now deletes the client
tokens paired to it, since the foreign key's SET NULL turned them into unbound
tokens that reach every device. Registration with a bound token only matches
its own device by fingerprint, so it can no longer reset syncs or overwrite
capabilities on another device.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(sync): bind heartbeat, sync session and online reads; pin push-pull ports
pr-ready step 2 (code-review xhigh --fix).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(device): one require_device helper; parse known_hosts once
pr-ready step 3 (simplify).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(sync): narrow await_args before reading known_hosts
pr-ready step 4 (review-polish).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(sync): plainer comments on the push-pull allowlist and token revoke
pr-ready step 5 (humanizer).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(device): keep bound tokens off other tokens; cascade tokens with their device
A device-bound token can no longer create tokens, or regenerate, pair or
revoke a token of another device or an unbound one. The save session_id
counter only counts a session of the caller's bound device.
client_tokens.device_id now cascades on device delete (0159), so no path
can leave an unbound token behind, and deleting a device records a
client_token.revoke audit event for each token it takes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* chore(env): drop the push-pull allowlist note from env.template
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* fix(auth): bound new client tokens by the caller's scopes and stop delegates removing admins
- Creating a client token, approving a device, and regenerating or pairing
an existing token now check against the scopes of the credential making
the request rather than the whole account's. A narrow client token can no
longer mint, or recover the secret of, a broader one. Session and basic
auth carry the account's scopes, so they behave as before.
- Deleting an admin requires the caller to be an admin, mirroring the
existing rule for creating one, since users.write can be delegated.
- Editing another user requires the users.write scope on top of the admin
role, so an admin's me.write-only token can't change other accounts.
- own_only is rejected (422) on grants and overrides for entities with no
owner (platforms, roms, firmware, users, tasks, logs), where it silently
meant "all". The catalog exposes the ownable entities and the permission
matrices only offer "own" on those.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(permissions): keep owner-less own_only grants' access on save
Addresses Greptile review on #5346:
- An own_only delete grant on an entity with no owner allows nothing
today (delete has no coarse scope and the fine check runs with
owned=False), so it is now dropped from group grants, or turned into
a revoke for overrides, instead of being saved as full delete access.
The matrices show it the same way.
- The group dialog no longer saves when the permission catalog failed
to load, which would have stripped every own_only grant.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(auth): require users.write in the credential to act on another user (pr-ready: code-review)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* refactor(auth): share client token effective scopes and canOwn (pr-ready: simplify)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* docs(permissions): tighten grantEffect docstring and matrix comment (pr-ready: review-polish)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(auth): make user, token and streaming admin rules consistent
- Acting on another user (edit, delete, RA refresh) needs users.write in
the credential, and only admins may target an admin or grant the admin
role. One assert_admin_for_admin helper now backs add_user, invite
links, update, delete and admin token revocation.
- Re-issuing a client token's secret (regenerate, pair) needs every scope
it stores; deleting one needs every scope it currently grants, so a
narrow token can't remove a broader sibling.
- Streaming admin overrides need users.read or users.write in the
credential, not just the admin role.
- Migration 0154 rewrites stored own_only grants and overrides on
owner-less entities to the access they already gave, so the frontend
normalisation and the catalog save gate are gone.
- own_only is no longer rejected on revoke overrides.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Four query shapes had no index that could serve both their predicate and
their sort, and a class of foreign-key columns was relying on an implicit
index that only MariaDB and MySQL provide.
New portable indexes:
- `screenshots (rom_id, user_id)`. `Save.screenshot` / `State.screenshot`
resolve a thumbnail per row, so this lookup runs once per card on the
continue-playing rail. 0093 added the equivalent to `states` and noted
`saves` already had it; `screenshots` was missed.
- `rom_user (user_id, rom_id)` and `rom_user (user_id, last_played)`.
`unique_rom_user_props` leads with `rom_id`, which covers the gallery's
outer join but not the reverse direction, where sorting or scoping by a
per-user column starts from this table.
- `roms (platform_id, name_sort_key)`. The gallery is browsed one platform
at a time and ordered by name, and no index carried both columns. On a
120k-row library across 20 platforms this takes the first page from
5.8ms/1568 buffers to 0.6ms/75 buffers. Deep offsets converge, since the
skipped rows are walked either way.
New PostgreSQL-only foreign-key indexes:
MariaDB and MySQL index every foreign key whose column is not already some
index's leftmost prefix; PostgreSQL does not. Eight such columns are queried
directly or walked by `ON DELETE`, so they get a real index there. Creating
them everywhere would leave the other backends with duplicates, so they are
dialect-gated and exempt from autogenerate.
Model/schema reconciliation:
Fifteen live indexes existed only in the migrations. Worse, `alembic/env.py`
never imported six models, so `devices`, `client_tokens`, `device_save_sync`,
`sync_sessions`, `play_sessions` and `streaming_container_adoptions` were
absent from the metadata autogenerate compares against, and autogenerate in
fact failed outright resolving `saves.origin_device_id`. With the imports
fixed it would have proposed dropping every one of those indexes.
Declares all of them on their models, matches `PlaySession` to the unique
index 0076 actually created, and adds a test that fails on any future index
drift so this cannot silently recur.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgeQBXh6SJyxXVHSL5EuoF
Implements RFC 8628-style device authorization so clients
(argosy-launcher, grout) can pair by display instead of manually
copying tokens. Device posts to an open /api/auth/device/init with
its identifier and requested scopes; the server returns device_code
+ user_code + QR URL. User scans QR, lands at /pair/device, approves
(optionally editing name/scopes/expiry); the device's next poll on
/api/auth/device/token returns a ClientToken bound 1:1 to a newly-
created (or deduped) Device record. Downstream endpoints
(/play-sessions, /sync/negotiate) infer device_id from the bound
token so the client doesn't have to ship it on every call.
- Migrations 0080/0081: devices.client_device_identifier (unique
per user) and client_tokens.device_id FK (ON DELETE SET NULL)
- Five new endpoints under /api/auth/device (init/pending/approve/
deny/token) with Redis-backed state, per-IP rate limits, and
RFC-compliant error codes (authorization_pending, slow_down,
expired_token, access_denied)
- HybridAuthBackend surfaces bound device_id on request.state and
bumps devices.last_seen with a 5-minute debounce
- /api/users/me returns current_device_id for bound tokens so a
device can identify itself from its token alone
- Frontend approval screen at /pair/device with editable scopes/
name/expiry (defaults to Never), 3s auto-close countdown
- ClientApiTokens settings list shows bound-device chip
- 20 i18n keys added to all 17 locales; generated models updated
- 52 new tests across 13 classes; full suite 1334 passed
Planning and review assisted by Claude Code.