diff --git a/packages/studio/vite.config.hosts.test.ts b/packages/studio/vite.config.hosts.test.ts new file mode 100644 index 0000000000..86cbefe748 --- /dev/null +++ b/packages/studio/vite.config.hosts.test.ts @@ -0,0 +1,147 @@ +import { EventEmitter } from "node:events"; +import type { IncomingMessage, ServerResponse } from "node:http"; +import type { Plugin, ViteDevServer } from "vite"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import config from "./vite.config"; + +const ENV = "HYPERFRAMES_PREVIEW_ALLOWED_HOSTS"; + +type Middleware = (req: IncomingMessage, res: ServerResponse, next: () => void) => unknown; + +interface Reply { + status: number | undefined; + body: string; +} + +const httpServers: EventEmitter[] = []; + +afterEach(() => { + // Closes the plugin's project watcher. + for (const httpServer of httpServers.splice(0)) httpServer.emit("close"); + vi.unstubAllEnvs(); +}); + +function devApiPlugin(): Plugin { + const plugin = (config.plugins ?? []) + .flat(Infinity) + .find( + (p): p is Plugin => + typeof p === "object" && p !== null && "name" in p && p.name === "studio-dev-api", + ); + if (!plugin) throw new Error("studio-dev-api plugin not found"); + return plugin; +} + +// Stands in for the studio-server module so an allowed /api/ request has something to reach. +function fakeStudioServerModule(): Record { + return { + createStudioApi: () => ({ fetch: async () => Response.json({ ok: true }) }), + previewAllowedHostsFromEnv: () => [], + identifyFileWrite: () => null, + fileContentVersion: () => "v", + affectsPreview: () => true, + openProjectHistory: async () => null, + DELETED_VERSION: "deleted", + }; +} + +function startPlugin(): Middleware[] { + const middlewares: Middleware[] = []; + const httpServer = new EventEmitter(); + httpServers.push(httpServer); + const server = { + middlewares: { use: (fn: Middleware) => void middlewares.push(fn) }, + config: { server: { host: "127.0.0.1" } }, + httpServer, + ws: { send: () => {} }, + ssrLoadModule: async () => fakeStudioServerModule(), + } as unknown as ViteDevServer; + const configureServer = devApiPlugin().configureServer as (s: ViteDevServer) => void; + configureServer(server); + return middlewares; +} + +// Runs the request down the plugin's middlewares the way connect would; `undefined` status means every one passed it on. +function request(middlewares: Middleware[], url: string, host: string): Promise { + return new Promise((resolve) => { + const reply: Reply = { status: undefined, body: "" }; + const res = Object.assign(new EventEmitter(), { + headersSent: false, + writeHead(status: number) { + reply.status = status; + res.headersSent = true; + return res; + }, + write(chunk: Uint8Array | string) { + reply.body += typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"); + return true; + }, + end(chunk?: string) { + if (chunk) reply.body += chunk; + resolve(reply); + }, + }); + const req = { url, method: "GET", headers: { host } } as IncomingMessage; + const run = (index: number): void => { + const middleware = middlewares[index]; + if (!middleware) return resolve(reply); + void middleware(req, res as unknown as ServerResponse, () => run(index + 1)); + }; + run(0); + }); +} + +describe("studio dev server host check", () => { + it("refuses a foreign Host on the plugin's API and config routes", async () => { + vi.stubEnv("HYPERFRAMES_PREVIEW_PROJECT_DIR", "/tmp/project"); + vi.stubEnv("HYPERFRAMES_PREVIEW_PROJECT_NAME", "project"); + const middlewares = startPlugin(); + for (const url of ["/api/projects", "/__hyperframes_config", "/api/runtime.js"]) { + const reply = await request(middlewares, url, "other.example:5190"); + expect(reply.status, url).toBe(403); + expect(JSON.parse(reply.body).error, url).toContain(`"other.example:5190"`); + expect(JSON.parse(reply.body).error, url).toContain(ENV); + } + }); + + it("serves loopback and IP-literal hosts", async () => { + vi.stubEnv("HYPERFRAMES_PREVIEW_PROJECT_DIR", "/tmp/project"); + vi.stubEnv("HYPERFRAMES_PREVIEW_PROJECT_NAME", "project"); + const middlewares = startPlugin(); + for (const host of ["localhost:5190", "127.0.0.1:5190", "[::1]:5190", "192.168.1.20:5190"]) { + expect((await request(middlewares, "/api/projects", host)).status, host).toBe(200); + expect((await request(middlewares, "/__hyperframes_config", host)).status, host).toBe(200); + } + expect((await request(middlewares, "/api/projects", "[fe80::1]:5190")).status).toBe(200); + }); + + it(`serves a host listed in ${ENV}`, async () => { + vi.stubEnv(ENV, "studio.tunnel.example, other.tunnel.example"); + const middlewares = startPlugin(); + expect((await request(middlewares, "/api/projects", "studio.tunnel.example")).status).toBe(200); + expect((await request(middlewares, "/api/projects", "evil.tunnel.example")).status).toBe(403); + }); + + it("serves the *.localhost and leading-dot hosts Vite serves the page on", async () => { + vi.stubEnv(ENV, ".tunnel.example"); + const middlewares = startPlugin(); + for (const host of ["studio.localhost:5190", "tunnel.example", "a.b.tunnel.example:443"]) { + expect((await request(middlewares, "/api/projects", host)).status, host).toBe(200); + } + for (const host of ["localhost.evil.example", "eviltunnel.example", "tunnel.example.evil"]) { + expect((await request(middlewares, "/api/projects", host)).status, host).toBe(403); + } + }); + + it("leaves routes the plugin does not own to Vite's own host check", async () => { + const middlewares = startPlugin(); + expect((await request(middlewares, "/", "other.example:5190")).status).toBeUndefined(); + }); + + it(`adds ${ENV} hosts to server.allowedHosts so Vite serves the page on them`, async () => { + vi.stubEnv(ENV, "studio.tunnel.example"); + vi.resetModules(); + const { default: fresh } = await import("./vite.config"); + expect(fresh.server?.allowedHosts).toEqual(["studio.tunnel.example"]); + }); +}); diff --git a/packages/studio/vite.config.ts b/packages/studio/vite.config.ts index f53dd2bc4d..f4edc425f2 100644 --- a/packages/studio/vite.config.ts +++ b/packages/studio/vite.config.ts @@ -9,6 +9,7 @@ import { lstatSync, realpathSync, } from "node:fs"; +import { isIP } from "node:net"; import { join, resolve } from "node:path"; import { readNodeRequestBody } from "./vite.request-body.js"; import { bindNodeRequestSignal } from "./vite.request-signal.js"; @@ -18,6 +19,11 @@ import { previewConfigPayload } from "./vite.preview-config"; import { loadStudioServerDevModule } from "./vite.studio-server-module"; import type { openProjectHistory } from "@hyperframes/studio-server"; import { previewChangeOwner } from "./vite.preview-watch"; +import { + hostHeaderAllowed, + PREVIEW_ALLOWED_HOSTS_ENV, + previewAllowedHostsFromEnv, +} from "../studio-server/src/helpers/requestIdentity"; async function loadRuntimeSourceForDev( server: import("vite").ViteDevServer, @@ -91,6 +97,17 @@ async function bridgeHonoResponse( res.end(); } +// Vite's own rule (IP literals, *.localhost, leading-dot entries), so the API serves every Host Vite serves the page on. +function viteAllowsHost(host: string, allowedHosts: readonly string[]): boolean { + const bracketed = /^\[([^\]]+)\]/.exec(host); + if (bracketed) return isIP(bracketed[1]!) === 6; + const name = host.split(":")[0]!.toLowerCase(); + if (isIP(name) === 4 || name === "localhost" || name.endsWith(".localhost")) return true; + return allowedHosts.some( + (allowed) => allowed.startsWith(".") && (name === allowed.slice(1) || name.endsWith(allowed)), + ); +} + // ── Vite plugin ────────────────────────────────────────────────────────────── function devProjectApi(): Plugin { @@ -197,6 +214,28 @@ function devProjectApi(): Plugin { return _api; }; + // Vite runs its own Host check after the middlewares added here, so the + // plugin's routes check Host first: the CLI preview server's rule plus Vite's. + const allowedHosts = [...previewAllowedHostsFromEnv()]; + if (typeof server.config.server.host === "string") + allowedHosts.push(server.config.server.host); + server.middlewares.use((req, res, next) => { + const url = req.url ?? ""; + if (url !== "/__hyperframes_config" && !url.startsWith("/api/")) return next(); + const host = req.headers.host ?? ""; + if (hostHeaderAllowed(host, allowedHosts) || viteAllowsHost(host, allowedHosts)) + return next(); + res.writeHead(403, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + error: + `forbidden: host "${host}" is not allowed. ` + + `Set ${PREVIEW_ALLOWED_HOSTS_ENV}=${host} to allow it ` + + `(comma separated for several hosts).`, + }), + ); + }); + server.middlewares.use((req, res, next) => { if (req.url !== "/__hyperframes_config") return next(); const payload = previewConfigPayload(process.env, process.pid, studioPkg.version); @@ -347,6 +386,8 @@ export default defineConfig({ }, server: { port: 5190, + // Vite's defaults (localhost, IP literals) still apply on top of this list. + allowedHosts: previewAllowedHostsFromEnv(), watch: { // A composition lives under this package's root, so Vite's HMR sees a // write to one as an html page dependency changing and full-reloads the