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(show): require the page's exact Origin on HMR websockets (#2603)
Maxf36f610b5f5f
diff --git a/tests/test_ui_show_pages.py b/tests/test_ui_show_pages.pyindex a15f1074e..e39da65c7 100644--- a/tests/test_ui_show_pages.py+++ b/tests/test_ui_show_pages.py@@ -12765,7 +12765,7 @@ def test_private_show_page_hmr_websocket_requires_private_page(monkeypatch, tmp_try:with app.test_client().websocket_connect("/show/ses123/__vite_hmr",- headers={"host": "127.0.0.1:5123"},+ headers={"host": "127.0.0.1:5123", "origin": "http://127.0.0.1:5123"},subprotocols=["vite-hmr"],):raise AssertionError("websocket should not connect")@@ -12780,7 +12780,7 @@ def test_private_show_page_hmr_websocket_requires_remote_session(monkeypatch, tmwith app.test_client().websocket_connect("wss://alex.avibe.bot/show/ses123/__vite_hmr",- headers={"host": "alex.avibe.bot"},+ headers={"host": "alex.avibe.bot", "origin": "https://alex.avibe.bot"},subprotocols=["vite-hmr"],) as websocket:with pytest.raises(WebSocketDisconnect) as exc:@@ -12804,7 +12804,7 @@ def test_private_show_page_hmr_websocket_requires_editor(monkeypatch, tmp_path):try:with client.websocket_connect("wss://alex.avibe.bot/show/ses123/__vite_hmr",- headers={"host": "alex.avibe.bot"},+ headers={"host": "alex.avibe.bot", "origin": "https://alex.avibe.bot"},subprotocols=["vite-hmr"],) as websocket:websocket.receive_text()@@ -12889,7 +12889,7 @@ def test_private_show_page_hmr_websocket_accepts_remote_session(monkeypatch, tmptry:with client.websocket_connect("wss://alex.avibe.bot/show/ses123/__vite_hmr",- headers={"host": "alex.avibe.bot"},+ headers={"host": "alex.avibe.bot", "origin": "https://alex.avibe.bot"},subprotocols=["vite-hmr"],) as websocket:websocket.receive_text()@@ -13096,6 +13096,7 @@ def test_private_show_page_hmr_websocket_accepts_setup_host_local_peer(monkeypat"/show/ses123/__vite_hmr",headers={"host": "192.168.2.3:5123",+ "origin": "http://192.168.2.3:5123","x-vibe-test-remote-addr": "192.168.2.44",},subprotocols=["vite-hmr"],@@ -13138,6 +13139,7 @@ def test_shared_mode_show_page_hmr_websocket_accepts_local_peer("/show/ses123/__vite_hmr",headers={"host": "192.168.2.3:5123",+ "origin": "http://192.168.2.3:5123","x-vibe-test-remote-addr": "192.168.2.44",},subprotocols=["vite-hmr"],@@ -13221,7 +13223,7 @@ def test_public_show_page_hmr_websocket_uses_share_path(monkeypatch, tmp_path):try:with app.test_client().websocket_connect(f"wss://alex.avibe.bot/p/{share_id}/__vite_hmr?token=test-token",- headers={"host": "alex.avibe.bot"},+ headers={"host": "alex.avibe.bot", "origin": "https://alex.avibe.bot"},subprotocols=["vite-hmr"],) as websocket:websocket.receive_text()@@ -13247,7 +13249,7 @@ def test_public_show_page_hmr_websocket_requires_public_page(monkeypatch, tmp_patry:with app.test_client().websocket_connect(f"wss://alex.avibe.bot/p/{share_id}/__vite_hmr",- headers={"host": "alex.avibe.bot"},+ headers={"host": "alex.avibe.bot", "origin": "https://alex.avibe.bot"},subprotocols=["vite-hmr"],):raise AssertionError("websocket should not connect")@@ -13255,6 +13257,85 @@ def test_public_show_page_hmr_websocket_requires_public_page(monkeypatch, tmp_paassert getattr(exc, "code", None) == 1008+_HMR_ORIGIN_ROUTES = (+ # Private HMR from a loopback peer: no cookie gate, so Origin is the only guard.+ pytest.param("/show/ses123/__vite_hmr", "127.0.0.1:5123", "http://127.0.0.1:5123", id="private-loopback"),+ # Public HMR: public visibility, no authentication at all.+ pytest.param(None, "alex.avibe.bot", "https://alex.avibe.bot", id="public-share"),+)+++def _hmr_route_url(route, host, share_id):+ if route is not None:+ return route+ return f"wss://{host}/p/{share_id}/__vite_hmr"+++def _hmr_origin_variants(page_origin: str) -> dict[str, str | None]:+ parsed = urllib.parse.urlsplit(page_origin)+ other_scheme = "https" if parsed.scheme == "http" else "http"+ return {+ "cross-site": "https://evil.example",+ "other-port": f"{parsed.scheme}://{parsed.hostname}:6123",+ "other-scheme": f"{other_scheme}://{parsed.netloc}",+ "missing": None,+ }++[email protected]("variant", ("cross-site", "other-port", "other-scheme", "missing"))[email protected]("route,host,page_origin", _HMR_ORIGIN_ROUTES)+def test_show_page_hmr_websocket_rejects_foreign_origin(monkeypatch, tmp_path, route, host, page_origin, variant):+ # Browsers do not enforce same-origin on WebSocket handshakes. Whatever other+ # gate a route has, an Origin other than the page's own must never reach the+ # runtime socket.+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))+ _save_config(tmp_path)+ share_id = _create_show_page("ses123", "public")+ manager = _FakeShowRuntimeManager()+ set_show_runtime_manager_for_tests(manager)+ headers = {"host": host}+ origin = _hmr_origin_variants(page_origin)[variant]+ if origin is not None:+ headers["origin"] = origin+ try:+ with app.test_client().websocket_connect(+ _hmr_route_url(route, host, share_id),+ headers=headers,+ subprotocols=["vite-hmr"],+ ):+ raise AssertionError("websocket should not connect")+ except Exception as exc:+ assert getattr(exc, "code", None) == 1008+ finally:+ set_show_runtime_manager_for_tests(None)++ assert manager.websocket_paths == []++[email protected]("route,host,page_origin", _HMR_ORIGIN_ROUTES)+def test_show_page_hmr_websocket_accepts_page_origin(monkeypatch, tmp_path, route, host, page_origin):+ # Control: the page's own origin passes the gate and reaches the runtime proxy+ # (which fails with 1011 here because the runtime is a fake).+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))+ _save_config(tmp_path)+ share_id = _create_show_page("ses123", "public")+ manager = _FakeShowRuntimeManager()+ set_show_runtime_manager_for_tests(manager)+ try:+ with app.test_client().websocket_connect(+ _hmr_route_url(route, host, share_id),+ headers={"host": host, "origin": page_origin},+ subprotocols=["vite-hmr"],+ ) as websocket:+ websocket.receive_text()+ except Exception as exc:+ assert getattr(exc, "code", None) == 1011+ finally:+ set_show_runtime_manager_for_tests(None)++ assert len(manager.websocket_paths) == 1++def test_private_show_page_redirects_without_trailing_slash(monkeypatch, tmp_path):monkeypatch.setenv("AVIBE_HOME", str(tmp_path))_save_config(tmp_path)diff --git a/vibe/ui_server.py b/vibe/ui_server.pyindex b71fa4fc7..cf5553412 100644--- a/vibe/ui_server.py+++ b/vibe/ui_server.py@@ -3663,6 +3663,11 @@ async def show_runtime_hmr_websocket(websocket: WebSocket, session_id: str):async def public_show_runtime_hmr_websocket(websocket: WebSocket, share_id: str):from core.show_pages import ShowPageStore+ # Public visibility lets anyone view the page; it does not let a cross-site+ # page attach to its live socket.+ if not _show_runtime_hmr_origin_allowed(websocket):+ await websocket.close(code=1008)+ returnstore = ShowPageStore()try:page = store.get_by_share_id(share_id)@@ -4007,10 +4012,15 @@ def _show_runtime_websocket_resource_context(def _show_runtime_hmr_origin_allowed(websocket: Any) -> bool:- config = _load_remote_access_config()- if not _websocket_trusted_public_origin_local_request(websocket, config):- return True- return _websocket_origin_matches_effective_request(websocket)+ """Gate every Show HMR socket on the page's own exact origin.++ Browsers do not apply same-origin policy to WebSocket handshakes, so a+ cross-site page could otherwise open the socket (local requests carry no+ cookie gate, and a public share has no gate at all). Show pages are served+ same-origin (their iframes keep ``allow-same-origin``), so the real client+ always presents a matching Origin. This is the terminal socket's contract.+ """+ return _terminal_origin_allowed(websocket)def _authorization_websocket_close_code(state: str) -> int:@@ -4231,22 +4241,6 @@ def _terminal_origin_allowed(websocket: Any) -> bool:)-def _websocket_origin_matches_effective_request(websocket: Any) -> bool:- origin = _request_origin(websocket.headers.get("origin"))- if not origin:- return False- parsed_origin = urlparse(origin)- if _normalized_host(parsed_origin.netloc) != _websocket_normalized_host(websocket):- return False- origin_port = _origin_port(parsed_origin.netloc, parsed_origin.scheme)- websocket_scheme = _websocket_effective_scheme(websocket)- request_port = _origin_port(_websocket_effective_request_host(websocket), websocket_scheme)- return origin_port == request_port and _terminal_origin_scheme_matches_socket(- parsed_origin.scheme,- websocket_scheme,- )--def _origin_port(netloc: str | None, scheme: str | None) -> int | None:if not netloc:return None