Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Skill harvest copies paired files from a folder that starts with the skill's name (#9604)
Garry Tan0e839c81fc1a
diff --git a/changelog.d/harvest-sibling-folder.md b/changelog.d/harvest-sibling-folder.mdnew file mode 100644index 0000000000..c5770acb2c--- /dev/null+++ b/changelog.d/harvest-sibling-folder.md@@ -0,0 +1,6 @@+**`gbrain skillpack harvest` now harvests a skill whose paired file sits in a folder that starts with the skill's name, such as `skills/foo-extra/` next to `skills/foo/`.**++### Fixed+- A paired source in a sibling folder whose name starts with the skill's name was treated as a file of the skill folder, so the harvest stopped with `path_traversal` and copied nothing. The harvest now copies it to the same path as any other paired source. Symlinks and files outside the repo are still refused.+- A harvest that refuses a paired source now stops before it writes anything. Before, it could copy the skill folder first and leave it behind, so a second try stopped with `slug_collision`.+- A paired source reached through a linked folder that points outside the host repo is now refused with `path_traversal`. Before, harvest refused only a linked file, not a linked folder.diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex 280bc868bc..c62bead269 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -422,6 +422,7 @@ test/skillpack-flow.test.ts skillpack flow (E2E) 17 readFileSynctest/skillpack-harness-bridge.test.ts review-driven hardening (ship pre-landing findings) 5 readFileSynctest/skillpack-harvest.test.ts addToBundleManifest 1 readFileSynctest/skillpack-harvest.test.ts runHarvest — happy path 2 readFileSync+test/skillpack-harvest.test.ts runHarvest — paired source in a sibling folder that starts with the slug 5 readFileSynctest/skillpack-harvest.test.ts runHarvest — privacy linter integration (T7) 2 readFileSynctest/skillpack-init-brain-pack.test.ts runInitBrainPack 5 readFileSynctest/skillpack-init-pack.test.ts runInitScaffold — cathedral default 6 readFileSyncdiff --git a/src/core/skillpack/harvest.ts b/src/core/skillpack/harvest.tsindex 4ab8708378..2949206fd5 100644--- a/src/core/skillpack/harvest.ts+++ b/src/core/skillpack/harvest.ts@@ -34,7 +34,8 @@ import {writeFileSync,} from 'fs';import { homedir } from 'os';-import { dirname, join, relative } from 'path';+import { dirname, join, relative, sep } from 'path';+import { resolvedPrefixContained } from '../path-confine.ts';import { copyArtifacts, walkSourceDir } from './copy.ts';import { loadSkillSources } from './bundle.ts';@@ -136,29 +137,40 @@ export function runHarvest(opts: HarvestOptions): HarvestResult {});}- // Copy with D13 confinement + symlink reject. The confinement root is- // the HOST skill dir (every source must canonicalize inside it). For- // paired sources outside the skill dir, fall through to symlink-only- // protection (the host repo is user-trusted at this granularity).- const skillItems = items.filter(i => i.source.startsWith(hostSkillDir));- const pairedItems = items.filter(i => !i.source.startsWith(hostSkillDir));+ // Copy with D13 confinement + symlink reject. Skill-dir sources must+ // canonicalize inside the HOST skill dir; paired sources must+ // canonicalize inside the host repo, so a linked folder cannot pull in+ // a file from outside it.+ // "Inside" is separator-aware: a sibling folder that starts with the+ // slug (skills/foo-extra/ next to skills/foo/) is a paired source.+ const inSkillDir = (p: string) => resolvedPrefixContained(p, hostSkillDir, sep);+ const skillItems = items.filter(i => inSkillDir(i.source));+ const pairedItems = items.filter(i => !inSkillDir(i.source));let filesCopied: string[] = [];try {+ // Validate both batches before writing either, so a refused paired+ // source cannot leave a half-copied skill dir behind. The real copies+ // below re-run the same gates.+ copyArtifacts(skillItems, {+ rejectSymlinks: true,+ confineRealpath: hostSkillDir,+ dryRun: true,+ });+ copyArtifacts(pairedItems, {+ rejectSymlinks: true,+ confineRealpath: opts.hostRepoRoot,+ dryRun: true,+ });if (!dryRun) {copyArtifacts(skillItems, {rejectSymlinks: true,confineRealpath: hostSkillDir,});- copyArtifacts(pairedItems, { rejectSymlinks: true });- } else {- // Dry-run still validates safety gates but doesn't copy.- copyArtifacts(skillItems, {+ copyArtifacts(pairedItems, {rejectSymlinks: true,- confineRealpath: hostSkillDir,- dryRun: true,+ confineRealpath: opts.hostRepoRoot,});- copyArtifacts(pairedItems, { rejectSymlinks: true, dryRun: true });}filesCopied = items.map(i => i.target);} catch (err) {diff --git a/test/skillpack-harvest.test.ts b/test/skillpack-harvest.test.tsindex e3f6b6aa25..b749ab1a65 100644--- a/test/skillpack-harvest.test.ts+++ b/test/skillpack-harvest.test.ts@@ -127,6 +127,110 @@ describe('runHarvest — happy path', () => {});});+describe('runHarvest — paired source in a sibling folder that starts with the slug', () => {+ // A skill `foo` may pair a source under `skills/foo-extra/`. That folder is+ // NOT the skill folder, so the file must take the paired-source path+ // (copied to the same relative path) instead of the skill-folder path,+ // whose confinement to `skills/foo/` refuses it.+ function hostWithSiblingSource(): string {+ const root = mkdtempSync(join(tmpdir(), 'sp-h-sibling-'));+ created.push(root);+ mkdirSync(join(root, 'skills', 'foo'), { recursive: true });+ mkdirSync(join(root, 'skills', 'foo-extra'), { recursive: true });+ writeFileSync(+ join(root, 'skills', 'foo', 'SKILL.md'),+ '---\nname: foo\ntriggers:\n - trigger\nsources:\n - skills/foo-extra/x.ts\n---\n# foo\n\nGeneric placeholder content.\n',+ );+ writeFileSync(join(root, 'skills', 'foo-extra', 'x.ts'), 'export const x = 1;\n');+ return root;+ }++ it('harvests the skill and copies the sibling-folder source', () => {+ const hostRoot = hostWithSiblingSource();+ const gbrainRoot = scratchGbrain();++ const result = runHarvest({ slug: 'foo', hostRepoRoot: hostRoot, gbrainRoot });++ expect(result.status).toBe('harvested');+ expect(result.pairedSources).toEqual(['skills/foo-extra/x.ts']);+ expect(readFileSync(join(gbrainRoot, 'skills', 'foo', 'SKILL.md'), 'utf-8')).toContain('# foo');+ expect(readFileSync(join(gbrainRoot, 'skills', 'foo-extra', 'x.ts'), 'utf-8')).toBe(+ 'export const x = 1;\n',+ );+ });++ it('dry-run accepts the sibling-folder source and writes nothing', () => {+ const hostRoot = hostWithSiblingSource();+ const gbrainRoot = scratchGbrain();++ const result = runHarvest({ slug: 'foo', hostRepoRoot: hostRoot, gbrainRoot, dryRun: true });++ expect(result.status).toBe('harvested');+ expect(existsSync(join(gbrainRoot, 'skills', 'foo'))).toBe(false);+ expect(existsSync(join(gbrainRoot, 'skills', 'foo-extra'))).toBe(false);+ });++ it('privacy-lint rollback also removes the sibling-folder source', () => {+ const hostRoot = hostWithSiblingSource();+ writeFileSync(join(hostRoot, 'skills', 'foo-extra', 'x.ts'), '// contact: [email protected]\n');+ const gbrainRoot = scratchGbrain();++ const result = runHarvest({ slug: 'foo', hostRepoRoot: hostRoot, gbrainRoot });++ expect(result.status).toBe('lint_failed');+ expect(existsSync(join(gbrainRoot, 'skills', 'foo'))).toBe(false);+ expect(existsSync(join(gbrainRoot, 'skills', 'foo-extra', 'x.ts'))).toBe(false);+ });++ it('a symlink in the sibling folder is still refused', () => {+ const hostRoot = hostWithSiblingSource();+ const outside = mkdtempSync(join(tmpdir(), 'sp-h-outside-'));+ created.push(outside);+ writeFileSync(join(outside, 'secret.txt'), 'PRIVATE\n');+ rmSync(join(hostRoot, 'skills', 'foo-extra', 'x.ts'));+ symlinkSync(join(outside, 'secret.txt'), join(hostRoot, 'skills', 'foo-extra', 'x.ts'));+ const gbrainRoot = scratchGbrain();+ const manifestBefore = readFileSync(join(gbrainRoot, 'openclaw.plugin.json'), 'utf-8');++ try {+ runHarvest({ slug: 'foo', hostRepoRoot: hostRoot, gbrainRoot, noLint: true });+ throw new Error('expected throw');+ } catch (err) {+ expect(err).toBeInstanceOf(HarvestError);+ expect((err as HarvestError).code).toBe('symlink_rejected');+ }+ // Refused before any write: no half-copied skill dir blocks a retry+ // with slug_collision, and the manifest is untouched.+ expect(existsSync(join(gbrainRoot, 'skills', 'foo'))).toBe(false);+ expect(existsSync(join(gbrainRoot, 'skills', 'foo-extra', 'x.ts'))).toBe(false);+ expect(readFileSync(join(gbrainRoot, 'openclaw.plugin.json'), 'utf-8')).toBe(manifestBefore);+ });++ it('a sibling folder that links outside the host repo is refused', () => {+ // The file itself is ordinary; only its folder is a link. The paired+ // batch must still stay inside the host repo.+ const hostRoot = hostWithSiblingSource();+ const outside = mkdtempSync(join(tmpdir(), 'sp-h-outside-dir-'));+ created.push(outside);+ writeFileSync(join(outside, 'x.ts'), 'PRIVATE\n');+ rmSync(join(hostRoot, 'skills', 'foo-extra'), { recursive: true });+ symlinkSync(outside, join(hostRoot, 'skills', 'foo-extra'));+ const gbrainRoot = scratchGbrain();+ const manifestBefore = readFileSync(join(gbrainRoot, 'openclaw.plugin.json'), 'utf-8');++ try {+ runHarvest({ slug: 'foo', hostRepoRoot: hostRoot, gbrainRoot, noLint: true });+ throw new Error('expected throw');+ } catch (err) {+ expect(err).toBeInstanceOf(HarvestError);+ expect((err as HarvestError).code).toBe('path_traversal');+ }+ expect(existsSync(join(gbrainRoot, 'skills', 'foo'))).toBe(false);+ expect(existsSync(join(gbrainRoot, 'skills', 'foo-extra', 'x.ts'))).toBe(false);+ expect(readFileSync(join(gbrainRoot, 'openclaw.plugin.json'), 'utf-8')).toBe(manifestBefore);+ });+});+describe('runHarvest — error paths', () => {it('host_skill_missing when --from points at the wrong place', () => {const hostRoot = scratchHost();