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(lark): route auth_test through configured proxy

cyh54f6c518d2a1
diff --git a/tests/test_im_proxy_unification.py b/tests/test_im_proxy_unification.py
index 53d0f1b00..c04886c35 100644
--- a/tests/test_im_proxy_unification.py
+++ b/tests/test_im_proxy_unification.py
@@ -281,3 +281,115 @@ def test_telegram_auth_test_prefers_explicit_over_system() -> None:
vibe_api.telegram_auth_test("123456:abc", proxy_url="socks5://explicit:1080")
assert captured["proxy_url"] == "socks5://explicit:1080"
+
+
+# ---------------------------------------------------------------------------
+# lark_auth_test must route through the configured proxy (round-3 P1 review)
+# ---------------------------------------------------------------------------
+
+
+def test_lark_auth_test_threads_proxy_into_token_helper() -> None:
+ """lark_auth_test must resolve proxy and forward it to _lark_tenant_token.
+
+ Before the fix, lark_auth_test called the internal helper with no proxy
+ info, so users behind a corporate/SOCKS proxy got a false auth failure
+ even after providing proxy_url in the wizard.
+ """
+ from vibe import api as vibe_api
+
+ captured: dict = {}
+
+ def fake_token(app_id, app_secret, domain="feishu", proxy_url=None):
+ captured["app_id"] = app_id
+ captured["proxy_url"] = proxy_url
+ return "tok-123"
+
+ with patch.object(vibe_api, "_lark_tenant_token", new=fake_token), patch.object(
+ proxy_module, "get_system_socks_proxy", return_value=None
+ ):
+ result = vibe_api.lark_auth_test("cli_x", "secret", proxy_url="socks5://explicit:1080")
+
+ assert result == {"ok": True}
+ assert captured["proxy_url"] == "socks5://explicit:1080"
+
+
+def test_lark_auth_test_falls_back_to_system_proxy() -> None:
+ """Empty proxy_url should pick up the system SOCKS proxy."""
+ from vibe import api as vibe_api
+
+ captured: dict = {}
+
+ def fake_token(app_id, app_secret, domain="feishu", proxy_url=None):
+ captured["proxy_url"] = proxy_url
+ return "tok-123"
+
+ with patch.object(vibe_api, "_lark_tenant_token", new=fake_token), patch.object(
+ proxy_module, "get_system_socks_proxy", return_value="socks5://system:1080"
+ ):
+ vibe_api.lark_auth_test("cli_x", "secret")
+
+ assert captured["proxy_url"] == "socks5://system:1080"
+
+
+def test_lark_tenant_token_uses_aiohttp_for_socks() -> None:
+ """SOCKS proxy_url branches into the aiohttp helper, not urllib."""
+ import asyncio
+
+ from unittest.mock import AsyncMock
+
+ from vibe import api as vibe_api
+
+ aiohttp_called = {"hit": False}
+
+ async def fake_aiohttp(url, body, headers, proxy):
+ aiohttp_called["hit"] = True
+ aiohttp_called["proxy"] = proxy
+ return {"code": 0, "tenant_access_token": "tok-from-socks"}
+
+ def fail_urlopen(*args, **kwargs):
+ raise AssertionError("urllib should not be used for SOCKS proxies")
+
+ with patch.object(vibe_api, "_lark_tenant_token_via_aiohttp", new=fake_aiohttp), patch(
+ "urllib.request.build_opener", side_effect=fail_urlopen
+ ):
+ token = vibe_api._lark_tenant_token(
+ "cli_x", "secret", proxy_url="socks5://127.0.0.1:1080"
+ )
+
+ assert token == "tok-from-socks"
+ assert aiohttp_called["hit"] is True
+ assert aiohttp_called["proxy"] == "socks5://127.0.0.1:1080"
+ # touch asyncio so import is used in case lints flag it
+ assert asyncio.iscoroutinefunction(fake_aiohttp)
+ AsyncMock # unused-import guard
+
+
+def test_lark_tenant_token_uses_urllib_proxy_for_http() -> None:
+ """HTTP proxy_url uses urllib.ProxyHandler with the proxy applied."""
+ from unittest.mock import MagicMock
+
+ from vibe import api as vibe_api
+
+ fake_resp_cm = MagicMock()
+ fake_resp_cm.__enter__ = MagicMock(
+ return_value=MagicMock(read=MagicMock(return_value=b'{"code":0,"tenant_access_token":"tok-http"}'))
+ )
+ fake_resp_cm.__exit__ = MagicMock(return_value=None)
+
+ fake_opener = MagicMock()
+ fake_opener.open = MagicMock(return_value=fake_resp_cm)
+
+ captured: dict = {}
+
+ def capture_build_opener(*handlers):
+ captured["handlers"] = handlers
+ return fake_opener
+
+ with patch("urllib.request.build_opener", side_effect=capture_build_opener):
+ token = vibe_api._lark_tenant_token(
+ "cli_x", "secret", proxy_url="http://proxy.local:8080"
+ )
+
+ assert token == "tok-http"
+ # ProxyHandler should be in the chain when proxy_url is set
+ assert any("ProxyHandler" in type(h).__name__ for h in captured["handlers"])
diff --git a/vibe/api.py b/vibe/api.py
index 30cc4038e..437bcd478 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -1573,20 +1573,55 @@ def _lark_api_base(domain: str = "feishu") -> str:
return "https://open.feishu.cn"
-def _lark_tenant_token(app_id: str, app_secret: str, domain: str = "feishu") -> Optional[str]:
- """Get Lark tenant access token (internal helper, not exposed to frontend)."""
+def _lark_tenant_token(
+ app_id: str,
+ app_secret: str,
+ domain: str = "feishu",
+ proxy_url: str | None = None,
+) -> Optional[str]:
+ """Get Lark tenant access token (internal helper, not exposed to frontend).
+
+ ``proxy_url`` is honored when set: SOCKS schemes route through
+ ``aiohttp_socks``, HTTP schemes use ``urllib.ProxyHandler``. The runtime
+ Feishu/Lark adapter still bypasses this because ``lark-oapi`` has no
+ proxy hook — that gap is surfaced by the adapter, not here.
+ """
import urllib.request
url = f"{_lark_api_base(domain)}/open-apis/auth/v3/tenant_access_token/internal"
- data = json.dumps({"app_id": app_id, "app_secret": app_secret}).encode()
- req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
- with urllib.request.urlopen(req, timeout=10) as resp:
- result = json.loads(resp.read().decode())
- if result.get("code") == 0:
- return result.get("tenant_access_token")
+ body = json.dumps({"app_id": app_id, "app_secret": app_secret}).encode()
+ headers = {"Content-Type": "application/json"}
+
+ if proxy_url and "socks" in proxy_url.lower():
+ result = asyncio.run(_lark_tenant_token_via_aiohttp(url, body, headers, proxy_url))
+ else:
+ if proxy_url:
+ opener = urllib.request.build_opener(
+ urllib.request.ProxyHandler({"http": proxy_url, "https": proxy_url})
+ )
+ else:
+ opener = urllib.request.build_opener()
+ req = urllib.request.Request(url, data=body, headers=headers)
+ with opener.open(req, timeout=10) as resp:
+ result = json.loads(resp.read().decode())
+
+ if result.get("code") == 0:
+ return result.get("tenant_access_token")
return None
+async def _lark_tenant_token_via_aiohttp(url: str, body: bytes, headers: dict, proxy: str) -> dict:
+ import aiohttp
+ from aiohttp_socks import ProxyConnector
+
+ connector = ProxyConnector.from_url(proxy, rdns=True)
+ timeout = aiohttp.ClientTimeout(total=10)
+ async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
+ async with session.post(url, data=body, headers=headers) as resp:
+ resp.raise_for_status()
+ return await resp.json()
+
+
def lark_auth_test(
app_id: str,
app_secret: str,
@@ -1595,20 +1630,15 @@ def lark_auth_test(
) -> dict:
"""Test Lark/Feishu app credentials. Only returns ok/error, never exposes token.
- ``proxy_url`` is accepted for API parity, but ``lark-oapi`` provides no
- proxy hook today, so the auth test (and the runtime SDK) bypass it. We
- log once when a value is provided so users understand the gap.
+ ``proxy_url`` is honored for the auth call itself; the runtime SDK
+ (``lark-oapi``) has no proxy hook and bypasses it — that limitation is
+ surfaced by ``modules/im/feishu.py`` once at adapter init.
"""
- if proxy_url and proxy_url.strip():
- from vibe.proxy import redact_proxy_url
+ from vibe.proxy import resolve_proxy
- logger.warning(
- "Feishu/Lark auth_test received proxy_url=%s but lark-oapi has no "
- "proxy hook; the request will bypass the configured proxy.",
- redact_proxy_url(proxy_url),
- )
+ proxy = resolve_proxy(proxy_url)
try:
- token = _lark_tenant_token(app_id, app_secret, domain)
+ token = _lark_tenant_token(app_id, app_secret, domain, proxy_url=proxy)
if not token:
return {"ok": False, "error": "Invalid credentials"}
return {"ok": True}