fix(sfm): one guard in front of every /device route -- UM serials 501 off the serial-aware routes, held lines 409 serial-less requests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
6dd34f8fd7
commit
82b91835fb
4 files changed
+109
-1
No files matched your search
+46
-1
@@ -50,7 +50,7 @@ import tempfile
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Mapping, Optional
|
||||
|
||||
# FastAPI / Pydantic
|
||||
try:
|
||||
@@ -84,6 +84,8 @@ from sfm.waveform_store import WaveformStore
|
||||
from sfm import db_snapshot
|
||||
import dataclasses
|
||||
from sfm.device_access import DeviceAccess, DeviceError, UnitAddress
|
||||
from sfm.device_access.errors import BadRequest
|
||||
from sfm.device_access.series import normalise_serial
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -379,6 +381,49 @@ def _via_device_access(fn) -> dict:
|
||||
return dataclasses.asdict(result) if dataclasses.is_dataclass(result) else result
|
||||
|
||||
|
||||
_SERIAL_AWARE_PATHS = frozenset({
|
||||
"/device/info", "/device/connect", "/device/monitor/status", "/device/settings",
|
||||
})
|
||||
|
||||
|
||||
def _device_guard(path: str, query: Mapping[str, str]) -> Optional[tuple]:
|
||||
"""Refuse a /device/* request that would reach the wrong series' code.
|
||||
|
||||
FastAPI drops query params a route does not declare, so `?serial=UM...` on
|
||||
a Series-3 route would silently run Series-3 bytes against a Micromate
|
||||
(0xA2 is CONFIRM ERASE there). Returns (status, detail) to refuse, or None.
|
||||
/device/hangup is always allowed: it must always be able to release a line.
|
||||
"""
|
||||
if not path.startswith("/device/") or path == "/device/hangup":
|
||||
return None
|
||||
serial = normalise_serial(query.get("serial") or "")
|
||||
if serial:
|
||||
if serial.startswith("UM") and path not in _SERIAL_AWARE_PATHS:
|
||||
return 501, f"not supported for Series 4 yet: {path}"
|
||||
return None
|
||||
if not (query.get("host") or query.get("port")):
|
||||
return None
|
||||
try:
|
||||
tcp_port = int(query.get("tcp_port") or DEFAULT_TCP_PORT)
|
||||
baud = int(query["baud"]) if query.get("baud") else None
|
||||
addr = _unit_address(query.get("port") or None, baud, query.get("host") or None, tcp_port)
|
||||
except (BadRequest, ValueError):
|
||||
return None
|
||||
held = _device_access().held_serial(addr)
|
||||
if held is not None:
|
||||
return 409, (f"a Series-4 line to {held} is held at {addr.key}; "
|
||||
"POST /device/hangup first")
|
||||
return None
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def _device_guard_middleware(request, call_next):
|
||||
refusal = _device_guard(request.url.path, request.query_params)
|
||||
if refusal is not None:
|
||||
return JSONResponse(status_code=refusal[0], content={"detail": refusal[1]})
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
_SERIAL_DOC = "Unit serial -- UM... (Series 4) or BE.../BA... (Series 3). Returns the shared model."
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user