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): reject distinct files during case-only renames

Zhao Dengfeng3486724d356b
diff --git a/packages/file-browser-core/src/__tests__/scanner.test.ts b/packages/file-browser-core/src/__tests__/scanner.test.ts
index 57288fecf2..af11760079 100644
--- a/packages/file-browser-core/src/__tests__/scanner.test.ts
+++ b/packages/file-browser-core/src/__tests__/scanner.test.ts
@@ -1,4 +1,14 @@
-import { mkdir, mkdtemp, readFile as fsReadFile, rm, symlink, writeFile as fsWriteFile } from 'node:fs/promises';
+import {
+ mkdir,
+ mkdtemp,
+ readFile as fsReadFile,
+ readdir as fsReaddir,
+ rename as fsRename,
+ rm,
+ stat as fsStat,
+ symlink,
+ writeFile as fsWriteFile,
+} from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
@@ -155,6 +165,55 @@ describe('file-browser scanner path boundaries', () => {
});
});
+describe('file-browser scanner case-only renames', () => {
+ it('rejects a case-only rename when source and destination are different files', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
+ try {
+ const source = path.join(root, 'Foo.md');
+ const destination = path.join(root, 'foo.md');
+ await fsWriteFile(source, 'source', 'utf8');
+ await fsWriteFile(destination, 'destination', 'utf8');
+ const [sourceStat, destinationStat] = await Promise.all([fsStat(source), fsStat(destination)]);
+ // Case-insensitive hosts cannot hold the two-file fixture. Keep this regression
+ // active on case-sensitive CI hosts instead of turning an overwrite into a pass.
+ if (sourceStat.dev === destinationStat.dev && sourceStat.ino === destinationStat.ino) return;
+
+ await expect(renameEntry(root, 'Foo.md', 'foo.md')).rejects.toThrow('目标已存在: foo.md');
+ await expect(fsReadFile(source, 'utf8')).resolves.toBe('source');
+ await expect(fsReadFile(destination, 'utf8')).resolves.toBe('destination');
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+
+ it('allows a genuine case-only rename when the host filesystem supports it', async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'xdt-file-browser-'));
+ try {
+ const probe = path.join(root, 'probe');
+ const workdir = path.join(root, 'workdir');
+ await mkdir(probe);
+ await mkdir(workdir);
+ await fsWriteFile(path.join(probe, 'Probe.md'), 'probe', 'utf8');
+ try {
+ await fsRename(path.join(probe, 'Probe.md'), path.join(probe, 'probe.md'));
+ } catch {
+ return;
+ }
+
+ await fsWriteFile(path.join(workdir, 'Foo.md'), 'source', 'utf8');
+ await expect(renameEntry(workdir, 'Foo.md', 'foo.md')).resolves.toMatchObject({
+ relPath: 'foo.md',
+ type: 'file',
+ size: 6,
+ });
+ await expect(fsReadFile(path.join(workdir, 'foo.md'), 'utf8')).resolves.toBe('source');
+ await expect(fsReaddir(workdir)).resolves.toEqual(['foo.md']);
+ } 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 3bd1af9a9b..cb470f5976 100644
--- a/packages/file-browser-core/src/scanner.ts
+++ b/packages/file-browser-core/src/scanner.ts
@@ -582,10 +582,9 @@ export async function createFolder(
* to 都走 assertInsideWorkdir 防越界。
*
* 冲突策略:
- * - 目标已存在 → 抛错(留给上层 toast)。EXCEPT 大小写仅改写的情况:
- * NTFS / HFS+ 默认是 case-insensitive,'Foo.md' → 'foo.md' 时 access 会
- * 误报"存在",但 fs.rename 本身能正确改名。所以仅在大小写不同时跳过冲突
- * 检查。
+ * - 目标已存在 → 抛错(留给上层 toast)。大小写仅改写时比较文件身份:
+ * NTFS / HFS+ 默认是 case-insensitive,'Foo.md' → 'foo.md' 指向同一文件,
+ * 可以放行;case-sensitive FS 上两个名字也可能是不同文件,不能静默覆盖。
* - source 不存在 → fs.rename 自然抛 ENOENT,上层 toast。
* - 跨卷不会发生,因为 from/to 都在同一 workdir 下。
*/
@@ -614,10 +613,18 @@ export async function renameEntry(
const absTo = path.join(workdir, toSub);
const realFrom = await assertRealPathInsideWorkdir(workdir, absFrom);
await assertRealParentInsideWorkdir(workdir, absTo);
- // 大小写仅改写检查:若 normalize 后小写相同,跳过 conflict 检查 — case-only
- // rename 在 case-insensitive FS 上需要直接 rename。
const caseOnly = fromSub.toLowerCase() === toSub.toLowerCase();
- if (!caseOnly) {
+ if (caseOnly) {
+ try {
+ const [fromStat, toStat] = await Promise.all([fs.stat(realFrom), fs.stat(absTo)]);
+ if (fromStat.dev !== toStat.dev || fromStat.ino !== toStat.ino) {
+ throw new Error(`目标已存在: ${toSub}`);
+ }
+ } catch (err) {
+ const code = (err as NodeJS.ErrnoException).code;
+ if (code !== 'ENOENT') throw err;
+ }
+ } else {
try {
await fs.access(absTo);
throw new Error(`目标已存在: ${toSub}`);