fix(audit): emit device.paired + device.revoked audit entries#134
Merged
Conversation
`device_routes::redeem` and `::revoke` were both mutating the
`devices` table (inserting a new row + setting `revoked_at`
respectively) without writing anything to `audit_log`. Surfaced
2026-05-28 during a tray cloud-sync investigation: a user's device
had been revoked overnight, but the audit log contained zero
device-lifecycle entries to attribute the action. The forensic
trail was a `revoked_at` timestamp on the row and nothing else.
`set_sync` (the sync-toggle handler) already emitted the right
shape — this PR mirrors that pattern for both endpoints.
## Changes
- `redeem` now emits `device.paired` with `{device_id, label}`
payload. The endpoint is unauthenticated (pre-pair) so actor
identity comes from the looked-up user, not a bearer claim.
- `revoke` now emits `device.revoked` with `{device_id}` payload,
attributed to the calling user.
- 404 revokes (device not owned by caller) emit nothing —
pre-auth probes shouldn't fill the audit log.
- `routes()` now layers the audit Extension on the redeem_router
too (previously only list_router got it; production redeem
would have 500'd on the new `Extension(audit)` extractor).
## Tests
- `revoke_emits_device_revoked_audit_entry` — happy-path emit
with actor_sub + actor_handle + payload assertions.
- `revoke_404_does_not_emit_audit_entry` — 404 path silent.
- `redeem_emits_device_paired_audit_entry` — happy-path emit
with all four fields (action, actor_sub, actor_handle, label).
- All 3 existing `revoke()` direct-invocation tests updated to
pass the new `Extension(audit)` parameter via `noop_audit()`.
- Fixture's redeem router gets the audit layer too.
650/650 starstats-server tests pass. fmt + clippy clean.
## Compatibility
No schema change, no API contract change. The audit_log table
gains new action strings (`device.paired`, `device.revoked`) —
consumers iterating `action LIKE '%device%'` already match these.
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_routes::redeemand::revokemutated thedevicestable (inserting rows / settingrevoked_at) without writing toaudit_log. Every device pair + unpair was a forensic black hole. Surfaced 2026-05-28 when a tray cloud-sync investigation needed to identify who/when revoked a device, but the audit log had zero device-lifecycle entries.Now mirrors the pattern already established by
set_sync(which correctly emitsdevice.sync_enabled/device.sync_disabled):redeem→ emitsdevice.pairedwith{device_id, label}. Actor identity comes from the looked-up user (the endpoint is unauthenticated since the desktop client is being paired because it doesn't yet have a token).revoke→ emitsdevice.revokedwith{device_id}. Actor is the calling user.routes()now layers the audit Extension onredeem_router(previously onlylist_routergot it; production redeem would have 500'd on the new extractor).Test plan
cargo test -p starstats-server --bins→ 650 passed (added 3 audit-emission tests:revoke_emits_device_revoked_audit_entry,revoke_404_does_not_emit_audit_entry,redeem_emits_device_paired_audit_entry).cargo fmt -p starstats-server --checkclean.cargo clippy -p starstats-server --bins --tests -- -D warningsclean.SELECT * FROM audit_log WHERE action = 'device.paired' ORDER BY occurred_at DESC LIMIT 1;→ expect the new entry with actor_handle + device_id + label.device.revoked.Compatibility
No schema change, no API contract change. Adds new
actionstrings to the existingaudit_logcolumn. Consumers usingaction LIKE '%device%'already match.Related