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(proxy): detect SOCKS by URL scheme, not substring match

cyhe3ee3231b78b
diff --git a/tests/test_im_proxy_unification.py b/tests/test_im_proxy_unification.py
index c04886c35..65aa7de5b 100644
--- a/tests/test_im_proxy_unification.py
+++ b/tests/test_im_proxy_unification.py
@@ -28,7 +28,7 @@ from config.v2_config import (
WeChatConfig,
)
from vibe import proxy as proxy_module
-from vibe.proxy import redact_proxy_url, resolve_proxy
+from vibe.proxy import is_socks_proxy, redact_proxy_url, resolve_proxy
CONFIG_FACTORIES = [
@@ -157,6 +157,45 @@ def test_redact_proxy_url_handles_garbage() -> None:
assert redact_proxy_url("not a url") == "<configured>"
+# ---------------------------------------------------------------------------
+# is_socks_proxy() detects by scheme, not substring (regression for round-4 P2)
+# ---------------------------------------------------------------------------
+
+
+ "url",
+ [
+ "socks5://127.0.0.1:1080",
+ "socks5h://127.0.0.1:1080",
+ "socks4://127.0.0.1:1080",
+ "socks4a://127.0.0.1:1080",
+ "SOCKS5://127.0.0.1:1080", # case-insensitive
+ " socks5://127.0.0.1:1080 ", # leading/trailing whitespace tolerated
+ "socks5://user:pass@host:1080",
+ ],
+)
+def test_is_socks_proxy_true_for_socks_schemes(url) -> None:
+ assert is_socks_proxy(url) is True
+
+
+ "url",
+ [
+ # Regression: hostname containing "socks" must not be classified as SOCKS.
+ "http://socks-gateway.corp:8080",
+ "https://socks-gateway.corp:8080",
+ "http://user:[email protected]:8080", # credentials containing "socks"
+ "http://127.0.0.1:8080",
+ "https://proxy.local:443",
+ "",
+ None,
+ "not a url",
+ ],
+)
+def test_is_socks_proxy_false_for_non_socks(url) -> None:
+ assert is_socks_proxy(url) is False
+
+
# ---------------------------------------------------------------------------
# Discord auth_test SOCKS path: non-2xx must raise (regression for P1 review)
# ---------------------------------------------------------------------------
@@ -393,3 +432,37 @@ def test_lark_tenant_token_uses_urllib_proxy_for_http() -> None:
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"])
+
+
+def test_lark_tenant_token_does_not_misroute_http_with_socks_hostname() -> None:
+ """Regression for round-4 P2: an HTTP proxy whose hostname contains
+ 'socks' (e.g. http://socks-gateway.corp:8080) must take the urllib path,
+ not the aiohttp_socks path. The previous substring-based check would
+ misclassify this as a SOCKS proxy.
+ """
+ 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-via-http"}')
+ )
+ )
+ fake_resp_cm.__exit__ = MagicMock(return_value=None)
+
+ fake_opener = MagicMock()
+ fake_opener.open = MagicMock(return_value=fake_resp_cm)
+
+ def fail_aiohttp(*args, **kwargs):
+ raise AssertionError("aiohttp must not be used for HTTP proxies, even when hostname contains 'socks'")
+
+ with patch("urllib.request.build_opener", return_value=fake_opener), patch.object(
+ vibe_api, "_lark_tenant_token_via_aiohttp", side_effect=fail_aiohttp
+ ):
+ token = vibe_api._lark_tenant_token(
+ "cli_x", "secret", proxy_url="http://socks-gateway.corp:8080"
+ )
+
+ assert token == "tok-via-http"
diff --git a/vibe/api.py b/vibe/api.py
index 437bcd478..b49875949 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -737,7 +737,7 @@ def opencode_options(cwd: str) -> dict:
def _discord_api_get(bot_token: str, path: str, proxy_url: str | None = None) -> dict:
import urllib.request
- from vibe.proxy import resolve_proxy
+ from vibe.proxy import is_socks_proxy, resolve_proxy
if not bot_token:
raise ValueError("bot_token is required")
@@ -745,7 +745,7 @@ def _discord_api_get(bot_token: str, path: str, proxy_url: str | None = None) ->
headers = {"Authorization": f"Bot {bot_token}", "User-Agent": "vibe-remote"}
proxy = resolve_proxy(proxy_url)
- if proxy and "socks" in proxy.lower():
+ if proxy and is_socks_proxy(proxy):
# urllib has no native SOCKS support; route via aiohttp + aiohttp_socks.
return asyncio.run(_discord_api_get_via_aiohttp(url, headers, proxy))
@@ -1588,11 +1588,13 @@ def _lark_tenant_token(
"""
import urllib.request
+ from vibe.proxy import is_socks_proxy
+
url = f"{_lark_api_base(domain)}/open-apis/auth/v3/tenant_access_token/internal"
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():
+ if proxy_url and is_socks_proxy(proxy_url):
result = asyncio.run(_lark_tenant_token_via_aiohttp(url, body, headers, proxy_url))
else:
if proxy_url:
diff --git a/vibe/proxy.py b/vibe/proxy.py
index fe5b385a8..9b18a8bc5 100644
--- a/vibe/proxy.py
+++ b/vibe/proxy.py
@@ -13,6 +13,24 @@ from urllib.parse import urlsplit
logger = logging.getLogger(__name__)
+def is_socks_proxy(proxy_url: Optional[str]) -> bool:
+ """Return True when ``proxy_url``'s URL scheme is a SOCKS variant.
+
+ Detection is by URL scheme (``socks4``/``socks4a``/``socks5``/``socks5h``),
+ not by substring match. A naive ``"socks" in url`` check would misclassify
+ HTTP proxies whose hostname or credentials happen to contain ``socks``
+ (for example ``http://socks-gateway.corp:8080``) and route them through
+ ``aiohttp_socks`` instead of ``urllib.ProxyHandler``.
+ """
+ if not proxy_url:
+ return False
+ try:
+ scheme = urlsplit(proxy_url.strip()).scheme.lower()
+ except Exception:
+ return False
+ return scheme.startswith("socks")
+
+
def redact_proxy_url(proxy_url: Optional[str]) -> str:
"""Return a proxy URL safe for logs (userinfo stripped).
@@ -62,7 +80,7 @@ def get_system_socks_proxy() -> Optional[str]:
"""
for var in ("ALL_PROXY", "all_proxy", "HTTPS_PROXY", "https_proxy"):
val = os.environ.get(var, "")
- if val and "socks" in val.lower():
+ if val and is_socks_proxy(val):
return val
# macOS: query system SOCKS proxy