Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
train K: justify #7830's path joins for semgrep (every joined segment is confined or validated first)
garrytan9d87456083a6
diff --git a/src/commands/skillpack/update.ts b/src/commands/skillpack/update.tsindex e640632a45..a2e986f222 100644--- a/src/commands/skillpack/update.ts+++ b/src/commands/skillpack/update.ts@@ -33,6 +33,7 @@ export const UPDATE_HELP ='2 a skill failed and was rolled back (files touched and restore command printed).';function workspaceSkillsRoot(workspace: string | null): string {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- workspace is the local operator's own --workspace arg; skillpack update runs only on the trusted local CLI (never MCP/remote)return skillsRootKey(join(resolveWorkspace({ workspace }), 'skills'));}diff --git a/src/core/skillpack/safe-update.ts b/src/core/skillpack/safe-update.tsindex 81e9439b9b..b4cf184556 100644--- a/src/core/skillpack/safe-update.ts+++ b/src/core/skillpack/safe-update.ts@@ -110,12 +110,15 @@ export interface UpdateOptions {/** The key a skills root (or a file under it) is recorded under: the realpath of its deepest existing ancestor plus the rest. */export function skillsRootKey(dir: string): string {const missing: string[] = [];+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- skillsRootKey only normalizes a caller-given skills root into its realpath key; no read or writelet current = resolve(dir);for (;;) {try {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- same: realpath key of the deepest existing ancestor plus its missing basenamesreturn join(realpathSync(current), ...missing.reverse());} catch {const parent = dirname(current);+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- same: fallback key when no ancestor resolvesif (parent === current) return resolve(dir);missing.push(basename(current));current = parent;@@ -194,6 +197,7 @@ export function classifySkill(ctx: ClassifyContext, slug: string): SkillOutcomeoutcome.reason = reason;if (!ctx.noDiff && reason !== 'pinned') {outcome.diffs = changed.filter(f => f.pre !== null).map(f => ({+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- f.rel passed confinedSkillChildWrite in classifySkill (an escaping rel is skipped as unsafe_target before this read)rel: f.rel, ...boundedRedactedDiff(readFileSync(join(ctx.skillsRoot, f.rel)), readFileSync(f.source), f.rel),}));}@@ -222,6 +226,7 @@ export function defaultValidator(skillsRoot: string): (root: string, slug: strin}return (root, slug) => {const problems: string[] = [];+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- slug is a bundled-manifest skill that classified and applied; read-only conformance checkconst content = readFileSync(join(root, slug, 'SKILL.md'), 'utf-8');const fm = content.startsWith('---') ? parseSkillFrontmatter(content) : null;if (!fm?.name) problems.push(`conformance: ${slug}/SKILL.md has no frontmatter with a name`);@@ -244,6 +249,7 @@ export function defaultValidator(skillsRoot: string): (root: string, slug: strinfunction removeStaleStages(skillsRoot: string): void {if (!existsSync(skillsRoot)) return;for (const name of readdirSync(skillsRoot)) {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- name is a direct readdirSync entry of skillsRoot carrying the gbrain stage prefixif (name.startsWith(STAGE_PREFIX)) rmSync(join(skillsRoot, name), { recursive: true, force: true });}}@@ -278,6 +284,7 @@ async function applySkill(ctx: ApplyContext, outcome: SkillOutcome, js: JournalSreturn { ...outcome, result: 'failed', failure, touched: renamed.map(f => f.rel), rollback_skipped: rollbackSkipped };};mkdirSync(skillsRoot, { recursive: true });+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- constant STAGE_PREFIX under the skills rootconst stage = mkdtempSync(join(skillsRoot, STAGE_PREFIX));try {const staged: string[] = [];@@ -287,6 +294,7 @@ async function applySkill(ctx: ApplyContext, outcome: SkillOutcome, js: JournalSif (f.rel === `${outcome.slug}/SKILL.md` && !parseSkillFrontmatter(bytes.toString('utf-8'))?.name) {return fail(`staged ${f.rel} has no frontmatter with a name`, 'abandoned');}+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- String(i) under the fresh mkdtemp stage directoryconst path = join(stage, String(i));writeFileSync(path, bytes);staged.push(path);@@ -299,6 +307,7 @@ async function applySkill(ctx: ApplyContext, outcome: SkillOutcome, js: JournalSwriteJournal(ctx.journal, ctx.backups);try {for (const file of js.files) {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- file.rel passed confinedSkillChildWrite in classifySkill; the read feeds a hash-verified backupif (file.pre !== null) file.backup = backupFile(ctx.journal.id, file.rel, join(skillsRoot, file.rel), file.pre, ctx.backups);}} catch (err) {@@ -342,6 +351,7 @@ export async function runSkillpackUpdate(opts: UpdateOptions): Promise<UpdateResconst backups = opts.backups ?? backupsRoot();const manifest = loadBundleManifest(opts.gbrainRoot);const slugs = opts.slugs ?? bundledSkillSlugs(manifest);+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- gbrainRoot is the install's own root (findGbrainOrDie)const history = loadVerifiedSkillsLockHistory(join(opts.gbrainRoot, 'skills'));const pending = pendingJournals({ root: backups, skillsRoot: opts.skillsRoot }).map(j => j.id);if (pending.length && !opts.dryRun) {diff --git a/src/core/skillpack/update-journal.ts b/src/core/skillpack/update-journal.tsindex a64b0847ee..99a268e62e 100644--- a/src/core/skillpack/update-journal.ts+++ b/src/core/skillpack/update-journal.ts@@ -92,12 +92,14 @@ export function journalDir(id: string, root = backupsRoot()): string {throw new OperationError('skillpack_journal_not_found', `'${id}' is not a skillpack backup id.`,'List the backups with gbrain skillpack restore --list.');}+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- id matched the backup-id pattern above (timestamp digits, T, Z, '-' and six hex)return join(root, id);}export function writeJournal(journal: Journal, root = backupsRoot()): void {const dir = journalDir(journal.id, root);mkdirSync(dir, { recursive: true });+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- dir came from journalDir, which validated the idconst path = join(dir, 'journal.json');const tmp = `${path}.${process.pid}.${randomUUID()}.tmp`;writeFileSync(tmp, JSON.stringify(journal, null, 2) + '\n', { mode: 0o600 });@@ -105,6 +107,7 @@ export function writeJournal(journal: Journal, root = backupsRoot()): void {}export function readJournal(id: string, root = backupsRoot()): Journal {+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- journalDir validated the idconst path = join(journalDir(id, root), 'journal.json');if (!existsSync(path)) {throw new OperationError('skillpack_journal_not_found', `No skillpack backup '${id}' under ${root}.`,@@ -150,6 +153,7 @@ export function pruneJournals(root = backupsRoot(), now = Date.now()): string[]/** Copy `target` into the journal's backup tree and verify the copy hashes to `expected`. */export function backupFile(journalId: string, rel: string, target: string, expected: string, root = backupsRoot()): string {const backupRel = `files/${rel}`;+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- rel passed confinedSkillChildWrite during classification; dest stays under the validated journal dirconst dest = join(journalDir(journalId, root), backupRel);mkdirSync(dirname(dest), { recursive: true });copyFileSync(target, dest);@@ -161,6 +165,7 @@ export function backupFile(journalId: string, rel: string, target: string, expecexport function replaceFile(target: string, bytes: Buffer): void {assertLegacySkillFilesystemWrite(target);mkdirSync(dirname(target), { recursive: true });+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- target passed assertLegacySkillFilesystemWrite; the temp name is pid + UUID in the same directoryconst tmp = join(dirname(target), `.gbrain-skillpack-${process.pid}-${randomUUID()}.tmp`);writeFileSync(tmp, bytes);try {@@ -183,6 +188,7 @@ export function undoFile(journalId: string, skillsRoot: string, file: JournalFilunlinkSync(target);return true;}+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- file.backup is read from the user's own journal and its bytes are hash-checked against file.pre before any writeconst bytes = readFileSync(join(journalDir(journalId, root), file.backup));if (sha256(bytes) !== file.pre) {throw new OperationError('skillpack_backup_corrupt', `The backup of ${file.rel} in ${journalId} does not hash to the journal's pre-update hash.`,