From 8045370fba49d899137c23a843838e61f754883a Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Mon, 16 Nov 2026 09:17:27 +0100 Subject: [PATCH] ui: let the dev proxy pass llama-server's cross-origin check llama-server refuses browser requests whose Origin does not match its own host unless --cors-origins allows it, and the Vite proxy forwarded the page's Origin (localhost:5173, or a LAN or Tailscale address). Loading a model, saving tools and /v1 calls failed with 403 under `vite dev`. The proxy now calls the server with its own Host and no Origin, WebSocket upgrades included. Sec-Fetch-Site still passes through, so requests from other sites stay refused. Assisted-by: Claude (Opus 5.5 Medium) --- ui/apps/chat/vite.config.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/ui/apps/chat/vite.config.ts b/ui/apps/chat/vite.config.ts index 7f24f5963876..c97098ccbeca 100644 --- a/ui/apps/chat/vite.config.ts +++ b/ui/apps/chat/vite.config.ts @@ -13,7 +13,7 @@ import { playwright } from '@vitest/browser-playwright'; import { mdsvex } from 'mdsvex'; import { dirname, resolve } from 'path'; import { fileURLToPath } from 'url'; -import { defineConfig, loadEnv, searchForWorkspaceRoot } from 'vite'; +import { defineConfig, loadEnv, type ProxyOptions, searchForWorkspaceRoot } from 'vite'; const __dirname = dirname(fileURLToPath(import.meta.url)); // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -35,6 +35,18 @@ const outDir = process.env.LLAMA_UI_OUT_DIR ?? './dist'; export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), 'VITE_PUBLIC_'); const SERVER_ORIGIN = env.VITE_PUBLIC_SERVER_ORIGIN || 'http://localhost:9931'; + // llama-server takes cross-origin browser requests only from --cors-origins, so the proxy + // calls it as its own page: the Host of the server and no Origin. Sec-Fetch-Site still + // passes through, so the server keeps refusing requests from other sites. + const toServer = (ws = false): ProxyOptions => ({ + changeOrigin: true, + configure: (proxy) => { + proxy.on('proxyReq', (req) => req.removeHeader('origin')); + proxy.on('proxyReqWs', (req) => req.removeHeader('origin')); + }, + target: SERVER_ORIGIN, + ws + }); return { build: { @@ -90,12 +102,12 @@ export default defineConfig(({ mode }) => { 'Cross-Origin-Opener-Policy': 'same-origin' }, proxy: { - '/cors-proxy': SERVER_ORIGIN, - '/models': SERVER_ORIGIN, - '/props': SERVER_ORIGIN, - '/slots': SERVER_ORIGIN, - '/tools': SERVER_ORIGIN, - '/v1': SERVER_ORIGIN + '/cors-proxy': toServer(), + '/models': toServer(), + '/props': toServer(), + '/slots': toServer(), + '/tools': toServer(), + '/v1': toServer() } },