Files
windmill/cli/test/dev_server_access_unit.test.ts
T
Ruben FiszelandClaude Opus 5.5 46a64b1b46 fix(cli): dev server and sync path handling improvements
Dev servers bind to localhost by default (new --host option) and sync keeps paths within the project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 18:53:29 +00:00

68 lines
3.0 KiB
TypeScript

/**
* Which clients the local dev servers (`wmill dev`, `wmill app dev`) accept
* on their WebSocket, and which paths and job ids their messages may name.
*/
import { expect, test } from "bun:test";
import * as path from "node:path";
import {
bindsLocalhost,
isAllowedDevPageOrigin,
isAllowedHostHeader,
isJobId,
isWithinFolder,
} from "../src/utils/dev_server_access.ts";
test("the wmill dev socket accepts the dev page's origins only", () => {
const remote = "https://windmill.example.com/";
expect(isAllowedDevPageOrigin("https://windmill.example.com", remote)).toBe(true);
// The --proxy-port page and a locally run frontend are on this machine.
expect(isAllowedDevPageOrigin("http://localhost:3002", remote)).toBe(true);
expect(isAllowedDevPageOrigin("http://127.0.0.1:3000", remote)).toBe(true);
// Non-browser clients send no Origin.
expect(isAllowedDevPageOrigin(undefined, remote)).toBe(true);
expect(isAllowedDevPageOrigin("https://other.example", remote)).toBe(false);
expect(isAllowedDevPageOrigin("http://windmill.example.com", remote)).toBe(false);
expect(isAllowedDevPageOrigin("https://windmill.example.com.other.example", remote)).toBe(false);
expect(isAllowedDevPageOrigin("null", remote)).toBe(false);
});
test("a Host that DNS could re-point is refused on a loopback bind", () => {
expect(isAllowedHostHeader("localhost:3001", "127.0.0.1")).toBe(true);
expect(isAllowedHostHeader("127.0.0.1:3001", "127.0.0.1")).toBe(true);
expect(isAllowedHostHeader("[::1]:3001", "localhost")).toBe(true);
expect(isAllowedHostHeader("192.168.1.5:4000", "localhost")).toBe(true);
expect(isAllowedHostHeader("other.example:3001", "127.0.0.1")).toBe(false);
expect(isAllowedHostHeader(undefined, "127.0.0.1")).toBe(false);
// An explicit --host is a name the developer chose.
expect(isAllowedHostHeader("devbox:4000", "devbox")).toBe(true);
// A wildcard bind is reachable under any name the developer exposed.
expect(isAllowedHostHeader("devbox.lan:3001", "0.0.0.0")).toBe(true);
});
test("message paths stay inside the checkout", () => {
const base = path.resolve("/tmp/checkout");
expect(isWithinFolder(base, path.join(base, "f/foo.flow/a.ts"))).toBe(true);
expect(isWithinFolder(base, base)).toBe(true);
expect(isWithinFolder(base, path.join(base, "f/../../ops/deploy"))).toBe(false);
expect(isWithinFolder(base, "/home/someone/.bashrc")).toBe(false);
expect(isWithinFolder(base, "/tmp/checkout-other/a.ts")).toBe(false);
});
test("only a UUID is taken as a job id", () => {
expect(isJobId("0190c5e4-6b2a-7c3d-8e9f-0123456789ab")).toBe(true);
expect(isJobId("../../variables/list")).toBe(false);
expect(isJobId("0190c5e4-6b2a-7c3d-8e9f-0123456789ab/../x")).toBe(false);
expect(isJobId(undefined)).toBe(false);
});
test("wmill dev binds only addresses the dev page reaches on localhost", () => {
for (const h of ["127.0.0.1", "localhost", "::1", "0.0.0.0", "::"]) {
expect(bindsLocalhost(h)).toBe(true);
}
for (const h of ["127.0.0.2", "192.168.1.10", "example.com"]) {
expect(bindsLocalhost(h)).toBe(false);
}
});