Files
gabrielvidalandClaude Opus 5.5 39b8798405 feat(remoteaccess): the owner's internet address, signed in with the hub's own passkey
OWNER_HOSTS (homelab: hub.gabvdl.xyz) answers as the owner without
Authelia, for the one device that can't be on the personal tailnet (the
Orus Mac). On it the gate takes only the owner's __Host-aia_owner session
(7 days, host-only, Strict) — no Remote-User, host token or guest cookie is
anyone; without one only the sign-in answers.

- remoteaccess/webauthn.py: WebAuthn checked by the hub on `cryptography`
  (challenge, exact origin, RP ID, user present + verified, signature,
  counter); a small CBOR reader for new passkeys; fuzzed (80k inputs).
- passkeys added only through a one-time link made on the admin host
  (15 min, one use, hashed, fragment-borne); no mail fallback — Pocket ID
  was rejected because a mailed code there can enrol a passkey.
- Settings → Security: Internet access off (drops every session, back on
  from the admin host only); pushes on every sign-in and passkey added.
- machine paths (worker proxy tokens, tool proxy) refused there.
- tests: test_remote_access.py (31), test_webauthn.py (7).

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

113 lines
4.3 KiB
Python

"""The owner's passkeys for the internet address, and the one-time links that
add one (``/data/owner-passkeys.json``).
A passkey is only ever added through a link the owner made on the admin
host (tailnet + Authelia): 15 minutes, one use, its token stored hashed. No
mail, no recovery code — losing every passkey means making a new link from
the admin host."""
from __future__ import annotations
import os
import secrets
import time
from remoteaccess.webauthn import b64u
from users.store import _hash, _JsonFile
INVITE_TTL_S = int(os.environ.get("OWNER_PASSKEY_INVITE_TTL_S", str(15 * 60)))
MAX_PASSKEYS = 20
class PasskeyStore(_JsonFile):
def __init__(self, path: str):
super().__init__(path, {"passkeys": [], "invites": {}, "userHandle": ""})
with self._lock:
if not self._data.get("userHandle"):
# The WebAuthn user handle the owner's passkeys carry: random,
# never the name (it is stored in the authenticator).
self._data["userHandle"] = b64u(secrets.token_bytes(32))
try:
self._save()
except OSError:
pass
@property
def user_handle(self) -> str:
return self._data["userHandle"]
# ── passkeys ─────────────────────────────────────────────────────────────
def all(self) -> list[dict]:
with self._lock:
return [dict(p) for p in self._data["passkeys"]]
def get(self, cid: str) -> dict | None:
with self._lock:
return next((dict(p) for p in self._data["passkeys"] if p["id"] == cid), None)
def add(self, cred: dict, name: str) -> dict:
now = time.time()
rec = {**cred, "name": (name or "").strip()[:60] or "Passkey", "created": now,
"lastUsed": now}
with self._lock:
if any(p["id"] == rec["id"] for p in self._data["passkeys"]):
raise ValueError("this passkey is already added")
if len(self._data["passkeys"]) >= MAX_PASSKEYS:
raise ValueError(f"at most {MAX_PASSKEYS} passkeys")
self._data["passkeys"].append(rec)
self._save()
return dict(rec)
def used(self, cid: str, sign_count: int) -> None:
with self._lock:
for p in self._data["passkeys"]:
if p["id"] == cid:
p["signCount"], p["lastUsed"] = sign_count, time.time()
self._save()
return
def remove(self, cid: str) -> bool:
with self._lock:
n = len(self._data["passkeys"])
self._data["passkeys"] = [p for p in self._data["passkeys"] if p["id"] != cid]
if len(self._data["passkeys"]) == n:
return False
self._save()
return True
# ── one-time links ───────────────────────────────────────────────────────
def _prune(self, now: float) -> None:
for h in [h for h, r in self._data["invites"].items() if r["exp"] < now]:
del self._data["invites"][h]
def invite(self) -> tuple[str, float]:
"""A new link's token (only the hash is kept) and when it expires.
Making one voids the previous: there is never more than one open."""
token = secrets.token_urlsafe(32)
exp = time.time() + INVITE_TTL_S
with self._lock:
self._data["invites"] = {_hash(token): {"exp": exp}}
self._save()
return token, exp
def invite_ok(self, token: str) -> bool:
now = time.time()
with self._lock:
self._prune(now)
return bool(token) and _hash(token) in self._data["invites"]
def spend_invite(self, token: str) -> bool:
now = time.time()
with self._lock:
self._prune(now)
if self._data["invites"].pop(_hash(token or ""), None) is None:
return False
self._save()
return True
def cancel_invites(self) -> None:
with self._lock:
if self._data["invites"]:
self._data["invites"] = {}
self._save()