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): block local terminal for remote sessions (#5342)

Tsai Yuan949e08060e1f
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
index fb448f74be..a706006334 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
@@ -19,7 +19,7 @@ import { Button } from '@/components/ui/button';
*/
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
-import { RotateCw, AlertTriangle } from 'lucide-react';
+import { RotateCw, AlertTriangle, ServerOff } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { Spinner } from '@/components/ui/spinner';
@@ -46,6 +46,32 @@ interface RuntimeError {
}
export function TerminalTabBody({ state, ctx, active, shellVisible = true }: Props) {
+ // The PTY IPC always executes on this Desktop instance. Remote workdirs must
+ // fail closed, including while device-link ownership is still unresolved.
+ const isLocalSession = ctx.remoteHostId === null && ctx.deviceLinkDeviceId === null;
+ if (!isLocalSession) return <RemoteTerminalUnavailable />;
+
+ return <LocalTerminalTabBody state={state} ctx={ctx} active={active} shellVisible={shellVisible} />;
+}
+
+function RemoteTerminalUnavailable() {
+ const { t } = useTranslation();
+ return (
+ <div className="flex h-full min-w-0 select-none items-center justify-center gap-2.5 bg-[var(--panel-bg)] p-6 text-left text-[var(--text-tertiary)]">
+ <ServerOff aria-hidden size={18} strokeWidth={1.6} />
+ <div className="max-w-[300px]">
+ <div className="text-13 font-medium leading-[1.384615] text-[var(--text-primary)]">
+ {t('rightSidebar.terminal.remoteUnavailableTitle')}
+ </div>
+ <div className="mt-[3px] text-12 leading-[1.5] text-[var(--text-secondary)]">
+ {t('rightSidebar.terminal.remoteUnavailableDescription')}
+ </div>
+ </div>
+ </div>
+ );
+}
+
+function LocalTerminalTabBody({ state, ctx, active, shellVisible = true }: Props) {
const { tabId, workdir, patchState } = ctx;
const { t } = useTranslation();
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx
index 56471621ee..6467f06b4f 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { cleanup, render } from '@testing-library/react';
+import { cleanup, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { TabKindHostContext } from '../../../types';
@@ -41,15 +41,17 @@ class TestResizeObserver {
unobserve() {}
}
-function makeContext(): TabKindHostContext {
+function makeContext(overrides: Partial<TabKindHostContext> = {}): TabKindHostContext {
return {
tabId: 'terminal-1',
sessionId: 'session-1',
workdir: '/workspace',
remoteHostId: null,
+ deviceLinkDeviceId: null,
patchState: vi.fn(),
onVisibilityChange: vi.fn(),
setCloseInterceptor: vi.fn(() => vi.fn()),
+ ...overrides,
};
}
@@ -147,3 +149,61 @@ describe('TerminalTabBody sidebar visibility', () => {
expect(resize).not.toHaveBeenCalled();
});
});
+
+describe('TerminalTabBody remote ownership', () => {
+ it('continues to create a PTY for a confirmed local session', async () => {
+ render(
+ <TerminalTabBody
+ state={{ created: false, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={makeContext()}
+ active
+ />,
+ );
+
+ await waitFor(() => expect(window.electronAPI.terminal.create).toHaveBeenCalledWith(
+ expect.objectContaining({ id: 'terminal-1', cwd: '/workspace' }),
+ ));
+ });
+
+ it('creates the local PTY after device-link ownership resolves from unknown to local', async () => {
+ const unresolvedContext = makeContext({ deviceLinkDeviceId: undefined });
+ const state = { created: false, exited: null, title: '', shellId: '', shellDisplayName: '' };
+ const view = render(<TerminalTabBody state={state} ctx={unresolvedContext} active />);
+
+ expect(screen.getByText('rightSidebar.terminal.remoteUnavailableTitle')).toBeTruthy();
+ expect(window.electronAPI.terminal.create).not.toHaveBeenCalled();
+
+ view.rerender(
+ <TerminalTabBody
+ state={state}
+ ctx={{ ...unresolvedContext, deviceLinkDeviceId: null }}
+ active
+ />,
+ );
+
+ await waitFor(() => expect(window.electronAPI.terminal.create).toHaveBeenCalledWith(
+ expect.objectContaining({ id: 'terminal-1', cwd: '/workspace' }),
+ ));
+ expect(screen.queryByText('rightSidebar.terminal.remoteUnavailableTitle')).toBeNull();
+ });
+
+ it.each([
+ ['an SSH session', { remoteHostId: 'ssh-host-1', deviceLinkDeviceId: null }],
+ ['a device-link session', { remoteHostId: null, deviceLinkDeviceId: 'device-1' }],
+ ['unresolved device-link ownership', { remoteHostId: null, deviceLinkDeviceId: undefined }],
+ ])('fails closed for %s without creating a local PTY', (_label, ownership) => {
+ const ctx = makeContext(ownership);
+ render(
+ <TerminalTabBody
+ state={{ created: false, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={ctx}
+ active
+ />,
+ );
+
+ expect(screen.getByText('rightSidebar.terminal.remoteUnavailableTitle')).toBeTruthy();
+ expect(screen.getByText('rightSidebar.terminal.remoteUnavailableDescription')).toBeTruthy();
+ expect(window.electronAPI.terminal.create).not.toHaveBeenCalled();
+ expect(entry.terminal.open).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index 9ad84cb9e2..70ae377f0a 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -7226,7 +7226,9 @@
"processKilled": "Process killed (signal {{signal}})",
"restart": "Restart",
"shellNotFound": "Shell not found: {{detail}}",
- "spawnFailed": "Failed to start terminal: {{detail}}"
+ "spawnFailed": "Failed to start terminal: {{detail}}",
+ "remoteUnavailableTitle": "Unavailable for remote sessions",
+ "remoteUnavailableDescription": "Terminal is currently available only for sessions running on this device."
},
"review": {
"turn": {
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 8342b553da..1b2a482d9c 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -7224,7 +7224,9 @@
"processKilled": "プロセスが強制終了されました (シグナル {{signal}})",
"restart": "再起動",
"shellNotFound": "シェルが見つかりません: {{detail}}",
- "spawnFailed": "ターミナルを起動できません: {{detail}}"
+ "spawnFailed": "ターミナルを起動できません: {{detail}}",
+ "remoteUnavailableTitle": "リモートセッションでは利用できません",
+ "remoteUnavailableDescription": "ターミナルは現在、このデバイスでローカル実行されるセッションでのみ利用できます。"
},
"review": {
"turn": {
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index cbbbb29517..25fa485c13 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -7224,7 +7224,9 @@
"processKilled": "프로세스가 강제 종료되었습니다 (신호 {{signal}})",
"restart": "다시 시작",
"shellNotFound": "셸을 찾을 수 없습니다: {{detail}}",
- "spawnFailed": "터미널을 시작할 수 없습니다: {{detail}}"
+ "spawnFailed": "터미널을 시작할 수 없습니다: {{detail}}",
+ "remoteUnavailableTitle": "원격 세션에서는 사용할 수 없음",
+ "remoteUnavailableDescription": "터미널은 현재 이 기기에서 로컬로 실행되는 세션에서만 사용할 수 있습니다."
},
"review": {
"turn": {
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 05b517c498..8eefede6e7 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -7224,7 +7224,9 @@
"processKilled": "进程已终止(信号 {{signal}})",
"restart": "重启",
"shellNotFound": "未找到可用 shell:{{detail}}",
- "spawnFailed": "无法启动终端:{{detail}}"
+ "spawnFailed": "无法启动终端:{{detail}}",
+ "remoteUnavailableTitle": "远程任务暂不可用",
+ "remoteUnavailableDescription": "终端目前仅适用于在此设备上运行的本机任务。"
},
"review": {
"turn": {
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index b93b3f20ff..01e1ccc02a 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -7224,7 +7224,9 @@
"processKilled": "處理程序已終止(訊號 {{signal}})",
"restart": "重啟",
"shellNotFound": "未找到可用 shell:{{detail}}",
- "spawnFailed": "無法啟動終端:{{detail}}"
+ "spawnFailed": "無法啟動終端:{{detail}}",
+ "remoteUnavailableTitle": "遠端任務暫不可用",
+ "remoteUnavailableDescription": "終端目前僅適用於在此裝置上執行的本機任務。"
},
"review": {
"turn": {