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(plugins): reject Windows network links before resolution

Dashf0493efe055a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/pluginWorkerDirectoryWindows.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/pluginWorkerDirectoryWindows.test.ts
new file mode 100644
index 0000000000..5cdc322727
--- /dev/null
+++ b/apps/desktop/src/main/maker-ipc/__tests__/pluginWorkerDirectoryWindows.test.ts
@@ -0,0 +1,55 @@
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { expect, it, vi } from 'vitest';
+import ts from 'typescript';
+import { PluginTaskError } from '../pluginTaskService.js';
+
+// Execute the production resolver with Windows path semantics, even on macOS.
+const source = readFileSync(new URL('../pluginWorkerDirectory.ts', import.meta.url), 'utf8');
+const js = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, esModuleInterop: true } }).outputText;
+function fixture(links: Record<string, string>) {
+ const fs = {
+ lstat: vi.fn(async (p: string) => ({isSymbolicLink:()=>p in links})),
+ readlink: vi.fn(async (p: string) => links[p]),
+ realpath: vi.fn(async (p: string) => p),
+ stat: vi.fn(async () => ({isDirectory:()=>true})),
+ };
+ const exports: Record<string, any> = {};
+ const inside=(root:string,dir:string)=>{const relative=path.win32.relative(root,dir);return !relative||(!relative.startsWith('..')&&!path.win32.isAbsolute(relative));};
+ new Function('require','exports','process',js)((id:string)=>id==='node:path'?path.win32:id==='node:fs/promises'?fs:id.endsWith('dirDeposit.js')?{isPathInsideDir:inside}:{PluginTaskError},exports,{platform:'win32'});
+ return {fs,resolve:exports.resolvePluginWorkerDirectory};
+}
+
+it.each(['\\\\server\\share','//server/share','\\\\?\\UNC\\server\\share','\\??\\UNC\\server\\share','\\\\.\\pipe\\name'])('rejects a Windows link to %s before following it',async target=>{
+ const f=fixture({'C:\\picked':target});
+ await expect(f.resolve({requested:'C:\\picked\\child',leadDirectory:'C:\\picked',isPickedDirectory:()=>false,assertCurrent:()=>{}})).rejects.toMatchObject({code:'PERMISSION_DENIED'});
+ expect(f.fs.realpath).not.toHaveBeenCalled();expect(f.fs.stat).not.toHaveBeenCalled();
+ expect(f.fs.lstat.mock.calls.map(([p])=>p)).toEqual(['C:\\picked']);
+});
+it('checks multi-hop links, stored roots and cycles before canonical lookup',async()=>{
+ for(const links of [
+ {'C:\\picked':'C:\\alias','C:\\alias':'\\\\server\\share'},
+ {'C:\\picked':'C:\\alias','C:\\alias':'C:\\picked'},
+ ]){
+ const f=fixture(links);
+ await expect(f.resolve({requested:'C:\\picked',configuredDirectory:'C:\\picked',isPickedDirectory:()=>false,assertCurrent:()=>{}})).rejects.toMatchObject({code:'PERMISSION_DENIED'});
+ expect(f.fs.realpath).not.toHaveBeenCalled();
+ }
+ const f=fixture({'C:\\stored':'\\\\server\\share'});
+ await expect(f.resolve({requested:'C:\\stored\\child',leadDirectory:'C:\\stored',isPickedDirectory:()=>false,assertCurrent:()=>{}})).rejects.toMatchObject({code:'PERMISSION_DENIED'});
+ expect(f.fs.realpath).not.toHaveBeenCalled();
+});
+it('retains local relative aliases, multi-hop aliases and local long-path syntax',async()=>{
+ const f=fixture({'C:\\alias':'target','C:\\target':'\\\\?\\C:\\selected'});
+ f.fs.realpath.mockImplementation(async p=>p==='C:\\alias'?'C:\\selected':p);
+ const input={requested:'C:\\alias',isPickedDirectory:(p:string)=>p==='C:\\alias'||p==='C:\\selected',assertCurrent:()=>{}};
+ await expect(f.resolve(input)).resolves.toBe('C:\\selected');
+ await expect(f.resolve({...input,requested:'\\\\?\\C:\\selected'})).resolves.toBe('C:\\selected');
+});
+
+it('anchors root-relative local link targets to the link drive',async()=>{
+ const f=fixture({'C:\\alias':'\\selected'});
+ await expect(f.resolve({requested:'C:\\alias',isPickedDirectory:(p:string)=>p==='C:\\alias'||p==='C:\\selected',assertCurrent:()=>{}})).resolves.toBe('C:\\selected');
+ expect(f.fs.realpath).toHaveBeenCalledWith('C:\\selected');
+ expect(f.fs.lstat.mock.calls.every(([p])=>p.startsWith('C:\\'))).toBe(true);
+});
diff --git a/apps/desktop/src/main/maker-ipc/pluginWorkerDirectory.ts b/apps/desktop/src/main/maker-ipc/pluginWorkerDirectory.ts
index b00ab9972b..47184241de 100644
--- a/apps/desktop/src/main/maker-ipc/pluginWorkerDirectory.ts
+++ b/apps/desktop/src/main/maker-ipc/pluginWorkerDirectory.ts
@@ -1,4 +1,4 @@
-import { realpath, stat } from 'node:fs/promises';
+import { lstat, readlink, realpath, stat } from 'node:fs/promises';
import path from 'node:path';
import { isPathInsideDir } from '../cindy-brain/dirDeposit.js';
import { PluginTaskError } from './pluginTaskService.js';
@@ -14,6 +14,36 @@ function localDirectory(directory: string): string {
return path.normalize(directory);
}
+// Inspect each Windows link before any API follows it. This handles existing
+// network links, not concurrent directory replacement or mapped network drives.
+async function localRealpath(directory: string): Promise<string> {
+ if (process.platform !== 'win32') return localDirectory(await realpath(directory));
+ let remaining = localDirectory(directory);
+ for (let hops = 0; hops < 40; hops += 1) {
+ const root = path.parse(remaining).root;
+ const parts = remaining.slice(root.length).split(path.sep).filter(Boolean);
+ let current = root;
+ let redirected = false;
+ for (let index = 0; index < parts.length; index += 1) {
+ current = path.join(current, parts[index]!);
+ if (!(await lstat(current)).isSymbolicLink()) continue;
+ const target = await readlink(current);
+ // Absolute link targets pass the raw namespace check before normalization.
+ // Drive-relative targets depend on process state rather than this directory.
+ if (/^[A-Za-z]:/.test(target) && !path.isAbsolute(target)) {
+ throw new PluginTaskError('PERMISSION_DENIED', 'Plugin task link must have a local unambiguous target');
+ }
+ const checkedTarget = path.isAbsolute(target) ? localDirectory(target) : target;
+ const localTarget = localDirectory(path.resolve(path.dirname(current), checkedTarget));
+ remaining = path.join(localTarget, ...parts.slice(index + 1));
+ redirected = true;
+ break;
+ }
+ if (!redirected) return localDirectory(await realpath(remaining));
+ }
+ throw new PluginTaskError('PERMISSION_DENIED', 'Plugin task directory contains too many links');
+}
+
/** Plans describe work; only host directory facts authorize it. */
export async function resolvePluginWorkerDirectory(input: {
requested: string;
@@ -34,11 +64,11 @@ export async function resolvePluginWorkerDirectory(input: {
if (!(leadRoot && isPathInsideDir(leadRoot, candidate))
&& !(configuredRoot && sameDirectory(configuredRoot, candidate))
&& !input.isPickedDirectory(candidate)) throw deny();
- const resolved = localDirectory(await realpath(candidate));
+ const resolved = await localRealpath(candidate);
if (!(await stat(resolved)).isDirectory()) throw deny();
// Stored Host roots are canonical identities, not aliases to resolve into new grants.
const unchangedRoot = async (stored: string) => {
- const current = localDirectory(await realpath(stored));
+ const current = await localRealpath(stored);
return sameDirectory(stored, current) ? current : null;
};
let allowed = false;