Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/cli/commands/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* windsurf Windsurf (~/.codeium/windsurf/mcp_config.json)
*/

import { readFile, writeFile, mkdir, rename, copyFile } from "node:fs/promises"
import { readFile, writeFile, mkdir, rename, copyFile, lstat } from "node:fs/promises"
import { existsSync } from "node:fs"
import { resolve, dirname } from "node:path"
import { homedir, platform } from "node:os"
Expand Down Expand Up @@ -101,6 +101,7 @@ function isToolDetected(tool: ToolDef): boolean {

export async function readJsonConfig(path: string): Promise<Record<string, unknown>> {
if (!existsSync(path)) return {}
await assertSafeConfigPath(path)
const raw = await readFile(path, "utf-8")
const normalized = raw.replace(/^\uFEFF/, "").trim()
if (normalized.length === 0) return {}
Expand All @@ -110,13 +111,24 @@ export async function readJsonConfig(path: string): Promise<Record<string, unkno
export async function writeJsonConfig(path: string, data: Record<string, unknown>): Promise<void> {
await mkdir(dirname(path), { recursive: true })
if (existsSync(path)) {
await assertSafeConfigPath(path)
await copyFile(path, path + ".backup")
}
const tmp = path + ".tmp"
await writeFile(tmp, JSON.stringify(data, null, 2) + "\n", "utf-8")
await rename(tmp, path)
}

async function assertSafeConfigPath(path: string): Promise<void> {
const stats = await lstat(path)
if (stats.isSymbolicLink()) {
throw new Error(`Refusing to use symlinked config path: ${path}`)
}
if (!stats.isFile()) {
throw new Error(`Config path is not a regular file: ${path}`)
}
}


function hasYouMdInstalled(config: Record<string, unknown>, tool: ToolDef): boolean {
let cur: unknown = config
Expand Down
20 changes: 19 additions & 1 deletion test/cli/skill.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { join } from "node:path";
import { mkdirSync, rmSync, writeFileSync, readFileSync, existsSync } from "node:fs";
import { mkdirSync, rmSync, writeFileSync, readFileSync, existsSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { readJsonConfig, writeJsonConfig } from "../../src/cli/commands/skill";

Expand Down Expand Up @@ -52,6 +52,15 @@ describe("readJsonConfig", () => {
const result = await readJsonConfig(filePath);
expect(result).toEqual(data);
});

it("rejects symlinked config files", async () => {
const targetPath = join(tempDir, "target.json");
const linkPath = join(tempDir, "link.json");
writeFileSync(targetPath, JSON.stringify({ ok: true }), "utf-8");
symlinkSync(targetPath, linkPath);

await expect(readJsonConfig(linkPath)).rejects.toThrow(/symlinked config path/);
});
});

describe("writeJsonConfig", () => {
Expand Down Expand Up @@ -105,4 +114,13 @@ describe("writeJsonConfig", () => {
const content = readFileSync(filePath, "utf-8");
expect(content).toBe(JSON.stringify(data, null, 2) + "\n");
});

it("rejects writing to symlinked config files", async () => {
const targetPath = join(tempDir, "target-write.json");
const linkPath = join(tempDir, "link-write.json");
writeFileSync(targetPath, JSON.stringify({ existing: true }), "utf-8");
symlinkSync(targetPath, linkPath);

await expect(writeJsonConfig(linkPath, { updated: true })).rejects.toThrow(/symlinked config path/);
});
});
Loading