fix(auth): disambiguate "device revoked" from "device row missing"#131
Merged
Conversation
The auth middleware's device-token check called `is_device_active` and
returned `"device revoked"` for ANY `false` result. But the underlying
predicate collapsed two distinct states into that single bool:
- The row exists with `revoked_at IS NOT NULL` (an actual revocation).
- The row does not exist at all (FK cascade from a user delete, a
manual `DELETE FROM devices`, or a JWT carrying a `device_id` that
never matched anything).
Operators reading server logs — and the tray reading the 401 response
body — couldn't tell the two apart. A user reporting "my device says
revoked but I never touched the website" needed a manual SQL query
against `devices` to know whether the row was actually present.
Surfaced 2026-05-28 investigating a tray cloud-sync failure: the
device row was returning `"token rejected: device revoked"` overnight
with no user-initiated revoke action. Whether the row was actually
revoked or had been deleted entirely was indeterminable from the log.
## Changes
- New `DeviceAuthStatus` enum (`Active | Revoked | Missing`) in
`devices.rs`.
- `DeviceStore::is_device_active(bool)` replaced by
`DeviceStore::device_auth_status(DeviceAuthStatus)`. Both impls
(Postgres + Memory) updated; the SQL is unchanged, only the
pattern-match around `fetch_optional` is.
- `auth.rs` middleware now matches all three variants:
- `Active` → pass.
- `Revoked` → `401 "device revoked"` (unchanged legacy message).
- `Missing` → `401 "device not found"` + a server-side
`WARN device_id=… sub=…` so operators can cross-reference the
sub claim against the users table.
- Five test call sites updated from the bool API to the enum API.
- New `device_auth_status_distinguishes_active_revoked_missing` test
pins the 3-state contract.
No DB schema change. No API contract change beyond the error-string
distinction (clients that treated both responses as "token dead and
needs re-pair" continue to work — both still 401, both should still
trigger token clearing).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
"device revoked"for both actually-revoked rows AND rows that didn't exist at all (FK cascade from user delete, manual DELETE). Operators couldn't tell the two apart without a manual SQL query.DeviceStore::is_device_active(bool)withdevice_auth_status(DeviceAuthStatus)— a 3-state enum (Active | Revoked | Missing). The SQL is unchanged; only the post-fetch pattern-match differs."device revoked"(legacy, row+revoked_at) vs"device not found"(no row) and logsWARN device_id=… sub=…on the Missing path so operators can cross-reference the sub claim.Why now
Investigating an overnight tray cloud-sync failure on a freshly-paired device (no user-initiated revoke action). The 401 response said
"device revoked"but the user had not touched the website. Without disambiguation in the middleware, we couldn't tell from logs alone whether the row was actually revoked or had been deleted entirely. This change makes the next occurrence diagnosable in one log line.Test plan
cargo test -p starstats-server --bins— 647 passed (newdevice_auth_status_distinguishes_active_revoked_missingtest + 5 existing call sites updated).cargo fmt -p starstats-server --check— clean.cargo clippy -p starstats-server --bins --tests -- -D warnings— clean.WARN auth: device JWT presented but no matching row in devices tableline will appear when the row is gone.Compatibility
No API contract change beyond the error-string distinction. Clients that treated
"device revoked"as "token dead, clear and re-pair" continue to work — both responses are still 401 and both should still trigger the same token-clearing behavior in the tray (crates/starstats-client/src/sync.rs::clear_persisted_device_token).No DB schema change.