// src/addons/resolve.ts // Which add-on an agent's tool call uses, for the approved list // (node9Firewall#376 item 6, phase 3). The agent hook calls this; the MCP // gateway knows its own server and does not. // // mcp____ the server entries of that name in the agents' // configs, by identity, from the index the inventory // writes (index-file.ts). Servers that run through the // node9 gateway are left to it (it sees every call and // holds one review per session). A name in no config // (a claude.ai connector, a plugin's server, a tool // the agent's app adds) is stopped only when the // workspace blocked it: the rule for add-ons nobody // approved does not apply, so an agent's own tools // never break (mcp-seen.ts). // Skill { skill } Claude Code loads a skill through this tool call. The // skill's folder is found by name and hashed now, so a // copy that changed after the last report is judged as // it is. A name with no folder is built into the agent // or the app, not an add-on. // // Read-only and bounded; any failure yields "nothing to judge", never a throw: // node9 failing must never stop the agent. import fs from 'fs'; import os from 'os'; import path from 'path'; import { collectFileAccesses, normalizeFilePath } from '@node9/policy-engine'; import { hashSkillRoot } from '../skill-pin'; import { readMcpIndex } from './index-file'; import { claudeMcpEntries } from './claude-mcp'; import { agentIdOf } from './agent-ids'; import { addonPolicyEnforces, mcpVerdict, seenMcpVerdict, skillVerdict, stricter, maliciousVerdict, type AddonPolicy, type AddonVerdict, } from './policy'; import { anyMalicious, maliciousAdvisories } from './standing-file'; const ALLOW: AddonVerdict = { action: 'allow' }; /** How an agent spells a config's server name inside `mcp____`. */ export function toolServerName(configName: string): string { return configName.replace(/[^A-Za-z0-9_-]/g, '_'); } /** A server the hook must judge: its identity, and whether a config names * it. `seen`: no config does (mcp-seen.ts), so only an explicit block holds. */ export interface McpHookTarget { identity: string; seen: boolean; } /** * The servers named `server` that the hook must judge: in the calling * agent's config when it has one by that name, else in any. None when every * such entry runs through the gateway. Claude Code's own config files are * read now (claude-mcp.ts), so a server renamed or added since the last * report is still judged; other agents come from the inventory index. * * A name no config holds is judged as a seen server: by the identity the * index gives it (a connector's `url:`), and always by its own name * (`name:`), which needs no index and so holds from the first call. */ export function mcpTargetsForHook( server: string, agentLabel: string | undefined, home: string = os.homedir(), cwd?: string ): McpHookTarget[] { const id = agentIdOf(agentLabel); const entries = [ ...(id === 'claude' || id === undefined ? claudeMcpEntries(cwd, home) : []), ...readMcpIndex(home), ]; const named = entries.filter((e) => toolServerName(e.name) === server); // A config row always wins: the server is then judged by what it runs. const configured = named.filter((e) => e.state !== 'seen'); const out = new Map(); if (configured.length > 0) { const own = id ? configured.filter((e) => e.agents.includes(id)) : []; for (const e of own.length ? own : configured) if (e.state !== 'governed') out.set(e.identity, { identity: e.identity, seen: false }); return [...out.values()]; } for (const e of named) out.set(e.identity, { identity: e.identity, seen: true }); out.set(`name:${server}`, { identity: `name:${server}`, seen: true }); return [...out.values()]; } export interface SkillUse { name: string; hash: string; plugin?: { key: string; versionId: string }; } type SkillFs = typeof import('./skill-fs.js'); /** The skills under `root` an agent may call `name`: by front matter name or * by folder name. Each keeps the name the add-ons report gives it, which is * the name the approved list files it under. */ function skillUsesNamed( sfs: SkillFs, root: string, name: string, plugin?: SkillUse['plugin'] ): SkillUse[] { const out: SkillUse[] = []; for (const dir of sfs.findSkillDirs(root)) { const reported = sfs.skillNameOf(dir); if (!reported || (reported !== name && path.basename(dir) !== name)) continue; const h = hashSkillRoot(dir); if (h.exists) out.push({ name: reported, hash: h.contentHash, ...(plugin ? { plugin } : {}) }); } return out; } /** The project folders Claude Code reads `.claude/skills` from: the working * directory and its parents, below the home folder. */ function projectDirs(cwd: string | undefined, home: string): string[] { if (!cwd || !path.isAbsolute(cwd)) return []; const out: string[] = []; let dir = path.resolve(cwd); for (let i = 0; i < 32; i++) { if (dir === home) break; out.push(dir); const up = path.dirname(dir); if (up === dir) break; dir = up; } return out; } /** * The skill folders a Claude Code `Skill` call may load, with their hashes. * `plugin:skill` is a plugin's skill (or a synced one, under the user skills * folder); a bare name is a project or user skill. Every folder that answers * to the name is returned, so the stricter verdict wins when two do. */ export async function resolveSkillUses( skillArg: string, cwd: string | undefined, home: string = os.homedir() ): Promise { // Loaded here, not at the top: the front matter parser and its YAML library // stay out of every other hook call. const sfs = await import('./skill-fs.js'); const arg = skillArg.trim().replace(/^\//, ''); if (!arg) return []; const userRoot = path.join(home, '.claude', 'skills'); const out: SkillUse[] = []; const colon = arg.indexOf(':'); if (colon > 0) { const prefix = arg.slice(0, colon); const name = arg.slice(colon + 1); const plugins = sfs.installedPlugins(home).filter((p) => p.name === prefix); for (const p of plugins) { if (!p.installPath) continue; const h = hashSkillRoot(p.installPath); const plugin = { key: `${p.name}@${p.marketplace}`, // The version id the add-ons report gives the plugin row. versionId: h.exists ? h.contentHash : `v:${p.version ?? '?'}`, }; out.push(...skillUsesNamed(sfs, path.join(p.installPath, 'skills'), name, plugin)); } if (plugins.length === 0) out.push(...skillUsesNamed(sfs, userRoot, name)); return out; } for (const dir of projectDirs(cwd, home)) out.push(...skillUsesNamed(sfs, path.join(dir, '.claude', 'skills'), arg)); out.push(...skillUsesNamed(sfs, userRoot, arg)); return out; } /** `p` is strictly below `root`. path.relative compares with the platform's * rules (case-insensitive on Windows, either separator). */ function inside(p: string, root: string): boolean { const rel = path.relative(root, p); return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel); } // A Windows path: drive letter (C:\, C:/) or UNC (\\server\share). const WIN_PATH = /^(?:[A-Za-z]:[\\/]|\\\\)/; /** * The absolute path a read names. The policy engine's normalizer is POSIX * only: it glues the working directory in front of `C:\...` and its shell * parser drops the backslashes of an unquoted Windows path. On Windows a * Windows-shaped path is resolved with Windows rules instead. */ function readTarget(p: string, cwd: string, home: string): string { if (process.platform === 'win32' && (WIN_PATH.test(p) || WIN_PATH.test(cwd))) { const expanded = p === '~' || /^~[\\/]/.test(p) ? home + p.slice(1) : p; return path.resolve(cwd, expanded); } return normalizeFilePath(p, cwd, home); } // SKILL.md paths written into a command, for Windows only, where the shell // is not bash and a parse as bash loses the backslashes. Quoted or bare, // either separator; anything found must still sit in a skill folder below. const WIN_SKILL_IN_COMMAND = /(?:"([^"]*?skill\.md)"|'([^']*?skill\.md)'|((?:[A-Za-z]:|\\\\)[^\s"'`|;&<>]*?skill\.md))(?=$|[\s"'`|;&<>)])/gi; function windowsSkillPathsInCommand(toolInput: unknown): string[] { const command = (toolInput as { command?: unknown } | null)?.command; if (typeof command !== 'string') return []; const out: string[] = []; for (const m of command.matchAll(WIN_SKILL_IN_COMMAND)) { const p = m[1] ?? m[2] ?? m[3]; if (p && WIN_PATH.test(p)) out.push(path.resolve(p)); } return out; } function realDir(p: string): string { try { return fs.realpathSync(p); } catch { return p; } } /** * The skills a call loads by reading their SKILL.md directly, for agents with * no skill tool: Codex loads a skill by reading the file through the shell. * Claude Code loads skills through its Skill tool (and a typed slash * command), and its Read of a SKILL.md is how a skill gets edited, so it is * not judged here: under the block rule nobody could edit a skill they are * writing. A SKILL.md counts only inside a folder the agents load skills * from (the user's, a plugin's, the project's), compared after resolving * links; a skill anywhere else (a repository under review) is a file. Codex's * own system skills (`~/.codex/skills/.system`) are built in, not add-ons. */ export async function skillReadUses( toolName: string, toolInput: unknown, cwd: string | undefined, home: string = os.homedir() ): Promise { const base = cwd && path.isAbsolute(cwd) ? cwd : home; const read = collectFileAccesses(toolName, toolInput, { cwd: base, homeDirs: [home] }) .filter((a) => a.operation === 'read' && !a.listing) .flatMap((a) => a.candidates && !(process.platform === 'win32' && WIN_PATH.test(a.path)) ? a.candidates : [readTarget(a.path, a.cwd ?? base, home)] ) .concat(process.platform === 'win32' ? windowsSkillPathsInCommand(toolInput) : []) .filter((p) => path.isAbsolute(p) && path.basename(p).toLowerCase() === 'skill.md'); if (read.length === 0) return []; const sfs = await import('./skill-fs.js'); const roots: Array<{ root: string; plugin?: { name: string; marketplace: string; installPath: string; version?: string }; }> = [ { root: path.join(home, '.claude', 'skills') }, { root: path.join(home, '.codex', 'skills') }, { root: path.join(home, '.agents', 'skills') }, ...projectDirs(cwd, home).flatMap((d) => [ { root: path.join(d, '.claude', 'skills') }, { root: path.join(d, '.agents', 'skills') }, ]), ]; for (const p of sfs.installedPlugins(home)) if (p.installPath) roots.push({ root: path.join(p.installPath, 'skills'), plugin: { name: p.name, marketplace: p.marketplace, installPath: p.installPath, version: p.version, }, }); const realRoots = roots.map((r) => ({ ...r, real: realDir(r.root) })); const builtIn = path.join(home, '.codex', 'skills', '.system'); const builtInReal = realDir(builtIn); const out: SkillUse[] = []; for (const file of new Set(read)) { const dir = path.dirname(file); const real = realDir(dir); const isIn = (p: string, root: string) => p === root || inside(p, root); if (isIn(dir, builtIn) || isIn(real, builtInReal)) continue; const where = realRoots.find((r) => inside(dir, r.root) || inside(real, r.real)); if (!where) continue; const name = sfs.skillNameOf(real); const h = hashSkillRoot(real); if (!name || !h.exists) continue; let plugin: SkillUse['plugin']; if (where.plugin) { // Hashed only for the plugin the file is in. const ph = hashSkillRoot(where.plugin.installPath); plugin = { key: `${where.plugin.name}@${where.plugin.marketplace}`, versionId: ph.exists ? ph.contentHash : `v:${where.plugin.version ?? '?'}`, }; } out.push({ name, hash: h.contentHash, ...(plugin ? { plugin } : {}) }); } return out; } /** * The approved list's verdict on one hook call: an MCP tool by its server's * identity, a Claude Code `Skill` call by the skill's folder, another agent's * read of a skill's SKILL.md by that skill. Allow when the list enforces nothing (no blocks, unapproved off or log), before any file * is read. */ export async function addonVerdictForHookCall( policy: AddonPolicy | undefined, call: { toolName: string; toolInput: unknown; mcpServer?: string; agent?: string; cwd?: string; }, home: string = os.homedir(), /** `policy.packageCheck.onMalicious` when the package check is on: what a * server whose package is on the malicious list gets, list or no list. */ onMalicious?: 'block' | 'review' ): Promise { const listEnforces = addonPolicyEnforces(policy); try { // With no list in force, the only question left is the malicious-package // one, and on almost every machine the answer is "nothing stored": said // from one small file, before any agent config is read (Claude Code's // can be megabytes, and this runs on every MCP call). const checkMalicious = !!onMalicious && !!call.mcpServer && anyMalicious(home); if (!listEnforces && !checkMalicious) return ALLOW; if (call.mcpServer) { let v = ALLOW; for (const t of mcpTargetsForHook(call.mcpServer, call.agent, home, call.cwd)) { if (t.seen) { // No package to look up, and no "nobody approved" rule: a block only. if (listEnforces) v = stricter(v, seenMcpVerdict(policy, t.identity)); continue; } if (listEnforces) v = stricter(v, mcpVerdict(policy, t.identity)); if (checkMalicious && onMalicious) { const advisories = maliciousAdvisories(t.identity, home); if (advisories) v = stricter(v, maliciousVerdict(t.identity, advisories, onMalicious)); } } return v; } if (!listEnforces) return ALLOW; if (call.toolName === 'Skill') { const input = call.toolInput as { skill?: unknown } | null; if (typeof input?.skill !== 'string') return ALLOW; let v = ALLOW; for (const use of await resolveSkillUses(input.skill, call.cwd, home)) v = stricter(v, skillVerdict(policy, use)); return v; } // Claude Code loads skills through the Skill tool (above); see skillReadUses. if (call.agent === 'Claude Code') return ALLOW; let v = ALLOW; for (const use of await skillReadUses(call.toolName, call.toolInput, call.cwd, home)) v = stricter(v, skillVerdict(policy, use)); return v; } catch { /* an unreadable config or folder judges nothing */ } return ALLOW; }