Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(file-browser): workdir 守卫拒绝绝对路径,不再静默改写成相对路径 (#577)

Silas7f950e6ce6c2
diff --git a/packages/file-browser-core/src/__tests__/scanner.test.ts b/packages/file-browser-core/src/__tests__/scanner.test.ts
index 663d38af10..2355bc96ac 100644
--- a/packages/file-browser-core/src/__tests__/scanner.test.ts
+++ b/packages/file-browser-core/src/__tests__/scanner.test.ts
@@ -98,6 +98,63 @@ describe('file-browser scanner symlink boundaries', () => {
});
});
+describe('file-browser scanner path boundaries', () => {
+ it('rejects absolute paths instead of silently resolving them workdir-relative', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
+ try {
+ // assertInsideWorkdir's docstring promises a throw on absolute paths. A
+ // leading slash must not be silently stripped and reinterpreted as
+ // `${workdir}/etc/passwd` — that turns a caller bug into a wrong-file read.
+ await expect(readFile(root, '/etc/passwd')).rejects.toThrow(/absolute path not allowed/);
+ await expect(statEntry(root, '/etc/passwd')).rejects.toThrow(/absolute path not allowed/);
+ // Backslashes are normalized to '/' first, so a Windows-style absolute
+ // path hits the same guard rather than slipping through.
+ await expect(statEntry(root, '\\Windows\\system32')).rejects.toThrow(
+ /absolute path not allowed/,
+ );
+ // Windows drive paths are rejected via a host-independent check
+ // (path.win32.isAbsolute + a drive-letter regex), so they never reach
+ // path.resolve where a Windows host would mis-interpret them. This covers
+ // both drive-*absolute* ('C:\\x' / 'C:/x') and drive-*relative* ('C:foo' /
+ // 'C:') — the latter is Windows drive-relative syntax (resolved against
+ // drive C:'s CWD), not a literal workdir entry, so it must not slip through.
+ await expect(statEntry(root, 'C:\\Windows\\system32')).rejects.toThrow(
+ /absolute path not allowed/,
+ );
+ await expect(readFile(root, 'C:/Windows/system32')).rejects.toThrow(
+ /absolute path not allowed/,
+ );
+ await expect(statEntry(root, 'C:foo')).rejects.toThrow(/absolute path not allowed/);
+ await expect(statEntry(root, 'C:')).rejects.toThrow(/absolute path not allowed/);
+ await expect(readFile(root, 'c:bar')).rejects.toThrow(/absolute path not allowed/);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+
+ it('rejects parent-traversal that escapes the workdir', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
+ try {
+ await expect(readFile(root, '../outside.txt')).rejects.toThrow(/escapes workdir/);
+ await expect(statEntry(root, '../../etc/passwd')).rejects.toThrow(/escapes workdir/);
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+
+ it('still normalizes a leading "./" to a plain workdir-relative path', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
+ try {
+ await fsWriteFile(path.join(root, 'note.txt'), 'hi', 'utf8');
+ const stat = await statEntry(root, './note.txt');
+ expect(stat.relPath).toBe('note.txt');
+ expect(stat.type).toBe('file');
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+});
+
describe('readFileChunk', () => {
it('reassembles a file losslessly across chunk boundaries (binary, no zero-padding)', async () => {
const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
diff --git a/packages/file-browser-core/src/scanner.ts b/packages/file-browser-core/src/scanner.ts
index d1f847075e..099c4d533a 100644
--- a/packages/file-browser-core/src/scanner.ts
+++ b/packages/file-browser-core/src/scanner.ts
@@ -72,11 +72,26 @@ export interface FileStat {
function assertInsideWorkdir(workdir: string, relPath: string): string {
// Normalize and reject anything that escapes the workdir. We resolve to
// absolute and verify the resolved path starts with workdir + sep.
- const cleaned = relPath.replace(/\\/g, '/').replace(/^\.?\/+/, '');
- if (cleaned === '' || cleaned === '.') return '';
- if (cleaned.startsWith('/')) {
+ // Reject anything absolute on ANY host OS, checked on the raw input so the
+ // guard is host-independent (this browser runs on macOS/Windows/Linux):
+ // - path.posix.isAbsolute → POSIX absolute "/etc/passwd";
+ // - path.win32.isAbsolute → "\x", UNC "\\srv\share", drive-absolute "C:\x"/"C:/x";
+ // - /^[a-zA-Z]:/ → drive-*relative* "C:foo"/"C:", which win32.isAbsolute
+ // misses yet path.resolve on Windows reinterprets against drive C:'s current
+ // directory (NOT as a workdir entry literally named "C:foo"). A ":"-carrying
+ // drive token is Windows path syntax and cannot be a workdir-relative POSIX
+ // name cross-platform, so reject it rather than silently mis-resolving it.
+ if (
+ path.posix.isAbsolute(relPath) ||
+ path.win32.isAbsolute(relPath) ||
+ /^[a-zA-Z]:/.test(relPath)
+ ) {
throw new Error(`absolute path not allowed: ${relPath}`);
}
+ // What remains is a workdir-relative path: normalize separators and strip a
+ // leading "./". Traversal ("../…") is caught by the containment check below.
+ const cleaned = relPath.replace(/\\/g, '/').replace(/^\.\/+/, '');
+ if (cleaned === '' || cleaned === '.') return '';
const abs = path.resolve(workdir, cleaned);
const wdAbs = path.resolve(workdir);
if (abs !== wdAbs && !abs.startsWith(wdAbs + path.sep)) {