Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): reject win32 traversal in path containment (PRD-8341) (#4038)

tim-inkeepf6f8e7da42ef
diff --git a/.changeset/win32-exec-path-containment.md b/.changeset/win32-exec-path-containment.md
new file mode 100644
index 000000000..4d8705d79
--- /dev/null
+++ b/.changeset/win32-exec-path-containment.md
@@ -0,0 +1,11 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Path containment holds on Windows.
+
+The check that keeps agent tools inside the project root compared the resolved relative path against a literal `../` prefix. On Windows the separator is a backslash, so a traversal written as `..\` was not recognised and the path was treated as contained.
+
+`exec` does sandbox its shell to a root, but that root is whatever the `cwd` argument resolved to. When the `cwd` itself escaped, the sandbox was faithfully rooted at the escaped directory and had nothing left to catch. That asymmetry is what made this reachable.
+
+Separators are now normalised before the check. Absolute paths, other drives, and UNC shares were already refused and still are. Windows was the only platform at risk, though one behaviour changes everywhere: a file whose name literally begins with `..\` is now refused as well.
diff --git a/packages/server/src/mcp/tools/path-safety.test.ts b/packages/server/src/mcp/tools/path-safety.test.ts
index 5a8fd1f64..547eff6c2 100644
--- a/packages/server/src/mcp/tools/path-safety.test.ts
+++ b/packages/server/src/mcp/tools/path-safety.test.ts
@@ -1,5 +1,6 @@
+import { win32 } from 'node:path';
import { describe, expect, test } from 'vitest';
-import { resolveWithinRoot } from './path-safety.ts';
+import { escapesRoot, resolveWithinRoot } from './path-safety.ts';
describe('resolveWithinRoot — accepts inputs contained in root', () => {
test('plain relative path', () => {
@@ -107,3 +108,37 @@ describe('resolveWithinRoot — rejects escapes', () => {
expect(result.ok).toBe(false);
});
});
+
+describe('escapesRoot — win32 separators (PRD-8341)', () => {
+ const root = 'C:\\srv\\project';
+
+ test.each([
+ ['..\\etc\\passwd'],
+ ['..\\..\\..\\Windows\\System32\\config\\SAM'],
+ ['articles\\..\\..\\secrets.md'],
+ ])('refuses win32 traversal %s', (candidate) => {
+ expect(escapesRoot(win32.relative(root, win32.resolve(root, candidate)))).toBe(true);
+ });
+
+ test.each([
+ ['C:\\Windows\\win.ini'],
+ ['\\\\server\\share\\secrets.md'],
+ ['D:\\other.md'],
+ ])('refuses win32 absolute %s', (candidate) => {
+ const rel = win32.relative(root, win32.resolve(root, candidate));
+ expect(escapesRoot(rel) || win32.isAbsolute(rel)).toBe(true);
+ });
+
+ test.each([
+ ['articles\\auth.md'],
+ ['articles/auth.md'],
+ ['..abc\\notes.md'],
+ ['deep\\..\\ok.md'],
+ ])('still accepts contained win32 path %s', (candidate) => {
+ expect(escapesRoot(win32.relative(root, win32.resolve(root, candidate)))).toBe(false);
+ });
+
+ test('refuses a posix name that mimics a win32 escape', () => {
+ expect(escapesRoot('..\\etc')).toBe(true);
+ });
+});
diff --git a/packages/server/src/mcp/tools/path-safety.ts b/packages/server/src/mcp/tools/path-safety.ts
index d9ccb01b2..425013a7b 100644
--- a/packages/server/src/mcp/tools/path-safety.ts
+++ b/packages/server/src/mcp/tools/path-safety.ts
@@ -13,6 +13,11 @@ interface ContainmentErr {
type ContainmentResult = ContainmentOk | ContainmentErr;
+export function escapesRoot(rel: string): boolean {
+ const forwardSlashed = rel.replace(/\\/g, '/');
+ return forwardSlashed === '..' || forwardSlashed.startsWith('../') || isAbsolute(rel);
+}
+
export function resolveWithinRoot(root: string, candidate: string): ContainmentResult {
if (typeof root !== 'string' || !isAbsolute(root)) {
return { ok: false, reason: `root path is not absolute: ${String(root)}` };
@@ -29,7 +34,7 @@ export function resolveWithinRoot(root: string, candidate: string): ContainmentR
if (rel === '') {
return { ok: true, abs, rel: '' };
}
- if (rel === '..' || rel.startsWith('../') || isAbsolute(rel)) {
+ if (escapesRoot(rel)) {
return {
ok: false,
reason: `path "${candidate}" escapes the configured root`,