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): 面板嵌入的本机页面可以再套一层同源帧,换版后沙箱按新安装重开

Dashe57f46794de3
diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.ts
index 3efaf61a61..67cf71ed83 100644
--- a/apps/desktop/src/main/cindy-brain/forge.ts
+++ b/apps/desktop/src/main/cindy-brain/forge.ts
@@ -4499,8 +4499,8 @@ if (!opened.ok) console.warn(opened.errorCode, opened.message);
\`preview.hosts\` 里的 \`localhost\` / \`127.0.0.1\` 的插件,其面板页可以把随包 Node
进程在本机提供的页面装进 \`<iframe>\`(主机为这类插件的页面 CSP 追加
\`frame-src 'self' http://127.0.0.1:* http://localhost:*\`);嵌入帧内的脚本、样式、
- XHR 与 WebSocket 只许回到同一主机:端口,面板页自身仍不能联网,嵌入页也不能申请
- 摄像头 / 定位等权限。用它做"没有浏览器栏的本机实时画面"(如模拟器),不要用它
+ XHR 与 WebSocket 只许回到同一主机:端口,嵌入页自己再套一层 \`<iframe>\` 也只许停在
+ 同一主机:端口;面板页自身仍不能联网,嵌入页也不能申请摄像头 / 定位等权限。用它做"没有浏览器栏的本机实时画面"(如模拟器),不要用它
绕开网络声明。
## 4.16 捆绑 Agent Skills(skill 能力)
diff --git a/apps/desktop/src/main/cindy-brain/runtime/GhostRuntime.ts b/apps/desktop/src/main/cindy-brain/runtime/GhostRuntime.ts
index 889ce61ce9..f125b672a6 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/GhostRuntime.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/GhostRuntime.ts
@@ -1,4 +1,4 @@
-import type { InstalledGhost } from '../../../shared/ghost.js';
+import { ghostInstallApprovalToken, type InstalledGhost } from '../../../shared/ghost.js';
import { installedGhostStoragePart } from '../../../shared/pluginIdentity.js';
/**
@@ -57,10 +57,27 @@ interface GhostRuntimeOptions {
interface RuntimeEntry {
state: GhostRuntimeState;
handle: SandboxHandle | null;
+ /** 当前沙箱按哪一份安装拉起(sandboxIdentity);换版后据此识别过期沙箱。 */
+ identity: string | null;
/** 熔断窗口内的崩溃时间戳(旧的滚动淘汰)。 */
crashTimestamps: number[];
}
+/**
+ * 沙箱的安装身份:adapter 按安装目录 + 安装回执划分区,面板 webview 也按同一份
+ * 现装记录解析分区。更新换版期间若有人(面板 /wake、常驻拉起)拿旧记录把沙箱
+ * 拉了起来,提交后它就留在旧分区,与重挂载的面板永远不同源——spawn 据此识别
+ * 并换新。
+ */
+function sandboxIdentity(ghost: InstalledGhost): string {
+ return JSON.stringify([
+ ghost.dir,
+ ghostInstallApprovalToken(ghost.approval),
+ ghost.manifest.version,
+ ghost.namespace ?? null,
+ ]);
+}
+
export class GhostRuntime {
private readonly options: Required<Pick<GhostRuntimeOptions, 'crashWindowMs' | 'crashLimit'>> &
GhostRuntimeOptions;
@@ -88,7 +105,13 @@ export class GhostRuntime {
async spawn(ghost: InstalledGhost): Promise<{ ok: true; state: GhostRuntimeState } | { ok: false; reason: string }> {
const id = installedGhostStoragePart(ghost);
const entry = this.getEntry(id);
- if (entry.state === 'starting' || entry.state === 'running') return { ok: true, state: entry.state };
+ const identity = sandboxIdentity(ghost);
+ if (entry.state === 'starting' || entry.state === 'running') {
+ if (entry.identity === identity) return { ok: true, state: entry.state };
+ // 旧安装的沙箱(更新换版窗口里被拉起)按新记录重开,否则它困在旧分区。
+ this.options.log?.info('ghost sandbox restarts for the updated install', { id });
+ this.stop(id);
+ }
if (entry.state === 'fused') return { ok: false, reason: '已熔断(反复崩溃),重新唤醒后可再试' };
if (entry.state === 'stopping') return { ok: false, reason: '正在熄灯,稍后再试' };
const blocked = this.options.startBlockedReason?.(id) ?? null;
@@ -106,6 +129,7 @@ export class GhostRuntime {
return { ok: false, reason: '沙箱创建失败' };
}
entry.handle = handle;
+ entry.identity = identity;
handle.onGone((details) => this.handleGone(id, details.reason));
try {
await handle.load();
@@ -169,7 +193,7 @@ export class GhostRuntime {
private getEntry(id: string): RuntimeEntry {
let entry = this.entries.get(id);
if (!entry) {
- entry = { state: 'off', handle: null, crashTimestamps: [] };
+ entry = { state: 'off', handle: null, identity: null, crashTimestamps: [] };
this.entries.set(id, entry);
}
return entry;
diff --git a/apps/desktop/src/main/cindy-brain/runtime/__tests__/GhostRuntime.test.ts b/apps/desktop/src/main/cindy-brain/runtime/__tests__/GhostRuntime.test.ts
index f5fb2c8f88..b9757b8a5a 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/__tests__/GhostRuntime.test.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/__tests__/GhostRuntime.test.ts
@@ -86,6 +86,22 @@ describe('GhostRuntime · 状态机', () => {
expect(handles.length).toBe(1);
});
+ it('换版后再 spawn:旧安装拉起的沙箱销毁并按新回执重开(面板分区才同源)', async () => {
+ const { runtime, handles } = setup();
+ await runtime.spawn(chipGhost());
+ const updated = chipGhost();
+ updated.manifest = { ...updated.manifest, version: '1.0.1' };
+ updated.approval = { state: 'approved', revision: '00000000-0000-4000-8000-000000000002' };
+ const result = await runtime.spawn(updated);
+ expect(result).toEqual({ ok: true, state: 'running' });
+ expect(handles.length).toBe(2);
+ expect(handles[0].destroyed).toBe(true);
+ expect(handles[1].destroyed).toBe(false);
+ // 同一份新记录再 spawn 仍幂等。
+ await runtime.spawn(updated);
+ expect(handles.length).toBe(2);
+ });
+
it('adapter.create 同步 throw(如 app 未 ready)→ 结构化失败 + crashed 记账,state 不卡 starting', async () => {
let createFails = true;
const handles: FakeHandle[] = [];
diff --git a/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts b/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
index f6ca1793b5..ada7ece4f3 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
@@ -634,6 +634,9 @@ describe('electronSandboxAdapter owner partition', () => {
// 面板把本机页面装进 <iframe>
expect(cancelled({ url: page, resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: panelUrl } } })).toBe(false);
expect(cancelled({ url: page, resourceType: 'subFrame', referrer: panelUrl })).toBe(false);
+ // 本机页面自己的更深一层子帧(控制页把设备画面再套一层 iframe):同端口才放行
+ expect(cancelled({ url: 'http://127.0.0.1:50365/simulators/x?handheld=1', resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: page } } })).toBe(false);
+ expect(cancelled({ url: 'http://127.0.0.1:50365/simulators/x?handheld=1', resourceType: 'subFrame', referrer: page })).toBe(false);
// 子帧内回到同一主机:端口的资源与 WebSocket
for (const [url, resourceType] of [
['http://127.0.0.1:50365/control.js', 'script'],
@@ -654,6 +657,9 @@ describe('electronSandboxAdapter owner partition', () => {
{ url: 'http://10.0.0.5:50365/x', resourceType: 'xhr', frame: { url: page } },
{ url: page, resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: 'cindy-ghost://other-ghost/panel.html' } } },
{ url: page, resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: 'https://evil.example/' } } },
+ // 更深的子帧跨到另一个回环端口/主机:拒绝
+ { url: 'http://127.0.0.1:1/control?udid=x', resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: page } } },
+ { url: 'http://10.0.0.5:50365/control?udid=x', resourceType: 'subFrame', frame: { url: 'about:blank', parent: { url: page } } },
{ url: page, resourceType: 'subFrame' },
{ url: 'http://user:[email protected]:50365/x', resourceType: 'xhr', frame: { url: page } },
{ url: 'https://127.0.0.1:50365/x', resourceType: 'xhr', frame: { url: page } },
diff --git a/apps/desktop/src/main/cindy-brain/runtime/electronSandboxAdapter.ts b/apps/desktop/src/main/cindy-brain/runtime/electronSandboxAdapter.ts
index 506f80af2e..eafa99d7c4 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/electronSandboxAdapter.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/electronSandboxAdapter.ts
@@ -390,7 +390,9 @@ interface LoopbackEmbedRequestDetails {
/**
* 回环嵌入请求是否放行:
- * - 子帧导航(subFrame)到回环地址:父帧必须是本插件自己的页面;
+ * - 首层子帧(subFrame)到回环地址:父帧必须是本插件自己的页面;
+ * - 更深的子帧:父帧必须是同一回环主机:端口(插件自己那台本机服务内部的
+ * 分层页面),跨到另一个端口一律拒绝;
* - 子帧内的其它请求(脚本/样式/XHR/WebSocket/媒体…):发起帧与目标必须是
* 同一回环主机:端口;
* - 顶层导航(mainFrame)与来自 cindy-ghost 页面自身的回环请求一律拒绝。
@@ -401,7 +403,7 @@ function loopbackEmbedRequestAllowed(details: LoopbackEmbedRequestDetails, selfP
if (details.resourceType === 'mainFrame') return false;
if (details.resourceType === 'subFrame') {
const parent = details.frame?.parent?.url ?? details.referrer ?? '';
- return parent.startsWith(selfPrefix);
+ return parent.startsWith(selfPrefix) || ghostLoopbackOrigin(parent) === target;
}
const from = details.frame?.url ?? details.referrer;
return ghostLoopbackOrigin(from) === target;
diff --git a/docs/dev-rules/plugin-security-and-authoring.md b/docs/dev-rules/plugin-security-and-authoring.md
index e6579d3a37..cc553940e1 100644
--- a/docs/dev-rules/plugin-security-and-authoring.md
+++ b/docs/dev-rules/plugin-security-and-authoring.md
@@ -536,7 +536,8 @@ Library 请求同时绑定迁移代次和安装批准,根解析、排队及开
- 回环嵌入是第二个、也是更窄的页面网络例外:插件同时声明 `panel`、`node` 工作进程和
`preview.hosts` 中的 `localhost` / `127.0.0.1` 时(`ghostPanelLoopbackEmbedAllowed`),
其页面 CSP 追加 `frame-src 'self' http://127.0.0.1:* http://localhost:*`,分区请求闸放行
- 两类请求:面板页作为父帧发起的回环子帧导航;该子帧内回到**同一主机:端口**的资源、
+ 三类请求:面板页作为父帧发起的回环子帧导航;该子帧内部再套一层、**父帧与目标同
+ 主机:端口**的子帧(本机页面自己的分层结构);该子帧内回到**同一主机:端口**的资源、
XHR 与 WebSocket。面板页自身发出的回环 fetch/XHR、顶层导航到回环地址、换端口或换主机
的请求、带凭证的 URL 与 HTTPS 伪装一律继续拒绝。嵌入页由插件自己的随包 Node 进程在
本机提供,因此不扩大插件可达的网络范围;权限请求处理器不变(嵌入页不能申请摄像头、