Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mcp): harden install merge, prompts, and device code validation

Tinf1ef0689c426
diff --git a/apps/mcp/src/auth/device-flow.ts b/apps/mcp/src/auth/device-flow.ts
index 81f1769ca..97a0fb544 100644
--- a/apps/mcp/src/auth/device-flow.ts
+++ b/apps/mcp/src/auth/device-flow.ts
@@ -36,7 +36,39 @@ export async function requestDeviceCode(
if (!("device_code" in body) || typeof body.device_code !== "string") {
throw new Error(`device/code: unexpected response ${JSON.stringify(body)}`);
}
- return body;
+ if (
+ typeof body.user_code !== "string" ||
+ typeof body.verification_uri !== "string"
+ ) {
+ throw new Error(
+ `device/code: missing user_code or verification_uri ${JSON.stringify(body)}`,
+ );
+ }
+ const interval = toFiniteNumber((body as DeviceCodeResponse).interval);
+ const expiresIn = toFiniteNumber((body as DeviceCodeResponse).expires_in);
+ if (interval === undefined || expiresIn === undefined) {
+ throw new Error(
+ `device/code: invalid interval or expires_in ${JSON.stringify(body)}`,
+ );
+ }
+ return {
+ ...body,
+ interval,
+ expires_in: expiresIn,
+ } as DeviceCodeResponse;
+}
+
+function toFiniteNumber(v: unknown): number | undefined {
+ if (typeof v === "number" && Number.isFinite(v)) {
+ return v;
+ }
+ if (typeof v === "string" && v.trim() !== "") {
+ const n = Number(v);
+ if (Number.isFinite(n)) {
+ return n;
+ }
+ }
+ return undefined;
}
/**
diff --git a/apps/mcp/src/install/index.ts b/apps/mcp/src/install/index.ts
index f6914e6f0..27d8385a2 100644
--- a/apps/mcp/src/install/index.ts
+++ b/apps/mcp/src/install/index.ts
@@ -237,15 +237,26 @@ export async function runInstall(argv: string[]): Promise<void> {
let existingText: string | null = null;
try {
existingText = await readFile(configPath, "utf8");
- } catch {
- existingText = null;
+ } catch (err: unknown) {
+ const code =
+ err &&
+ typeof err === "object" &&
+ "code" in err &&
+ typeof (err as NodeJS.ErrnoException).code === "string"
+ ? (err as NodeJS.ErrnoException).code
+ : undefined;
+ if (code === "ENOENT") {
+ existingText = null;
+ } else {
+ throw err;
+ }
}
const already = hasExistingServerEntry(existingText, parsed.name);
if (already && !parsed.yes) {
if (input.isTTY && output.isTTY) {
- const ok = await promptConfirmOverwrite(parsed.name);
+ const ok = await promptConfirmOverwrite(parsed.name, configPath);
if (!ok) {
console.log(`Skipped:\n ${configPath}`);
continue;
diff --git a/apps/mcp/src/install/merge-config.ts b/apps/mcp/src/install/merge-config.ts
index 34001f321..3cd3e7e59 100644
--- a/apps/mcp/src/install/merge-config.ts
+++ b/apps/mcp/src/install/merge-config.ts
@@ -4,6 +4,16 @@ export type McpServerEntry = {
env?: Record<string, string>;
};
+const RESERVED_MCP_SERVER_KEYS = new Set([
+ "__proto__",
+ "constructor",
+ "prototype",
+]);
+
+function isPlainObject(v: unknown): v is Record<string, unknown> {
+ return typeof v === "object" && v !== null && !Array.isArray(v);
+}
+
/**
* Merges or replaces `mcpServers[serverName]` and returns formatted JSON.
* Preserves other top-level keys and other MCP server entries.
@@ -13,30 +23,40 @@ export function mergeMcpServerEntry(
serverName: string,
serverConfig: McpServerEntry,
): string {
+ if (RESERVED_MCP_SERVER_KEYS.has(serverName)) {
+ throw new Error(
+ `Refusing MCP server name "${serverName}" (reserved key; use a different --name).`,
+ );
+ }
+
let root: Record<string, unknown> = {};
if (existingJson) {
+ let parsed: unknown;
try {
- const parsed: unknown = JSON.parse(existingJson);
- if (
- typeof parsed === "object" &&
- parsed !== null &&
- !Array.isArray(parsed)
- ) {
- root = { ...(parsed as Record<string, unknown>) };
- }
+ parsed = JSON.parse(existingJson);
} catch {
- console.warn(
- "[kaneo-mcp] Existing MCP config is not valid JSON; overwriting with a fresh object.",
+ throw new Error("Existing MCP config is not valid JSON.");
+ }
+ if (!isPlainObject(parsed)) {
+ throw new Error(
+ "Existing MCP config must be a JSON object (not an array or primitive).",
);
}
+ root = { ...parsed };
}
const mcpServers = (() => {
+ const map = Object.create(null) as Record<string, unknown>;
const raw = root.mcpServers;
if (typeof raw === "object" && raw !== null && !Array.isArray(raw)) {
- return { ...(raw as Record<string, unknown>) };
+ for (const key of Object.keys(raw as object)) {
+ if (RESERVED_MCP_SERVER_KEYS.has(key)) {
+ continue;
+ }
+ map[key] = (raw as Record<string, unknown>)[key];
+ }
}
- return {};
+ return map;
})();
mcpServers[serverName] = serverConfig;
diff --git a/apps/mcp/src/install/wizard.ts b/apps/mcp/src/install/wizard.ts
index 2ce1c1b5a..c2bd182e4 100644
--- a/apps/mcp/src/install/wizard.ts
+++ b/apps/mcp/src/install/wizard.ts
@@ -71,12 +71,13 @@ export async function promptCustomConfigPath(): Promise<string> {
export async function promptConfirmOverwrite(
serverName: string,
+ configPath: string,
): Promise<boolean> {
const answer = await prompts(
{
type: "confirm",
name: "overwrite",
- message: `MCP server "${serverName}" is already in this file. Overwrite it?`,
+ message: `MCP server "${serverName}" is already in this file (${configPath}). Overwrite it?`,
initial: false,
},
{ onCancel },