From aa0d11966c00d04a1b35bd071fdb4f4b02868b77 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Thu, 3 Dec 2026 08:14:47 +0700 Subject: [PATCH 1/2] fix(skills): a skill folder that links to itself is listed with a problem, not a failed list Symptom: one skill folder holding a link back to itself (`a -> .`) or to a folder above the link (`sub/up -> ..`) made the whole Skills list fail. GET /api/ui/skills returned an error for every skill instead of listing them, and inspecting the skill failed the same way. Cause: scanSkillDir's walk followed any folder link whose target stays inside the skill root, the root itself included, and read through the growing link path (a/a/a/...). readdirSync threw ELOOP after about 32 levels, and nothing caught it, so discoverLocalSkills threw. A long chain of folder links that never loops hit the same ELOOP. Fix: walk reads each folder through its real path and keeps the folders on the current descent, keyed by realpathSync.native (the plain real path where that call fails) so a link that spells a folder in another case still matches on a case-insensitive disk. A folder link back to one of them is reported as `symlink loop ""` and not followed. That problem starts with "symlink ", so install, pack and adopt refuse the skill the way they already refuse a link that escapes it. Skills without loops list and hash as before. Signed-off-by: Connor Burgess --- server/ui/skills.test.ts | 61 +++++++++++++++++++++++++++++++++++++++- server/ui/skillsCore.ts | 37 ++++++++++++++++++------ 2 files changed, 89 insertions(+), 9 deletions(-) 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) { From 5bada41626ec080b176adee550c73f4fd1a9d145 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 07:19:26 +0700 Subject: [PATCH 2/2] docs: record the fix under Unreleased Signed-off-by: Connor Burgess --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5299e266..ca931e5c 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.18] - 2026-12-04 ### Fixed