Files
gabrielvidalandClaude Opus 5.5 bfcb3d3b3b fix(remoteaccess): what the security review found — revocation that revokes
- removing a passkey ends every session it opened (sessions record the id);
- an open Chrome view on the internet address re-checks its session every
  5 s and closes when it ends (sign-out, passkey removed, access off);
- Sign out there always ends the owner's session unless the guest cookie
  beside it is a live impersonation — a cookie a guest's *.gabvdl.xyz site
  planted can no longer turn it into a no-op;
- the passkey link and "back on" need Authelia on the admin host, not just
  "not the internet address": the host token / loopback (what a run started
  from a stolen session can call) are refused; making a link now pushes;
- switching internet access off cancels an open link;
- ceremony tickets capped per address (10) so one address can't drain them;
- docs: what the off switch can't undo (the owner is a shell).
- 13 regression tests (test_remote_access: 51).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 15:19:41 +01:00

115 lines
4.3 KiB
Python

"""The owner's browser sessions on the internet address (``OWNER_HOSTS``).
Unlike a guest's sliding month, one of these ends ``OWNER_SESSION_TTL_S``
after the sign-in (7 days — Gabriel's pick, form 2026-11-24) however busy it
is, then a passkey again; ``OWNER_SESSION_IDLE_S`` can end it earlier when
unused (default: the same 7 days, i.e. off). The token itself is only ever
stored hashed (``/data/owner-sessions.json``)."""
from __future__ import annotations
import os
import secrets
import time
from users.store import _hash, _JsonFile
OWNER_SESSION_TTL_S = int(os.environ.get("OWNER_SESSION_TTL_S", str(7 * 24 * 3600)))
OWNER_SESSION_IDLE_S = int(os.environ.get("OWNER_SESSION_IDLE_S", str(OWNER_SESSION_TTL_S)))
# ``seen`` moves in memory on every request; the file only every this often
# (a restart then forgets at most this much activity — never extends a session).
_PERSIST_S = 300
COOKIE = "__Host-aia_owner"
class OwnerSessions(_JsonFile):
def __init__(self, path: str):
super().__init__(path, {"sessions": {}})
def _alive(self, rec: dict, now: float) -> bool:
return rec.get("exp", 0) > now and now - rec.get("seen", 0) < OWNER_SESSION_IDLE_S
def _prune(self, now: float) -> bool:
dead = [h for h, r in self._data["sessions"].items() if not self._alive(r, now)]
for h in dead:
del self._data["sessions"][h]
return bool(dead)
def open(self, ip: str, ua: str, via: str, passkey: str | None = None) -> str:
"""A new session; the token goes in the cookie, its hash here.
``passkey`` (its credential id) ends it when that passkey is removed."""
token = secrets.token_urlsafe(32)
now = time.time()
with self._lock:
self._prune(now)
self._data["sessions"][_hash(token)] = {
"id": secrets.token_hex(6), "created": now, "seen": now, "saved": now,
"exp": now + OWNER_SESSION_TTL_S, "ip": ip[:64], "ua": (ua or "")[:160],
"via": via, "passkey": passkey}
self._save()
return token
def close_passkey(self, pid: str) -> int:
"""Every session a passkey opened (it was removed)."""
with self._lock:
dead = [h for h, r in self._data["sessions"].items() if r.get("passkey") == pid]
for h in dead:
del self._data["sessions"][h]
if dead:
self._save()
return len(dead)
def check(self, token: str) -> dict | None:
"""The live session behind ``token`` (and one more request on it), else None."""
if not token:
return None
h = _hash(token)
now = time.time()
with self._lock:
rec = self._data["sessions"].get(h)
if rec is None:
return None
if not self._alive(rec, now):
del self._data["sessions"][h]
self._save()
return None
rec["seen"] = now
if now - rec.get("saved", 0) > _PERSIST_S:
rec["saved"] = now
self._save()
return dict(rec)
def close(self, token: str) -> None:
with self._lock:
if self._data["sessions"].pop(_hash(token or ""), None) is not None:
self._save()
def close_id(self, sid: str) -> bool:
with self._lock:
for h, r in list(self._data["sessions"].items()):
if r.get("id") == sid:
del self._data["sessions"][h]
self._save()
return True
return False
def close_all(self) -> int:
with self._lock:
n = len(self._data["sessions"])
self._data["sessions"] = {}
self._save()
return n
def list(self, current: str = "") -> list[dict]:
"""The live sessions, latest activity first; ``current`` (a cookie's
token) marks the one it belongs to."""
now = time.time()
mine = _hash(current) if current else None
with self._lock:
if self._prune(now):
self._save()
rows = [{**{k: r.get(k) for k in ("id", "created", "seen", "exp", "ip", "ua", "via")},
"current": h == mine}
for h, r in self._data["sessions"].items()]
return sorted(rows, key=lambda r: -(r["seen"] or 0))