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。

refactor(desktop): 拆分官方插件 id 判据为四个谓词

fmfsaisai4085387155a4
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index 6d3f086e38..0ed220d03e 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -18,7 +18,7 @@ import {
ghostLocalePathFor,
ghostInstallApprovalToken,
ghostIconMimeType,
- isOfficialGhostId,
+ isBrokerEligibleGhostId,
isValidGhostId,
resolveGhostManifestLocale,
validateGhostManifest,
@@ -1857,7 +1857,7 @@ export class GhostManager {
// tokenBroker 门控与装入侧同一条规则(XDT 授权 broker 仅第一方官方插件可用,
// 不区分 dev/packaged):走已装目录重新确认的都不是随包插件,声明即拒。
if (
- !isOfficialGhostId(id) &&
+ !isBrokerEligibleGhostId(id) &&
(validated.manifest.network?.secrets ?? []).some((s) => s.oauth?.tokenBroker !== undefined)
) {
return {
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
index bfd3712c04..557f948256 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -1807,21 +1807,34 @@ describe('GhostManager · review 第 6 轮回归(P0/P1 修复钉住)', () => {
});
it('P1-6:声明 tokenBroker 的目录拒走已装目录重新确认', async () => {
+ const brokerNetwork = {
+ hosts: ['api.example.com', 'accounts.example.com'],
+ secrets: [
+ {
+ key: 'token',
+ label: 'Account',
+ source: 'oauth' as const,
+ inject: { header: 'Authorization', format: 'Bearer {value}' },
+ oauth: {
+ authorizeUrl: 'https://accounts.example.com/authorize',
+ tokenUrl: 'https://accounts.example.com/token',
+ clientId: 'broker-client',
+ tokenBroker: 'github',
+ },
+ },
+ ],
+ };
await writeLegacyInstall('brokered', {
...goodManifest('brokered'),
- network: {
- hosts: ['api.example.com'],
- secrets: [
- {
- key: 'token',
- source: 'oauth',
- oauth: { tokenBroker: 'github' },
- },
- ],
- },
+ slots: ['tool', 'network'],
+ settingsHtml: 'settings.html',
+ network: brokerNetwork,
});
const inspected = await manager.inspectInstalledReapproval('brokered');
expect('rejection' in inspected && inspected.rejection.code).toBe('file-invalid');
+ if ('rejection' in inspected) {
+ expect(inspected.rejection.reason).toMatch(/tokenBroker/);
+ }
});
it('P1-7:重新确认的启停默认值取镜像读数,不重置用户停用偏好', async () => {
diff --git a/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts b/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts
index 4c9ad5da74..1ec3bfbd60 100644
--- a/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts
@@ -46,7 +46,7 @@ import {
} from './ghostOauthFlow.js';
import {
changedBuiltinOauthClientSecretKeys,
- isOfficialGhostId,
+ isFirstPartyHostPrivilegeGhostId,
type GhostManifest,
type GhostSecretOauthDecl,
} from '../../shared/ghost.js';
@@ -899,7 +899,7 @@ export class GhostOauthAccountManager {
// 回收 = 强杀占用进程,而"杀谁"由 redirectPort 决定——第三方 manifest
// 可声明任意端口(如 5432),放开等于让任意意识借「连接账号」之手
// 强杀用户本地服务(Postgres 等),故第三方一律回落"占用即报错"。
- reclaimPort: isOfficialGhostId(ghostId) ? this.deps.reclaimPort : undefined,
+ reclaimPort: isFirstPartyHostPrivilegeGhostId(ghostId) ? this.deps.reclaimPort : undefined,
});
if (!flow.ok) return { ok: false, error: flow.error, detail: flow.detail };
if (this.deps.isConnectTargetCurrent?.(ghostId, secretKey, decl) === false) {
@@ -933,7 +933,7 @@ export class GhostOauthAccountManager {
// 头像地址是身份端点响应里的任意 https,不受 hosts 白名单约束——放开
// 等于给第三方意识一个"主机代发 GET + 小图字节回沙箱"的 SSRF 读原语。
// 下载本身不带任何凭证(CDN 域名不在注入白名单);失败降级无头像。
- if (identity.avatarUrl !== null && isOfficialGhostId(ghostId)) {
+ if (identity.avatarUrl !== null && isFirstPartyHostPrivilegeGhostId(ghostId)) {
avatar = await fetchGhostOauthAvatar({
url: identity.avatarUrl,
fetchImpl: this.deps.fetchImpl,
@@ -1329,7 +1329,7 @@ export class GhostOauthAccountManager {
// 头像回填同样只对第一方官方意识放行(connectAccount 处的 SSRF 口径)。
const needAvatar =
decl.identity.avatarPath !== undefined &&
- isOfficialGhostId(ghostId) &&
+ isFirstPartyHostPrivilegeGhostId(ghostId) &&
this.readAvatar(ghostId, secretKey, accountId) === null;
if (!needDisplay && !needAvatar) return;
const identity = await fetchGhostOauthIdentity({
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index b1b5d2a269..603e371377 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -32,7 +32,9 @@ import {
unreviewedGhostPermissionItems,
ghostWebviewEntryPaths,
isCindyAccountGhostId,
+ isBrokerEligibleGhostId,
isOfficialGhostId,
+ isUserInstallReservedGhostId,
ghostPermissionItems,
ghostPermissionProjectionFingerprint,
isValidGhostId,
@@ -5133,7 +5135,7 @@ function rejectReservedGhostId(id: string): void {
// packaged-only so first-party iteration can still reload cindy-/xd- plugins.
rejectReservedPublisherSlug(id);
if (!app.isPackaged) return;
- if (!isOfficialGhostId(id)) return;
+ if (!isUserInstallReservedGhostId(id)) return;
throwIpcError(
'GHOST_ID_RESERVED',
`id "${id}" 使用了官方保留前缀(cindy- / filo- / xd-),用户通道不可装入`,
@@ -5155,7 +5157,7 @@ export function rejectReservedGhostIdForCustomMarket(id: string): void {
* dev/packaged:broker 是服务端资产,dev 也不豁免。
*/
function rejectUnauthorizedTokenBroker(manifest: GhostManifest): void {
- if (isOfficialGhostId(manifest.id)) return;
+ if (isBrokerEligibleGhostId(manifest.id)) return;
const brokered = (manifest.network?.secrets ?? []).some(
(s) => s.oauth?.tokenBroker !== undefined,
);
@@ -7245,7 +7247,7 @@ export function registerGhostIpc(): void {
const brokered = (probe.manifest.network?.secrets ?? []).some(
(s) => s.oauth?.tokenBroker !== undefined,
);
- if (brokered && !isOfficialGhostId(probe.manifest.id)) return false;
+ if (brokered && !isBrokerEligibleGhostId(probe.manifest.id)) return false;
// 指令查重(同 install/update):与当前已装撞名即拒,排除自身。
const commandFold = probe.manifest.command?.toLowerCase();
if (commandFold === undefined) return true;
diff --git a/apps/desktop/src/main/cindy-brain/runtime/ghostOauthEndpoint.ts b/apps/desktop/src/main/cindy-brain/runtime/ghostOauthEndpoint.ts
index f8d73563b7..96d4f553ab 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/ghostOauthEndpoint.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/ghostOauthEndpoint.ts
@@ -28,7 +28,7 @@
* 产生的全部令牌只存在主机保险库与内存缓存,本端点没有任何读回动作。
*/
-import { isOfficialGhostId } from '../../../shared/ghost.js';
+import { isBrokerEligibleGhostId } from '../../../shared/ghost.js';
import { GhostKvError } from '../ghostKvStore.js';
import { GHOST_SECRET_VALUE_MAX_CHARS } from './ghostSecretsEndpoint.js';
import type {
@@ -207,7 +207,7 @@ export async function handleGhostOauthRequest(args: {
if (method !== 'POST') return { status: 405 };
// tokenBroker 第一方门控·连接闸(装入闸的运行时兜底,覆盖 dev 装入等
// 旁路):XDT 授权 broker 只对官方前缀意识开放。
- if (decl.tokenBroker !== undefined && !isOfficialGhostId(ghostId)) {
+ if (decl.tokenBroker !== undefined && !isBrokerEligibleGhostId(ghostId)) {
return {
status: 200,
body: JSON.stringify({
diff --git a/apps/desktop/src/shared/__tests__/ghost.test.ts b/apps/desktop/src/shared/__tests__/ghost.test.ts
index 9a719da87e..ccad4f4984 100644
--- a/apps/desktop/src/shared/__tests__/ghost.test.ts
+++ b/apps/desktop/src/shared/__tests__/ghost.test.ts
@@ -26,6 +26,9 @@ import {
isGhostCallToolName,
isValidGhostId,
isOfficialGhostId,
+ isUserInstallReservedGhostId,
+ isBrokerEligibleGhostId,
+ isFirstPartyHostPrivilegeGhostId,
isValidGhostNetworkHostPattern,
layoutWithGhostPanel,
parseGhostPartition,
@@ -2664,6 +2667,31 @@ describe('ghost · 官方保留 id 前缀(cindy-)', () => {
expect(isOfficialGhostId('my-cindy-tool')).toBe(false);
expect(isOfficialGhostId('web-search')).toBe(false);
});
+
+ it('四个官方 id 谓词对本阶段同一组输入返回完全相同的结果', () => {
+ const ids = [
+ 'cindy-art',
+ 'filo-google',
+ 'xd-mivo',
+ 'acme-feishu',
+ 'my-plugin',
+ 'cindyart',
+ 'my-cindy-tool',
+ '',
+ ];
+ const predicates = [
+ isOfficialGhostId,
+ isUserInstallReservedGhostId,
+ isBrokerEligibleGhostId,
+ isFirstPartyHostPrivilegeGhostId,
+ ];
+ for (const id of ids) {
+ const expected = isOfficialGhostId(id);
+ for (const predicate of predicates) {
+ expect(predicate(id), `${predicate.name}(${JSON.stringify(id)})`).toBe(expected);
+ }
+ }
+ });
});
describe('ghost · network 域名条目格式与匹配', () => {
diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.ts
index f2bd4b90d3..bd19c254be 100644
--- a/apps/desktop/src/shared/ghost.ts
+++ b/apps/desktop/src/shared/ghost.ts
@@ -1636,11 +1636,35 @@ export const GHOST_OFFICIAL_ID_PREFIXES: readonly string[] = [
'xd-',
];
-/** id 是否属于官方保留命名空间。 */
+/** id 是否属于官方保留命名空间。静态命名空间与不可逆恢复能力继续用这个。 */
export function isOfficialGhostId(id: string): boolean {
return GHOST_OFFICIAL_ID_PREFIXES.some((prefix) => id.startsWith(prefix));
}
+/**
+ * 用户装入通道(拖入 / 选文件 / forge 转交)是否拒装该 id。
+ * 本阶段与 `isOfficialGhostId` 等价;后续会放宽,让本组织前缀通过。
+ */
+export function isUserInstallReservedGhostId(id: string): boolean {
+ return isOfficialGhostId(id);
+}
+
+/**
+ * 该 id 能否声明 `oauth.tokenBroker`(装入闸与连接闸)。
+ * 本阶段与 `isOfficialGhostId` 等价;后续会既放宽又收紧。
+ */
+export function isBrokerEligibleGhostId(id: string): boolean {
+ return isOfficialGhostId(id);
+}
+
+/**
+ * 该 id 能否拿宿主原语:OAuth 端口回收、身份头像代下载。
+ * 本阶段与 `isOfficialGhostId` 等价;后续会收紧。
+ */
+export function isFirstPartyHostPrivilegeGhostId(id: string): boolean {
+ return isOfficialGhostId(id);
+}
+
/**
* Bundled Ghosts whose execution depends on Cindy account-plane services.
*