Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): withhold .git/node_modules under skill roots in any case (PRD-8700) (#5075)
tim-inkeepc0a22345a8fc
diff --git a/.changeset/skill-root-vcs-dirs-withheld.md b/.changeset/skill-root-vcs-dirs-withheld.mdnew file mode 100644index 000000000..a2ff13b81--- /dev/null+++ b/.changeset/skill-root-vcs-dirs-withheld.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Skills cloned with git no longer expose `.git` or `node_modules`, in any letter case, to the editor, API, watcher or indexers. Skill files reached through a symlink that resolves outside the skill are now refused; keep shared files inside each skill instead.diff --git a/packages/server/src/api-skill-file-composition.test.ts b/packages/server/src/api-skill-file-composition.test.tsindex d4aaa50f4..26a89232f 100644--- a/packages/server/src/api-skill-file-composition.test.ts+++ b/packages/server/src/api-skill-file-composition.test.ts@@ -1,4 +1,12 @@-import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import {+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ rmSync,+ symlinkSync,+ writeFileSync,+} from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { afterAll, afterEach, beforeAll, expect, test, vi } from 'vitest';@@ -184,6 +192,180 @@ test('raw bundle writes preserve text, resolve sibling extensions, and reject bi).toBe(true);});+test('bundle reads withhold VCS, package and OS-artifact entries below the skill root', async () => {+ const name = 'withheld-bundle';+ const skill = await request('/api/skill', 'PUT', {+ name,+ scope: 'project',+ frontmatter: { name, description: 'Withheld entries' },+ body: 'Body.\n',+ });+ const dir = join(root, (skill.path as string).slice(0, -9));+ const token = '[remote "origin"]\n\turl = https://user:[email protected]/r.git\n';+ for (const path of ['.git/config', 'NODE_MODULES/x.md', 'assets/.DS_Store']) {+ mkdirSync(join(dir, path, '..'), { recursive: true });+ writeFileSync(join(dir, path), token);+ }+ writeFileSync(join(dir, 'assets/keep.txt'), 'Keep.\n');++ const got = await request(`/api/skill?name=${name}&scope=project`);+ expect(got.skill.files.map((f: { path: string }) => f.path)).toEqual(['assets/keep.txt']);+ const listed = await request('/api/skills');+ expect(listed.skills.find((s: { name: string }) => s.name === name)?.filePaths).toEqual([+ 'assets/keep.txt',+ ]);+ for (const path of ['.git/config', '.GIT/config', 'NODE_MODULES/x.md', 'assets/.DS_Store']) {+ const response = await rawRequest(+ server.port,+ `/api/skill-file?name=${name}&path=${encodeURIComponent(path)}`,+ );+ expect(`${path}=${response.status}`).toBe(`${path}=404`);+ }+ expect((await request(`/api/skill-file?name=${name}&path=assets/keep.txt`)).text).toBe('Keep.\n');+});++test('bundle writes refuse withheld entries below the skill root', async () => {+ const name = 'withheld-writes';+ const skill = await request('/api/skill', 'PUT', {+ name,+ scope: 'project',+ frontmatter: { name, description: 'Withheld writes' },+ body: 'Body.\n',+ });+ const dir = join(root, (skill.path as string).slice(0, -9));+ mkdirSync(join(dir, '.git'), { recursive: true });+ writeFileSync(join(dir, '.git/config'), 'Original.\n');+ await request('/api/skill-file', 'PUT', {+ name,+ scope: 'project',+ path: 'assets/keep.txt',+ content: 'Keep.\n',+ });+ const json = { 'Content-Type': 'application/json' };+ const attempts = {+ put: await rawRequest(server.port, '/api/skill-file', {+ method: 'PUT',+ headers: json,+ body: JSON.stringify({ name, scope: 'project', path: '.GIT/config', content: 'Evil.\n' }),+ }),+ delete: await rawRequest(+ server.port,+ `/api/skill-file?name=${name}&scope=project&path=.git/config`,+ { method: 'DELETE' },+ ),+ rename: await rawRequest(server.port, '/api/skill-file/rename', {+ method: 'POST',+ headers: json,+ body: JSON.stringify({+ name,+ scope: 'project',+ from: 'assets/keep.txt',+ to: 'node_modules/keep.txt',+ }),+ }),+ };+ const withheldTitle =+ 'Skill file path falls under an entry Open Knowledge withholds from skill bundles (.git, node_modules, OK and editor host dirs, OS artifact files).';+ for (const [verb, response] of Object.entries(attempts)) {+ expect(`${verb}=${response.status} ${JSON.parse(response.body).title}`).toBe(+ `${verb}=400 ${withheldTitle}`,+ );+ }+ expect(readFileSync(join(dir, '.git/config'), 'utf-8')).toBe('Original.\n');+ expect(readFileSync(join(dir, 'assets/keep.txt'), 'utf-8')).toBe('Keep.\n');+});++test('bundle reads and writes refuse paths a symlink resolves into a withheld entry or out of the skill', async () => {+ const name = 'linked-bundle';+ const skill = await request('/api/skill', 'PUT', {+ name,+ scope: 'project',+ frontmatter: { name, description: 'Linked entries' },+ body: 'Body.\n',+ });+ const dir = join(root, (skill.path as string).slice(0, -9));+ const outside = mkdtempSync(join(tmpdir(), 'ok-skill-file-outside-'));+ writeFileSync(join(outside, 'secret.txt'), 'Outside.\n');+ mkdirSync(join(dir, '.git'));+ writeFileSync(join(dir, '.git/config'), 'Original.\n');+ writeFileSync(join(dir, '.git/HEAD'), 'ref: refs/heads/main\n');+ mkdirSync(join(dir, '.git/objects'));+ await request('/api/skill-file', 'PUT', {+ name,+ scope: 'project',+ path: 'assets/keep.txt',+ content: 'Keep.\n',+ });+ symlinkSync('.git', join(dir, 'cfg'), 'dir');+ symlinkSync('.git', join(dir, 'references'), 'dir');+ symlinkSync('.git/config', join(dir, 'leaf'), 'file');+ symlinkSync('.git/config', join(dir, 'notes.mdx'), 'file');+ symlinkSync(outside, join(dir, 'ext'), 'dir');+ symlinkSync('.git/newfile', join(dir, 'dangling'), 'file');+ symlinkSync('.git/objects', join(dir, 'y'), 'dir');+ symlinkSync('y/../info/attributes', join(dir, 'dotdot'), 'file');+ try {+ const reads: Record<string, number> = {};+ for (const path of ['cfg/config', 'leaf', 'notes.md', 'ext/secret.txt']) {+ const response = await rawRequest(+ server.port,+ `/api/skill-file?name=${name}&path=${encodeURIComponent(path)}`,+ );+ reads[path] = response.status;+ }+ const put = (path: string) =>+ rawRequest(server.port, '/api/skill-file', {+ method: 'PUT',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ name, scope: 'project', path, content: 'Evil.\n' }),+ });+ const rename = (from: string, to: string) =>+ rawRequest(server.port, '/api/skill-file/rename', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ name, scope: 'project', from, to }),+ });+ const writes: Record<string, number> = {+ 'put cfg/config': (await put('cfg/config')).status,+ 'put references/config.md': (await put('references/config.md')).status,+ 'put ext/secret.txt': (await put('ext/secret.txt')).status,+ 'put dangling': (await put('dangling')).status,+ 'put dotdot': (await put('dotdot')).status,+ 'rename to cfg/keep.txt': (await rename('assets/keep.txt', 'cfg/keep.txt')).status,+ 'rename cfg/config': (await rename('cfg/config', 'assets/moved.txt')).status,+ 'delete cfg/HEAD': (+ await rawRequest(server.port, `/api/skill-file?name=${name}&scope=project&path=cfg/HEAD`, {+ method: 'DELETE',+ })+ ).status,+ };+ expect({ reads, writes }).toEqual({+ reads: { 'cfg/config': 404, leaf: 404, 'notes.md': 404, 'ext/secret.txt': 400 },+ writes: {+ 'put cfg/config': 400,+ 'put references/config.md': 400,+ 'put ext/secret.txt': 400,+ 'put dangling': 400,+ 'put dotdot': 400,+ 'rename to cfg/keep.txt': 400,+ 'rename cfg/config': 400,+ 'delete cfg/HEAD': 400,+ },+ });+ expect(readFileSync(join(dir, '.git/config'), 'utf-8')).toBe('Original.\n');+ expect(existsSync(join(dir, '.git/HEAD'))).toBe(true);+ expect(existsSync(join(dir, '.git/config.md'))).toBe(false);+ expect(existsSync(join(dir, '.git/keep.txt'))).toBe(false);+ expect(existsSync(join(dir, '.git/newfile'))).toBe(false);+ expect(existsSync(join(dir, '.git/info/attributes'))).toBe(false);+ expect(existsSync(join(dir, 'info/attributes'))).toBe(false);+ expect(readFileSync(join(dir, 'assets/keep.txt'), 'utf-8')).toBe('Keep.\n');+ expect(readFileSync(join(outside, 'secret.txt'), 'utf-8')).toBe('Outside.\n');+ } finally {+ rmSync(outside, { recursive: true, force: true });+ }+});+test('bundle path and size errors leave the existing file unchanged', async () => {const name = 'file-errors';await request('/api/skill', 'PUT', {diff --git a/packages/server/src/content-filter-skill-roots.test.ts b/packages/server/src/content-filter-skill-roots.test.tsindex db02c0099..179ebd64e 100644--- a/packages/server/src/content-filter-skill-roots.test.ts+++ b/packages/server/src/content-filter-skill-roots.test.ts@@ -2,7 +2,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { describe, expect, test } from 'vitest';-import { createContentFilter } from './content-filter.ts';+import { createContentFilter, createContentFilterAsync } from './content-filter.ts';describe('skill-root admission', () => {function withFixture<T>(fn: (contentDir: string) => T): T {@@ -60,3 +60,98 @@ describe('skill-root admission', () => {});});});++describe.each([+ ['sync', createContentFilter],+ ['async', createContentFilterAsync],+] as const)('always-skip bound under skill roots (%s factory)', (_name, factory) => {+ const skillRoots = ['.ok/skills/demo', '.claude/skills/demo'];+ const skippedBelow = [+ '.git',+ '.GIT',+ '.Git',+ 'node_modules',+ 'NODE_MODULES',+ '.ok',+ '.OK',+ '.open-knowledge',+ '.openknowledge',+ '.claude',+ '.cursor',+ '.codex',+ '.agents',+ '.opencode',+ '.pi',+ ];++ async function build(contentDir: string) {+ return factory({+ projectDir: contentDir,+ contentDir,+ inPlaceSkillDirs: new Set(['.claude/skills/demo']),+ });+ }++ test.each(skillRoots)(+ '%s: skill files are admitted, VCS and package dirs are not',+ async (root) => {+ const contentDir = mkdtempSync(join(tmpdir(), 'ok-skill-bound-'));+ try {+ const filter = await build(contentDir);+ expect(filter.isExcluded(`${root}/SKILL.md`)).toBe(false);+ expect(filter.isDirExcluded(root)).toBe(false);+ expect(filter.isDirExcluded(`${root}/scripts`)).toBe(false);+ expect(filter.isExcluded(`${root}/.DS_Store`)).toBe(true);+ expect(filter.isExcluded(`${root}/.ds_store`)).toBe(true);+ expect(filter.isPathIgnored(`${root}/.DS_Store`)).toBe(true);+ expect(filter.isPathIgnored(`${root}/.ds_store`)).toBe(true);+ for (const dir of skippedBelow) {+ expect(`${dir}=${filter.isExcluded(`${root}/${dir}/config.md`)}`).toBe(`${dir}=true`);+ expect(`${dir}=${filter.isPathIgnored(`${root}/${dir}/config.md`)}`).toBe(`${dir}=true`);+ expect(`${dir}=${filter.isDirExcluded(`${root}/${dir}`)}`).toBe(`${dir}=true`);+ expect(`${dir}=${filter.isDirExcluded(`${root}/${dir}/objects`)}`).toBe(`${dir}=true`);+ }+ } finally {+ rmSync(contentDir, { recursive: true, force: true });+ }+ },+ );++ test('case variants of always-skip dirs are withheld at the content root too', async () => {+ const contentDir = mkdtempSync(join(tmpdir(), 'ok-skill-bound-'));+ try {+ const filter = await build(contentDir);+ for (const path of ['.GIT/config.md', 'NODE_MODULES/p/README.md', '.CLAUDE/notes.md']) {+ expect(`${path}=${filter.isExcluded(path)}`).toBe(`${path}=true`);+ expect(`${path}=${filter.isPathIgnored(path)}`).toBe(`${path}=true`);+ }+ for (const dir of ['.GIT', 'other/.Git', 'NODE_MODULES']) {+ expect(`${dir}=${filter.isDirExcluded(dir)}`).toBe(`${dir}=true`);+ }+ expect(filter.isExcluded('library/notes.md')).toBe(false);+ expect(filter.isExcluded('Library/notes.md')).toBe(true);+ } finally {+ rmSync(contentDir, { recursive: true, force: true });+ }+ });++ test('.ok artifacts under any case of an always-skip dir are withheld', async () => {+ const contentDir = mkdtempSync(join(tmpdir(), 'ok-skill-bound-'));+ try {+ const filter = await build(contentDir);+ for (const skip of ['node_modules', 'NODE_MODULES']) {+ const template = `${skip}/p/.ok/templates/x.md`;+ const frontmatter = `${skip}/p/.ok/frontmatter.yml`;+ const synced = filter.isExcluded(frontmatter, { syncScope: { pathBase: 'content' } });+ expect(`${template}=${filter.isExcluded(template)}`).toBe(`${template}=true`);+ expect(`${template}=${filter.isPathIgnored(template)}`).toBe(`${template}=true`);+ expect(`${frontmatter}=${synced}`).toBe(`${frontmatter}=true`);+ for (const dir of [`${skip}/p/.ok`, `${skip}/p/.ok/templates`]) {+ expect(`${dir}=${filter.isDirExcluded(dir)}`).toBe(`${dir}=true`);+ }+ }+ } finally {+ rmSync(contentDir, { recursive: true, force: true });+ }+ });+});diff --git a/packages/server/src/content-filter.test.ts b/packages/server/src/content-filter.test.tsindex 77f3e1630..1e49d968b 100644--- a/packages/server/src/content-filter.test.ts+++ b/packages/server/src/content-filter.test.ts@@ -2030,6 +2030,20 @@ describe('ContentFilter', () => {expect(result.nestedFileCount).toBe(2);});+ test('skips nested ignore files under any case of node_modules', async () => {+ mkdirSync(join(projectDir, 'subdir'));+ writeFileSync(join(projectDir, 'subdir', '.okignore'), 'private.md\n');+ mkdirSync(join(projectDir, 'NODE_MODULES', 'p'), { recursive: true });+ writeFileSync(join(projectDir, 'NODE_MODULES', 'p', '.gitignore'), 'tmp/\n');++ const filter = createContentFilter({ projectDir, contentDir: projectDir });+ const result = await filter.rebuildIgnorePatterns();++ expect(result.ok).toBe(true);+ if (!result.ok) throw new Error('unreachable');+ expect(result.nestedFileCount).toBe(1);+ });+test('fires onAfterRebuild on success', async () => {let calls = 0;const filter = createContentFilter({diff --git a/packages/server/src/content-filter.ts b/packages/server/src/content-filter.tsindex 164275dc5..3d812e7d9 100644--- a/packages/server/src/content-filter.ts+++ b/packages/server/src/content-filter.ts@@ -67,6 +67,10 @@ const ALWAYS_SKIP_DIRS = new Set<string>([const OK_ALWAYS_SKIP_CHILDREN = new Set(['worktrees', 'local']);+function isBuiltinSkipDirName(name: string): boolean {+ return BUILTIN_SKIP_DIRS.has(name) || ALWAYS_SKIP_DIRS.has(name.toLowerCase());+}+type AttachmentFolderShape =| { kind: 'sibling' }| { kind: 'content-root' }@@ -127,7 +131,7 @@ function pathHasAlwaysSkipSegment(relativePath: string, showOk?: boolean): booleconst segments = relativePath.split('/');for (let i = 0; i < segments.length; i++) {const segment = segments[i];- const canonicalSegment = segment.toLowerCase() === OK_DIR ? OK_DIR : segment;+ const canonicalSegment = segment.toLowerCase();if (!ALWAYS_SKIP_DIRS.has(canonicalSegment)) continue;if (showOk &&@@ -141,15 +145,26 @@ function pathHasAlwaysSkipSegment(relativePath: string, showOk?: boolean): boolereturn false;}+export function isSkillBundlePathWithheld(below: string): boolean {+ return pathHasAlwaysSkipSegment(below) || isAlwaysSkipFile(below);+}++function skillDescendantIsSkipped(relativePath: string, root: string): boolean {+ return isSkillBundlePathWithheld(relativePath.slice(root.length + 1));+}+function isSkillContentFile(relativePath: string): boolean {- return relativePath.startsWith(`${LEGACY_SKILL_STORE_ROOT}/`);+ return (+ relativePath.startsWith(`${LEGACY_SKILL_STORE_ROOT}/`) &&+ !skillDescendantIsSkipped(relativePath, LEGACY_SKILL_STORE_ROOT)+ );}function isSkillContentAncestorDir(relativePath: string): boolean {return (relativePath === '.ok' ||relativePath === LEGACY_SKILL_STORE_ROOT ||- relativePath.startsWith(`${LEGACY_SKILL_STORE_ROOT}/`)+ isSkillContentFile(relativePath));}@@ -230,7 +245,7 @@ function buildWatcherIgnoreGlobs(patterns: readonly string[]): string[] {function templateFolderPrefixIsSkipped(segments: string[], okIndex: number): boolean {for (let i = 0; i < okIndex; i++) {- if (BUILTIN_SKIP_DIRS.has(segments[i]) || segments[i].toLowerCase() === OK_DIR) return true;+ if (isBuiltinSkipDirName(segments[i])) return true;}return false;}@@ -297,7 +312,9 @@ function isUnderSkillRoot(relativePath: string, roots: ReadonlySet<string>): boofunction isInPlaceSkillFile(relativePath: string, dirs: ReadonlySet<string>): boolean {if (dirs.size === 0) return false;for (const d of dirs) {- if (relativePath.startsWith(`${d}/`)) return true;+ if (relativePath.startsWith(`${d}/`) && !skillDescendantIsSkipped(relativePath, d)) {+ return true;+ }}return false;}@@ -308,7 +325,7 @@ function isInPlaceSkillAncestorDir(relativePath: string, dirs: ReadonlySet<strinif (d === relativePath ||d.startsWith(`${relativePath}/`) ||- relativePath.startsWith(`${d}/`)+ (relativePath.startsWith(`${d}/`) && !skillDescendantIsSkipped(relativePath, d))) {return true;}@@ -774,7 +791,7 @@ export function createContentFilter(opts: ContentFilterOptions): ContentFilter {function isRejectedByConfigurableRules(relativePath: string): boolean {for (const segment of relativePath.split('/')) {- if (BUILTIN_SKIP_DIRS.has(segment)) return true;+ if (isBuiltinSkipDirName(segment)) return true;}if (contentOutsideProject) return false;@@ -883,7 +900,7 @@ export function createContentFilter(opts: ContentFilterOptions): ContentFilter {);}for (const segment of relativePath.split('/')) {- if (BUILTIN_SKIP_DIRS.has(segment)) return true;+ if (isBuiltinSkipDirName(segment)) return true;}if (contentOutsideProject) return false;return (@@ -1105,7 +1122,7 @@ async function populateDirCountYielding(for (const entry of entries) {const childRel = relPath ? `${relPath}/${entry.name}` : entry.name;if (entry.isDirectory()) {- if (BUILTIN_SKIP_DIRS.has(entry.name)) continue;+ if (isBuiltinSkipDirName(entry.name)) continue;if (isIgnored(childRel) || isIgnored(`${childRel}/`)) continue;stack.push({ dir: join(cur, entry.name), relPath: childRel });} else if (entry.isFile() && isSupportedDocFile(entry.name) && !isIgnored(childRel)) {@@ -1131,7 +1148,7 @@ function populateDirCount(for (const entry of entries) {const childRel = relPath ? `${relPath}/${entry.name}` : entry.name;if (entry.isDirectory()) {- if (BUILTIN_SKIP_DIRS.has(entry.name)) continue;+ if (isBuiltinSkipDirName(entry.name)) continue;if (isIgnored(childRel) || isIgnored(`${childRel}/`)) continue;populateDirCount(join(dir, entry.name), childRel, isIgnored, dirCount);} else if (entry.isFile() && isSupportedDocFile(entry.name) && !isIgnored(childRel)) {@@ -1182,7 +1199,7 @@ function loadNestedIgnoreFiles(for (const entry of entries) {if (!entry.isDirectory()) continue;- if (BUILTIN_SKIP_DIRS.has(entry.name)) continue;+ if (isBuiltinSkipDirName(entry.name)) continue;const dirPath = join(dir, entry.name);const relToProject = toPosix(relative(projectDir, dirPath));@@ -1235,7 +1252,7 @@ async function initContentDirStateAsync(const childRel = relPath ? `${relPath}/${entry.name}` : entry.name;if (entry.isDirectory()) {- if (BUILTIN_SKIP_DIRS.has(entry.name)) continue;+ if (isBuiltinSkipDirName(entry.name)) continue;const dirPath = join(dir, entry.name);@@ -1336,7 +1353,7 @@ export async function createContentFilterAsync(opts: ContentFilterOptions): Prom}function isRejectedByConfigurableRules(relativePath: string): boolean {for (const segment of relativePath.split('/')) {- if (BUILTIN_SKIP_DIRS.has(segment)) return true;+ if (isBuiltinSkipDirName(segment)) return true;}if (contentOutsideProject) return false;return isIgnored(relativePath);@@ -1496,7 +1513,7 @@ export async function createContentFilterAsync(opts: ContentFilterOptions): Prom);}for (const segment of relativePath.split('/')) {- if (BUILTIN_SKIP_DIRS.has(segment)) return true;+ if (isBuiltinSkipDirName(segment)) return true;}if (contentOutsideProject) return false;return (diff --git a/packages/server/src/content/skills-write.test.ts b/packages/server/src/content/skills-write.test.tsindex 13d94b9c7..90315c3bf 100644--- a/packages/server/src/content/skills-write.test.ts+++ b/packages/server/src/content/skills-write.test.ts@@ -1,4 +1,12 @@-import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';+import {+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ rmSync,+ symlinkSync,+ writeFileSync,+} from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { afterEach, beforeEach, describe, expect, test } from 'vitest';@@ -353,6 +361,31 @@ describe('applySkillBundleFileWrite / applySkillBundleFileDelete (fs-direct)', (if (!overflow.ok) expect(overflow.error.code).toBe('TOO_MANY_FILES');});+ test('the file-count cap ignores withheld entries and links a caller cannot see or delete', () => {+ seedSkill();+ const skillDir = join(skillsRoot, 'trip-log');+ for (const dir of ['.git/objects', '.GIT', 'node_modules/pkg', 'assets']) {+ mkdirSync(join(skillDir, dir), { recursive: true });+ }+ for (let i = 0; i < 30; i++) writeFileSync(join(skillDir, `.git/objects/o${i}`), 'x');+ writeFileSync(join(skillDir, '.GIT/config'), 'x');+ writeFileSync(join(skillDir, 'node_modules/pkg/index.js'), 'x');+ writeFileSync(join(skillDir, 'assets/.DS_Store'), 'x');+ symlinkSync('.git', join(skillDir, 'cfg'), 'dir');+ symlinkSync('.git/objects/o0', join(skillDir, 'leaf'), 'file');+ const writes: string[] = [];+ for (let i = 0; i < 51; i++) {+ const r = applySkillBundleFileWrite({+ skillsRoot,+ name: 'trip-log',+ relPath: `references/r${i}.md`,+ content: `# ${i}`,+ });+ writes.push(r.ok ? 'ok' : r.error.code);+ }+ expect(writes).toEqual([...Array(50).fill('ok'), 'TOO_MANY_FILES']);+ });+test('accepts an explicit bounded policy for bulk import without changing edit defaults', () => {seedSkill();for (let i = 0; i < 50; i++) {diff --git a/packages/server/src/content/skills-write.ts b/packages/server/src/content/skills-write.tsindex d6ea6a52f..1be4cc548 100644--- a/packages/server/src/content/skills-write.ts+++ b/packages/server/src/content/skills-write.ts@@ -1,4 +1,4 @@-import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';+import { type Dirent, existsSync, readdirSync, readFileSync, statSync } from 'node:fs';import { basename, dirname, isAbsolute, join, normalize, sep } from 'node:path';import {applyPatchToFm,@@ -11,6 +11,7 @@ import {} from '@inkeep/open-knowledge-core';import { ATOMIC_TEMP_INFIX, atomicTempPath } from '@inkeep/open-knowledge-core/server';import { stringify as stringifyYaml } from 'yaml';+import { isSkillBundlePathWithheld } from '../content-filter.ts';/** STOP: every disk write in this module goes through `fs-traced.ts` so it* carries an `fs.*` span. A bare `node:fs` call here drops the span.@@ -274,26 +275,21 @@ function resolveBundleFileAbs(export function countBundleFiles(skillDir: string): number {let count = 0;- const walk = (dir: string): void => {- let names: string[];+ const walk = (dir: string, prefix: string): void => {+ let entries: Dirent[];try {- names = readdirSync(dir);+ entries = readdirSync(dir, { withFileTypes: true });} catch {return;}- for (const entryName of names) {- const abs = join(dir, entryName);- let isDir: boolean;- try {- isDir = statSync(abs).isDirectory();- } catch {- continue;- }- if (isDir) walk(abs);- else if (!(dir === skillDir && entryName === SKILL_FILE)) count++;+ for (const entry of entries) {+ const rel = prefix === '' ? entry.name : `${prefix}/${entry.name}`;+ if (isSkillBundlePathWithheld(rel)) continue;+ if (entry.isDirectory()) walk(join(dir, entry.name), rel);+ else if (entry.isFile() && !(dir === skillDir && entry.name === SKILL_FILE)) count++;}};- walk(skillDir);+ walk(skillDir, '');return count;}diff --git a/packages/server/src/http/skills-file-routes.ts b/packages/server/src/http/skills-file-routes.tsindex 9579eca53..b6b516f62 100644--- a/packages/server/src/http/skills-file-routes.ts+++ b/packages/server/src/http/skills-file-routes.ts@@ -1,7 +1,7 @@-import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';+import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';import { readFile } from 'node:fs/promises';import type { ServerResponse } from 'node:http';-import { dirname, join, relative, resolve, sep } from 'node:path';+import { basename, dirname, join, relative, resolve, sep } from 'node:path';import {EmptyRequestSchema,type Principal,@@ -23,6 +23,7 @@ import {BUNDLE_MAX_FILES,countBundleFiles,} from '../content/skills-write.ts';+import { isSkillBundlePathWithheld } from '../content-filter.ts';import type {DerivedDocumentIndexApiPort,DerivedDocumentIndexMutation,@@ -31,10 +32,12 @@ import { SUPPORTED_DOC_EXTENSIONS } from '../doc-extensions.ts';import type { StoreFailure } from '../document-durability-state.ts';import { extractActorIdentity } from '../extract-actor-identity.ts';import type { PinoLogger } from '../logger.ts';+import { isWithinDir } from '../path-utils.ts';import { isInternalBundleSkillName } from '../skill-bundles.ts';import { type ApiRouteGroup, createApiRouteGroup } from './api-pipeline.ts';import type { ErrorExtensions } from './error-response.ts';import { errorResponse } from './error-response.ts';+import { errnoCode } from './handler-utils.ts';import { methodRouter } from './method-router.ts';import { withValidation } from './request-validation.ts';import { successResponse } from './success-response.ts';@@ -119,6 +122,61 @@ export interface SkillsFileRouteDeps {) => Promise<void>;}+function realPathOf(abs: string): string | null {+ const pending: string[] = [];+ let cur = abs;+ for (;;) {+ try {+ return join(realpathSync.native(cur), ...pending);+ } catch (err) {+ const code = errnoCode(err);+ if (code === 'ELOOP') return null;+ if (code !== 'ENOENT' && code !== 'ENOTDIR') throw err;+ }+ try {+ if (lstatSync(cur).isSymbolicLink()) return null;+ } catch {}+ const parent = dirname(cur);+ if (parent === cur) return null;+ pending.unshift(basename(cur));+ cur = parent;+ }+}++function bundleTargetVerdict(skillDir: string, abs: string): 'admitted' | 'withheld' | 'escapes' {+ if (!isWithinDir(abs, skillDir)) return 'escapes';+ if (isSkillBundlePathWithheld(relative(skillDir, abs).split(sep).join('/'))) return 'withheld';+ const realRoot = realPathOf(skillDir);+ const realTarget = realPathOf(abs);+ if (realRoot === null || realTarget === null || !isWithinDir(realTarget, realRoot)) {+ return 'escapes';+ }+ return isSkillBundlePathWithheld(relative(realRoot, realTarget).split(sep).join('/'))+ ? 'withheld'+ : 'admitted';+}++function rejectUnadmittedBundleTarget(+ res: ServerResponse,+ skillDir: string,+ rel: string,+ handler: string,+ escapeTitle: string,+): boolean {+ const verdict = bundleTargetVerdict(skillDir, resolve(skillDir, rel));+ if (verdict === 'admitted') return false;+ errorResponse(+ res,+ 400,+ 'urn:ok:error:invalid-request',+ verdict === 'withheld'+ ? 'Skill file path falls under an entry Open Knowledge withholds from skill bundles (.git, node_modules, OK and editor host dirs, OS artifact files).'+ : escapeTitle,+ { handler, detail: rel },+ );+ return true;+}+export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroup {const {validateSkillName,@@ -228,7 +286,8 @@ export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroup}const skillDir = resolvedSkillDir ?? resolve(resolveSkillsRoot(scope), name);const abs = resolve(skillDir, rel);- if (abs !== skillDir && !abs.startsWith(`${skillDir}${sep}`)) {+ const verdict = bundleTargetVerdict(skillDir, abs);+ if (verdict === 'escapes') {errorResponse(res,400,@@ -242,11 +301,14 @@ export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroup}let resolvedAbs = abs;let resolvedRel = rel;- if (!existsSync(resolvedAbs)) {- const docStem = rel.match(/^(.*)\.(?:md|mdx)$/);+ if (verdict === 'withheld' || !existsSync(resolvedAbs)) {+ const docStem = verdict === 'withheld' ? null : rel.match(/^(.*)\.(?:md|mdx)$/);const sibling = docStem? SUPPORTED_DOC_EXTENSIONS.map((ext) => `${docStem[1]}${ext}`).find(- (candidate) => candidate !== rel && existsSync(resolve(skillDir, candidate)),+ (candidate) =>+ candidate !== rel &&+ bundleTargetVerdict(skillDir, resolve(skillDir, candidate)) === 'admitted' &&+ existsSync(resolve(skillDir, candidate)),): undefined;if (sibling === undefined) {@@ -341,6 +403,16 @@ export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroupconst fileBase = body.scope === 'project' ? contentDir : skillsHome;const skillDirRel = relative(fileBase, skillDirAbs).split(sep).join('/');const rel = body.path.replace(/\\/g, '/');+ if (+ rejectUnadmittedBundleTarget(+ res,+ skillDirAbs,+ rel,+ 'skill-file-put',+ 'Skill file path escapes the skill dir.',+ )+ )+ return;const routedThroughContent = isProjectMdReference(body.scope, kind, rel);let created: boolean;@@ -485,7 +557,18 @@ export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroupconst realDir = resolveSkillDirForRead(scope, name);const skillsRoot = realDir !== null ? dirname(realDir) : resolveSkillsRoot(scope);- const bundleAbs = resolve(realDir ?? join(skillsRoot, name), rel);+ const bundleDir = realDir ?? join(skillsRoot, name);+ const bundleAbs = resolve(bundleDir, rel);+ if (+ rejectUnadmittedBundleTarget(+ res,+ bundleDir,+ rel,+ 'skill-file-delete',+ 'Invalid skill file path (must name a file inside the skill dir).',+ )+ )+ return;if (existsSync(bundleAbs) && isProjectMdReference(scope, kind, rel)) {const extLess = rel.replace(/\.md$/i, '');const refDoc =@@ -597,6 +680,18 @@ export function createSkillsFileRoutes(deps: SkillsFileRouteDeps): ApiRouteGroupreturn;}const skillsRoot = dirname(realDir);+ if (+ [from, to].some((rel) =>+ rejectUnadmittedBundleTarget(+ res,+ realDir,+ rel,+ 'skill-file-rename',+ 'Both paths must stay inside the skill dir.',+ ),+ )+ )+ return;const fromIsDoc = isProjectMdReference(body.scope, fromKind, from);const toIsDoc = isProjectMdReference(body.scope, toKind, to);diff --git a/packages/server/src/in-place-skills.ts b/packages/server/src/in-place-skills.tsindex daae3070b..a78bf9991 100644--- a/packages/server/src/in-place-skills.ts+++ b/packages/server/src/in-place-skills.ts@@ -207,9 +207,11 @@ function bundleStamp(absDir: string): string | null {for (const e of readdirSync(dir, { withFileTypes: true })) {const p = join(dir, e.name);const r = rel === '' ? e.name : `${rel}/${e.name}`;- const st = statSync(p);- if (st.isDirectory()) walk(p, r);- else parts.push(`${r}:${st.size}:${st.mtimeMs}`);+ if (e.isDirectory()) walk(p, r);+ else if (e.isFile()) {+ const st = statSync(p);+ parts.push(`${r}:${st.size}:${st.mtimeMs}`);+ }}};try {diff --git a/packages/server/src/skill-projection.ts b/packages/server/src/skill-projection.tsindex 9e8ad6b3a..ad31ac99e 100644--- a/packages/server/src/skill-projection.ts+++ b/packages/server/src/skill-projection.ts@@ -13,6 +13,7 @@ import {} from '@inkeep/open-knowledge-core';import type { SkillHostId } from '@inkeep/open-knowledge-core/skills-catalog';import { parse as parseYaml } from 'yaml';+import { isSkillBundlePathWithheld } from './content-filter.ts';import {tracedCpSync,tracedMkdirSync,@@ -538,6 +539,7 @@ function listSkillFiles(dir: string, prefix = ''): string[] {const out: string[] = [];for (const entry of readdirSync(dir, { withFileTypes: true })) {const rel = prefix ? `${prefix}/${entry.name}` : entry.name;+ if (isSkillBundlePathWithheld(rel)) continue;if (entry.isDirectory()) out.push(...listSkillFiles(join(dir, entry.name), rel));else if (entry.isFile()) out.push(rel);}