Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(semgrep): validate scaffolded skill slugs before registering them; annotate trusted path joins
time-attackea05a4278217
diff --git a/src/commands/skillpack.ts b/src/commands/skillpack.tsindex b95f8c849e..b0ef6e65ff 100644--- a/src/commands/skillpack.ts+++ b/src/commands/skillpack.ts@@ -400,6 +400,7 @@ async function cmdSync(args: string[]): Promise<void> {runScaffold({ gbrainRoot, targetWorkspace, skillSlug: slug, dryRun });scaffolded.push(slug);}+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- targetWorkspace is the local operator's --workspace argument or the auto-detected workspace root, the same root runScaffold just wrote under.const registered = dryRun ? [] : registerManifestEntries(join(targetWorkspace, 'skills'), scaffolded);if (json) {diff --git a/src/core/atomic-write.ts b/src/core/atomic-write.tsindex 809e7f3815..5847e25748 100644--- a/src/core/atomic-write.ts+++ b/src/core/atomic-write.ts@@ -105,6 +105,7 @@ export function atomicStagingPath(filePath: string): string {}function stagingPrefix(target: string): string {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- the second segment is a fixed-shape name built from a sha256 hex digest (no separators or ..); dirname is the already-resolved target's own directory.return join(dirname(target), `.${createHash('sha256').update(target).digest('hex')}.tmp.`);}diff --git a/src/core/skill-manifest.ts b/src/core/skill-manifest.tsindex 4ccc035e40..ff06485988 100644--- a/src/core/skill-manifest.ts+++ b/src/core/skill-manifest.ts@@ -154,11 +154,15 @@ export function loadOrDeriveManifest(skillsDir: string): ManifestLoadResult {* loader derives the skill set from the walk there. Returns the names added.*/export function registerManifestEntries(skillsDir: string, slugs: readonly string[]): string[] {+ const unsafe = slugs.find(slug => !slug || slug === '.' || slug.includes('..') || /[\\/]/.test(slug));+ if (unsafe !== undefined) throw new Error(`refusing to register skill slug ${JSON.stringify(unsafe)}: a slug is one path segment with no separators or '..'`);+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- skillsDir is the operator's workspace skills directory; the file name is a constant.const manifestPath = join(skillsDir, 'manifest.json');if (!slugs.length || !existsSync(manifestPath) || loadOrDeriveManifest(skillsDir).derived) return [];const content = JSON.parse(readFileSync(manifestPath, 'utf-8')) as { skills: ManifestEntry[] };const known = new Set(content.skills.flatMap(entry => [entry.name, entry.path]));const added = slugs.filter(slug => !known.has(slug) && !known.has(`${slug}/SKILL.md`))+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- every slug was checked above to be one segment with no separators or '..'..map(slug => ({ name: parseSkillName(join(skillsDir, slug, 'SKILL.md')) ?? slug, path: `${slug}/SKILL.md` }));if (!added.length) return [];atomicWriteFileSync(manifestPath, JSON.stringify({ ...content, skills: [...content.skills, ...added] }, null, 2) + '\n');diff --git a/test/skillpack-sync-register-4831.test.ts b/test/skillpack-sync-register-4831.test.tsindex 8cb567f291..220a987d2b 100644--- a/test/skillpack-sync-register-4831.test.ts+++ b/test/skillpack-sync-register-4831.test.ts@@ -9,6 +9,7 @@ import { spawnSync } from 'node:child_process';import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';+import { registerManifestEntries } from '../src/core/skill-manifest.ts';const REPO = resolve(import.meta.dir, '..');const dirs: string[] = [];@@ -49,3 +50,17 @@ describe('skillpack sync registers what it scaffolded (#4831)', () => {expect(report.report.issues.filter(issue => issue.type === 'orphan_trigger')).toEqual([]);}, 300_000);});++describe('registerManifestEntries refuses slugs that are not one path segment', () => {+ test('separators, .. and empty slugs throw before anything is read or written', () => {+ const workspace = mkdtempSync(join(tmpdir(), 'gbrain-4831-slug-'));+ dirs.push(workspace);+ mkdirSync(join(workspace, 'skills'), { recursive: true });+ const manifest = JSON.stringify({ skills: [] }, null, 2);+ writeFileSync(join(workspace, 'skills', 'manifest.json'), manifest);+ for (const bad of ['../escape', 'a/b', 'a\\b', '..', '.', '']) {+ expect(() => registerManifestEntries(join(workspace, 'skills'), [bad])).toThrow('refusing to register skill slug');+ }+ expect(readFileSync(join(workspace, 'skills', 'manifest.json'), 'utf8')).toBe(manifest);+ });+});