Skip to content

armgit / avibe

Public Imported from GitHub
0

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.

fix(remote-access): rate-limit unauthenticated OAuth callback diagnostics

cyh8d62fbe50cae
diff --git a/tests/test_ui_remote_access_auth.py b/tests/test_ui_remote_access_auth.py
index 15a5f1d20..d8fb120d2 100644
--- a/tests/test_ui_remote_access_auth.py
+++ b/tests/test_ui_remote_access_auth.py
@@ -1233,6 +1233,26 @@ def test_oauth_handshake_store_caps_entries(monkeypatch, tmp_path):
assert remote_access.pop_oauth_handshake("rid-0") is not None
+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.
+ clock = {"t": 1000.0}
+ monkeypatch.setattr(ui_server.time, "monotonic", lambda: clock["t"])
+ ui_server._oauth_diag_log_state.pop("test_key", None)
+
+ emitted = []
+ monkeypatch.setattr(ui_server.logger, "warning", lambda msg, *a: emitted.append(msg % a if a else msg))
+
+ for _ in range(5):
+ ui_server._log_oauth_diag("test_key", "boom x=%s", 1)
+ assert len(emitted) == 1 # only the first hit in the window is logged
+
+ clock["t"] += ui_server._OAUTH_DIAG_LOG_INTERVAL_SECONDS + 1
+ ui_server._log_oauth_diag("test_key", "boom x=%s", 1)
+ assert len(emitted) == 2
+ assert "suppressed" in emitted[1] # the 4 suppressed hits are reported
+
+
def test_oauth_error_page_localizes_from_accept_language(monkeypatch, tmp_path):
# The re-login page copy must come from vibe/i18n and honor the browser's
# Accept-Language (the only server-readable locale signal pre-auth).
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index dc6407685..d0f28530e 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -1432,6 +1432,31 @@ def _render_oauth_error_html(error: str, *, retry_href: str, lang: str = "en") -
"""
+_OAUTH_DIAG_LOG_INTERVAL_SECONDS = 60.0
+_oauth_diag_log_lock = threading.Lock()
+# key -> [window_start_monotonic, suppressed_count]
+_oauth_diag_log_state: dict[str, list[float]] = {}
+
+
+def _log_oauth_diag(key: str, message: str, *args: Any) -> None:
+ """Emit an unauthenticated-reachable OAuth diagnostic at WARNING, rate-limited
+ per ``key`` (~once / ``_OAUTH_DIAG_LOG_INTERVAL_SECONDS``).
+
+ The OAuth callback is reachable without auth, so a flood of invalid callbacks
+ would otherwise grow the (unrotated) service log without bound. Suppressed hits
+ are counted and folded into the next emitted line so the signal isn't lost.
+ """
+ now = time.monotonic()
+ with _oauth_diag_log_lock:
+ window_start, suppressed = _oauth_diag_log_state.get(key, (0.0, 0))
+ if window_start and now - window_start < _OAUTH_DIAG_LOG_INTERVAL_SECONDS:
+ _oauth_diag_log_state[key] = [window_start, suppressed + 1]
+ return
+ _oauth_diag_log_state[key] = [now, 0]
+ extra = f" [+{int(suppressed)} suppressed in {int(_OAUTH_DIAG_LOG_INTERVAL_SECONDS)}s]" if suppressed else ""
+ logger.warning(message + extra, *args)
+
+
def _oauth_callback_error_response(error: str, *, next_target: Any, status: int = 400):
"""Build the HTML re-login response for a failed OAuth callback.
@@ -1441,8 +1466,10 @@ def _oauth_callback_error_response(error: str, *, next_target: Any, status: int
"""
# Diagnostic only: whether the handshake cookie reached us at all is the key
# signal for cookie-loss cases (e.g. iOS standalone PWA). No token values are
- # logged — only presence and a few non-secret request hints.
- logger.warning(
+ # logged — only presence and a few non-secret request hints. Rate-limited
+ # because this path is unauthenticated and could be flooded.
+ _log_oauth_diag(
+ "callback_rejected",
"oauth callback rejected: error=%s handshake_cookie_present=%s ua=%r sec_fetch_site=%s",
error,
bool(request.cookies.get(REMOTE_OAUTH_COOKIE_NAME)),
@@ -2598,7 +2625,9 @@ def remote_access_auth_callback():
next_target = store_record.get("next")
else:
# Neither the cookie nor the server-side store yielded the handshake.
- logger.warning(
+ # Rate-limited: this branch is unauthenticated-reachable.
+ _log_oauth_diag(
+ "state_check_failed",
"oauth state check failed: cookie_parsed=%s cookie_state_rid=%s url_state_rid=%s url_state_valid=%s",
cookie_state is not None,
_peek_oauth_state_rid(cookie_state.get("state")) if cookie_state else None,