Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#5988): confine hold and staged-check file paths to the source root (semgrep path-join)

garrytanc02302eb61ba
diff --git a/src/commands/frontmatter.ts b/src/commands/frontmatter.ts
index 251e4ba970..a6d5c8b050 100644
--- a/src/commands/frontmatter.ts
+++ b/src/commands/frontmatter.ts
@@ -44,6 +44,7 @@ import {
} from '../core/brain-writer.ts';
import { collectGitVisibleFiles } from '../core/git-visible-files.ts';
import { isMarkdownFilePath, pruneDir, slugifyPath } from '../core/sync.ts';
+import { isPathContained } from '../core/path-confine.ts';
/** Test seams: the stream `validate --stdin` reads, and the directory `--staged` runs git in. */
export interface FrontmatterIo {
@@ -404,8 +405,9 @@ async function runValidate(rest: string[], io: FrontmatterIo): Promise<void> {
const check = checkContent(file.content, file.rel, flags.importable);
const result = toResult(file.rel, check);
if (check.failed) {
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- file.rel is a staged path from git diff --cached; isPathContained rejects anything outside the work tree
const working = join(staged.root, file.rel);
- result.working_copy_ok = existsSync(working) && lstatSync(working).isFile()
+ result.working_copy_ok = isPathContained(working, staged.root) && lstatSync(working).isFile()
&& !checkContent(readFileSync(working, 'utf8'), file.rel, flags.importable).failed;
}
results.push(result);
diff --git a/src/commands/sync/holds.ts b/src/commands/sync/holds.ts
index dad014019a..ab565a7fcf 100644
--- a/src/commands/sync/holds.ts
+++ b/src/commands/sync/holds.ts
@@ -27,6 +27,14 @@ import {
} from '../../core/persistence/sync-holds.ts';
import { DEFAULT_SOURCE_ID, hasMalformedPathSegment, isCodeFilePath, resolveSlugForPath, sanitizePathForDisplay, slugifyPath } from '../../core/sync.ts';
import type { SyncActivePack } from './sync-run.ts';
+import { isPathContained } from '../../core/path-confine.ts';
+
+/** The file at a repo-relative path from git or a hold row, or null when it is missing or resolves outside the source root. */
+function fileUnderRoot(root: string, path: string): string | null {
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal -- path is a repo-relative path from git or a hold row; isPathContained realpaths both sides and rejects anything outside root
+ const filePath = join(root, path);
+ return isPathContained(filePath, root) ? filePath : null;
+}
export interface LegacyHolds {
readonly sourceId: string;
@@ -97,8 +105,8 @@ export function planHoldRescreen(holds: LegacyHolds, input: { root: string; touc
}
const selected = input.selected(path);
if (selected === null) continue;
- const filePath = join(input.root, path);
- if (!selected || !existsSync(filePath)) {
+ const filePath = fileUnderRoot(input.root, path);
+ if (!selected || !filePath) {
holds.gone.push(path);
if (holds.retryPaths.has(path)) holds.retryTaken.push(path);
continue;
@@ -221,8 +229,8 @@ export async function wouldHoldFields(engine: BrainEngine, holds: LegacyHolds |
const now = new Date().toISOString();
const refused: Array<{ path: string; screen: HoldScreen & { refusal: ContentRefusal } }> = [];
for (const path of new Set(paths)) {
- const filePath = join(root, path);
- if (!existsSync(filePath)) continue;
+ const filePath = fileUnderRoot(root, path);
+ if (!filePath) continue;
const screen = screenLegacyFile(filePath, path, activePack);
if (screen.refusal) refused.push({ path, screen: screen as HoldScreen & { refusal: ContentRefusal } });
}
@@ -320,8 +328,8 @@ export async function moveHeldRenames(engine: BrainEngine, holds: LegacyHolds |
if (!holds?.active) return;
for (const record of holds.existing.values()) {
const origin = record.meta.rename_from;
- const filePath = join(root, record.path);
- if (!origin || !existsSync(filePath)) continue;
+ const filePath = fileUnderRoot(root, record.path);
+ if (!origin || !filePath) continue;
const screen = screenLegacyFile(filePath, record.path);
if (screen.refusal) continue;
const current = await currentPage(engine, holds.sourceId, { id: origin.pageId });
@@ -346,7 +354,7 @@ export async function settleFullSyncHolds(engine: BrainEngine, holds: LegacyHold
if (!holds) return;
for (const path of holds.existing.keys()) {
if (input.failed.has(path)) continue;
- if (input.walked.has(path) || !existsSync(join(input.root, path))) await clearLegacyHold(engine, holds, path);
+ if (input.walked.has(path) || !fileUnderRoot(input.root, path)) await clearLegacyHold(engine, holds, path);
}
holds.retryTaken.push(...holds.retryPaths);
await settleLegacyHolds(engine, holds);