/** * ============================================================ * src/plugins/pluginManifest.ts —— `.claude-plugin/plugin.json` 的唯一解析处 * ------------------------------------------------------------ * **中立叶子模块**:只做「读一份 manifest 并校验」,不发现插件、不路由命令、 * 不加载执行器。因此谁都可以依赖它而不会顺带拖进插件框架的其余部分 —— * `commandRouter`(命令发现)与 `vision/appGuide`(说明书发现)就是两个 * 互不相干的消费方。 * * ★ 为什么必须只有一处解析:manifest 字段一旦有两份读法就会漂移。本文件出现 * 之前,`appGuide` 打算自己 `existsSync(/app)` 判断说明书目录,而 * `commandRouter` 另有一套 JSON 读取 —— 同一份文件、两种理解,是那种「改了 * 一边、另一边悄悄不同步」的经典种子。 * * ★ 路径字段一律经 `resolveInsidePlugin` 收敛。manifest 是**插件作者写的数据**, * 不是可信输入:`"appManuals": "../../../etc"` 或一个指向 `/` 的软链都必须 * 被挡住。宿主读说明书是无条件发生的(不需要用户敲命令),所以这条边界不能 * 指望调用方自觉。 * ============================================================ */ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'; import { isAbsolute, join, relative, resolve, sep } from 'node:path'; /** 解析并校验后的 manifest。字段缺失一律 undefined,不造默认值。 */ export interface PluginManifestFile { /** 插件目录绝对路径 */ root: string; /** 声明的名字;缺省由调用方回落到目录名 */ name?: string; version?: string; description?: string; /** * 该插件提供的**应用说明书**目录(绝对路径,已做安全收敛且确认存在)。 * * manifest 里写相对路径,可以是单个字符串或数组: * `"appManuals": "app"` / `"appManuals": ["app", "app-extra"]` * * 这是**显式 opt-in**:没声明就不提供说明书。在此之前是纯目录约定 * (任何插件目录下的 `app` 子目录自动生效),等于「任意目录自动影响可信内核」。 */ appManualDirs: string[]; /** 校验中发现的问题(作者可见),不影响其余字段可用。 */ issues: string[]; } const MANIFEST_RELATIVE = join('.claude-plugin', 'plugin.json'); const asString = (value: unknown): string | undefined => typeof value === 'string' && value.trim() ? value.trim() : undefined; /** * 把 manifest 里的相对路径收敛到插件目录内部。 * * 三道闸门,任一不过即拒: * ① 不接受绝对路径 —— manifest 描述的是「我这个插件里的哪个子目录」 * ② 不接受任何 `..` 段 —— 在拼接之前就挡掉,不依赖后续 resolve 的结果 * ③ `realpath` 之后仍须在插件目录内 —— 挡住软链逃逸(`app -> /etc`) * * 返回 null 表示拒绝;理由由调用方写进 `issues`。 */ export function resolveInsidePlugin(pluginRoot: string, candidate: string): string | null { const raw = candidate.trim(); if (!raw || isAbsolute(raw)) return null; if (raw.split(/[\\/]/).some((segment) => segment === '..')) return null; const target = resolve(pluginRoot, raw); const rootReal = realpathQuiet(pluginRoot); const targetReal = realpathQuiet(target); if (!rootReal || !targetReal) return null; const inside = relative(rootReal, targetReal); if (inside === '' || inside.startsWith('..') || isAbsolute(inside)) return null; // Windows 上 relative 可能返回带盘符的路径;上面 isAbsolute 已挡住,这里只是 // 再确认分隔符没被绕过。 if (inside.split(sep).some((segment) => segment === '..')) return null; return targetReal; } function realpathQuiet(path: string): string | null { try { return realpathSync(path); } catch { return null; } } /** * 读一个插件目录的 manifest。 * * 文件不存在返回 null(那不是错误:声明式插件可以只有 `commands/`)。 * JSON 坏了返回带 issue 的空 manifest —— 「这份 manifest 写坏了」和「没有 * manifest」是两件事,前者要能被看见。 */ export function readPluginManifest(pluginRoot: string): PluginManifestFile | null { const manifestPath = join(pluginRoot, MANIFEST_RELATIVE); if (!existsSync(manifestPath)) return null; const issues: string[] = []; let parsed: Record = {}; try { const raw = JSON.parse(readFileSync(manifestPath, 'utf8')) as unknown; if (raw && typeof raw === 'object' && !Array.isArray(raw)) { parsed = raw as Record; } else { issues.push('plugin.json 顶层不是对象,已按空 manifest 处理'); } } catch (error) { issues.push(`plugin.json 解析失败:${error instanceof Error ? error.message : String(error)}`); } const appManualDirs: string[] = []; const declared = parsed.appManuals; const candidates = Array.isArray(declared) ? declared : declared === undefined ? [] : [declared]; for (const candidate of candidates) { const text = asString(candidate); if (!text) { issues.push('appManuals 只接受非空字符串或字符串数组'); continue; } const resolved = resolveInsidePlugin(pluginRoot, text); if (!resolved) { issues.push(`appManuals "${text}" 越出插件目录或不存在,已忽略`); continue; } if (!isDirectory(resolved)) { issues.push(`appManuals "${text}" 不是目录,已忽略`); continue; } if (!appManualDirs.includes(resolved)) appManualDirs.push(resolved); } return { root: pluginRoot, ...(asString(parsed.name) ? { name: asString(parsed.name)! } : {}), ...(asString(parsed.version) ? { version: asString(parsed.version)! } : {}), ...(asString(parsed.description) ? { description: asString(parsed.description)! } : {}), appManualDirs, issues, }; } function isDirectory(path: string): boolean { try { return statSync(path).isDirectory(); } catch { return false; } }