Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(remote-access): harden the /auth rate limiter (codex round 5)
cyhb2722a07510d
diff --git a/tests/test_ui_remote_access_auth.py b/tests/test_ui_remote_access_auth.pyindex d8494259b..f51c201fc 100644--- a/tests/test_ui_remote_access_auth.py+++ b/tests/test_ui_remote_access_auth.py@@ -1277,6 +1277,47 @@ def test_unauthenticated_auth_requests_are_rate_limited(monkeypatch, tmp_path):assert statuses[3:] == [429, 429] # over budget -> throttled+def test_auth_rate_limit_ignores_untrusted_forwarded_ip(monkeypatch, tmp_path):+ # A direct (non-loopback) peer can't dodge the limit by rotating CF-Connecting-IP:+ # the forwarded IP is trusted only from the loopback tunnel peer, so such a peer+ # is keyed by its real address and the rotating header is ignored.+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))+ _save_config(tmp_path)+ monkeypatch.setattr(ui_server, "_AUTH_RATELIMIT_MAX_PER_WINDOW", 3)+ client = app.test_client()++ statuses = [+ client.get(+ "/dashboard",+ base_url="https://alex.avibe.bot",+ environ_base={"REMOTE_ADDR": "203.0.113.90"},+ headers={"CF-Connecting-IP": f"9.9.9.{i}"}, # rotated each request+ follow_redirects=False,+ ).status_code+ for i in range(5)+ ]+ assert statuses[:3] == [302, 302, 302]+ assert statuses[3:] == [429, 429] # still limited despite the rotating header+++def test_auth_rate_limit_table_is_bounded(monkeypatch, tmp_path):+ # The limiter's own table is hard-capped (LRU eviction), so a burst of distinct+ # clients can't drive unbounded in-process memory growth.+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))+ _save_config(tmp_path)+ monkeypatch.setattr(ui_server, "_AUTH_RATELIMIT_MAX_TRACKED_CLIENTS", 3)+ client = app.test_client()++ for i in range(10): # 10 distinct peers+ client.get(+ "/dashboard",+ base_url="https://alex.avibe.bot",+ environ_base={"REMOTE_ADDR": f"198.51.100.{i}"},+ follow_redirects=False,+ )+ assert len(ui_server._auth_ratelimit) <= 3++def test_oauth_diag_log_is_rate_limited(monkeypatch):# The unauthenticated callback failure path must not grow the log without bound:# repeated hits within the window emit once, with the suppressed count folded in.diff --git a/vibe/ui_server.py b/vibe/ui_server.pyindex 888672b7d..f8e9df617 100644--- a/vibe/ui_server.py+++ b/vibe/ui_server.py@@ -16,6 +16,7 @@ import socketimport subprocessimport threadingimport time+from collections import OrderedDictfrom contextlib import contextmanagerfrom datetime import datetimefrom pathlib import Path@@ -1498,13 +1499,23 @@ _AUTH_RATELIMIT_WINDOW_SECONDS = 60.0_AUTH_RATELIMIT_MAX_PER_WINDOW = 60 # a real login spends a handful; this only stops floods_AUTH_RATELIMIT_MAX_TRACKED_CLIENTS = 4096_auth_ratelimit_lock = threading.Lock()-_auth_ratelimit: dict[str, list[float]] = {} # client -> [window_start_monotonic, count]+# Bounded LRU of client -> [window_start_monotonic, count]; the least-recently-seen+# entry is evicted once the table is full, so the table can't grow without bound.+_auth_ratelimit: OrderedDict[str, list[float]] = OrderedDict()def _auth_client_id() -> str:- """Client identity for rate limiting: the Cloudflare-forwarded IP when present- (remote traffic always arrives via the tunnel), else the connecting peer."""- return (request.headers.get("CF-Connecting-IP") or request.remote_addr or "unknown").strip()+ """Client identity for rate limiting.++ Trust the Cloudflare-forwarded client IP only when the request arrived via the+ local tunnel (loopback peer = cloudflared). A direct peer reaching the origin+ port could otherwise set/rotate ``CF-Connecting-IP`` to dodge the limit, so for+ such peers we key on the real connecting address instead.+ """+ forwarded = (request.headers.get("CF-Connecting-IP") or "").strip()+ if forwarded and _is_loopback_peer():+ return f"cf:{forwarded}"+ return f"peer:{(request.remote_addr or 'unknown').strip()}"def _auth_rate_limited() -> bool:@@ -1512,16 +1523,20 @@ def _auth_rate_limited() -> bool:client = _auth_client_id()now = time.monotonic()with _auth_ratelimit_lock:- if len(_auth_ratelimit) > _AUTH_RATELIMIT_MAX_TRACKED_CLIENTS:- for stale in [c for c, (ws, _c) in _auth_ratelimit.items() if now - ws >= _AUTH_RATELIMIT_WINDOW_SECONDS]:- _auth_ratelimit.pop(stale, None)- window_start, count = _auth_ratelimit.get(client, (0.0, 0))- if now - window_start >= _AUTH_RATELIMIT_WINDOW_SECONDS:+ bucket = _auth_ratelimit.get(client)+ if bucket is None or now - bucket[0] >= _AUTH_RATELIMIT_WINDOW_SECONDS:+ # New or rolled-over window. Hard-bound the table before admitting a+ # genuinely new client (evict the least-recently-seen).+ if client not in _auth_ratelimit:+ while len(_auth_ratelimit) >= _AUTH_RATELIMIT_MAX_TRACKED_CLIENTS:+ _auth_ratelimit.popitem(last=False)_auth_ratelimit[client] = [now, 1]+ _auth_ratelimit.move_to_end(client)return False- if count >= _AUTH_RATELIMIT_MAX_PER_WINDOW:+ if bucket[1] >= _AUTH_RATELIMIT_MAX_PER_WINDOW:return True- _auth_ratelimit[client] = [window_start, count + 1]+ bucket[1] += 1+ _auth_ratelimit.move_to_end(client)return False