diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d1a545..4c2ee673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- A skill containing a directory symlink loop is listed with a problem instead of failing the skills list. + ## [0.8.19] - 2026-12-04 ### Added diff --git a/server/ui/skills.test.ts b/server/ui/skills.test.ts index 9c9c1705..746c0a02 100644 --- a/server/ui/skills.test.ts +++ b/server/ui/skills.test.ts @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; @@ -103,6 +103,65 @@ describe("skillsCore local library", () => { }); }); +// Symlinks need extra rights on Windows, so these run on macOS and Linux only. +describe.skipIf(process.platform === "win32")("skillsCore symlink loops", () => { + function homeWithLoop(link: string, target: string) { + const home = makeTmp(); + const canon = path.join(home, ".agents", "skills"); + writeSkill(path.join(canon, "good"), "good"); + const loop = writeSkill(path.join(canon, "loop"), "loop"); + mkdirSync(path.dirname(path.join(loop, link)), { recursive: true }); + symlinkSync(target, path.join(loop, link), "dir"); + return { env: { home, env: {} }, loop }; + } + + it.each([ + ["a link to the skill folder itself", "a", "."], + ["a link to its own parent folder", "sub/up", ".."], + ])("lists the other skills and reports %s as a problem", (_label, link, target) => { + const { env } = homeWithLoop(link, target); + const snap = discoverLocalSkills(env); + expect(snap.skills.map((s) => s.slug).sort()).toEqual(["good", "loop"]); + expect(snap.skills.find((s) => s.slug === "good")?.problems).toEqual([]); + expect(snap.skills.find((s) => s.slug === "loop")?.problems).toEqual([`symlink loop "${link}"`]); + expect(inspectLocalSkill("loop", env)?.files.map((f) => f.path)).toEqual(["SKILL.md"]); + expect(() => packLocalSkill("loop", env)).toThrow(/symlink loop/); + }); + + it("follows a long chain of folder links that never loops", () => { + const { env, loop } = homeWithLoop("d0/next", "../d1"); + for (let i = 1; i <= 40; i++) mkdirSync(path.join(loop, `d${i}`)); + for (let i = 1; i < 40; i++) symlinkSync(`../d${i + 1}`, path.join(loop, `d${i}`, "next"), "dir"); + writeFileSync(path.join(loop, "d40", "end.md"), "end\n"); + const skill = discoverLocalSkills(env).skills.find((s) => s.slug === "loop"); + expect(skill?.problems).toEqual([]); + const files = inspectLocalSkill("loop", env)?.files.map((f) => f.path) ?? []; + expect(files).toContain(`d0/${"next/".repeat(40)}end.md`); + }); + + it("catches a loop whose link spells the folder in another case", (ctx) => { + const { env, loop } = homeWithLoop("sub/x", "../SUB"); + writeFileSync(path.join(loop, "sub", "f.md"), "f\n"); + if (!existsSync(path.join(loop, "SUB"))) ctx.skip(); // case-sensitive disk: the link is just broken + expect(discoverLocalSkills(env).skills.find((s) => s.slug === "loop")?.problems).toEqual([`symlink loop "sub/x"`]); + expect(inspectLocalSkill("loop", env)?.files.map((f) => f.path)).toEqual(["SKILL.md", "sub/f.md"]); + }); + + it("still follows a link to a sibling folder inside the skill", () => { + const { env, loop } = homeWithLoop("alias", "refs"); + mkdirSync(path.join(loop, "refs")); + writeFileSync(path.join(loop, "refs", "note.md"), "note\n"); + const snap = discoverLocalSkills(env); + const skill = snap.skills.find((s) => s.slug === "loop"); + expect(skill?.problems).toEqual([]); + expect(inspectLocalSkill("loop", env)?.files.map((f) => f.path)).toEqual([ + "alias/note.md", + "refs/note.md", + "SKILL.md", + ]); + }); +}); + describe("registerSkills HTTP routes", () => { async function invoke( router: Router, diff --git a/server/ui/skillsCore.ts b/server/ui/skillsCore.ts index 018146a5..f5ba231f 100644 --- a/server/ui/skillsCore.ts +++ b/server/ui/skillsCore.ts @@ -167,6 +167,16 @@ function safeRealpath(p: string): string | null { } } +// The OS's own spelling of a real path (case as stored on disk), for comparing folders. +// Falls back to the plain real path where the native call fails (some Windows virtual drives). +function folderKey(realPath: string): string { + try { + return realpathSync.native(realPath); + } catch { + return realPath; + } +} + function readSymlinkTarget(p: string): string | null { try { if (!lstatSync(p).isSymbolicLink()) return null; @@ -284,11 +294,16 @@ function scanSkillDir( files.push({ path: relPath, bytes, size: bytes.length, executable: (mode & 0o111) !== 0 }); }; - const walk = (absDir: string, prefix: string): void => { - const entries = readdirSync(absDir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name, "en")); + // The walk reads each folder through its real path, so a chain of folder links never + // builds a path the OS refuses (ELOOP). `onPath` holds the folders on the current descent, + // keyed by the OS's own spelling (`folderKey`, so `../SUB` and `../sub` match on + // a case-insensitive disk); a link back to one of them (`a -> .`, `sub/up -> ..`) is + // reported instead of followed. + const walk = (realDir: string, key: string, prefix: string, onPath: ReadonlySet): void => { + const entries = readdirSync(realDir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name, "en")); for (const e of entries) { if (IGNORED_NAMES.has(e.name)) continue; - const absPath = path.join(absDir, e.name); + const absPath = path.join(realDir, e.name); const relPath = prefix ? `${prefix}/${e.name}` : e.name; if (!isSafeRelPath(relPath)) { problems.push(`unsafe file path "${relPath}"`); @@ -306,16 +321,22 @@ function scanSkillDir( continue; } const st = statSync(targetReal); - if (st.isDirectory()) walk(absPath, relPath); - else addFile(targetReal, relPath, st.mode); + if (st.isDirectory()) { + const targetKey = folderKey(targetReal); + if (onPath.has(targetKey)) problems.push(`symlink loop "${relPath}"`); + else walk(targetReal, targetKey, relPath, new Set([...onPath, targetKey])); + } else addFile(targetReal, relPath, st.mode); continue; } - if (lst.isDirectory()) walk(absPath, relPath); - else if (lst.isFile()) addFile(absPath, relPath, lst.mode); + if (lst.isDirectory()) { + const childKey = path.join(key, e.name); + walk(absPath, childKey, relPath, new Set([...onPath, childKey])); + } else if (lst.isFile()) addFile(absPath, relPath, lst.mode); else problems.push(`unsupported file type "${relPath}"`); } }; - walk(rootReal, ""); + const rootKey = folderKey(rootReal); + walk(rootReal, rootKey, "", new Set([rootKey])); const skillMdFile = files.find((f) => f.path === "SKILL.md") ?? files.find((f) => f.path === "skill.md"); if (!skillMdFile) {