Restart safety: transient credential checks are 1013/503, not a refusal

- A device credential that cannot be checked now (device store not running,
  nonces unreadable) raises DeviceUnavailable: 503 over HTTP, and the
  /flows/ws handshake and the tunnel stream accept and close with 1013.
  1008 (a 403 before accepting) stays for a revoked or invalid one
- Device files are written through a temporary file, so a read mid-save no
  longer looks like a deleted device
- The first report turns Touch friendly on only for a device paired since
  pairings are noted (DeviceState.paired_at), so devices paired before keep
  the switch they were given
- NodeDef reads `device`/`device_policy` from older clients as the worker
  label, for a release
- Tests: transient vs revoked socket, the touch default, the old worker keys,
  and a restart with a device node and a node_health rule sending no push
This commit is contained in:
Melvin Strobl
2026-10-16 18:18:42 +02:00
parent 4286acadfd
commit 13d23f6baf
11 changed files with 228 additions and 26 deletions
+15 -9
View File
@@ -663,15 +663,21 @@ class CloudConnector:
if not path.startswith("/api/v1/") or not path.endswith("/flows/ws"):
await socket.send(_dump({"op": "ws_close", "id": stream_id, "code": 1008}))
return
with Session(engine) as session:
if user_from_token(session, token, app=self._app) is None:
await socket.send(
_dump({"op": "ws_close", "id": stream_id, "code": 1008})
)
return
only = panel_scope(token, self._app)
# A remote device is online for as long as this stream is open.
device = device_of(token, self._app) if only is not None else ""
try:
with Session(engine) as session:
user = user_from_token(session, token, app=self._app)
only = panel_scope(token, self._app) if user is not None else None
# A remote device is online for as long as this stream is open.
device = device_of(token, self._app) if only is not None else ""
except Exception as exc:
# Could not tell, which is not a refusal: 1008 is what makes a
# device check whether it is still paired.
logger.warning("Could not check a stream's credential: %s", exc)
await socket.send(_dump({"op": "ws_close", "id": stream_id, "code": 1013}))
return
if user is None:
await socket.send(_dump({"op": "ws_close", "id": stream_id, "code": 1008}))
return
await socket.send(_dump({"op": "ws_open_ok", "id": stream_id}))