Skip to content
Merged
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
55 changes: 35 additions & 20 deletions apps/ade-cli/src/commands/skill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
readAgentSkillFile,
} from "../services/skills/agentSkillCatalogService";
import { CliSkillUsageError, runSkillCommand, runSkillList, runSkillShow } from "./skill";
import { symlinksSupported } from "../test/symlinks";

describe("ade skill (bundled agent skills)", () => {
it("list --json returns entries for known bundled skills", () => {
Expand Down Expand Up @@ -68,32 +69,35 @@ describe("ade skill (bundled agent skills)", () => {

// `skill show <name> <file>` is read-only over ADE MCP, so a skill folder a
// repo controls must not become a way to read, or probe for, other files.
it("reads a skill's own Markdown files and nothing outside its folder", () => {
const skillFileFixture = () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "ade-skill-file-"));
const project = path.join(root, "project");
const skillDir = path.join(project, ".claude", "skills", "guide");
try {
fs.mkdirSync(path.join(skillDir, "references"), { recursive: true });
fs.writeFileSync(path.join(skillDir, "SKILL.md"), "---\nname: guide\ndescription: guide skill\n---\nBody.\n");
fs.writeFileSync(path.join(skillDir, "references", "detail.md"), "# Detail\n");
fs.writeFileSync(path.join(root, "outside.md"), "secret\n");
fs.symlinkSync(path.join(root, "outside.md"), path.join(skillDir, "references", "leak.md"));
const skill = readAgentSkill("guide", { cwd: project, includeUser: false, env: process.env });
expect(skill, "setup precondition: the project skill is found").not.toBeNull();
fs.mkdirSync(path.join(skillDir, "references"), { recursive: true });
fs.writeFileSync(path.join(skillDir, "SKILL.md"), "---\nname: guide\ndescription: guide skill\n---\nBody.\n");
fs.writeFileSync(path.join(skillDir, "references", "detail.md"), "# Detail\n");
fs.writeFileSync(path.join(root, "outside.md"), "secret\n");
const skill = readAgentSkill("guide", { cwd: project, includeUser: false, env: process.env });
expect(skill, "setup precondition: the project skill is found").not.toBeNull();
const refusal = (file: string): string => {
try {
readAgentSkillFile(skill!.entry, file);
} catch (error) {
expect(error).toBeInstanceOf(AgentSkillFileError);
return (error as Error).message;
}
throw new Error(`${file} was read`);
};
return { root, skillDir, entry: skill!.entry, refusal };
};

expect(readAgentSkillFile(skill!.entry, "references/detail.md").content).toBe("# Detail\n");
const refusal = (file: string): string => {
try {
readAgentSkillFile(skill!.entry, file);
} catch (error) {
expect(error).toBeInstanceOf(AgentSkillFileError);
return (error as Error).message;
}
throw new Error(`${file} was read`);
};
it("reads a skill's own Markdown files and refuses paths outside its folder", () => {
const { root, entry, refusal } = skillFileFixture();
try {
expect(readAgentSkillFile(entry, "references/detail.md").content).toBe("# Detail\n");
// An escaping path reads the same whether or not the file exists.
const outside = "is not a Markdown file inside the guide skill";
for (const file of ["../../../../outside.md", "../../../../missing.md", "references/leak.md", path.join(root, "outside.md"), "SKILL"]) {
for (const file of ["../../../../outside.md", "../../../../missing.md", path.join(root, "outside.md"), "SKILL"]) {
expect(refusal(file), file).toContain(outside);
}
expect(refusal("references/missing.md")).toContain("has no file");
Expand All @@ -102,6 +106,17 @@ describe("ade skill (bundled agent skills)", () => {
}
});

// Windows refuses a file symlink without Developer Mode or an elevated token.
it.skipIf(!symlinksSupported)("refuses a link in a skill folder that points outside it", () => {
const { root, skillDir, refusal } = skillFileFixture();
try {
fs.symlinkSync(path.join(root, "outside.md"), path.join(skillDir, "references", "leak.md"));
expect(refusal("references/leak.md")).toContain("is not a Markdown file inside the guide skill");
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

it("show <name> --json returns full content including frontmatter name", () => {
const result = runSkillShow(["ade-browser", "--json"]);
expect(result.exitCode).toBe(0);
Expand Down
3 changes: 2 additions & 1 deletion apps/ade-cli/src/proofPublish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
publishProofToPullRequest,
} from "./proofPublish";
import { resolveStoredProofFile, type PublishableArtifact } from "../../desktop/src/main/services/prs/prBodyProofStaging";
import { symlinksSupported } from "./test/symlinks";

type Run = typeof spawnSync;
type RunResult = { status: number; stdout?: string; stderr?: string };
Expand Down Expand Up @@ -69,7 +70,7 @@ describe("resolveStoredProofFile", () => {
.toBe(fs.realpathSync(path.join(root, ".ade", "artifacts", "pic.png")));
});

it("refuses a symlink inside the store that points outside it", () => {
it.skipIf(!symlinksSupported)("refuses a symlink inside the store that points outside it", () => {
const root = project();
const outside = path.join(root, "secret.png");
fs.writeFileSync(outside, "secret");
Expand Down
11 changes: 6 additions & 5 deletions apps/ade-cli/src/services/cloudChats/cloudLaneBackup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { CloudBackupReport, CloudLaneBackup, CloudLaneBackupFacts } from "../../../../account-directory/src/cloud/contract";
import type { CloudApiClient } from "../../../../desktop/src/shared/cloudApiClient";
import { prepareLaneBranchFromBackup, restoreLaneBackupIfNeeded, runLaneBackups } from "./cloudLaneBackup";
import { symlinksSupported } from "../../test/symlinks";

/**
* A lane's unpushed work lives only in its backup once its computer is
Expand Down Expand Up @@ -153,7 +154,7 @@ afterEach(() => {
});

describe("lane backups", () => {
it("a new computer gets the lane's work back exactly: commits, staged, unstaged, deleted, untracked, binary and symlink; ignored files stay out; nothing reaches GitHub", async () => {
it.skipIf(!symlinksSupported)("a new computer gets the lane's work back exactly: commits, staged, unstaged, deleted, untracked, binary and symlink; ignored files stay out; nothing reaches GitHub", async () => {
const { origin, source } = seedOrigin();
makeUnpushedWork(source);
const originBefore = git(origin, "for-each-ref");
Expand Down Expand Up @@ -193,7 +194,7 @@ describe("lane backups", () => {
expect(server.rows.size).toBe(0);
});

it.each<[string, (target: string) => void]>([
it.skipIf(!symlinksSupported).each<[string, (target: string) => void]>([
["uncommitted work of its own", (target) => write(target, "mine.txt", "newer\n")],
["a commit past the backup's base that the backup lacks", (target) => {
write(target, "other.txt", "other\n");
Expand All @@ -218,7 +219,7 @@ describe("lane backups", () => {
expect(server.rows.get(`key-old/${LANE}`)!.restoredByMachineKey).toBeNull();
});

it("a lane at the backup's commit: one that holds the work is left alone and marked restored; one with other uncommitted work keeps it", async () => {
it.skipIf(!symlinksSupported)("a lane at the backup's commit: one that holds the work is left alone and marked restored; one with other uncommitted work keeps it", async () => {
const { source } = seedOrigin();
makeUnpushedWork(source);
const server = fakeServer();
Expand All @@ -234,7 +235,7 @@ describe("lane backups", () => {
expect(treeOf(source)).toEqual(before);
});

it("a branch never pushed is made on the new computer from the backup, without a push, and its work is put back", async () => {
it.skipIf(!symlinksSupported)("a branch never pushed is made on the new computer from the backup, without a push, and its work is put back", async () => {
const { origin, source } = seedOrigin();
git(source, "checkout", "-q", "-b", "ade/never-pushed", "main");
makeUnpushedWork(source);
Expand Down Expand Up @@ -341,7 +342,7 @@ describe("lane backups", () => {
expect([server.rows.size, server.deleted]).toEqual([1, []]);
});

it("a lane whose folder is missing counts as saved only by its backup", async () => {
it.skipIf(!symlinksSupported)("a lane whose folder is missing counts as saved only by its backup", async () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Ordinary checks are skipped too

On Windows without file-symlink privileges, this guard skips the missing-lane checks even though they do not need a symlink. The link comes only from makeUnpushedWork. Make that fixture's link optional and keep these checks running.

The same applies to the ordinary path checks in skill.test.ts and regular-file cleanup checks in durableFile.test.ts: split them from the symlink cases so they still run.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/ade-cli/src/services/cloudChats/cloudLaneBackup.test.ts
Line: 345

Comment:
**Ordinary checks are skipped too**

On Windows without file-symlink privileges, this guard skips the missing-lane checks even though they do not need a symlink. The link comes only from `makeUnpushedWork`. Make that fixture's link optional and keep these checks running.

The same applies to the ordinary path checks in `skill.test.ts` and regular-file cleanup checks in `durableFile.test.ts`: split them from the symlink cases so they still run.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code

const { source } = seedOrigin();
makeUnpushedWork(source);
const missing = () => runLaneBackups({ api: fakeServer().api, instanceId: "box", machineKey: "key-old", logger, lanes: async () => ({ complete: true, lanes: [], missing: [LANE] }), mayGoOn: () => true });
Expand Down
29 changes: 29 additions & 0 deletions apps/ade-cli/src/test/symlinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

/**
* Whether this host can create a file symlink. Windows needs Developer Mode or
* an elevated token (SeCreateSymbolicLinkPrivilege); without it `fs.symlinkSync`
* throws EPERM. Tests that plant a symlink guard on this instead of failing
* before their first assertion. The probe is honest rather than a platform
* check so an elevated Windows CI runner still runs those tests.
*/
export const symlinksSupported = process.platform !== "win32" ? true : probeSymlinks();

function probeSymlinks(): boolean {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "ade-symlink-probe-"));
try {
fs.writeFileSync(path.join(root, "target"), "");
fs.symlinkSync(path.join(root, "target"), path.join(root, "link"));
return true;
} catch {
return false;
} finally {
try {
fs.rmSync(root, { recursive: true, force: true });
} catch {
// Best effort: the setup file's temp tracker also cleans this up.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { resolveWithinAllowedDirs } from "../ipc/allowedRendererPath";
import { symlinksSupported } from "../../../test/symlinks";
import { providerContextRevealRoots, resolveProviderContextRevealTarget } from "./providerContextReveal";

let tempRoot: string;
Expand Down Expand Up @@ -70,7 +71,7 @@ describe("providerContextRevealRoots", () => {
expect(() => resolveWithinAllowedDirs(roots(), file)).toThrow(/outside allowed directories/);
});

it("refuses a link inside an allowed folder whose target is outside every root", () => {
it.skipIf(!symlinksSupported)("refuses a link inside an allowed folder whose target is outside every root", () => {
const secret = write(path.join(home, ".ssh", "id_ed25519"));
const link = path.join(home, ".claude", "skills", "leak");
fs.mkdirSync(path.dirname(link), { recursive: true });
Expand All @@ -85,15 +86,16 @@ describe("resolveProviderContextRevealTarget", () => {
const dotfiles = write(path.join(tempRoot, "dotfiles", "lint", "SKILL.md"));
const link = path.join(home, ".claude", "skills", "lint");
fs.mkdirSync(path.dirname(link), { recursive: true });
fs.symlinkSync(path.dirname(dotfiles), link);
// A directory link works on Windows without Developer Mode as a junction.
fs.symlinkSync(path.resolve(path.dirname(dotfiles)), link, process.platform === "win32" ? "junction" : "dir");
expect(() => resolveWithinAllowedDirs(roots(), link)).toThrow(/outside allowed directories/);
expect(resolveProviderContextRevealTarget(roots(), link)).toBe(link);
});

it("refuses a path that passes through a symlinked folder", () => {
write(path.join(home, ".ssh", "id_ed25519"));
const linkedDir = path.join(project, "keys");
fs.symlinkSync(path.join(home, ".ssh"), linkedDir);
fs.symlinkSync(path.resolve(home, ".ssh"), linkedDir, process.platform === "win32" ? "junction" : "dir");
const through = path.join(linkedDir, "id_ed25519");
expect(fs.realpathSync(through)).toBe(path.join(home, ".ssh", "id_ed25519"));
expect(() => resolveProviderContextRevealTarget(roots(), through)).toThrow(/outside allowed directories/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { symlinksSupported } from "../../../test/symlinks";
import type { BuiltInBrowserNetworkLogEntry } from "../../../shared/types";
import {
resolveBuiltInBrowserEmulation,
Expand Down Expand Up @@ -296,7 +297,7 @@ describe("built-in browser upload roots", () => {

// Regression: a symlink an agent can write inside the project root used to
// pass the lexical containment check and upload its target to the page.
it("rejects a symlink inside an allowed root that points outside it", () => {
it.skipIf(!symlinksSupported)("rejects a symlink inside an allowed root that points outside it", () => {
const link = path.join(projectRoot, ".ade", "tmp", "report.txt");
fs.symlinkSync(path.join(sandbox, "secrets", "id_ed25519"), link);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";

import { resolveAdeLayout } from "../../../shared/adeLayout";
import { symlinksSupported } from "../../../test/symlinks";
import { resolveSceneStillOwner, SceneStillScopeKeyError } from "./sceneStills";
import { ingestSceneSnapshot } from "./sceneSnapshotIngest";

Expand Down Expand Up @@ -104,7 +105,7 @@ describe("ingesting a scene snapshot", () => {
* store passes string-only containment, and the ingest below it follows the
* link — so the jail and the read disagreed about which file they meant.
*/
it("refuses a symlink inside the store that points out of it", async () => {
it.skipIf(!symlinksSupported)("refuses a symlink inside the store that points out of it", async () => {
const { projectRoot, artifactsRoot, filed, broker } = createFixture();
const elsewhere = fs.mkdtempSync(path.join(os.tmpdir(), "ade-scene-link-"));
const secret = writePng(elsewhere, "secret.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe("pruneAgentObservationCacheRoot", () => {
const root = scratchDir();
const outside = scratchDir();
writeObservation(outside, "precious", 60 * 60_000);
fs.symlinkSync(outside, path.join(root, "session-link"));
// A directory link works on Windows without Developer Mode as a junction.
fs.symlinkSync(path.resolve(outside), path.join(root, "session-link"), process.platform === "win32" ? "junction" : "dir");

await pruneAgentObservationCacheRoot(root, 30 * 60_000);

Expand Down
17 changes: 12 additions & 5 deletions apps/desktop/src/main/services/state/durableFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "node:path";
import { isMainThread } from "node:worker_threads";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { injectFsFault } from "../../../test/faultInjection";
import { symlinksSupported } from "../../../test/symlinks";
import {
cleanupAbandonedTempFiles,
createFileVersionTracker,
Expand Down Expand Up @@ -368,24 +369,30 @@ describe("durableFile", () => {
});

describe("cleanupAbandonedTempFiles", () => {
it("removes only matching old regular files and never follows symlinks", () => {
it("removes only matching old regular files", () => {
const oldTemp = path.join(root, ".state.json.tmp-123-old");
const youngTemp = path.join(root, ".state.json.tmp-123-young");
const nonMatching = path.join(root, "state.json.tmp-123-old");
const outside = path.join(root, "outside.txt");
const symlink = path.join(root, ".state.json.tmp-123-link");
fs.writeFileSync(oldTemp, "old");
fs.writeFileSync(youngTemp, "young");
fs.writeFileSync(nonMatching, "keep");
fs.writeFileSync(outside, "outside");
fs.symlinkSync(outside, symlink);
const old = new Date(Date.now() - 10_000);
fs.utimesSync(oldTemp, old, old);

expect(cleanupAbandonedTempFiles(root, { maxAgeMs: 1_000 })).toBe(1);
expect(fs.existsSync(oldTemp)).toBe(false);
expect(fs.existsSync(youngTemp)).toBe(true);
expect(fs.existsSync(nonMatching)).toBe(true);
});

// Windows refuses a file symlink without Developer Mode or an elevated token.
it.skipIf(!symlinksSupported)("never follows a symlink", () => {
const outside = path.join(root, "outside.txt");
const symlink = path.join(root, ".state.json.tmp-123-link");
fs.writeFileSync(outside, "outside");
fs.symlinkSync(outside, symlink);

expect(cleanupAbandonedTempFiles(root, { maxAgeMs: 1_000 })).toBe(0);
expect(fs.lstatSync(symlink).isSymbolicLink()).toBe(true);
expect(fs.readFileSync(outside, "utf8")).toBe("outside");
});
Expand Down
29 changes: 29 additions & 0 deletions apps/desktop/src/test/symlinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

/**
* Whether this host can create a file symlink. Windows needs Developer Mode or
* an elevated token (SeCreateSymbolicLinkPrivilege); without it `fs.symlinkSync`
* throws EPERM. Tests that plant a symlink guard on this instead of failing
* before their first assertion. The probe is honest rather than a platform
* check so an elevated Windows CI runner still runs those tests.
*/
export const symlinksSupported = process.platform !== "win32" ? true : probeSymlinks();

function probeSymlinks(): boolean {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "ade-symlink-probe-"));
try {
fs.writeFileSync(path.join(root, "target"), "");
fs.symlinkSync(path.join(root, "target"), path.join(root, "link"));
return true;
} catch {
return false;
} finally {
try {
fs.rmSync(root, { recursive: true, force: true });
} catch {
// Best effort: the setup file's temp tracker also cleans this up.
}
}
}
Loading