Skip to content

Commit ad1a4da

Browse files
jrusso1020claude
andcommitted
fix(studio): accept the *.localhost and leading-dot hosts Vite serves on the API routes too
The dev server's API Host check was stricter than Vite's own rule, so a page Vite served on studio.localhost or under a leading-dot allowed host got a 403 from every /api call. The check now applies Vite's rule alongside the CLI's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98cfa71 commit ad1a4da

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎packages/studio/vite.config.hosts.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ describe("studio dev server host check", () => {
122122
expect((await request(middlewares, "/api/projects", "evil.tunnel.example")).status).toBe(403);
123123
});
124124

125+
it("serves the *.localhost and leading-dot hosts Vite serves the page on", async () => {
126+
vi.stubEnv(ENV, ".tunnel.example");
127+
const middlewares = startPlugin();
128+
for (const host of ["studio.localhost:5190", "tunnel.example", "a.b.tunnel.example:443"]) {
129+
expect((await request(middlewares, "/api/projects", host)).status, host).toBe(200);
130+
}
131+
for (const host of ["localhost.evil.example", "eviltunnel.example", "tunnel.example.evil"]) {
132+
expect((await request(middlewares, "/api/projects", host)).status, host).toBe(403);
133+
}
134+
});
135+
125136
it("leaves routes the plugin does not own to Vite's own host check", async () => {
126137
const middlewares = startPlugin();
127138
expect((await request(middlewares, "/", "other.example:5190")).status).toBeUndefined();

‎packages/studio/vite.config.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ async function bridgeHonoResponse(
9797
res.end();
9898
}
9999

100-
// Vite allows any IP-literal Host by default, which is what keeps `--host` on a LAN address working.
101-
function isIpLiteralHost(host: string): boolean {
100+
// Vite's own rule (IP literals, *.localhost, leading-dot entries), so the API serves every Host Vite serves the page on.
101+
function viteAllowsHost(host: string, allowedHosts: readonly string[]): boolean {
102102
const bracketed = /^\[([^\]]+)\]/.exec(host);
103-
return bracketed ? isIP(bracketed[1]!) === 6 : isIP(host.split(":")[0]!) === 4;
103+
if (bracketed) return isIP(bracketed[1]!) === 6;
104+
const name = host.split(":")[0]!.toLowerCase();
105+
if (isIP(name) === 4 || name === "localhost" || name.endsWith(".localhost")) return true;
106+
return allowedHosts.some(
107+
(allowed) => allowed.startsWith(".") && (name === allowed.slice(1) || name.endsWith(allowed)),
108+
);
104109
}
105110

106111
// ── Vite plugin ──────────────────────────────────────────────────────────────
@@ -210,16 +215,16 @@ function devProjectApi(): Plugin {
210215
};
211216

212217
// Vite runs its own Host check after the middlewares added here, so the
213-
// plugin's routes check Host first: the CLI preview server's rule plus
214-
// the IP literals and configured host Vite allows by default.
218+
// plugin's routes check Host first: the CLI preview server's rule plus Vite's.
215219
const allowedHosts = [...previewAllowedHostsFromEnv()];
216220
if (typeof server.config.server.host === "string")
217221
allowedHosts.push(server.config.server.host);
218222
server.middlewares.use((req, res, next) => {
219223
const url = req.url ?? "";
220224
if (url !== "/__hyperframes_config" && !url.startsWith("/api/")) return next();
221225
const host = req.headers.host ?? "";
222-
if (hostHeaderAllowed(host, allowedHosts) || isIpLiteralHost(host)) return next();
226+
if (hostHeaderAllowed(host, allowedHosts) || viteAllowsHost(host, allowedHosts))
227+
return next();
223228
res.writeHead(403, { "Content-Type": "application/json" });
224229
res.end(
225230
JSON.stringify({

0 commit comments

Comments
 (0)