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): skip cookie renewal on rejected requests

cyha6ebeb8a6819
diff --git a/tests/test_ui_remote_access_auth.py b/tests/test_ui_remote_access_auth.py
index 30e3649c7..15cbd6e1f 100644
--- a/tests/test_ui_remote_access_auth.py
+++ b/tests/test_ui_remote_access_auth.py
@@ -354,6 +354,36 @@ def test_remote_host_renews_cookie_past_half_ttl(monkeypatch, tmp_path):
assert payload["exp"] > near_exp
+def test_remote_host_does_not_renew_cookie_on_rejected_post(monkeypatch, tmp_path):
+ """A near-expiry cookie must NOT be slid by a request that later fails
+ a guard like CSRF/origin. Otherwise repeated rejected mutations could
+ keep a stolen session alive indefinitely."""
+ import time as _time
+
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ config = _save_config(tmp_path)
+ near_exp = int(_time.time()) + (remote_access.SESSION_TTL_SECONDS // 2) - 60
+ cookie = _forged_session_cookie(config, near_exp)
+ client = app.test_client()
+ client.set_cookie(remote_access.SESSION_COOKIE_NAME, cookie, domain="alex.avibe.bot")
+
+ # POST /config without CSRF/origin headers — protect_mutating_ui_requests
+ # will reject this with 403 inside the same request lifecycle that already
+ # set g.remote_session_renew in enforce_remote_access_cookie.
+ response = client.post(
+ "/config",
+ json={"remote_access": {"vibe_cloud": {"enabled": False}}},
+ base_url="https://alex.avibe.bot",
+ )
+
+ assert response.status_code == 403
+ refreshed = next(
+ (h for h in response.headers.getlist("Set-Cookie") if h.startswith(f"{remote_access.SESSION_COOKIE_NAME}=")),
+ None,
+ )
+ assert refreshed is None
+
+
def test_remote_host_fails_closed_when_config_load_fails(monkeypatch):
def fail_load():
raise ValueError("corrupt config")
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index 84011b296..2cea30881 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -728,6 +728,15 @@ def renew_remote_access_cookie(response: Response) -> Response:
renew = getattr(g, "remote_session_renew", None)
if not renew:
return response
+ # Only slide the session cookie when the request was actually accepted.
+ # The renew flag is set in the early `enforce_remote_access_cookie`
+ # before-request hook, but later guards (e.g. CSRF/origin checks in
+ # `protect_mutating_ui_requests`) may still reject the request. Refreshing
+ # the cookie on a rejected response would let repeated failed mutations
+ # keep a stolen session alive indefinitely without any successful
+ # authenticated action.
+ if response.status_code >= 400:
+ return response
config = _load_remote_access_config()
if config is None or not config.remote_access.vibe_cloud.session_secret:
return response