Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): make path containment separator-safe (#3415)

alexey-metaengage5f84fb8813c4
diff --git a/src/commands/sync.ts b/src/commands/sync.ts
index d7e540c356..8844a1d588 100644
--- a/src/commands/sync.ts
+++ b/src/commands/sync.ts
@@ -1,6 +1,6 @@
import { existsSync, readFileSync, writeFileSync, statSync, realpathSync } from 'fs';
import { execFileSync } from 'child_process';
-import { join, relative } from 'path';
+import { isAbsolute, join, relative, sep } from 'path';
import type { BrainEngine } from '../core/engine.ts';
import { DELETE_BATCH_SIZE } from '../core/engine-constants.ts';
import { importFile } from '../core/import-file.ts';
@@ -1152,6 +1152,20 @@ function createSyncBaselineCommit(repoPath: string): void {
);
}
+/**
+ * True when `childReal` is `rootReal` itself or lives inside it. Both arguments
+ * must already be realpath-resolved. Containment is decided by `relative()`
+ * rather than a string prefix, so it holds on Windows too: `realpathSync`
+ * returns backslash paths there, and a literal `rootReal + '/'` prefix can
+ * never match one. A sibling (`root-evil`) is rejected because `relative`
+ * yields `../root-evil`, and a cross-drive path because it yields an absolute.
+ */
+export function isWithinRoot(childReal: string, rootReal: string): boolean {
+ if (childReal === rootReal) return true;
+ const rel = relative(rootReal, childReal);
+ return rel !== '' && rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
+}
+
/**
* #774 NAV-1 TOCTOU: true only if filePath realpath-resolves inside gitRoot.
* Guards symlink escape at the per-file level (a committed symlink whose
@@ -1159,9 +1173,7 @@ function createSyncBaselineCommit(repoPath: string): void {
*/
function isPathSafe(filePath: string, gitRoot: string): boolean {
try {
- const real = realpathSync(filePath);
- const rootReal = realpathSync(gitRoot);
- return real === rootReal || real.startsWith(rootReal + '/');
+ return isWithinRoot(realpathSync(filePath), realpathSync(gitRoot));
} catch {
return false;
}
@@ -1932,7 +1944,7 @@ async function performSyncInner(engine: BrainEngine, opts: SyncOpts): Promise<Sy
// NAV-1/NAV-2 scope-entry guard: the realpath-resolved scope must live
// inside the realpath-resolved git root. Catches `--src-subpath ../escape`
// AND a symlinked subdir pointing outside the repo, before any git op runs.
- if (syncScopeRoot !== gitContextRoot && !syncScopeRoot.startsWith(gitContextRoot + '/')) {
+ if (!isWithinRoot(syncScopeRoot, gitContextRoot)) {
throw new Error(
`Sync scope ${syncScopeRoot} resolves outside git repo ${gitContextRoot}. ` +
`Refusing to sync: possible path traversal via --src-subpath.`,
diff --git a/test/sync-path-containment.test.ts b/test/sync-path-containment.test.ts
new file mode 100644
index 0000000000..0824ae8a4c
--- /dev/null
+++ b/test/sync-path-containment.test.ts
@@ -0,0 +1,61 @@
+/**
+ * #774 NAV-1/NAV-2 path-containment guard (`isWithinRoot` in sync.ts).
+ *
+ * The guard decides whether a realpath-resolved file lives inside the
+ * realpath-resolved git root. It backs BOTH the `--src-subpath` scope-entry
+ * check and the per-file TOCTOU re-check in the incremental import drain.
+ *
+ * It used to be a string prefix test (`real.startsWith(rootReal + '/')`). That
+ * hardcoded separator makes the guard fail CLOSED for every path on Windows,
+ * where `realpathSync` returns `C:\repo\file.md` and the `C:\repo/` prefix can
+ * never match: every file in the drain is recorded as a symlink escape and the
+ * sync imports nothing. These cases pin the containment semantics against the
+ * platform's own separator, so a future prefix-based rewrite is caught.
+ *
+ * The Windows regression itself is only OBSERVABLE on win32 — on POSIX the old
+ * prefix form and the current `relative()` form agree on every case here.
+ */
+
+import { describe, test, expect } from 'bun:test';
+import { join, sep, parse } from 'path';
+import { isWithinRoot } from '../src/commands/sync.ts';
+
+describe('isWithinRoot path containment (#774 NAV-1/NAV-2)', () => {
+ const root = join(parse(process.cwd()).root, 'repo');
+
+ test('accepts the root itself', () => {
+ expect(isWithinRoot(root, root)).toBe(true);
+ });
+
+ test('accepts a direct child', () => {
+ expect(isWithinRoot(join(root, 'page.md'), root)).toBe(true);
+ });
+
+ test('accepts a nested descendant (the case a hardcoded "/" broke on win32)', () => {
+ expect(isWithinRoot(join(root, 'wiki', 'notes', 'page.md'), root)).toBe(true);
+ });
+
+ test('rejects the parent directory', () => {
+ expect(isWithinRoot(parse(root).dir, root)).toBe(false);
+ });
+
+ test('rejects a path escaping upward', () => {
+ expect(isWithinRoot(join(root, '..', 'outside', 'page.md'), root)).toBe(false);
+ });
+
+ test('rejects a sibling sharing the root as a string prefix', () => {
+ expect(isWithinRoot(root + '-evil', root)).toBe(false);
+ expect(isWithinRoot(join(root + '-evil', 'page.md'), root)).toBe(false);
+ });
+
+ test('rejects a sibling whose name extends the last segment', () => {
+ expect(isWithinRoot(join(parse(root).dir, 'repository', 'page.md'), root)).toBe(false);
+ });
+
+ test('containment does not depend on a hardcoded forward slash', () => {
+ // On win32 `sep` is '\\'; the child below is genuinely inside `root` and a
+ // `root + '/'` prefix test would have rejected it.
+ const child = `${root}${sep}sub${sep}page.md`;
+ expect(isWithinRoot(child, root)).toBe(true);
+ });
+});