Commit debcc4d99 for llama.cpp
commit debcc4d99206ddf15a21d9be636c39f2b0c2e5a7
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date: Thu Dec 3 14:36:16 2026 +0100
ui: answer a list of CORS origins with the origin that asked (#33602)
* server: answer a list of CORS origins with the one that asked
With more than one origin in --cors-origins, Access-Control-Allow-Origin carried the whole
comma-separated list, which browsers reject. The request's own origin is now sent back
when it is in the list, with Vary: Origin, and none otherwise.
Assisted-by: Claude (Opus 5.5 Medium)
* server: keep "*" in --cors-origins working without credentials
The wildcard fell through to the origin list, so with --no-cors-credentials it matched
no origin and Access-Control-Allow-Origin was dropped. "*" is now handled before the list.
test_cors_options covers a two-origin list and "*" without credentials.
Assisted-by: Claude (Opus 5.5 Medium)
diff --git a/tools/server/server-http.cpp b/tools/server/server-http.cpp
index fc81f4713..41bf26a4b 100644
--- a/tools/server/server-http.cpp
+++ b/tools/server/server-http.cpp
@@ -437,13 +437,22 @@ bool server_http_context::init_listener(const common_params & params) {
);
return httplib::Server::HandlerResponse::Handled;
}
- if (params.cors_credentials && params.cors_origins == "*") {
- // special case: echo back the Origin header to allow any origin to access the server with credentials
- res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
+ if (params.cors_origins == "*") {
+ // any origin: with credentials the header can't be "*", so the request's own origin is echoed back
+ res.set_header("Access-Control-Allow-Origin", params.cors_credentials ? req.get_header_value("Origin") : "*");
} else if (params.cors_origins == "localhost") {
// special case: same-origin only, no CORS header needed
} else {
- res.set_header("Access-Control-Allow-Origin", params.cors_origins);
+ // a list of origins: the header takes one origin, never the list, so the request's
+ // own origin is echoed back when it is one of them, and left out otherwise
+ const std::string origin = req.get_header_value("Origin");
+ for (const auto & allowed : string_split<std::string>(params.cors_origins, ',')) {
+ if (!origin.empty() && string_strip(allowed) == origin) {
+ res.set_header("Access-Control-Allow-Origin", origin);
+ res.set_header("Vary", "Origin");
+ break;
+ }
+ }
}
// If this is OPTIONS request, skip validation because browsers don't include Authorization header
if (req.method == "OPTIONS") {
diff --git a/tools/server/tests/unit/test_security.py b/tools/server/tests/unit/test_security.py
index 242a8957a..a168dd7fe 100644
--- a/tools/server/tests/unit/test_security.py
+++ b/tools/server/tests/unit/test_security.py
@@ -87,15 +87,21 @@ def test_openai_library_correct_api_key():
assert len(res.choices) == 1
-@pytest.mark.parametrize("origin,cors_header,cors_header_value", [
- ("localhost", "Access-Control-Allow-Origin", "localhost"),
- ("web.mydomain.fr", "Access-Control-Allow-Origin", "web.mydomain.fr"),
- ("origin", "Access-Control-Allow-Credentials", "true"),
- ("web.mydomain.fr", "Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS"),
- ("web.mydomain.fr", "Access-Control-Allow-Headers", "*"),
+@pytest.mark.parametrize("cors_origins,cors_credentials,origin,cors_header,cors_header_value", [
+ (None, None, "localhost", "Access-Control-Allow-Origin", "localhost"),
+ (None, None, "web.mydomain.fr", "Access-Control-Allow-Origin", "web.mydomain.fr"),
+ (None, None, "origin", "Access-Control-Allow-Credentials", "true"),
+ (None, None, "web.mydomain.fr", "Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS"),
+ (None, None, "web.mydomain.fr", "Access-Control-Allow-Headers", "*"),
+ ("*", False, "web.mydomain.fr", "Access-Control-Allow-Origin", "*"),
+ ("https://a.example, https://b.example", None, "https://a.example", "Access-Control-Allow-Origin", "https://a.example"),
+ ("https://a.example, https://b.example", None, "https://b.example", "Access-Control-Allow-Origin", "https://b.example"),
+ ("https://a.example, https://b.example", None, "https://c.example", "Access-Control-Allow-Origin", None),
])
-def test_cors_options(origin: str, cors_header: str, cors_header_value: str):
+def test_cors_options(cors_origins: str | None, cors_credentials: bool | None, origin: str, cors_header: str, cors_header_value: str | None):
global server
+ server.cors_origins = cors_origins
+ server.cors_credentials = cors_credentials
server.start()
res = server.make_request("OPTIONS", "/completions", headers={
"Origin": origin,
@@ -103,8 +109,10 @@ def test_cors_options(origin: str, cors_header: str, cors_header_value: str):
"Access-Control-Request-Headers": "Authorization",
})
assert res.status_code == 200
- assert cors_header in res.headers
- assert res.headers[cors_header] == cors_header_value
+ if cors_header_value is None:
+ assert cors_header not in res.headers
+ else:
+ assert res.headers[cors_header] == cors_header_value
@pytest.mark.parametrize("origin", [
diff --git a/tools/server/tests/utils.py b/tools/server/tests/utils.py
index ad503db5b..a67aa8117 100644
--- a/tools/server/tests/utils.py
+++ b/tools/server/tests/utils.py
@@ -141,6 +141,7 @@ class ServerProcess:
mcp_servers_config: str | None = None
mcp_servers_json: str | None = None
cors_origins: str | None = None
+ cors_credentials: bool | None = None
llama_cache: str | None = None
share_console: bool = False # Windows: console control events only reach a server on the console of the tests
@@ -209,6 +210,8 @@ class ServerProcess:
server_args.extend(["--models-preset", self.models_preset])
if self.cors_origins:
server_args.extend(["--cors-origins", self.cors_origins])
+ if self.cors_credentials is not None:
+ server_args.append("--cors-credentials" if self.cors_credentials else "--no-cors-credentials")
if self.n_batch:
server_args.extend(["--batch-size", self.n_batch])
if self.n_ubatch: