Device security and robustness (openpanel C0): check code, pnc, lenient report
- POST /devices/pair answers a 2-character `check`, listed by /devices/pending and shown beside each waiting entry; five codes a minute per address, and at 50 waiting the oldest gives way instead of a 429 - portal-minted device tokens are asked for with the device's nonce (`pnc`) and held against it, so unpair and delete revoke them; one without `pnc` stands only while the nonce is 0 - a device credential acts as the device (`device_principal`), not as the approving superuser, so deleting that account unpairs nothing - the report is validated port by port: unknown enum values read as `unknown`, unreadable ports and sections are left out and logged, `config` may name an entry of `config_schemas`; 422 only for no schema 1 or no object - devices.json is parsed once per change (cached by stat), delete drops the report and keeps the nonce, a busy recheck runs in the background - scope and io only from enabled flows (start/stop tells the devices), a port bound twice gives one io row - the Devices tab's saves cancel and hold back device refetches until settled
This commit is contained in:
+44
-15
@@ -53,9 +53,10 @@ def _device_for(payload: dict[str, Any], app: Any) -> DeviceDef:
|
||||
effect at once. Or the device's nonce moved on, which revokes exactly one
|
||||
screen's and leaves the device and its dashboards alone.
|
||||
|
||||
The nonce is only held against a credential this instance signed. One the
|
||||
portal minted for a remote screen carries none, the portal names the
|
||||
device and nothing else, and is revoked at the hub instead.
|
||||
The nonce is held against a credential the portal minted for a remote
|
||||
screen as well, which carries the nonce this instance named when it asked
|
||||
for it. One from an older portal carries none and reads as 0, so it
|
||||
stands only until the device is first unpaired or deleted.
|
||||
|
||||
The claim is still called ``panel``: tokens in circulation say it, and
|
||||
nothing pairs again for a rename.
|
||||
@@ -71,13 +72,12 @@ def _device_for(payload: dict[str, Any], app: Any) -> DeviceDef:
|
||||
device = store.find(str(payload.get("panel", "")))
|
||||
if device is None:
|
||||
raise InvalidTokenError("This device no longer exists")
|
||||
if payload.get("aud") == security.PANEL_AUDIENCE:
|
||||
try:
|
||||
nonce = store.nonce(device.id)
|
||||
except (OSError, ValueError) as exc:
|
||||
raise DeviceUnavailable(f"The device nonces are unreadable: {exc}")
|
||||
if int(payload.get("pnc") or 0) != nonce:
|
||||
raise InvalidTokenError("This device was paired again")
|
||||
try:
|
||||
nonce = store.nonce(device.id)
|
||||
except (OSError, ValueError) as exc:
|
||||
raise DeviceUnavailable(f"The device nonces are unreadable: {exc}")
|
||||
if int(payload.get("pnc") or 0) != nonce:
|
||||
raise InvalidTokenError("This device was paired again")
|
||||
presence.seen(device.id)
|
||||
return device
|
||||
|
||||
@@ -189,8 +189,9 @@ def _device_digests(device: DeviceDef, request: Request) -> set[str]:
|
||||
def _device_may(payload: dict[str, Any], request: Request) -> None:
|
||||
"""Refuse anything a wall panel has no business asking for.
|
||||
|
||||
A panel credential names the account that approved the pairing, so without
|
||||
this it would be that person's session hanging on a wall. What a panel
|
||||
A panel credential acts as the device itself, an account of no rights
|
||||
(``device_principal``), and what that account may reach is decided here
|
||||
and nowhere else. What a panel
|
||||
genuinely needs is small and worth writing out: the dashboards it was
|
||||
assigned, its own definition and report, the messages its own widgets and
|
||||
ports bind to, and the bytes those messages currently point at, for a tile
|
||||
@@ -214,9 +215,9 @@ def _device_may(payload: dict[str, Any], request: Request) -> None:
|
||||
The device is left on ``request.state`` for the route, which credits a
|
||||
value published on one of its ports to the node holding that port.
|
||||
|
||||
# ponytail: the device file, its nonce and the published dashboards are
|
||||
# re-read per request. Cache them behind the store's revision if this
|
||||
# shows up in a profile.
|
||||
# ponytail: the device file and the published dashboards are re-read per
|
||||
# request (the nonces are cached by the file's stat). Cache them behind
|
||||
# the store's revision if this shows up in a profile.
|
||||
"""
|
||||
device = _device_for(payload, request.app)
|
||||
request.state.device = device
|
||||
@@ -402,9 +403,35 @@ def _gate_panel(
|
||||
return payload
|
||||
|
||||
|
||||
#: Where the ids of device principals come from, so each device has its own.
|
||||
_DEVICE_IDS = uuid.UUID("5e1f2a0c-6d3b-4c1e-9f0a-0d3c1e5b7a20")
|
||||
|
||||
|
||||
def device_principal(device_id: str) -> User:
|
||||
"""The account a device credential acts as: the device, and no person.
|
||||
|
||||
Not stored. A device's credential is checked against the device itself
|
||||
(``_device_for``) and bounded by route (``_device_may``), so no account's
|
||||
rights are in play, and deleting or deactivating the superuser who
|
||||
approved the pairing leaves the device paired. Never a superuser, so a
|
||||
route gated on one refuses it even past the scope check.
|
||||
"""
|
||||
return User(
|
||||
id=uuid.uuid5(_DEVICE_IDS, device_id),
|
||||
email=f"{device_id}@device.invalid",
|
||||
hashed_password="",
|
||||
full_name=device_id,
|
||||
is_active=True,
|
||||
is_superuser=False,
|
||||
)
|
||||
|
||||
|
||||
def _user_for(session: Session, token_data: TokenPayload) -> User | None:
|
||||
"""The local account a payload names, by id or by portal identity.
|
||||
|
||||
A device credential, already checked by ``decode_token``, names the
|
||||
device instead, which acts as itself.
|
||||
|
||||
A token the portal minted names a person on the portal, not a user here, so
|
||||
a mapping is what turns one into the other. There are two ways one comes to
|
||||
exist: a superuser typed their code in, which makes the account up front,
|
||||
@@ -418,6 +445,8 @@ def _user_for(session: Session, token_data: TokenPayload) -> User | None:
|
||||
the account it named, and a person dropped at the portal is not adopted
|
||||
again.
|
||||
"""
|
||||
if token_data.panel:
|
||||
return device_principal(token_data.panel)
|
||||
if token_data.portal_sub:
|
||||
user = session.exec(
|
||||
select(User).where(User.portal_sub == token_data.portal_sub)
|
||||
|
||||
Reference in New Issue
Block a user