Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(vscode): only follow a serve lock file that points at this machine (#3040)

Raghav Chamadiya16b0e3a5b802
diff --git a/packages/claude-mod/src/data/discovery.ts b/packages/claude-mod/src/data/discovery.ts
index 11d28e377..eab010163 100644
--- a/packages/claude-mod/src/data/discovery.ts
+++ b/packages/claude-mod/src/data/discovery.ts
@@ -8,7 +8,7 @@ import { getHealth } from "@repowise-dev/api-client/health";
import { listRepos } from "@repowise-dev/api-client/repos";
import { ApiClientError } from "@repowise-dev/api-client";
import { normalizeRepoPath } from "@repowise-dev/types/repos";
-import { isServeLock, type ServeLock } from "@repowise-dev/types/serve-lock";
+import { isLoopbackUrl, isServeLock, type ServeLock } from "@repowise-dev/types/serve-lock";
import type { Host } from "../host";
import type { IndexFreshness, LiteReason, Mode } from "../model/session";
import { connectApiClient, withTimeout } from "./transport";
@@ -73,22 +73,6 @@ export async function readServeLock(host: Host, repoRoot: string): Promise<Serve
return isServeLock(parsed) ? parsed : null;
}
-const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "[::1]", "::1"]);
-
-/**
- * Whether the lock points at plain http on this machine. The lock sits in the
- * repo, so a cloned repo can carry one naming any host, and a pid gate is no
- * defence (some pids are always alive). Lens probes nothing else.
- */
-export function isLoopbackUrl(url: string): boolean {
- try {
- const u = new URL(url);
- return u.protocol === "http:" && LOOPBACK_HOSTS.has(u.hostname);
- } catch {
- return false;
- }
-}
-
/**
* Whether a process with this pid exists: true, false, or null when the probe
* could not run (no process API on this surface). A null does not block the
diff --git a/packages/claude-mod/test/discovery.test.ts b/packages/claude-mod/test/discovery.test.ts
index d3bc201a1..8fd5f4522 100644
--- a/packages/claude-mod/test/discovery.test.ts
+++ b/packages/claude-mod/test/discovery.test.ts
@@ -4,7 +4,6 @@ import {
discover,
findIndexedRoot,
isPidAlive,
- isLoopbackUrl,
isWindowsPath,
mcpReachable,
probeServer,
@@ -41,23 +40,6 @@ function server(status = 200) {
const inWorkTree = (argv: readonly string[]) =>
argv.join(" ") === "git rev-parse --is-inside-work-tree" ? ok("true\n") : failed;
-describe("isLoopbackUrl", () => {
- it.each(["http://127.0.0.1:7411", "http://localhost:7411/", "http://[::1]:7411"])("accepts %s", (url) => {
- expect(isLoopbackUrl(url)).toBe(true);
- });
-
- it.each([
- "https://127.0.0.1:7411",
- "http://evil.example:7411",
- "http://127.0.0.1.evil.example",
- "http://10.0.0.5:7411",
- "file:///etc/passwd",
- "not a url",
- ])("refuses %s", (url) => {
- expect(isLoopbackUrl(url)).toBe(false);
- });
-});
-
function host(o: FakeHostOptions = {}) {
return fakeHost({ cwd: ROOT, files: { [STATE]: JSON.stringify({ last_sync_commit: indexedAt }) }, run: tasklist, ...o });
}
diff --git a/packages/types/__tests__/serve-lock.test.ts b/packages/types/__tests__/serve-lock.test.ts
index f2a228541..22622d82e 100644
--- a/packages/types/__tests__/serve-lock.test.ts
+++ b/packages/types/__tests__/serve-lock.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { isServeLock, type ServeLock } from "../src/serve-lock.js";
+import { isLoopbackUrl, isServeLock, type ServeLock } from "../src/serve-lock.js";
const VALID: ServeLock = {
pid: 4242,
@@ -46,3 +46,28 @@ describe("isServeLock", () => {
}
});
});
+
+describe("isLoopbackUrl", () => {
+ it.each([
+ "http://127.0.0.1:7411",
+ "http://127.3.4.5:7411",
+ "https://127.0.0.1:7411",
+ "http://localhost:7411/",
+ "http://[::1]:7411",
+ ])("accepts %s", (url) => {
+ expect(isLoopbackUrl(url)).toBe(true);
+ });
+
+ it.each([
+ "http://evil.example:7411",
+ "http://127.0.0.1.evil.example",
+ "http://10.0.0.5:7411",
+ "http://0.0.0.0:7411",
+ "http://[::2]:7411",
+ "file:///etc/passwd",
+ "ftp://127.0.0.1",
+ "not a url",
+ ])("refuses %s", (url) => {
+ expect(isLoopbackUrl(url)).toBe(false);
+ });
+});
diff --git a/packages/types/src/serve-lock.ts b/packages/types/src/serve-lock.ts
index 95a218912..42655521e 100644
--- a/packages/types/src/serve-lock.ts
+++ b/packages/types/src/serve-lock.ts
@@ -27,3 +27,20 @@ export function isServeLock(value: unknown): value is ServeLock {
typeof v.started_at === "string"
);
}
+
+const LOOPBACK_V4 = /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
+
+/**
+ * Whether a URL is plain http(s) on this machine: `localhost`, 127.0.0.0/8 or
+ * `::1`. The lockfile sits in the repo, so a cloned repo can carry one naming
+ * any host; consumers check this before probing or talking to `url`.
+ */
+export function isLoopbackUrl(url: string): boolean {
+ try {
+ const u = new URL(url);
+ if (u.protocol !== "http:" && u.protocol !== "https:") return false;
+ return u.hostname === "localhost" || u.hostname === "[::1]" || LOOPBACK_V4.test(u.hostname);
+ } catch {
+ return false;
+ }
+}
diff --git a/packages/types/tsconfig.json b/packages/types/tsconfig.json
index 43bc507f1..7dd0683b9 100644
--- a/packages/types/tsconfig.json
+++ b/packages/types/tsconfig.json
@@ -3,7 +3,7 @@
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
- "lib": ["ES2022"],
+ "lib": ["ES2022", "DOM"],
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
diff --git a/packages/vscode/src/features/serverManager.ts b/packages/vscode/src/features/serverManager.ts
index 49fd21f0d..2fd82d96b 100644
--- a/packages/vscode/src/features/serverManager.ts
+++ b/packages/vscode/src/features/serverManager.ts
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { spawn, type ChildProcess } from "node:child_process";
import { Commands, MIN_SERVER_VERSION } from "../constants";
import type { RepowiseContext } from "../core/context";
+import { isLoopbackUrl, type ServeLock } from "@repowise-dev/types/serve-lock";
import { isPidAlive, readLockfile } from "../core/lockfile";
import type { HealthResult } from "../core/api";
@@ -157,7 +158,7 @@ export function registerServerManager(ctx: RepowiseContext): vscode.Disposable {
// The pid gate is not redundant with the probe: a dead writer's port can
// be re-bound by an unrelated server that answers /health convincingly.
- const lock = await readLockfile(ws.lockfilePath);
+ const lock = await readLocalLockfile(ws.lockfilePath);
if (lock && isPidAlive(lock.pid)) {
const health = await ctx.api.checkHealth(lock.url);
if (health) {
@@ -179,6 +180,20 @@ export function registerServerManager(ctx: RepowiseContext): vscode.Disposable {
}
}
+ /**
+ * The lockfile, unless its url points off this machine: the file sits in the
+ * repo, so a cloned repo can name any host. Such a lock is ignored and the
+ * normal start/connect path runs.
+ */
+ async function readLocalLockfile(path: string): Promise<ServeLock | null> {
+ const lock = await readLockfile(path);
+ if (lock && !isLoopbackUrl(lock.url)) {
+ ctx.log.warn("Ignoring serve.lock.json: its url is not a loopback address.");
+ return null;
+ }
+ return lock;
+ }
+
/**
* Polls for the lockfile to appear and its server to answer /health, backing
* off between attempts. Re-reads the lockfile every pass so a port the server
@@ -194,7 +209,7 @@ export function registerServerManager(ctx: RepowiseContext): vscode.Disposable {
if (disposed || aborted()) return null;
// Pid-gated so the poll cannot adopt a stale lockfile (and whatever
// re-bound its port) in the window before our child overwrites it.
- const lock = await readLockfile(lockfilePath);
+ const lock = await readLocalLockfile(lockfilePath);
if (lock && isPidAlive(lock.pid)) {
const health = await ctx.api.checkHealth(lock.url);
if (health) return { url: lock.url, health };
@@ -246,7 +261,7 @@ export function registerServerManager(ctx: RepowiseContext): vscode.Disposable {
// A healthy server may already be up (started outside the editor, or by a
// prior session). Adopt it instead of spawning a second one. Pid-gated:
// never adopt through a dead writer's lockfile.
- const existing = await readLockfile(ws.lockfilePath);
+ const existing = await readLocalLockfile(ws.lockfilePath);
if (existing && isPidAlive(existing.pid)) {
const health = await ctx.api.checkHealth(existing.url);
if (health) {
diff --git a/plugins/claude-code/hooks/lens/lens.js b/plugins/claude-code/hooks/lens/lens.js
index 267559619..1675cb01d 100644
--- a/plugins/claude-code/hooks/lens/lens.js
+++ b/plugins/claude-code/hooks/lens/lens.js
@@ -179,6 +179,17 @@ function isServeLock(value) {
return typeof v.pid === "number" && typeof v.host === "string" && typeof v.port === "number" && typeof v.url === "string" && (typeof v.ui_port === "number" || v.ui_port === null) && typeof v.server_version === "string" && typeof v.started_at === "string";
}
__name(isServeLock, "isServeLock");
+var LOOPBACK_V4 = /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
+function isLoopbackUrl(url) {
+ try {
+ const u = new URL(url);
+ if (u.protocol !== "http:" && u.protocol !== "https:") return false;
+ return u.hostname === "localhost" || u.hostname === "[::1]" || LOOPBACK_V4.test(u.hostname);
+ } catch {
+ return false;
+ }
+}
+__name(isLoopbackUrl, "isLoopbackUrl");
// src/data/transport.ts
function connectApiClient(host, baseUrl) {
@@ -248,16 +259,6 @@ async function readServeLock(host, repoRoot) {
return isServeLock(parsed) ? parsed : null;
}
__name(readServeLock, "readServeLock");
-var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["127.0.0.1", "localhost", "[::1]", "::1"]);
-function isLoopbackUrl(url) {
- try {
- const u = new URL(url);
- return u.protocol === "http:" && LOOPBACK_HOSTS.has(u.hostname);
- } catch {
- return false;
- }
-}
-__name(isLoopbackUrl, "isLoopbackUrl");
async function isPidAlive(host, pid, cwd2) {
try {
if (isWindowsPath(cwd2)) {