Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(dev-server): K8 Punkte 6 und 7, --host Warnung und echter Test

purple13c4a1fc91d8
diff --git a/dev-server/__tests__/host-warning-k8.test.ts b/dev-server/__tests__/host-warning-k8.test.ts
new file mode 100644
index 00000000..fdee2a5f
--- /dev/null
+++ b/dev-server/__tests__/host-warning-k8.test.ts
@@ -0,0 +1,33 @@
+/**
+ * K8 nachbessert (review-create.md), Punkt 6: starting the dev server with
+ * `--host` exposes /shell-execute and /execute-code (both behind
+ * /local-api, see waechter-und-port.test.ts) to anyone on the LAN, not just
+ * this machine. Nothing told the person starting it. hostWarningMessage()
+ * (dev-server/index.ts) is the pure, testable half of the one-line warning
+ * devServerPlugin now prints once the server is actually listening.
+ *
+ * Run: npx vitest run dev-server/__tests__/host-warning-k8.test.ts
+ */
+import { describe, expect, it } from 'vitest'
+import { hostWarningMessage } from '../index'
+
+describe('hostWarningMessage (K8 Punkt 6)', () => {
+ it('--host inactive: no warning', () => {
+ expect(hostWarningMessage({ config: { server: {} }, resolvedUrls: null })).toBeNull()
+ })
+
+ it('--host active: names both endpoints and says LAN-reachable', () => {
+ const msg = hostWarningMessage({ config: { server: { host: true } }, resolvedUrls: null })
+ expect(msg).not.toBeNull()
+ expect(msg).toContain('/shell-execute')
+ expect(msg).toContain('/execute-code')
+ expect(msg).toMatch(/LAN/)
+ })
+
+ it('--host as a bound address string (not just boolean true) still warns', () => {
+ // Vite accepts --host <address> too; server.config.server.host is then
+ // the address string, not `true`. The check must not be `=== true`.
+ const msg = hostWarningMessage({ config: { server: { host: '0.0.0.0' } }, resolvedUrls: null })
+ expect(msg).not.toBeNull()
+ })
+})
diff --git a/dev-server/__tests__/lan-origin-k8.test.ts b/dev-server/__tests__/lan-origin-k8.test.ts
index af0e74bf..56bb6c5a 100644
--- a/dev-server/__tests__/lan-origin-k8.test.ts
+++ b/dev-server/__tests__/lan-origin-k8.test.ts
@@ -1,6 +1,6 @@
/**
* K8 (GH #134, eloieloie, `npm run dev --host`): a deliberately LAN-exposed
- * dev server rejected its own onboarding write requests — the page loaded
+ * dev server rejected its own onboarding write requests: the page loaded
* from the machine's LAN IP (e.g. `http://192.168.1.23:5273`), which is
* neither `tauri://localhost` nor the loopback regex, so the guard's Origin
* check 403'd it with "Invalid Origin (CSRF Protection)" exactly as it
@@ -10,7 +10,7 @@
* createLocalApiGuard: a per-request getter for the server's own resolved
* LAN origin(s), supplied by dev-server/index.ts only when `--host` is
* active. Nothing here weakens the DNS-rebinding protection the guard
- * already had (waechter-und-port.test.ts) — a value the CALLER supplies
+ * already had (waechter-und-port.test.ts): a value the CALLER supplies
* (Origin, Host) still never authorises anything; only a value the SERVER
* computed about its own bind address can.
*
@@ -18,13 +18,14 @@
*/
import { describe, expect, it } from 'vitest'
import { createLocalApiGuard } from '../guard'
+import { lanOrigins } from '../index'
import { anfrage } from './echte-anfrage'
const csrf = { 'x-locally-uncensored': 'true' }
const json = { 'Content-Type': 'application/json', ...csrf }
const DURCHGEREICHT = 599
-describe('K8 — --host: the server accepts its own LAN origin', () => {
+describe('K8 (--host): the server accepts its own LAN origin', () => {
it('without a getLanOrigins getter (plain `npm run dev`), a LAN origin is still rejected', async () => {
const waechter = createLocalApiGuard(5273)
const res = await anfrage(waechter, {
@@ -43,7 +44,7 @@ describe('K8 — --host: the server accepts its own LAN origin', () => {
expect(res.status).toBe(DURCHGEREICHT)
})
- it('does NOT widen to a different address on the same LAN — only the exact bound one', async () => {
+ it('does NOT widen to a different address on the same LAN, only the exact bound one', async () => {
const waechter = createLocalApiGuard(5273, () => ['http://192.168.1.23:5273'])
const res = await anfrage(waechter, {
method: 'POST', url: '/onboarding-write',
@@ -111,38 +112,20 @@ describe('K8 — --host: the server accepts its own LAN origin', () => {
})
})
-describe('K8 — dev-server/index.ts derives LAN origins only from --host, never from a request', () => {
- it('lanOrigins() logic: --host inactive → empty regardless of resolvedUrls', async () => {
- // Mirrors the closure built in dev-server/index.ts's configureServer —
- // kept here as an executable spec of that logic's contract, since the
- // real closure needs a live ViteDevServer to construct.
- const lanOrigins = (server: { config: { server: { host?: string | boolean } }; resolvedUrls: { network: string[] } | null }): string[] => {
- if (!server.config.server.host) return []
- return (server.resolvedUrls?.network ?? [])
- .map((u) => { try { return new URL(u).origin } catch { return null } })
- .filter((o): o is string => !!o)
- }
+describe('K8: the real lanOrigins() (dev-server/index.ts) derives LAN origins only from --host, never from a request', () => {
+ // K8 nachbessert (Review-Punkt 7): these three cases used to run against a
+ // copy of the closure retyped into this test file, which only proved the
+ // copy did what the copy did. They now call the real exported function.
+ it('--host inactive: empty, regardless of resolvedUrls', async () => {
expect(lanOrigins({ config: { server: {} }, resolvedUrls: { network: ['http://192.168.1.23:5273/'] } })).toEqual([])
})
- it('lanOrigins() logic: --host active + resolved → the exact origin(s), trailing slash stripped', async () => {
- const lanOrigins = (server: { config: { server: { host?: string | boolean } }; resolvedUrls: { network: string[] } | null }): string[] => {
- if (!server.config.server.host) return []
- return (server.resolvedUrls?.network ?? [])
- .map((u) => { try { return new URL(u).origin } catch { return null } })
- .filter((o): o is string => !!o)
- }
+ it('--host active and resolved: the exact origin(s), trailing slash stripped', async () => {
expect(lanOrigins({ config: { server: { host: true } }, resolvedUrls: { network: ['http://192.168.1.23:5273/'] } }))
.toEqual(['http://192.168.1.23:5273'])
})
- it('lanOrigins() logic: --host active but not yet resolved (pre-listen) → empty, not a crash', async () => {
- const lanOrigins = (server: { config: { server: { host?: string | boolean } }; resolvedUrls: { network: string[] } | null }): string[] => {
- if (!server.config.server.host) return []
- return (server.resolvedUrls?.network ?? [])
- .map((u) => { try { return new URL(u).origin } catch { return null } })
- .filter((o): o is string => !!o)
- }
+ it('--host active but not yet resolved (pre-listen): empty, not a crash', async () => {
expect(lanOrigins({ config: { server: { host: true } }, resolvedUrls: null })).toEqual([])
})
})
diff --git a/dev-server/index.ts b/dev-server/index.ts
index 4513dba0..1d70f198 100644
--- a/dev-server/index.ts
+++ b/dev-server/index.ts
@@ -18,6 +18,48 @@ import { registerSystemRoutes } from './system-routes'
import { registerWebSearchRoutes } from './web-search'
import { registerWhisperRoutes } from './whisper'
+/** Shape of the pieces of a ViteDevServer this needs, kept structural so
+ * tests can build a plain object instead of a real Vite instance. */
+export interface LanOriginServer {
+ config: { server: { host?: string | boolean } }
+ resolvedUrls: { network: string[] } | null
+}
+
+/**
+ * K8 (GH #134): the dev server's own LAN origin(s), derived ONLY from what
+ * Vite itself resolved it is bound to, never from anything a request claims.
+ * Only when `--host` was actually passed does Vite set
+ * `server.config.server.host`; a plain `npm run dev` binds loopback only and
+ * this returns []. `resolvedUrls` populates after listen(), so callers must
+ * read this lazily (per request, inside the guard), never compute it once
+ * and cache it, see the doc comment on getLanOrigins in guard.ts.
+ *
+ * A named export, not an inline closure, so dev-server/__tests__/
+ * lan-origin-k8.test.ts exercises this exact function instead of a copy
+ * retyped into the test file.
+ */
+export function lanOrigins(server: LanOriginServer): string[] {
+ if (!server.config.server.host) return []
+ return (server.resolvedUrls?.network ?? [])
+ .map((u) => { try { return new URL(u).origin } catch { return null } })
+ .filter((o): o is string => !!o)
+}
+
+/**
+ * K8 nachbessert, Punkt 6: the one-line warning printed once the server is
+ * actually bound with `--host`, so whoever started it sees that
+ * /shell-execute and /execute-code (both behind /local-api) are now
+ * reachable from anyone on the LAN, not only this machine. A pure function
+ * (returns the message, or null when `--host` is not active) so
+ * dev-server/__tests__ can assert on it without standing up a real Vite
+ * HTTP server.
+ */
+export function hostWarningMessage(server: LanOriginServer): string | null {
+ if (!server.config.server.host) return null
+ return '[lu-dev-server] --host is active: /shell-execute and /execute-code ' +
+ 'are reachable from anyone on this LAN, not just this machine.'
+}
+
export interface DevServerOptions {
/**
* Der Port, auf dem Vite bindet. Ein Parameter, damit der Server ein zweites
@@ -50,18 +92,19 @@ export function devServerPlugin({ port }: DevServerOptions): Plugin {
use: (path, handler) => { server.middlewares.use(path, handler) },
}
- // K8 (GH #134): only when `--host` was actually passed does Vite set
- // `server.config.server.host` — a plain `npm run dev` binds loopback
- // only and this returns []. `resolvedUrls` populates after listen(),
- // so this must be read lazily (per request, inside the guard), never
- // computed once here — see the doc comment on getLanOrigins in guard.ts.
- const lanOrigins = (): string[] => {
- if (!server.config.server.host) return []
- return (server.resolvedUrls?.network ?? [])
- .map((u) => { try { return new URL(u).origin } catch { return null } })
- .filter((o): o is string => !!o)
- }
- routes.use('/local-api', createLocalApiGuard(port, lanOrigins))
+ // K8 (GH #134): read lazily, per request, since resolvedUrls only
+ // populates after listen(). See lanOrigins' own doc comment above.
+ routes.use('/local-api', createLocalApiGuard(port, () => lanOrigins(server)))
+
+ // K8 nachbessert, Punkt 6: warn once, after Vite has actually bound to
+ // the network interface, that --host also exposes /shell-execute and
+ // /execute-code (both under /local-api) to anyone on the LAN, not just
+ // this machine. Read lazily off the real listener the same way the
+ // guard above does, so this fires only when --host truly took effect.
+ server.httpServer?.once('listening', () => {
+ const warning = hostWarningMessage(server)
+ if (warning) console.warn(warning)
+ })
autostartOllama()
autostartComfy(comfy)