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(plugin): reject remote agent network reuse

liugaoweifbb295d12167
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/cardService.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/cardService.test.ts
index aa51395b35..c5dcf843ba 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/cardService.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/cardService.test.ts
@@ -305,8 +305,20 @@ describe('withCardToken(令牌注入纯函数)', () => {
it('inFlightCallInfoOf 只认真正在途:交卷/重开/查无一律 null(workspace 凭证语义)', () => {
const { svc } = makeService();
- svc.registerCall('c1', { ghostId: 'g1', toolUseId: null, sessionId: 's1' });
- expect(svc.inFlightCallInfoOf('c1')).toEqual({ ghostId: 'g1', sessionId: 's1', scriptWorkdir: null, scriptWritePath: null, channel: 'session' });
+ svc.registerCall('c1', {
+ ghostId: 'g1',
+ toolUseId: null,
+ sessionId: 's1',
+ remoteHostId: 'ssh-host-1',
+ });
+ expect(svc.inFlightCallInfoOf('c1')).toEqual({
+ ghostId: 'g1',
+ sessionId: 's1',
+ remoteHostId: 'ssh-host-1',
+ scriptWorkdir: null,
+ scriptWritePath: null,
+ channel: 'session',
+ });
// 交卷后:宽限窗内 callInfoOf 仍可查(卡片供片语义),但在途凭证必须失效。
svc.finalizeCall('c1');
expect(svc.callInfoOf('c1')).not.toBeNull();
@@ -324,7 +336,7 @@ describe('withCardToken(令牌注入纯函数)', () => {
expect(svc.callInfoOf('c1')).toEqual({ ghostId: 'g1', sessionId: null, scriptWorkdir: 'D:\\proj' });
// 在途期间严格查询命中(带 scriptWorkdir);交卷后立即失效——不等宽限窗、
// 不等懒清扫(目录授权上下文用完即废,review M1)。
- expect(svc.inFlightCallInfoOf('c1')).toEqual({ ghostId: 'g1', sessionId: null, scriptWorkdir: 'D:\\proj', scriptWritePath: null, channel: 'script' });
+ expect(svc.inFlightCallInfoOf('c1')).toEqual({ ghostId: 'g1', sessionId: null, remoteHostId: undefined, scriptWorkdir: 'D:\\proj', scriptWritePath: null, channel: 'script' });
svc.finalizeCall('c1');
expect(svc.callInfoOf('c1')).not.toBeNull(); // 宽限窗:卡片供片语义保留
expect(svc.inFlightCallInfoOf('c1')).toBeNull(); // 目录授权:交卷即失效
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
index 83a73a8ed3..b5865dd593 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
@@ -210,7 +210,7 @@ describe('networkSlot · 能力资格审', () => {
getGhost: () => fakeGhost({ network: null }),
inFlightCallInfo: (callId) =>
callId === 'call-agent'
- ? { ghostId: 'web-search', sessionId: 'session-1', channel: 'session' }
+ ? { ghostId: 'web-search', sessionId: 'session-1', remoteHostId: null, channel: 'session' }
: null,
});
expect(
@@ -224,7 +224,7 @@ describe('networkSlot · 能力资格审', () => {
const wrongOwner = makeSlot({
getGhost: () => fakeGhost({ network: null }),
- inFlightCallInfo: () => ({ ghostId: 'another-plugin', sessionId: 'session-1', channel: 'session' }),
+ inFlightCallInfo: () => ({ ghostId: 'another-plugin', sessionId: 'session-1', remoteHostId: null, channel: 'session' }),
});
expect(
await wrongOwner.slot.handleFetchRequest('web-search', {
@@ -237,7 +237,7 @@ describe('networkSlot · 能力资格审', () => {
it('脚本/后台通道不能冒充 Agent 授权访问未声明域名', async () => {
const { slot } = makeSlot({
getGhost: () => fakeGhost({ network: null }),
- inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: null, channel: 'script' }),
+ inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: null, remoteHostId: undefined, channel: 'script' }),
});
expect(
await slot.handleFetchRequest('web-search', {
@@ -247,12 +247,33 @@ describe('networkSlot · 能力资格审', () => {
).toMatchObject({ ok: false });
});
+ it('SSH remote Agent 不能借本机 Desktop 出口访问未声明域名', async () => {
+ const { slot, fetchImpl, fetchPublicImpl } = makeSlot({
+ getGhost: () => fakeGhost({ network: null }),
+ inFlightCallInfo: () => ({
+ ghostId: 'web-search',
+ sessionId: 'session-remote',
+ remoteHostId: 'ssh-host-1',
+ channel: 'session',
+ }),
+ });
+
+ expect(
+ await slot.handleFetchRequest('web-search', {
+ url: 'https://example.com/data',
+ callId: 'call-agent',
+ }),
+ ).toMatchObject({ ok: false });
+ expect(fetchImpl).not.toHaveBeenCalled();
+ expect(fetchPublicImpl).not.toHaveBeenCalled();
+ });
+
it('Agent 未声明目标的 SSRF 守门失败时不回退普通 fetch', async () => {
const guardedError = new Error('Blocked: resolves to private/internal/special-use IP address');
const guardedFetch = vi.fn(async () => { throw guardedError; });
const { slot, fetchImpl } = makeSlot({
getGhost: () => fakeGhost({ network: null }),
- inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', channel: 'session' }),
+ inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', remoteHostId: null, channel: 'session' }),
fetchPublicImpl: guardedFetch,
});
@@ -280,7 +301,7 @@ describe('networkSlot · 能力资格审', () => {
getGhost: () => fakeGhost({ network: null }),
inFlightCallInfo: () =>
live
- ? { ghostId: 'web-search', sessionId: 'session-1', channel: 'session' }
+ ? { ghostId: 'web-search', sessionId: 'session-1', remoteHostId: null, channel: 'session' }
: null,
fetchPublicImpl: guardedFetch,
});
@@ -299,7 +320,7 @@ describe('networkSlot · 能力资格审', () => {
it('Agent 访问已声明目标仍使用声明边界,不额外阻断私有服务', async () => {
const { slot, fetchImpl, fetchPublicImpl } = makeSlot({
getGhost: () => fakeGhost({ network: { hosts: ['127.0.0.1'], secrets: [] } }),
- inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', channel: 'session' }),
+ inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', remoteHostId: null, channel: 'session' }),
});
const result = await slot.handleFetchRequest('web-search', {
@@ -457,7 +478,7 @@ describe('networkSlot · 重定向逐跳守门', () => {
.mockResolvedValueOnce({ response: fakeResponse(), release });
const { slot, fetchImpl } = makeSlot({
getGhost: () => fakeGhost({ network: null }),
- inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', channel: 'session' }),
+ inFlightCallInfo: () => ({ ghostId: 'web-search', sessionId: 'session-1', remoteHostId: null, channel: 'session' }),
fetchPublicImpl: guardedFetch,
});
@@ -476,7 +497,12 @@ describe('networkSlot · 重定向逐跳守门', () => {
});
it('Agent 交卷后不再跟进未声明目标的后续重定向', async () => {
- const liveCall = { ghostId: 'web-search', sessionId: 'session-1', channel: 'session' as const };
+ const liveCall = {
+ ghostId: 'web-search',
+ sessionId: 'session-1',
+ remoteHostId: null,
+ channel: 'session' as const,
+ };
const inFlightCallInfo = vi.fn()
// 入口资格、首跳资格与重定向目标预检仍在途。
.mockReturnValueOnce(liveCall)
diff --git a/apps/desktop/src/main/cindy-brain/cardService.ts b/apps/desktop/src/main/cindy-brain/cardService.ts
index 8fb40b64c3..2e8d5607b2 100644
--- a/apps/desktop/src/main/cindy-brain/cardService.ts
+++ b/apps/desktop/src/main/cindy-brain/cardService.ts
@@ -153,6 +153,11 @@ interface CallEntry {
ghostId: string;
toolUseId: string | null;
sessionId: string | null;
+ /**
+ * SSH remote 会话的 host id;本地会话必须显式登记 null。省略表示调用方
+ * 没有提供 locality 事实,能力消费方必须 fail closed,不能当成本地。
+ */
+ remoteHostId: string | null | undefined;
/**
* 调用通道:'session' = 会话内 ghost_call(默认);'script' = scheduler
* 「仅运行脚本」broker 直调。显式字段而非由 sessionId/scriptWorkdir 推导
@@ -233,6 +238,8 @@ export class GhostCardService {
ghostId: string;
toolUseId: string | null;
sessionId: string | null;
+ /** SSH remote host;本地会话显式传 null,未知时省略并 fail closed。 */
+ remoteHostId?: string | null;
/** 脚本通道调用方传入 schedule.workingDir;普通会话调用省略。 */
scriptWorkdir?: string | null;
/** 脚本通道调用方传入脚本声明的 out_file(唯一可写相对路径);无写窗时省略。 */
@@ -246,6 +253,7 @@ export class GhostCardService {
ghostId: info.ghostId,
toolUseId: info.toolUseId,
sessionId: info.sessionId,
+ remoteHostId: info.remoteHostId,
channel: info.channel ?? 'session',
scriptWorkdir: info.scriptWorkdir ?? null,
scriptWritePath: info.scriptWritePath ?? null,
@@ -299,10 +307,10 @@ export class GhostCardService {
* 结束后继续充当"目录授权上下文"——否则插件记住一个旧 callId 就能跨
* 调用复用当时会话的 workdir 自动放行。
*/
- inFlightCallInfoOf(callId: string): { ghostId: string; sessionId: string | null; scriptWorkdir: string | null; scriptWritePath: string | null; channel: 'session' | 'script' } | null {
+ inFlightCallInfoOf(callId: string): { ghostId: string; sessionId: string | null; remoteHostId: string | null | undefined; scriptWorkdir: string | null; scriptWritePath: string | null; channel: 'session' | 'script' } | null {
const e = this.calls.get(callId);
if (!e || e.settledAt !== null || e.reopenedAt !== null) return null;
- return { ghostId: e.ghostId, sessionId: e.sessionId, scriptWorkdir: e.scriptWorkdir, scriptWritePath: e.scriptWritePath, channel: e.channel };
+ return { ghostId: e.ghostId, sessionId: e.sessionId, remoteHostId: e.remoteHostId, scriptWorkdir: e.scriptWorkdir, scriptWritePath: e.scriptWritePath, channel: e.channel };
}
/**
@@ -326,6 +334,7 @@ export class GhostCardService {
ghostId: info.ghostId,
toolUseId: null,
sessionId: info.sessionId,
+ remoteHostId: undefined,
// 交互卡重开只发生在会话通道(脚本通道条目拒供片,无卡可点)。
channel: 'session',
scriptWorkdir: null,
diff --git a/apps/desktop/src/main/cindy-brain/networkSlot.ts b/apps/desktop/src/main/cindy-brain/networkSlot.ts
index 9e106c0dfc..36cda09831 100644
--- a/apps/desktop/src/main/cindy-brain/networkSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/networkSlot.ts
@@ -66,12 +66,15 @@ export interface NetworkSlotDeps {
getGhost(id: string): InstalledGhost | null;
/**
* callId → 严格在途的 Agent 调用上下文。只有 channel:'session'、
- * sessionId 存在且 ghostId 匹配时,才能复用外层 ghost_call 已经
- * 通过的 Cindy Agent 授权;面板、订阅、后台与脚本通道都不在此列。
+ * sessionId 存在、明确是本地会话(remoteHostId === null)且 ghostId
+ * 匹配时,才能复用外层 ghost_call 已经通过的 Cindy Agent 授权;
+ * 面板、订阅、后台、远程 SSH 与脚本通道都不在此列。
*/
inFlightCallInfo?(callId: string): {
ghostId: string;
sessionId: string | null;
+ /** null = 已证明是本地会话;string = SSH remote;undefined = 未知。 */
+ remoteHostId: string | null | undefined;
channel: 'session' | 'script';
} | null;
/**
@@ -1045,7 +1048,8 @@ export class GhostNetworkSlot {
: null;
return callInfo?.ghostId === ghostId
&& callInfo.channel === 'session'
- && callInfo.sessionId !== null;
+ && callInfo.sessionId !== null
+ && callInfo.remoteHostId === null;
};
const agentMediated = hasLiveAgentAuthorization();
// 静态 hosts 与动态连接地址(network.connections,用户在设置页添加、
diff --git a/apps/desktop/src/main/mcp-integrations/ghost.ts b/apps/desktop/src/main/mcp-integrations/ghost.ts
index c0cf8a7fbb..02c310c86a 100644
--- a/apps/desktop/src/main/mcp-integrations/ghost.ts
+++ b/apps/desktop/src/main/mcp-integrations/ghost.ts
@@ -1690,12 +1690,16 @@ export function getCindyGhostsMcpDeps(
// 据此配对取卡;没供过 = 结果零变化,模型永远看不到内部 UUID)。
const callId = randomUUID();
const cardService = getGhostCardService();
+ const callSessionContext = resolveSessionContext();
cardService.registerCall(callId, {
ghostId,
toolUseId: agentToolUseId ?? null,
// ALS 优先(codex 每单恢复)、闭包兜底(claude 建线期按 session 绑定)
// ——此前 claude 路径这里恒为 null,卡片只能靠 toolUseId 启发式锚定。
- sessionId: resolveSessionContext()?.sessionId ?? null,
+ sessionId: callSessionContext?.sessionId ?? null,
+ // 未声明 network 的 Agent 调用只能借本机 Agent 授权走 Desktop 出网;
+ // SSH remote 会话保留 host id,由 networkSlot 明确拒绝本地出口。
+ remoteHostId: callSessionContext?.remoteHostId ?? null,
});
// GhostToolCallResult 与 CindyGhostCallResult 同构(错误码枚举一致),
// 原样透传;类型层若有漂移 tsc 会拦。