Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(setup): preserve launcher identity and uncertainty

morluto19329e43bb38
diff --git a/src/application/ClientRegistrationIdentity.ts b/src/application/ClientRegistrationIdentity.ts
index f1f43b9b2..7b4d457c3 100644
--- a/src/application/ClientRegistrationIdentity.ts
+++ b/src/application/ClientRegistrationIdentity.ts
@@ -1,5 +1,6 @@
import { realpathSync } from "node:fs";
-import { isAbsolute, resolve } from "node:path";
+import { isAbsolute } from "node:path";
+import { normalizeEntrypointPath } from "../process/CurrentEntrypointPath.js";
import { valid } from "semver";
import { PRODUCT_IDENTITY } from "../identity.js";
@@ -116,86 +117,77 @@ const isOwnedPackageReference = (packageReference: string): boolean => {
/** Check that one parsed command points to REA's MCP entry point. */
export type ClientRegistrationCommandIdentity = "owned" | "foreign" | "unknown";
-/** POSIX bin links can name the same dispatcher without changing stored commands. */
-const sameEntrypointPath = (candidate: string, current: string): boolean => {
- if (!isAbsolute(candidate) || !isAbsolute(current)) return false;
- if (resolve(candidate) === current) return true;
- // Windows package bins are command shims, not links to the dispatcher.
- if (process.platform === "win32") return false;
+/** Compare entrypoint paths without turning failed resolution into a foreign identity. */
+const entrypointIdentity = (
+ candidate: string,
+ current: string,
+): ClientRegistrationCommandIdentity => {
+ if (!isAbsolute(candidate) || !isAbsolute(current)) return "unknown";
+ if (candidate === current) return "owned";
try {
- return realpathSync(candidate) === realpathSync(current);
+ return realpathSync.native(candidate) === realpathSync.native(current)
+ ? "owned"
+ : "foreign";
} catch {
- return false;
+ return "unknown";
}
};
-/** Classify only what the command and current executable identity establish. */
+/** A direct launcher may be a native executable or a Node shebang script. */
+const directEntrypointIdentity = (
+ candidate: string,
+ current: string,
+): ClientRegistrationCommandIdentity => {
+ const nativeIdentity = entrypointIdentity(candidate, current);
+ const scriptPath = normalizeEntrypointPath(candidate);
+ if (scriptPath === candidate) return nativeIdentity;
+ // A shebang can pass the original spelling to Node, which selects its script
+ // lexically. Without executing the launcher, divergent interpretations are unknown.
+ return scriptPath !== undefined &&
+ entrypointIdentity(scriptPath, current) === nativeIdentity
+ ? nativeIdentity
+ : "unknown";
+};
+
+/** Classify the command once; uncertainty never grants positive ownership. */
export const classifyClientRegistrationCommand = (
command: readonly string[],
currentCommandPath: string | undefined,
): ClientRegistrationCommandIdentity => {
const packageReference = clientRegistrationPackageReference(command);
if (packageReference !== undefined)
- return isOwnedClientRegistrationCommand(command, currentCommandPath)
- ? "owned"
- : "foreign";
+ return isOwnedPackageReference(packageReference) ? "owned" : "foreign";
const currentPathAvailable =
currentCommandPath !== undefined && currentCommandPath.length > 0;
- const nodeScript = command.length === 3 && command[2] === "mcp";
- if (nodeScript) {
+ if (command.length === 3 && command[2] === "mcp") {
+ const interpreter = command[0] ?? "";
+ const entrypoint = command[1] ?? "";
if (
- !isAbsolute(command[0] ?? "") ||
- !isAbsolute(command[1] ?? "") ||
+ !isAbsolute(interpreter) ||
+ !isAbsolute(entrypoint) ||
!currentPathAvailable
)
return "unknown";
- return isOwnedClientRegistrationCommand(command, currentCommandPath)
- ? "owned"
- : "foreign";
+ const interpreterIdentity = entrypointIdentity(
+ interpreter,
+ process.execPath,
+ );
+ if (interpreterIdentity !== "owned") return interpreterIdentity;
+ // Node selects its script from normalized argv; executable paths use native
+ // traversal semantics, where symlink/.. can denote a different file.
+ const selectedEntrypoint = normalizeEntrypointPath(entrypoint);
+ return selectedEntrypoint === undefined
+ ? "unknown"
+ : entrypointIdentity(selectedEntrypoint, currentCommandPath);
}
if (command.length === 2 && command[1] === "mcp") {
const executable = command[0] ?? "";
if (executable === PRODUCT_IDENTITY.cliBinary) return "owned";
- const relativePathLike =
- !isAbsolute(executable) && /[./\\]/u.test(executable);
- if (relativePathLike || (isAbsolute(executable) && !currentPathAvailable))
- return "unknown";
+ if (isAbsolute(executable))
+ return currentPathAvailable
+ ? directEntrypointIdentity(executable, currentCommandPath)
+ : "unknown";
+ if (/[./\\]/u.test(executable)) return "unknown";
}
- if (isOwnedClientRegistrationCommand(command, currentCommandPath))
- return "owned";
return "foreign";
};
-
-export const isOwnedClientRegistrationCommand = (
- command: readonly string[],
- currentCommandPath: string | undefined,
-): boolean => {
- if (
- currentCommandPath !== undefined &&
- currentCommandPath.length > 0 &&
- command.length === 3 &&
- command[2] === "mcp" &&
- isAbsolute(command[0] ?? "") &&
- isAbsolute(command[1] ?? "") &&
- resolve(command[0] ?? "") === resolve(process.execPath) &&
- sameEntrypointPath(command[1] ?? "", currentCommandPath)
- )
- return true;
-
- if (command.length === 2 && command[1] === "mcp") {
- const executable = command[0] ?? "";
- if (
- executable === PRODUCT_IDENTITY.cliBinary ||
- (isAbsolute(executable) &&
- currentCommandPath !== undefined &&
- currentCommandPath.length > 0 &&
- sameEntrypointPath(executable, currentCommandPath))
- )
- return true;
- }
-
- const packageReference = clientRegistrationPackageReference(command);
- return (
- packageReference !== undefined && isOwnedPackageReference(packageReference)
- );
-};
diff --git a/src/application/ClientRegistrationStatus.ts b/src/application/ClientRegistrationStatus.ts
index 4c8c6c9a5..b8c041105 100644
--- a/src/application/ClientRegistrationStatus.ts
+++ b/src/application/ClientRegistrationStatus.ts
@@ -469,7 +469,7 @@ const remediation = (client: SetupClient): string => {
};
const unknownEntryPointRemediation = (): string =>
- "The existing registration was retained because its executable identity cannot be verified. Run the installed REA CLI or a supported package runner, then rerun rea doctor; no registration was changed.";
+ "The existing registration was retained because its executable identity cannot be verified. Check the registered launcher path, symlink targets and access permissions. Run the installed REA CLI or a supported package runner, then rerun rea doctor; no registration was changed.";
const configuredStatus = (
client: SetupClient,
diff --git a/src/application/Uninstall.ts b/src/application/Uninstall.ts
index f0a456927..5e4dc6de1 100644
--- a/src/application/Uninstall.ts
+++ b/src/application/Uninstall.ts
@@ -22,7 +22,7 @@ import {
readSkillFile,
skillDestinations,
} from "./SetupSkill.js";
-import { isOwnedClientRegistrationCommand } from "./ClientRegistrationIdentity.js";
+import { classifyClientRegistrationCommand } from "./ClientRegistrationIdentity.js";
import { CURRENT_ENTRYPOINT_PATH } from "../process/CurrentEntrypointPath.js";
import { dshDocument } from "./DshCordisPatch.js";
import { resolveClientConfigTransactionPath } from "./ClientConfigPath.js";
@@ -604,14 +604,19 @@ const isOwnedRegistration = (value: unknown, client: SetupClient): boolean => {
.safeParse(value);
if (!parsed.success) return false;
const command = parsed.data.command;
- return isOwnedClientRegistrationCommand(command, CURRENT_ENTRYPOINT_PATH);
+ return (
+ classifyClientRegistrationCommand(command, CURRENT_ENTRYPOINT_PATH) ===
+ "owned"
+ );
}
const parsed = registrationSchema.safeParse(value);
if (!parsed.success) return false;
const { command, args } = parsed.data;
- return isOwnedClientRegistrationCommand(
- [command, ...args],
- CURRENT_ENTRYPOINT_PATH,
+ return (
+ classifyClientRegistrationCommand(
+ [command, ...args],
+ CURRENT_ENTRYPOINT_PATH,
+ ) === "owned"
);
};
diff --git a/src/application/UpdateMaintenance.ts b/src/application/UpdateMaintenance.ts
index fd55c8f23..6c8b7fbaf 100644
--- a/src/application/UpdateMaintenance.ts
+++ b/src/application/UpdateMaintenance.ts
@@ -1,6 +1,6 @@
import { z } from "zod";
-import { isOwnedClientRegistrationCommand } from "./ClientRegistrationIdentity.js";
+import { classifyClientRegistrationCommand } from "./ClientRegistrationIdentity.js";
import { readClientRegistrationInspections } from "./ClientRegistrationStatus.js";
import { existingSkillDestinations } from "./SetupSkill.js";
import type { SetupAction } from "./SetupTypes.js";
@@ -65,7 +65,8 @@ export const existingMaintenanceScope = async (
if (
activation !== "enabled" ||
(registration.state !== "aligned" && registration.state !== "stale") ||
- !isOwnedClientRegistrationCommand(registration.command, entryPoint)
+ classifyClientRegistrationCommand(registration.command, entryPoint) !==
+ "owned"
)
continue;
clients.push(registration.client);
diff --git a/src/process/CurrentEntrypointPath.ts b/src/process/CurrentEntrypointPath.ts
index 8005e1aab..23434472c 100644
--- a/src/process/CurrentEntrypointPath.ts
+++ b/src/process/CurrentEntrypointPath.ts
@@ -1,6 +1,8 @@
import { resolve } from "node:path";
-/** Absolute argv entrypoint, or undefined when the process did not supply one. */
+/** Node's absolute argv script selection, or undefined when none was supplied.
+ * Native executable paths have different traversal semantics and must not use this.
+ */
export const normalizeEntrypointPath = (
entrypoint: string | undefined,
): string | undefined =>
diff --git a/tests/acceptance/setup/launcherAliasesCli.test.ts b/tests/acceptance/setup/launcherAliasesCli.test.ts
index 7b2db98c4..21faefff7 100644
--- a/tests/acceptance/setup/launcherAliasesCli.test.ts
+++ b/tests/acceptance/setup/launcherAliasesCli.test.ts
@@ -62,3 +62,129 @@ cliTest.skipIf(process.platform === "win32").for([
});
},
);
+
+cliTest.skipIf(process.platform === "win32").for([
+ ["dangling", "direct"],
+ ["dangling", "node"],
+ ["cycle", "direct"],
+ ["cycle", "node"],
+] as const)(
+ "retains %s launcher identity as unknown for a %s registration",
+ async ([failure, form], { cli }) => {
+ const home = await createTestTempDirectory("rea-unresolved-registration-");
+ const directory = join(home, ".cursor");
+ const config = join(directory, "mcp.json");
+ await mkdir(directory);
+ const launcher = join(home, "rea");
+ if (failure === "dangling")
+ await symlink(join(home, "removed-release.mjs"), launcher);
+ else {
+ await symlink(join(home, "other-alias"), launcher);
+ await symlink(launcher, join(home, "other-alias"));
+ }
+ const command =
+ form === "direct"
+ ? [launcher, "mcp"]
+ : [process.execPath, launcher, "mcp"];
+ const original = JSON.stringify({
+ mcpServers: {
+ rea: { command: command[0], args: command.slice(1) },
+ unrelated: { command: "other-server" },
+ },
+ });
+ await writeFile(config, original);
+ const environment = {
+ HOME: home,
+ USERPROFILE: home,
+ XDG_CONFIG_HOME: join(home, ".config"),
+ };
+ const doctor = await cli.run({
+ arguments: ["doctor", "--client", "cursor", "--skill=false", "--json"],
+ cwd: home,
+ environment,
+ });
+ expect(doctor.exitCode, doctor.stderr).toBe(1);
+ expect(doctor.json).toMatchObject({
+ identity: {
+ registrations: expect.arrayContaining([
+ expect.objectContaining({
+ client: "cursor",
+ state: "unknown",
+ command,
+ remediation: expect.stringContaining("symlink targets"),
+ }),
+ ]),
+ },
+ });
+ expect(await readFile(config, "utf8")).toBe(original);
+ const uninstall = await cli.run({
+ arguments: ["uninstall", "--json"],
+ cwd: home,
+ environment,
+ });
+ expect(uninstall.exitCode, uninstall.stderr).toBe(0);
+ expect(await readFile(config, "utf8")).toBe(original);
+ },
+);
+
+cliTest.skipIf(process.platform === "win32")(
+ "uninstall preserves a different executable reached through symlink parent traversal",
+ async ({ processes }) => {
+ const root = await createTestTempDirectory(
+ "rea-launcher-native-traversal-",
+ );
+ const home = join(root, "home");
+ const config = join(home, ".cursor/mcp.json");
+ await mkdir(join(home, ".cursor"), { recursive: true });
+ const current = join(root, "rea");
+ await symlink(resolve("scripts/rea.mjs"), current);
+ const other = join(root, "other");
+ await mkdir(join(other, "child"), { recursive: true });
+ await writeFile(join(other, "rea"), "#!/bin/sh\nprintf foreign\n", {
+ mode: 0o700,
+ });
+ await symlink(join(other, "child"), join(root, "link"));
+ const candidate = `${root}/link/../rea`;
+ expect(await readFile(candidate, "utf8")).toContain("printf foreign");
+ const original = JSON.stringify({
+ mcpServers: {
+ rea: { command: candidate, args: ["mcp"] },
+ unrelated: { command: "other-server" },
+ },
+ });
+ await writeFile(config, original);
+ const run = (arguments_: readonly string[]) =>
+ processes.run(process.execPath, [current, ...arguments_], {
+ cwd: home,
+ env: {
+ HOME: home,
+ USERPROFILE: home,
+ XDG_CONFIG_HOME: join(home, ".config"),
+ PATH: process.env.PATH ?? "",
+ },
+ timeoutMs: 30_000,
+ });
+ const uninstall = await run(["uninstall", "--json"]);
+ expect(uninstall.exitCode, uninstall.stderr).toBe(0);
+ expect(await readFile(config, "utf8")).toBe(original);
+ const doctor = await run([
+ "doctor",
+ "--client",
+ "cursor",
+ "--skill=false",
+ "--json",
+ ]);
+ expect(doctor.exitCode, doctor.stderr).toBe(1);
+ expect(JSON.parse(doctor.stdout)).toMatchObject({
+ identity: {
+ registrations: expect.arrayContaining([
+ expect.objectContaining({
+ client: "cursor",
+ command: [candidate, "mcp"],
+ state: "unknown",
+ }),
+ ]),
+ },
+ });
+ },
+);
diff --git a/tests/boundary/filesystem/clientConfigurationDialects.test.ts b/tests/boundary/filesystem/clientConfigurationDialects.test.ts
index 7e79e544a..ca688295c 100644
--- a/tests/boundary/filesystem/clientConfigurationDialects.test.ts
+++ b/tests/boundary/filesystem/clientConfigurationDialects.test.ts
@@ -14,10 +14,7 @@ import { configureClientConfiguration } from "../../../src/application/SetupClie
import { systemUninstallHost } from "../../../src/application/Uninstall.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
-import {
- classifyClientRegistrationCommand,
- isOwnedClientRegistrationCommand,
-} from "../../../src/application/ClientRegistrationIdentity.js";
+import { classifyClientRegistrationCommand } from "../../../src/application/ClientRegistrationIdentity.js";
import {
CURRENT_ENTRYPOINT_PATH,
normalizeEntrypointPath,
@@ -724,15 +721,12 @@ describe("client registration command ownership", () => {
expect(
classifyClientRegistrationCommand(command, join(root, current)),
).toBe("owned");
- expect(
- isOwnedClientRegistrationCommand(command, join(root, current)),
- ).toBe(true);
}
},
);
it.skipIf(process.platform === "win32")(
- "does not own a same-named launcher for another script or a dangling link",
+ "distinguishes a confirmed different script from an unresolved alias",
async () => {
const root = await createTestTempDirectory("rea-launcher-foreign-");
const owned = join(root, "entry.mjs");
@@ -746,66 +740,135 @@ describe("client registration command ownership", () => {
const command = [join(root, candidate), "mcp"];
expect(
classifyClientRegistrationCommand(command, join(root, "rea")),
- ).toBe("foreign");
- expect(
- isOwnedClientRegistrationCommand(command, join(root, "rea")),
- ).toBe(false);
+ ).toBe(candidate === "missing" ? "unknown" : "foreign");
}
},
);
+ it
+ .skipIf(process.platform === "win32")
+ .each(["registered", "current"] as const)(
+ "preserves unknown identity when the %s alias cannot resolve",
+ async (unresolved) => {
+ const root = await createTestTempDirectory("rea-launcher-unresolved-");
+ const script = join(root, "entry.mjs");
+ await writeFile(script, "// current dispatcher\n");
+ await symlink(join(root, "removed-release.mjs"), join(root, "dangling"));
+ await symlink(join(root, "cycle-b"), join(root, "cycle-a"));
+ await symlink(join(root, "cycle-a"), join(root, "cycle-b"));
+ for (const alias of ["dangling", "cycle-a"]) {
+ const registered =
+ unresolved === "registered" ? join(root, alias) : script;
+ const current = unresolved === "current" ? join(root, alias) : script;
+ for (const command of [
+ [registered, "mcp"],
+ [process.execPath, registered, "mcp"],
+ ])
+ expect(classifyClientRegistrationCommand(command, current)).toBe(
+ "unknown",
+ );
+ }
+ },
+ );
+});
+
+describe("client launcher traversal semantics", () => {
+ it.skipIf(process.platform === "win32")(
+ "distinguishes native executable traversal from Node script argument selection",
+ async () => {
+ const root = await createTestTempDirectory(
+ "rea-launcher-parent-segment-",
+ );
+ const current = join(root, "entry.mjs");
+ const foreign = join(root, "other", "entry.mjs");
+ await mkdir(join(root, "other", "child"), { recursive: true });
+ await writeFile(current, "// current dispatcher\n");
+ await writeFile(foreign, "// foreign dispatcher\n");
+ await symlink(join(root, "other", "child"), join(root, "link"));
+ const candidate = `${root}/link/../entry.mjs`;
+ expect(await readFile(candidate, "utf8")).toBe("// foreign dispatcher\n");
+ expect(
+ classifyClientRegistrationCommand([candidate, "mcp"], current),
+ ).toBe("unknown");
+ expect(
+ classifyClientRegistrationCommand([candidate, "mcp"], foreign),
+ ).toBe("unknown");
+ expect(
+ classifyClientRegistrationCommand(
+ [process.execPath, candidate, "mcp"],
+ current,
+ ),
+ ).toBe("owned");
+ const unresolvable = `${root}/missing/../entry.mjs`;
+ expect(
+ classifyClientRegistrationCommand([unresolvable, "mcp"], current),
+ ).toBe("unknown");
+ expect(
+ classifyClientRegistrationCommand([current, "mcp"], unresolvable),
+ ).toBe("unknown");
+ expect(
+ classifyClientRegistrationCommand(
+ [process.execPath, unresolvable, "mcp"],
+ current,
+ ),
+ ).toBe("owned");
+ const nodeAlias = join(root, "node-alias");
+ await symlink(process.execPath, nodeAlias);
+ expect(
+ classifyClientRegistrationCommand([nodeAlias, current, "mcp"], current),
+ ).toBe("owned");
+ },
+ );
+
it("recognizes prior exact REA releases while rejecting foreign package commands", () => {
- expect(isOwnedClientRegistrationCommand(["rea", "mcp"], undefined)).toBe(
- true,
+ expect(classifyClientRegistrationCommand(["rea", "mcp"], undefined)).toBe(
+ "owned",
);
const cwdMatchingEntry = resolve("./rea");
expect(
classifyClientRegistrationCommand(["./rea", "mcp"], cwdMatchingEntry),
).toBe("unknown");
expect(
- isOwnedClientRegistrationCommand(["./rea", "mcp"], cwdMatchingEntry),
- ).toBe(false);
- expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
["npx", "-y", `${PRODUCT_IDENTITY.packageName}@3.2.0`, "mcp"],
undefined,
),
- ).toBe(true);
+ ).toBe("owned");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
["npx", "-y", "another-package", "mcp"],
undefined,
),
- ).toBe(false);
+ ).toBe("foreign");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
["bunx", `${PRODUCT_IDENTITY.packageName}@3.2.0`, "mcp"],
undefined,
),
- ).toBe(true);
+ ).toBe("owned");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
["bunx", "another-package", "mcp"],
undefined,
),
- ).toBe(false);
+ ).toBe("foreign");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
[process.execPath, "/packaged/rea.mjs", "mcp"],
"/packaged/rea.mjs",
),
- ).toBe(true);
+ ).toBe("owned");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
[process.execPath, resolve("unknown"), "mcp"],
normalizeEntrypointPath(""),
),
- ).toBe(false);
+ ).toBe("unknown");
expect(
- isOwnedClientRegistrationCommand(
+ classifyClientRegistrationCommand(
["node", "/packaged/other-agent.mjs", "mcp"],
"/packaged/rea.mjs",
),
- ).toBe(false);
+ ).toBe("unknown");
});
});
diff --git a/tests/boundary/filesystem/clientRegistrationStatus.test.ts b/tests/boundary/filesystem/clientRegistrationStatus.test.ts
index 1612ac6aa..c713851f5 100644
--- a/tests/boundary/filesystem/clientRegistrationStatus.test.ts
+++ b/tests/boundary/filesystem/clientRegistrationStatus.test.ts
@@ -43,6 +43,10 @@ describe("client registration status", () => {
it("distinguishes aligned, stale, missing, and invalid registrations", async () => {
const home = await createTestTempDirectory("rea-registrations-");
+ const currentEntry = join(home, "current-rea.mjs");
+ const foreignEntry = join(home, "foreign-rea.mjs");
+ await writeFile(currentEntry, "// current dispatcher\n");
+ await writeFile(foreignEntry, "// different dispatcher\n");
await Promise.all([
mkdir(join(home, ".codex")),
mkdir(join(home, ".cursor")),
@@ -57,13 +61,13 @@ describe("client registration status", () => {
join(home, ".cursor/mcp.json"),
JSON.stringify({
mcpServers: {
- rea: { command: "/old/rea", args: ["mcp"], env: { SECRET: "x" } },
+ rea: { command: foreignEntry, args: ["mcp"], env: { SECRET: "x" } },
},
}),
);
await writeFile(join(home, ".gemini/settings.json"), "not-json");
- const statuses = await readClientRegistrationStatuses(home, "/current/rea");
+ const statuses = await readClientRegistrationStatuses(home, currentEntry);
expect(statuses).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -74,7 +78,7 @@ describe("client registration status", () => {
expect.objectContaining({
client: "cursor",
state: "stale",
- command: ["/old/rea", "mcp"],
+ command: [foreignEntry, "mcp"],
remediation: expect.any(String),
}),
expect.objectContaining({