Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(view): keep backend scope out of generated script (#412)

Joseph Mikhailab823e34b9ec
diff --git a/ix-cli/src/cli/__tests__/view-running-port.test.ts b/ix-cli/src/cli/__tests__/view-running-port.test.ts
index dbb9e944..c36fd56c 100644
--- a/ix-cli/src/cli/__tests__/view-running-port.test.ts
+++ b/ix-cli/src/cli/__tests__/view-running-port.test.ts
@@ -41,9 +41,20 @@ beforeEach(() => {
writeFileSync(join(compassDir, "index.html"), "<!doctype html>");
mocks.spawn.mockReturnValue({ pid: 4242, unref: vi.fn() });
+ // The same probe answers two opposite questions: before the spawn, "is this
+ // port already taken?" (it must not be), and after it, "is the server up
+ // yet?" (it must be). A mock that always refuses satisfies the first and
+ // hangs the second for the whole readiness budget, so model the transition:
+ // refuse until the server is spawned, accept once it has been.
mocks.createConnection.mockImplementation(() => {
- const connection = new EventEmitter();
- queueMicrotask(() => connection.emit("error", new Error("not listening")));
+ // `end()` because the connect path closes the socket it just opened.
+ const connection = Object.assign(new EventEmitter(), { end: vi.fn() });
+ const listening = mocks.spawn.mock.calls.length > 0;
+ queueMicrotask(() =>
+ listening
+ ? connection.emit("connect")
+ : connection.emit("error", new Error("not listening")),
+ );
return connection;
});
});
diff --git a/ix-cli/src/cli/__tests__/view-server.test.ts b/ix-cli/src/cli/__tests__/view-server.test.ts
index 7eab1be5..417d39c7 100644
--- a/ix-cli/src/cli/__tests__/view-server.test.ts
+++ b/ix-cli/src/cli/__tests__/view-server.test.ts
@@ -5,7 +5,14 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { createServer } from "node:net";
import * as http from "node:http";
-import { serverScript } from "../commands/view.js";
+import { serverRuntimeArgs, serverScript } from "../commands/view.js";
+
+interface StartServerOptions {
+ workspaceId?: string;
+ systemId?: string;
+ backendUrl?: string;
+ useMapMainOverride?: boolean;
+}
/** Pick a free TCP port by binding port 0 and releasing it. */
function getFreePort(): Promise<number> {
@@ -44,12 +51,12 @@ describe("view server (/__ix/remap)", () => {
beforeAll(async () => {
// A fake Compass dist: index.html is the SPA entry the fallback serves.
- distDir = mkdtempSync(join(tmpdir(), "ix-view-dist-"));
+ distDir = mkdtempSync(join(tmpdir(), "ix view dist "));
writeFileSync(join(distDir, "index.html"), "<h1>fake compass</h1>");
// The workspace the server is scoped to. The endpoint maps this, not its
// own cwd, so the test has to supply one.
- mapRoot = mkdtempSync(join(tmpdir(), "ix-view-root-"));
+ mapRoot = mkdtempSync(join(tmpdir(), "ix view root "));
// The marker records every stub invocation. It lives in the temp tree and
// is passed by env rather than written to process.cwd(): the stub's cwd is
@@ -73,11 +80,14 @@ describe("view server (/__ix/remap)", () => {
);
port = await getFreePort();
- const script = serverScript(distDir, port, "test-workspace", null, mapRoot);
+ const script = serverScript();
// The generated script must survive template-literal emission intact.
expect(script).toContain('"/__ix/remap"');
expect(script).toContain("IX_VIEW_MAP_MAIN");
expect(script).toContain('server.listen(PORT, "127.0.0.1"');
+ // Backend-derived scope is supplied only at runtime, never written into
+ // the generated executable script.
+ expect(script).toContain("const SYSTEM_ID = process.argv[5] || null");
await startServer({ STUB_EXIT: "0" });
});
@@ -108,15 +118,30 @@ describe("view server (/__ix/remap)", () => {
* could lose the bind to EADDRINUSE — invisibly, because stdio is ignored —
* while waitForPort was satisfied by the still-dying old server.
*/
- async function startServer(extraEnv: Record<string, string>, root: string | null = mapRoot) {
+ async function startServer(
+ extraEnv: Record<string, string>,
+ root: string | null = mapRoot,
+ options: StartServerOptions = {},
+ ) {
await stopServer();
const scriptPath = join(distDir, "compass-server.js");
- writeFileSync(scriptPath, serverScript(distDir, port, "test-workspace", null, root));
- const spawned = spawn(process.execPath, [scriptPath], {
+ writeFileSync(scriptPath, serverScript());
+ const spawned = spawn(process.execPath, [
+ scriptPath,
+ ...serverRuntimeArgs(
+ distDir,
+ port,
+ options.workspaceId ?? "test-workspace",
+ options.systemId ?? null,
+ root,
+ stubMain,
+ ),
+ ], {
env: {
...process.env,
- NODE_ENV: "test", // the IX_VIEW_MAP_MAIN seam is honoured only under this
- IX_VIEW_MAP_MAIN: stubMain,
+ NODE_ENV: options.useMapMainOverride === false ? "production" : "test",
+ IX_VIEW_MAP_MAIN: options.useMapMainOverride === false ? "" : stubMain,
+ IX_VIEW_BACKEND_URL: options.backendUrl ?? "",
STUB_MARKER: marker,
STUB_MS: "0",
...extraEnv,
@@ -205,6 +230,51 @@ describe("view server (/__ix/remap)", () => {
expect(runs()).toEqual([`map ${mapRoot} --silent`]);
});
+ it("uses the argv CLI path outside the test-only MAP_MAIN override", async () => {
+ await startServer({ STUB_EXIT: "0" }, mapRoot, { useMapMainOverride: false });
+ try {
+ resetRuns();
+ const res = await post("/__ix/remap", { host: `127.0.0.1:${port}` });
+ expect(res.status).toBe(200);
+ expect(runs()).toEqual([`map ${mapRoot} --silent`]);
+ } finally {
+ await startServer({ STUB_EXIT: "0" });
+ }
+ });
+
+ it("passes argv workspace and system scopes to proxied backend requests", async () => {
+ let proxiedHeaders: http.IncomingHttpHeaders | null = null;
+ const backend = http.createServer((req, res) => {
+ proxiedHeaders = req.headers;
+ res.writeHead(200, { "Content-Type": "application/json" });
+ res.end('{"ok":true}');
+ });
+ await new Promise<void>((resolve, reject) => {
+ backend.once("error", reject);
+ backend.listen(0, "127.0.0.1", resolve);
+ });
+ const backendAddress = backend.address();
+ if (!backendAddress || typeof backendAddress === "string") throw new Error("backend did not bind a TCP port");
+
+ try {
+ await startServer({}, mapRoot, {
+ workspaceId: "workspace:test",
+ systemId: "system:test",
+ backendUrl: `http://127.0.0.1:${backendAddress.port}`,
+ });
+ const res = await fetch(`http://127.0.0.1:${port}/v1/probe?source=test`);
+ expect(res.status).toBe(200);
+ expect(proxiedHeaders?.["x-ix-workspace"]).toBe("workspace:test");
+ expect(proxiedHeaders?.["x-ix-system"]).toBe("system:test");
+ } finally {
+ await stopServer();
+ await new Promise<void>((resolve, reject) =>
+ backend.close((err) => err ? reject(err) : resolve()),
+ );
+ await startServer({ STUB_EXIT: "0" });
+ }
+ });
+
it("accepts a same-origin loopback Origin", async () => {
const res = await post("/__ix/remap", { origin: `http://localhost:${port}`, host: `127.0.0.1:${port}` });
expect(res.status).toBe(200);
diff --git a/ix-cli/src/cli/commands/view.ts b/ix-cli/src/cli/commands/view.ts
index 59f63555..8fceda8a 100644
--- a/ix-cli/src/cli/commands/view.ts
+++ b/ix-cli/src/cli/commands/view.ts
@@ -177,6 +177,27 @@ function isPortInUse(port: number): Promise<boolean> {
});
}
+/**
+ * Wait for the detached server to accept connections.
+ *
+ * It is spawned with `stdio: "ignore"`, so anything it writes on the way down
+ * goes nowhere — including the throw that now guards its argument contract.
+ * Without this the CLI printed "[ok] Visualizer started", wrote a PID file for
+ * a dead process and opened a browser on a closed port.
+ *
+ * Polls rather than watching for `exit`: the child is detached and unref'd, and
+ * the question worth answering is whether the port is actually serving, not
+ * whether the process happens to still be alive.
+ */
+async function waitForServer(port: number, timeoutMs = 8000): Promise<boolean> {
+ const deadline = Date.now() + timeoutMs;
+ while (Date.now() < deadline) {
+ if (await isPortInUse(port)) return true;
+ await new Promise((r) => setTimeout(r, 100));
+ }
+ return false;
+}
+
/**
* Path to the CLI entry point of the install generating this script.
*
@@ -187,15 +208,34 @@ function cliMainForScript(): string {
return join(dirname(fileURLToPath(import.meta.url)), "..", "main.js");
}
-/** Generate the inline server script that serves static files + proxies /v1. */
-export function serverScript(
+/** Build the argv payload shared by the production launcher and server tests. */
+export function serverRuntimeArgs(
distDir: string,
port: number,
workspaceId: string | null,
systemId: string | null,
- mapRoot: string | null = null,
-): string {
- const cliMainPath = cliMainForScript();
+ mapRoot: string | null,
+ cliMainPath: string = cliMainForScript(),
+): string[] {
+ return [
+ distDir,
+ String(port),
+ workspaceId ?? "",
+ systemId ?? "",
+ mapRoot ?? "",
+ cliMainPath,
+ ];
+}
+
+/**
+ * Generate the invariant server script that serves static files + proxies /v1.
+ *
+ * Runtime values are supplied as argv when the script is spawned. In
+ * particular, SYSTEM_ID may come from the backend, so embedding it here would
+ * create a network-data-to-executable-file path even though JSON.stringify
+ * made the generated JavaScript syntactically safe.
+ */
+export function serverScript(): string {
return `
const http = require("http");
const fs = require("fs");
@@ -203,18 +243,20 @@ const path = require("path");
const url = require("url");
const { execFile } = require("child_process");
-const DIST = ${JSON.stringify(distDir)};
-const PORT = ${port};
-const BACKEND = ${JSON.stringify(BACKEND_URL)};
-const WORKSPACE_ID = ${JSON.stringify(workspaceId)};
-const SYSTEM_ID = ${JSON.stringify(systemId)};
+const DIST = process.argv[2];
+const PORT = Number(process.argv[3]);
+const BACKEND = (process.env.NODE_ENV === "test" && process.env.IX_VIEW_BACKEND_URL)
+ ? process.env.IX_VIEW_BACKEND_URL
+ : ${JSON.stringify(BACKEND_URL)};
+const WORKSPACE_ID = process.argv[4] || null;
+const SYSTEM_ID = process.argv[5] || null;
-// The workspace root /__ix/remap maps, resolved by ix view start and baked in
-// rather than re-derived here — null when --all left the view unscoped.
-const MAP_ROOT = ${JSON.stringify(mapRoot)};
+// The workspace root /__ix/remap maps, resolved by ix view start rather than
+// re-derived here — empty/null when --all left the view unscoped.
+const MAP_ROOT = process.argv[6] || null;
// The CLI that generated this script, resolved from its own location at
-// generation time. Deriving it here from DIST only worked for the installed
+// launch time. Deriving it here from DIST only worked for the installed
// layout; findCompassDist's dev branch returns a repo path, from which the
// same arithmetic lands on a file that never exists.
//
@@ -223,7 +265,11 @@ const MAP_ROOT = ${JSON.stringify(mapRoot)};
// environment.
const MAP_MAIN = (process.env.NODE_ENV === "test" && process.env.IX_VIEW_MAP_MAIN)
? process.env.IX_VIEW_MAP_MAIN
- : ${JSON.stringify(cliMainPath)};
+ : process.argv[7];
+
+if (!DIST || !Number.isInteger(PORT) || PORT < 1 || PORT > 65535 || !MAP_MAIN) {
+ throw new Error("invalid ix view server arguments");
+}
// One map at a time. execFile is asynchronous, so nothing else serialises them.
let mapInFlight = null;
@@ -320,7 +366,8 @@ const server = http.createServer((req, res) => {
// stream open for the whole map.
req.resume();
// MAP_ROOT is the workspace this visualizer is showing, resolved once by
- // ix view start and baked in. Mapping process.cwd() instead would follow
+ // ix view start and supplied at launch. Mapping process.cwd() instead would
+ // follow
// whatever directory the detached server was launched from: --all does not
// require that to be a workspace at all, so "ix view start --all" run from
// a home directory turned one click into an ingest of the whole of it.
@@ -466,10 +513,9 @@ export function registerViewCommand(program: Command): void {
// workspace scoping (single-repo behavior is unaffected).
}
}
- // The system id (from a local marker or the backend) is embedded in the
- // generated, then-executed compass server script and the scope file, so
- // constrain it to the known id charset before use (CodeQL
- // js/http-to-file-access barrier; serverScript also JSON-encodes it).
+ // The system id (from a local marker or the backend) becomes a proxy
+ // header and persisted scope label, so constrain it to the known id
+ // charset before either use.
if (systemId !== null && !/^[A-Za-z0-9_.:-]+$/.test(systemId)) {
systemId = null;
}
@@ -534,10 +580,16 @@ export function registerViewCommand(program: Command): void {
// Write server script to temp location
const scriptDir = dirname(SERVER_SCRIPT_FILE);
mkdirSync(scriptDir, { recursive: true });
- writeFileSync(SERVER_SCRIPT_FILE, serverScript(distDir, port, workspaceId, systemId, mapRoot));
+ // The file is deliberately invariant: systemId can be returned by the
+ // backend and must never flow into JavaScript that is written and then
+ // executed. Runtime values travel as argv instead.
+ writeFileSync(SERVER_SCRIPT_FILE, serverScript());
// Spawn detached process
- const child = spawn("node", [SERVER_SCRIPT_FILE], {
+ const child = spawn(process.execPath, [
+ SERVER_SCRIPT_FILE,
+ ...serverRuntimeArgs(distDir, port, workspaceId, systemId, mapRoot),
+ ], {
detached: true,
stdio: "ignore",
});
@@ -548,6 +600,9 @@ export function registerViewCommand(program: Command): void {
process.exit(1);
}
+ // A pid only means the spawn succeeded — see waitForServer.
+ const ready = await waitForServer(port);
+
// Save PID, scope, and port for subsequent start/status/stop commands.
mkdirSync(dirname(PID_FILE), { recursive: true });
writeFileSync(PID_FILE, String(child.pid));
@@ -555,6 +610,18 @@ export function registerViewCommand(program: Command): void {
writeFileSync(PORT_FILE, String(port));
const url = `http://localhost:${port}`;
+
+ // Reported rather than fatal: the wait is a heuristic, and a machine slow
+ // enough to exceed it would otherwise have a working visualizer killed off
+ // or the command fail. The PID/scope/port files are written either way, so
+ // `ix view status` and `ix view stop` still work on whatever did start.
+ if (!ready) {
+ console.error(`[!] Visualizer started (PID ${child.pid}) but is not yet serving ${url}.`);
+ console.error(" If it does not come up shortly, run 'ix view stop', then");
+ console.error(" 'ix upgrade' if the Compass bundle is missing or incomplete.");
+ return;
+ }
+
console.log(`[ok] Visualizer started (PID ${child.pid})`);
console.log(` ${url}`);
console.log(