* 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(db): bump client token and device last-used timestamps without a prior read
update_last_used() and update_last_seen_debounced() read the row with
session.get() and then updated it in the same transaction. When two requests
with the same Client API Token ran concurrently and both passed the debounce
check, the second UPDATE hit MariaDB error 1020 (Record has changed since last
read), so a read-only GET returned HTTP 500. Make each a single conditional
UPDATE that only touches the row when the debounce window has passed.
Fixes#5379
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
* test(db): code-review fixes for last-used race tests
Reuse the executed_statements fixture, scope race listeners to
sync_engine, add the device race test, drop an unused fixture, and
apply trunk fmt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(db): simplify pass on last-used race fix
Share the race hook as tests/db_race_stubs.run_before_first, look tokens
up by id in tests, trim duplicated docstrings, and state the handler
comment as current behaviour.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(db): humanizer pass on last-used handler comments
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(db): run MariaDB and MySQL sessions at READ COMMITTED
MariaDB 11.6+ turns on innodb_snapshot_isolation, so at REPEATABLE READ any
transaction that reads a row and then writes it fails with error 1020
("Record has changed since last read") when another transaction committed
to that row in between. READ COMMITTED, PostgreSQL's default, takes a fresh
snapshot per statement and removes the whole class of failures.
A server logging binlog_format=STATEMENT refuses InnoDB writes below
REPEATABLE READ, so its sessions keep the default and log a warning once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* perf(auth): skip last-used and last-active writes inside the debounce window
Client-token auth bumped the token, the device and the user on every
request, the user with an undebounced UPDATE plus a re-SELECT. The token's
loaded last_used_at now gates the token and device bumps, and
set_last_active checks the loaded last_active and bumps through a
debounced conditional UPDATE, so a request inside the window writes
nothing and takes no row lock.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
- Replace the inline bytes-to-text decodes with redis_handler.as_text
- Add utils.auth.token_device_id for a request's token-bound device
- Look up a group's member tokens in one query and close them in one pass
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>