Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: no Host check when an API key is set

Niko122173984cffe3b1
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 53974cba6..7a30387e1 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -506,22 +506,22 @@ print(r.choices[0].message.content)
clients then send it as their API key. Streamed answers carry `X-Accel-Buffering: no`, so nginx-style proxies pass
each token on at once. The web app's settings and MCP tools only answer Strata's own page: when you open it through
a proxy or tunnel whose address differs, add that address, e.g. `"trusted_origins": ["https://strata.example.com"]`.
- A tunnel or proxy that passes its own name on as `Host` needs that name allowed too (see Host names below; a
- `trusted_origins` entry already allows its name, and a quick tunnel's random name is covered by
- `"allowed_hosts": [".trycloudflare.com"]`).
+ With the key set, any `Host` name reaches the server (see Host names below).
- **From web apps in a browser (CORS).** Off by default. `"cors_origins": ["https://chat.example.com"]` lets pages of
those origins call `/v1/*` from the browser (Open WebUI's direct connections, browser extensions); `["*"]` lets any
page do it - only sensible with an API key. It never opens `/settings`, `/unload` or the MCP tools.
- **Host names (DNS rebinding).** A web page of another site can point its own name at `127.0.0.1` and then reach
- this server as if it were its own, so the server answers only requests whose `Host` is a name it knows:
+ this server as if it were its own, so without an API key the server answers only requests whose `Host` is a name
+ it knows (with a key the check is off: such a page cannot send the key, and tunnels and proxies that pass their
+ own name on keep working):
`localhost` (and `*.localhost`), any IP address (`127.0.0.1`, `[::1]`, `192.168.x.x`, ...), the address it
listens on and, when it listens beyond this PC (`0.0.0.0` or a LAN address), this PC's name (`mypc`, `mypc.local`)
and `host.docker.internal`; any port. Others get **403** naming the setting, and the server window prints one line
for each. Reach it under another name (a reverse proxy that keeps the name, a tunnel, a DNS name on your network,
another container's name for it)? Add the name: `"allowed_hosts": ["strata.example.com"]` in
`strata-<model>.json` or `STRATA_ALLOWED_HOSTS=strata.example.com` (comma-separated); `".example.com"` allows that
- name and every name below it, and `["*"]` turns the check off. The hosts of `trusted_origins` count as allowed.
- Requests without a `Host` header (HTTP/1.0 clients) pass.
+ name and every name below it, and `["*"]` turns the check off (so does setting `api_key`). The hosts of
+ `trusted_origins` count as allowed. Requests without a `Host` header (HTTP/1.0 clients) pass.
- **Web pages without an API key.** Without `api_key`, a `POST` to `/v1/*` that carries an `Origin` header (a
browser page sent it) is answered only for Strata's own page, pages on `localhost` or an allowed host name (any
port), the origins in `trusted_origins` or `cors_origins`, and browser extensions and desktop apps
diff --git a/serve/server.py b/serve/server.py
index 68f048d73..c491c0008 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -1973,18 +1973,22 @@ def make_handler(svc: Service):
pass
def parse_request(self):
- """Every request (any method) first passes the Host check: DNS rebinding protection (host_allowed)."""
+ """Without an API key, every request (any method) first passes the Host check: DNS rebinding protection
+ (host_allowed). With a key a rebinding page cannot authenticate, so the check is skipped: tunnels and
+ proxies that pass their own name on keep working."""
if not super().parse_request():
return False
host = self.headers.get("Host")
- if host_allowed(host, svc.host_names, "*" in svc.allowed_hosts):
+ if svc.api_key or host_allowed(host, svc.host_names, "*" in svc.allowed_hosts):
return True
print(f"[strata] refused a request for Host {host!r} from {self.client_address[0]}: not a name this server "
- f"answers to (add it to \"allowed_hosts\" in the config or STRATA_ALLOWED_HOSTS)", flush=True)
+ f"answers to (add it to \"allowed_hosts\" in the config or STRATA_ALLOWED_HOSTS, or set an API key)",
+ flush=True)
self._json(403, {"error": {"type": "forbidden", "message":
f"Host {host!r} is not allowed (DNS rebinding protection). Reaching Strata under this "
f"name on purpose? Add it to \"allowed_hosts\" in the config (strata-<model>.json) or to "
- f"the STRATA_ALLOWED_HOSTS environment variable"}})
+ f"the STRATA_ALLOWED_HOSTS environment variable, or set an API key (\"api_key\"), which "
+ f"turns this check off"}})
return False
def _foreign_page(self) -> bool:
diff --git a/serve/test_security.py b/serve/test_security.py
index f79b6b9e1..cebd756d1 100644
--- a/serve/test_security.py
+++ b/serve/test_security.py
@@ -170,6 +170,24 @@ class OverHttp(unittest.TestCase):
self.svc.allowed_hosts = ["*"]
self.assertEqual(self.req("GET", "/status", host="evil.example.com")[0], 200)
+ def test_refusal_names_both_ways_out(self):
+ self.start()
+ message = self.req("GET", "/status", host="evil.example.com")[1]["error"]["message"]
+ self.assertIn("allowed_hosts", message)
+ self.assertIn("api_key", message)
+
+ def test_with_a_key_the_host_check_is_off(self):
+ # a rebinding page cannot authenticate; tunnels and proxies that pass their own name on keep working
+ self.start(api_key="s3cret")
+ tunnel = "random-words.trycloudflare.com"
+ self.assertEqual(self.req("GET", "/status", host=tunnel)[0], 401)
+ self.assertEqual(self.req("GET", "/status", headers={"Authorization": "Bearer s3cret"}, host=tunnel)[0], 200)
+ self.assertEqual(self.req("GET", "/health", host=tunnel)[0], 200)
+ code, _, log = self.req("POST", "/v1/chat/completions", self.chat_body(),
+ {"Content-Type": "application/json", "Authorization": "Bearer s3cret"}, host=tunnel)
+ self.assertEqual(code, 200)
+ self.assertNotIn("refused", log)
+
def test_a_trusted_origin_s_host_is_allowed(self):
self.start(trusted_origins=["https://strata.example.com"])
self.assertEqual(self.req("GET", "/status", host="strata.example.com")[0], 200)