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(desktop): normalize Windows realpath drive casing

Yuewei-LLa3f5bab9ac51
diff --git a/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts b/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
index 74c6b24e54..0575330fdb 100644
--- a/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
+++ b/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
@@ -212,6 +212,31 @@ describe('readBoundedFileNoFollow', () => {
},
);
+ it.runIf(process.platform === 'win32')(
+ 'containWithin accepts the same Windows drive with different realpath drive-letter casing',
+ async () => {
+ const file = path.join(workDir, 'drive-case.json');
+ await fs.promises.writeFile(file, '{"ok":true}');
+ const realRoot = fs.realpathSync(workDir);
+ const alternateDriveCase = realRoot.replace(/^([A-Za-z]):/, (_, drive: string) => (
+ `${drive === drive.toLowerCase() ? drive.toUpperCase() : drive.toLowerCase()}:`
+ ));
+
+ expect(
+ (await readBoundedFileNoFollow(file, 1024, {
+ containWithin: alternateDriveCase,
+ noFollowFlag: null,
+ }))?.toString('utf8'),
+ ).toBe('{"ok":true}');
+ expect(
+ readBoundedFileNoFollowSync(file, 1024, {
+ containWithin: alternateDriveCase,
+ noFollowFlag: null,
+ })?.toString('utf8'),
+ ).toBe('{"ok":true}');
+ },
+ );
+
it.runIf(process.platform !== 'win32')('nonBlocking: FIFO 不阻塞并被拒绝', async () => {
const fifo = path.join(workDir, 'icon');
if (spawnSync('mkfifo', [fifo]).status !== 0) return;
diff --git a/apps/desktop/src/main/utils/readBoundedFile.ts b/apps/desktop/src/main/utils/readBoundedFile.ts
index dda046d32a..442b39f32b 100644
--- a/apps/desktop/src/main/utils/readBoundedFile.ts
+++ b/apps/desktop/src/main/utils/readBoundedFile.ts
@@ -75,10 +75,23 @@ export class BoundedFileReadChangedError extends Error {
}
/** realpath 产物是否落在同为 realpath 产物的根内(含根本身)。 */
+function normalizeRealPathForComparison(realPath: string): string {
+ if (process.platform !== 'win32') return realPath;
+ // Node's sync and async realpath implementations may preserve different
+ // casing for a Windows drive letter even though both paths name the same
+ // volume. Normalize only that OS-defined case-insensitive component; the
+ // opened-handle inode checks below still prove the file's identity.
+ return realPath.replace(/^([A-Z]):/, (_, drive: string) => `${drive.toLowerCase()}:`);
+}
+
function isWithinRoot(realFilePath: string, realRoot: string): boolean {
- if (realFilePath === realRoot) return true;
- const rootWithSep = realRoot.endsWith(path.sep) ? realRoot : `${realRoot}${path.sep}`;
- return realFilePath.startsWith(rootWithSep);
+ const comparableFilePath = normalizeRealPathForComparison(realFilePath);
+ const comparableRoot = normalizeRealPathForComparison(realRoot);
+ if (comparableFilePath === comparableRoot) return true;
+ const rootWithSep = comparableRoot.endsWith(path.sep)
+ ? comparableRoot
+ : `${comparableRoot}${path.sep}`;
+ return comparableFilePath.startsWith(rootWithSep);
}
/**