Files
VoiceStudio/backend/core/auth.py
T
19ae20111a fix(security): replace persistent admin keys with scoped sessions (#1528)
* fix(security): replace persistent admin keys with sessions

Exchange the remote administrator key once for bounded, revocable credentials. Canonicalize backend principals, enforce cookie CSRF and exact origins, and use path-bound one-use WebSocket tickets.

Migrate the bundled UI away from durable master-key storage and credential-bearing URLs. Add unit, integration, static-hygiene, and production-browser regressions plus synchronized operator documentation.

* docs: link session hardening to PR 1528

* fix(security): key session indexes with process pepper

Use HMAC-SHA-256 instead of an unkeyed digest for in-memory session and WebSocket-ticket indexes. This preserves constant-size lookup identifiers, makes copied records unusable without the process pepper, and resolves CodeQL's weak sensitive-data hash finding.

* fix(auth): align empty bearer migration precedence

Centralize the Authorization-channel presence decision with canonical principal parsing. Bearer followed only by spaces now remains an empty channel during legacy cookie migration, while unsupported or invalid explicit credentials stay authoritative and fail closed.

* fix(security): harden admin session review boundaries

* fix(security): derive key generations with HKDF

* fix(auth): anchor the admin-session store so module reloads cannot fork it

test_master_exchange_does_not_bypass_pin_on_normal_routes failed in full-suite
runs: test_mcp_bindings' client fixture purges the services.* tree from
sys.modules and reloads main, so api.routers.auth re-imported a fresh
services.admin_sessions (new AdminSessionStore) while core.auth kept its
import-time reference to the old one — the exchange issued the cookie into
one store and the middleware resolved it against another, turning the
expected "PIN required" into "API key required".

Root cause is the class of bug, not the one test: a process-global auth
store defined as a bare module-level singleton forks under importlib.reload
or purge-and-reimport. Fix at the source: admin_session_store now resolves
through a synthetic sys.modules anchor (_omnivoice_admin_session_store_anchor)
that reloads never re-execute and package-prefix purges never match, so every
copy of the module shares the one per-process store. No consumer or behavior
changes.

Regression test reproduces both fork vectors (in-place reload and
sys.modules purge + fresh import) and asserts previously issued sessions
still resolve and the store identity is preserved; it fails before this fix
and passes after.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(auth): honor X-Forwarded-Proto for CSRF origin and Secure cookies behind TLS proxies

Behind Tailscale Serve (docs/remote-gpu.md) or any TLS-terminating proxy,
the browser talks https while the backend hop stays http, so exact-origin
CSRF compared an https Origin against an http expectation and rejected
every legitimate request, and the session cookie shipped without Secure.
uvicorn's ProxyHeadersMiddleware only rewrites the scope for loopback
peers, which misses Docker and any non-loopback proxy topology.

New core.csrf.effective_scheme derives the client-facing scheme: resolved
scope first (uvicorn's trusted-proxy rewrite wins), then an upgrade-only
read of X-Forwarded-Proto's first value — https/wss promotes http to
https, everything else is ignored, and a genuine TLS hop can never be
downgraded. Used by both the destination-origin comparison and
auth._secure_cookie so the WS-ticket/logout CSRF paths and the cookie
Secure flag agree. Spoofing gains nothing: the host:port half of the
origin tuple is untouched, browsers cannot attach the header cross-site
without a preflight this API never grants, and forging it on plain http
only adds Secure (the browser then drops the cookie — self-harm only).

Regression tests: proxied https origin accepted (origin check, Secure
flag, logout), comma-separated chains, scope-fallback path, spoofed
header still rejects cross-origin, cannot downgrade real https, junk
values ignored.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(auth): consume the stored admin key only after a successful exchange

A remote-backend user upgrading with their backend unreachable lost the
only stored copy of OMNIVOICE_API_KEY: every migration path deleted the
durable ov_api_key BEFORE the session exchange settled, stranding them
until they recovered the key from the server box. Close the whole class:

- client.ts bootstrap: read the legacy key, exchange first, and remove
  the durable copy only after the exchange succeeds; on failure the key
  stays so the next launch retries the migration (auth gate still rises).
- authSession.ts exchangeApiKey: move removeLegacyMaster from before the
  fetch to the cookie/bearer success paths — the key never coexists with
  a live session, but a rejected or hung exchange no longer consumes it.
- remoteBackendProbe.ts configuredRemoteBackend: stop wiping the key on
  every app mount.
- RemoteBackendPanel: a connection test or an aborted save no longer
  wipes the pending key; only disabling the remote backend discards it.
- prefKeys.js: ov_api_key moves from PREF_KEYS to PRESERVED_KEYS —
  factory reset preserves the pending connection credential exactly like
  ov_backend_url; the successful migration is what deletes it.

Tighten the credential-hygiene static guard to match: it accepted
sessionStorage.setItem('ov_api_key', …) — the exact class it exists to
close. The guard now flags .setItem(<master key>) on any storage
receiver, quote style, or injected-store alias, with a self-test pinning
what it catches and what stays legal.

Fail-before/pass-after regression tests: backend unreachable retains the
key and the next bootstrap retries it; a successful exchange removes it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* perf(auth): make session validation occupancy-independent

* test(auth): catch optional master-key storage calls

* feat(docs): add PR control document for bultodepapas in VoiceStudio

* docs: keep the PR tracking board in the fork; credit the changelog line

The pr-control document is excellent process discipline, but it is the
contributor's own operational board (their inventory, their update
commands) — it lives naturally in their fork, and docs/agents/ here is
context every repo agent loads. Removed with appreciation; the changelog
line gains its contributor credit.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: debpalash <4178343+debpalash@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 20:46:24 +00:00

422 lines
14 KiB
Python

"""Canonical authentication identity for HTTP and WebSocket connections.
Transport parsing belongs here; authorization remains in FastAPI dependencies.
Each ASGI scope receives exactly one secret-free :class:`AuthPrincipal` so
middleware and route guards cannot disagree about credential precedence.
"""
from __future__ import annotations
import ipaddress
import importlib
import os
import secrets
from collections.abc import Mapping
from dataclasses import dataclass, field
from enum import Enum
from services.admin_sessions import (
AdminSessionStore,
)
_AUTH_STATE_KEY = "auth_principal"
_LOOPBACK_HOSTS = frozenset({"127.0.0.1", "::1", "localhost"})
CONSUME_CAPABILITIES = frozenset({"consume"})
ADMIN_CAPABILITIES = frozenset({"consume", "admin"})
LOOPBACK_CAPABILITIES = frozenset({"consume", "admin", "native"})
class PrincipalKind(str, Enum):
ANONYMOUS = "anonymous"
LOOPBACK = "loopback"
TRUSTED_NETWORK = "trusted_network"
PIN = "pin"
API_KEY = "api_key"
ADMIN_SESSION = "admin_session"
class CredentialTransport(str, Enum):
NONE = "none"
HEADER = "header"
QUERY = "query"
COOKIE = "cookie"
LEGACY_COOKIE = "legacy_cookie"
WS_TICKET = "ws_ticket"
@dataclass(frozen=True)
class AuthPrincipal:
kind: PrincipalKind
capabilities: frozenset[str]
credential_id: str | None = None
transport: CredentialTransport = CredentialTransport.NONE
def allows(self, capability: str) -> bool:
return capability in self.capabilities
@dataclass(frozen=True)
class _CredentialCandidate:
value: str = field(repr=False)
transport: CredentialTransport
allow_master: bool = False
allow_session: bool = False
allow_ticket: bool = False
def remote_api_key() -> str | None:
"""Normalized remote operator key, read dynamically for rotation support."""
return os.environ.get("OMNIVOICE_API_KEY", "").strip() or None
def credential_matches(supplied: str | None, configured: str | None) -> bool:
"""Constant-time credential comparison that accepts the full Unicode range."""
if not supplied or not configured:
return False
return secrets.compare_digest(
supplied.encode("utf-8", errors="surrogatepass"),
configured.encode("utf-8", errors="surrogatepass"),
)
def _active_admin_session_store() -> AdminSessionStore:
"""Resolve mutable process state at call time so app reloads cannot split it."""
module = importlib.import_module("services.admin_sessions")
return module.admin_session_store
def _trusted_networks() -> tuple[ipaddress.IPv4Network | ipaddress.IPv6Network, ...]:
networks = []
for value in os.environ.get("OMNIVOICE_TRUSTED_NETWORKS", "").split(","):
value = value.strip()
if not value:
continue
try:
networks.append(ipaddress.ip_network(value, strict=False))
except ValueError:
# Invalid configuration never makes the gate fail open or wedge the
# backend. It simply contributes no trusted range.
continue
return tuple(networks)
def is_loopback(host: str | None) -> bool:
return host in _LOOPBACK_HOSTS
def is_local_host(host: str | None) -> bool:
if is_loopback(host):
return True
try:
address = ipaddress.ip_address(host)
except (TypeError, ValueError):
return False
if getattr(address, "ipv4_mapped", None):
address = address.ipv4_mapped
return any(address in network for network in _trusted_networks())
def _mapping_get(mapping: Mapping[str, str] | object, name: str) -> str:
if not mapping:
return ""
getter = getattr(mapping, "get", None)
if callable(getter):
value = getter(name, "")
if value:
return str(value)
# Real Starlette Headers are case-insensitive. This small fallback keeps
# minimal request stubs and non-Starlette callers correct too.
items = getattr(mapping, "items", None)
if callable(items):
for key, value in items():
if str(key).lower() == name.lower():
return str(value or "")
return ""
def _scope_type(connection) -> str:
scope = getattr(connection, "scope", None)
return str(scope.get("type", "http")) if isinstance(scope, dict) else "http"
def _path(connection) -> str:
scope = getattr(connection, "scope", None)
if isinstance(scope, dict):
return str(scope.get("path", ""))
return str(getattr(connection, "url", "") or "")
def _canonical_websocket_path(connection) -> str:
"""Remove only the ASGI-configured deployment prefix from a WS path."""
path = _path(connection)
scope = getattr(connection, "scope", None)
if not isinstance(scope, dict):
return path
root_path = str(scope.get("root_path", "") or "").rstrip("/")
if not root_path or root_path == "/":
return path
root_path = "/" + root_path.lstrip("/")
if path.startswith(root_path + "/"):
return path[len(root_path) :]
return path
def _client_host(connection) -> str | None:
client = getattr(connection, "client", None)
if client is not None:
return getattr(client, "host", None)
scope = getattr(connection, "scope", None)
if isinstance(scope, dict) and scope.get("client"):
return scope["client"][0]
return None
def _credential_candidate(connection) -> _CredentialCandidate | None:
query = getattr(connection, "query_params", None) or {}
cookies = getattr(connection, "cookies", None) or {}
raw_authorization = authorization_header(connection)
authorization = raw_authorization.strip()
if raw_authorization.lower().startswith("bearer "):
value = raw_authorization[7:].strip()
if value:
return _CredentialCandidate(
value=value,
transport=CredentialTransport.HEADER,
allow_master=True,
allow_session=True,
)
# Preserve the legacy normalization contract: ``Bearer`` followed
# only by whitespace is equivalent to an empty credential channel.
elif authorization:
# Any non-empty explicit Authorization value is authoritative, even
# when its scheme is unsupported or its Bearer payload is missing.
# It must never fall through to a stale ambient cookie.
return _CredentialCandidate(
value=authorization,
transport=CredentialTransport.HEADER,
)
if _scope_type(connection) == "websocket":
ticket = _mapping_get(query, "ws_ticket").strip()
if ticket:
return _CredentialCandidate(
value=ticket,
transport=CredentialTransport.WS_TICKET,
allow_ticket=True,
)
query_key = _mapping_get(query, "api_key").strip()
if query_key:
return _CredentialCandidate(
value=query_key,
transport=CredentialTransport.QUERY,
allow_master=True,
)
session = _mapping_get(cookies, "ov_session").strip()
if session:
return _CredentialCandidate(
value=session,
transport=CredentialTransport.COOKIE,
allow_session=True,
)
legacy_key = _mapping_get(cookies, "ov_key").strip()
if legacy_key:
return _CredentialCandidate(
value=legacy_key,
transport=CredentialTransport.LEGACY_COOKIE,
allow_master=True,
)
return None
def presented_api_key(connection) -> str:
"""Compatibility extractor for the durable API-key transports only."""
candidate = _credential_candidate(connection)
if candidate is None or not candidate.allow_master:
return ""
return candidate.value
def authorization_header(connection) -> str:
headers = getattr(connection, "headers", None) or {}
return _mapping_get(headers, "authorization")
def authorization_credential_present(connection) -> bool:
"""Whether Authorization contains an authoritative credential channel.
This deliberately mirrors :func:`_credential_candidate`: whitespace and
``Bearer`` followed only by spaces are empty channels that may fall back to
legacy migration state. Unsupported schemes and ``Bearer`` without the
required separating space remain explicit invalid credentials.
"""
authorization = authorization_header(connection)
if authorization.lower().startswith("bearer ") and not authorization[7:].strip():
return False
return bool(authorization.strip())
def bearer_header_value(connection) -> str:
authorization = authorization_header(connection)
if not authorization.lower().startswith("bearer "):
return ""
return authorization[7:].strip()
def legacy_master_cookie_valid(connection) -> bool:
configured = remote_api_key()
cookies = getattr(connection, "cookies", None) or {}
supplied = _mapping_get(cookies, "ov_key").strip()
return credential_matches(supplied, configured)
def master_header_valid(connection) -> bool:
configured = remote_api_key()
supplied = bearer_header_value(connection)
return credential_matches(supplied, configured)
def _configured_pin(connection) -> str | None:
app = getattr(connection, "app", None)
state = getattr(app, "state", None) if app is not None else None
network_share = getattr(state, "network_share", None) if state is not None else None
pin = getattr(network_share, "pin", None) if network_share is not None else None
return str(pin) if pin else None
def _valid_pin(connection) -> bool:
configured = _configured_pin(connection)
if not configured:
return False
headers = getattr(connection, "headers", None) or {}
query = getattr(connection, "query_params", None) or {}
cookies = getattr(connection, "cookies", None) or {}
supplied = (
_mapping_get(headers, "x-omnivoice-pin").strip()
or _mapping_get(query, "pin").strip()
or _mapping_get(cookies, "ov_pin").strip()
)
return credential_matches(supplied, configured)
def _attached_principal(connection) -> AuthPrincipal | None:
scope = getattr(connection, "scope", None)
if not isinstance(scope, dict):
return None
state = scope.get("state")
if isinstance(state, dict):
principal = state.get(_AUTH_STATE_KEY)
return principal if isinstance(principal, AuthPrincipal) else None
return None
def _attach_principal(connection, principal: AuthPrincipal) -> AuthPrincipal:
scope = getattr(connection, "scope", None)
if isinstance(scope, dict):
state = scope.setdefault("state", {})
if isinstance(state, dict):
state[_AUTH_STATE_KEY] = principal
return principal
def resolve_principal(
connection,
*,
store: AdminSessionStore | None = None,
) -> AuthPrincipal:
"""Resolve and attach the single authentication decision for one scope."""
attached = _attached_principal(connection)
if attached is not None:
return attached
if store is None:
store = _active_admin_session_store()
host = _client_host(connection)
if is_loopback(host):
return _attach_principal(
connection,
AuthPrincipal(PrincipalKind.LOOPBACK, LOOPBACK_CAPABILITIES),
)
candidate = _credential_candidate(connection)
configured_key = remote_api_key()
if candidate is not None:
principal: AuthPrincipal | None = None
if (
candidate.allow_master
and credential_matches(candidate.value, configured_key)
):
principal = AuthPrincipal(
PrincipalKind.API_KEY,
ADMIN_CAPABILITIES,
credential_id="api-key",
transport=candidate.transport,
)
elif candidate.allow_session:
session = store.resolve(candidate.value, configured_key)
if session is not None:
principal = AuthPrincipal(
PrincipalKind.ADMIN_SESSION,
session.capabilities,
credential_id=session.credential_id,
transport=candidate.transport,
)
elif candidate.allow_ticket:
session = store.consume_ws_ticket(
candidate.value,
_canonical_websocket_path(connection),
configured_key,
)
if session is not None:
principal = AuthPrincipal(
PrincipalKind.ADMIN_SESSION,
session.capabilities,
credential_id=session.credential_id,
transport=candidate.transport,
)
if principal is not None:
return _attach_principal(connection, principal)
# An explicit, non-empty credential is authoritative. Do not silently
# fall back to network or PIN trust after an invalid higher-priority
# credential was presented.
return _attach_principal(
connection,
AuthPrincipal(
PrincipalKind.ANONYMOUS,
frozenset(),
transport=candidate.transport,
),
)
if is_local_host(host):
return _attach_principal(
connection,
AuthPrincipal(PrincipalKind.TRUSTED_NETWORK, CONSUME_CAPABILITIES),
)
if _valid_pin(connection):
return _attach_principal(
connection,
AuthPrincipal(
PrincipalKind.PIN,
CONSUME_CAPABILITIES,
transport=CredentialTransport.HEADER,
),
)
return _attach_principal(
connection,
AuthPrincipal(PrincipalKind.ANONYMOUS, frozenset()),
)
def principal_for(
connection,
*,
store: AdminSessionStore | None = None,
) -> AuthPrincipal:
return _attached_principal(connection) or resolve_principal(connection, store=store)