Skip to content

The agent that grows with you

fix(proxy): refuse every browser Origin on a wildcard bind

John Paul Solivab7ea7a7759b1
diff --git a/hermes_cli/proxy/server.py b/hermes_cli/proxy/server.py
index c9746aa38ef..21d103b0235 100644
--- a/hermes_cli/proxy/server.py
+++ b/hermes_cli/proxy/server.py
@@ -8,6 +8,7 @@ The one shim: after a *clean* upstream EOF, a ``text/event-stream`` response tha
from __future__ import annotations
import asyncio
+import ipaddress
import logging
import signal
from typing import Optional
@@ -67,7 +68,15 @@ def _authority_hostname(authority: str) -> str:
parts.port # noqa: B018 — raises ValueError on a malformed port
except ValueError:
return ""
- return (parts.hostname or "").lower()
+ # ``localhost.`` is the absolute form of ``localhost``; a rebound name stays foreign either way.
+ return (parts.hostname or "").lower().rstrip(".")
+
+
+def _is_wildcard(bound: str) -> bool:
+ try:
+ return bound in _WILDCARD_HOSTS or ipaddress.ip_address(bound).is_unspecified
+ except ValueError:
+ return False
def _local_request_error(host_header: str, origin: Optional[str], bound_host: str) -> Optional[str]:
@@ -76,12 +85,14 @@ def _local_request_error(host_header: str, origin: Optional[str], bound_host: st
to drive it: a loopback/specific-IP bind accepts only its own Host names (a DNS-rebound
hostname fails), and any Origin other than the proxy's own is a cross-site browser request.
Non-browser clients send no Origin and are unaffected. A wildcard bind is an explicit LAN
- opt-in, so only the Origin rule applies there."""
+ opt-in, so any Host name may reach it; it therefore has no origin of its own to match, and a
+ DNS-rebound page's Origin always equals its Host, so every Origin-bearing request is refused."""
hostname = _authority_hostname(host_header)
bound = bound_host.strip("[]").lower()
- if not hostname or (bound not in _WILDCARD_HOSTS and hostname not in _LOOPBACK_HOSTS | {bound}):
+ wildcard = _is_wildcard(bound)
+ if not hostname or (not wildcard and hostname not in _LOOPBACK_HOSTS | {bound}):
return "host_not_allowed"
- if origin is not None and origin.strip().lower() != f"http://{host_header.strip().lower()}":
+ if origin is not None and (wildcard or origin.strip().lower() != f"http://{host_header.strip().lower()}"):
return "origin_not_allowed"
return None
diff --git a/tests/hermes_cli/test_proxy.py b/tests/hermes_cli/test_proxy.py
index f0a558abfae..b55798e69cd 100644
--- a/tests/hermes_cli/test_proxy.py
+++ b/tests/hermes_cli/test_proxy.py
@@ -365,21 +365,29 @@ def test_server_strips_client_auth_header():
asyncio.run(run())
[email protected]("headers,allowed", [
- ({}, True), # plain local client
- ({"Origin": "http://{authority}"}, True), # the proxy's own origin
- ({"Host": "localhost:{port}"}, True), # loopback alias
- ({"Host": "rebound.example:{port}"}, False), # foreign Host
- ({"Origin": "https://site.example"}, False), # cross-site browser request
- ({"Origin": "null"}, False), # opaque browser origin
[email protected]("bound,headers,allowed", [
+ ("127.0.0.1", {}, True), # plain local client
+ ("127.0.0.1", {"Origin": "http://{authority}"}, True), # the proxy's own origin
+ ("127.0.0.1", {"Host": "localhost:{port}"}, True), # loopback alias
+ ("127.0.0.1", {"Host": "localhost.:{port}"}, True), # same alias, absolute form
+ ("127.0.0.1", {"Host": "rebound.example:{port}"}, False), # foreign Host
+ ("127.0.0.1", {"Origin": "https://site.example"}, False), # cross-site browser request
+ ("127.0.0.1", {"Origin": "null"}, False), # opaque browser origin
+ ("0.0.0.0", {"Host": "lan-name.example:{port}"}, True), # LAN API client, no Origin
+ ("0.0.0.0", {"Host": "rebound.example:{port}", # DNS-rebound page: its
+ "Origin": "http://rebound.example:{port}"}, False), # Origin equals its Host
+ ("::", {"Host": "rebound.example:{port}",
+ "Origin": "http://rebound.example:{port}"}, False),
])
-def test_loopback_proxy_serves_only_local_non_browser_requests(headers, allowed):
- """A loopback-bound proxy forwards with the operator's credential only for requests whose Host
- names the loopback listener and that carry no foreign Origin; refused ones never go upstream."""
+def test_loopback_proxy_serves_only_local_non_browser_requests(bound, headers, allowed):
+ """The proxy forwards with the operator's credential only for requests whose Host names its
+ listener (any name on a wildcard LAN bind) and that carry no browser Origin it cannot vouch for;
+ refused ones never go upstream. A wildcard bind has no single origin of its own, and a
+ DNS-rebound page's Origin always equals its Host, so no Origin is trusted there."""
async def run():
captured: Dict[str, Any] = {"requests": []}
upstream_runner, upstream_base = await _start_runner(_build_fake_upstream(captured))
- proxy_runner, proxy_base = await _start_runner(create_app(FakeAdapter(f"{upstream_base}/v1")))
+ proxy_runner, proxy_base = await _start_runner(create_app(FakeAdapter(f"{upstream_base}/v1"), bound_host=bound))
authority = proxy_base.removeprefix("http://")
sent = {k: v.format(authority=authority, port=authority.rsplit(":", 1)[1]) for k, v in headers.items()}
try:
diff --git a/website/docs/user-guide/features/subscription-proxy.md b/website/docs/user-guide/features/subscription-proxy.md
index 08d97357f14..535a3b4fe27 100644
--- a/website/docs/user-guide/features/subscription-proxy.md
+++ b/website/docs/user-guide/features/subscription-proxy.md
@@ -173,8 +173,9 @@ hermes proxy start --host 0.0.0.0 --port 8645
```
⚠ **Be aware:** anyone on your network can now use your Portal
-subscription. A wildcard bind skips the `Host` check (any name may reach it);
-the browser `Origin` check still applies. The proxy has no auth of its own — it accepts any bearer.
+subscription. A wildcard bind skips the `Host` check (any name may reach it),
+so it refuses every request that carries a browser `Origin`: browsers cannot use
+a wildcard-bound proxy at all. The proxy has no auth of its own — it accepts any bearer.
Use a firewall, VPN, or reverse proxy with proper auth if you expose
this beyond your trusted network.
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/subscription-proxy.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/subscription-proxy.md
index 8497e57f763..7c8f480707d 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/subscription-proxy.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/subscription-proxy.md
@@ -141,7 +141,7 @@ INFERENCE_TEXT_MODEL=Hermes-4-70B
hermes proxy start --host 0.0.0.0 --port 8645
```
-⚠ **注意:** 你网络中的任何人现在都可以使用你的 Portal 订阅。通配符绑定会跳过 `Host` 检查(任何主机名都可以访问);浏览器 `Origin` 检查仍然生效。代理本身没有认证机制——它接受任意 bearer。如果你将其暴露在可信网络之外,请使用防火墙、VPN 或带有适当认证的反向代理。
+⚠ **注意:** 你网络中的任何人现在都可以使用你的 Portal 订阅。通配符绑定会跳过 `Host` 检查(任何主机名都可以访问),因此它会拒绝所有带有浏览器 `Origin` 的请求:浏览器完全无法使用以通配符绑定的代理。代理本身没有认证机制——它接受任意 bearer。如果你将其暴露在可信网络之外,请使用防火墙、VPN 或带有适当认证的反向代理。
## 速率限制