Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(file-browser): stop atomic save from following a planted temp symlink
Zhao Dengfeng036410ef6385
diff --git a/packages/file-browser-core/src/__tests__/scanner.test.ts b/packages/file-browser-core/src/__tests__/scanner.test.tsindex af11760079..69f3524867 100644--- a/packages/file-browser-core/src/__tests__/scanner.test.ts+++ b/packages/file-browser-core/src/__tests__/scanner.test.ts@@ -1,4 +1,5 @@import {+ lstat,mkdir,mkdtemp,readFile as fsReadFile,@@ -62,6 +63,29 @@ describe('file-browser scanner symlink boundaries', () => {}});+ it('does not follow a pre-planted .xdt-tmp symlink when saving an in-workdir file', async () => {+ const fixture = await makeSymlinkFixture();+ try {+ if (fixture.kind === 'skip') return;+ const outside = path.join(fixture.root, 'outside.txt');+ const target = path.join(fixture.workdir, 'notes.txt');+ await fsWriteFile(target, 'original', 'utf8');+ // 旧实现的临时路径固定为 `<target>.xdt-tmp`,预置成逃逸链接即可越界写入。+ await symlink(outside, `${target}.xdt-tmp`, 'file');++ await writeFile(fixture.workdir, 'notes.txt', 'edited');++ expect(await fsReadFile(outside, 'utf8')).toBe('secret outside workdir');+ expect(await fsReadFile(target, 'utf8')).toBe('edited');+ expect((await lstat(target)).isSymbolicLink()).toBe(false);+ // 只剩预置的链接,本次写入没有残留自己的临时文件。+ expect((await fsReaddir(fixture.workdir)).filter((name) => name.endsWith('.xdt-tmp')))+ .toEqual(['notes.txt.xdt-tmp']);+ } finally {+ await rm(fixture.root, { recursive: true, force: true });+ }+ });+it('rejects create operations when the parent directory is an escaping symlink', async () => {const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));const workdir = path.join(root, 'workdir');diff --git a/packages/file-browser-core/src/scanner.ts b/packages/file-browser-core/src/scanner.tsindex cb470f5976..e151d33153 100644--- a/packages/file-browser-core/src/scanner.ts+++ b/packages/file-browser-core/src/scanner.ts@@ -13,6 +13,7 @@* `assertInsideWorkdir` — renderer cannot ask for `../../etc/passwd`.*/+import { randomBytes } from 'node:crypto';import { promises as fs, type Stats, type Dirent } from 'node:fs';import path from 'node:path';@@ -57,7 +58,7 @@ function hasPdfExtension(relPath: string): boolean {}/**- * 原子写中间产物的后缀。writeFile 把内容先落到 `${target}.xdt-tmp`,fsync 后+ * 原子写中间产物的后缀。writeFile 把内容先落到 `${target}.<随机>.xdt-tmp`,fsync 后* rename 成正式名。这个临时文件理论上只活一两毫秒,但在边栏 listDir 路径上仍* 可能被瞥见 → 一闪而过的 ghost row,视觉上像"刷一下"。*@@ -471,7 +472,7 @@ export async function readFileChunk(/*** Atomically write text content to a file inside workdir. Pattern: write to- * `${target}.xdt-tmp`, fsync handle, rename → target. Crash-safe (consumer+ * `${target}.<random>.xdt-tmp`, fsync handle, rename → target. Crash-safe (consumer* either sees old content or new, never partial).** Constraints:@@ -522,17 +523,25 @@ export async function writeFile(await probeHandle.close();}- const tmp = `${realAbs}${XDT_TMP_SUFFIX}`;- const handle = await fs.open(tmp, 'w');+ // 临时名带随机段并以 'wx' 独占创建:固定名可被工作目录里预置的符号链接+ // 占位,'w' 会跟随链接写到工作目录外,rename 还会把链接本身搬成目标文件。+ // 仍以 XDT_TMP_SUFFIX 结尾,listDir / watcher 的过滤照旧生效。+ const tmp = `${realAbs}.${randomBytes(6).toString('hex')}${XDT_TMP_SUFFIX}`;+ const handle = await fs.open(tmp, 'wx');try {- await handle.writeFile(buf);- await handle.sync();- } finally {- await handle.close();+ try {+ await handle.writeFile(buf);+ await handle.sync();+ } finally {+ await handle.close();+ }+ // rename is atomic on same filesystem (which .xdt-tmp guarantees since+ // we wrote it in the same directory).+ await fs.rename(tmp, realAbs);+ } catch (err) {+ await fs.rm(tmp, { force: true }).catch(() => {});+ throw err;}- // rename is atomic on same filesystem (which .xdt-tmp guarantees since- // we wrote it in the same directory).- await fs.rename(tmp, realAbs);const after = await fs.stat(realAbs);return { size: after.size, mtimeMs: after.mtimeMs };