Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
feat(desktop): 建插件第一方资格判据(纯函数,尚未接线)
fmfsaisaib42277fa5147
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostFirstPartyPrivilege.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostFirstPartyPrivilege.test.tsnew file mode 100644index 0000000000..7262cd013d--- /dev/null+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostFirstPartyPrivilege.test.ts@@ -0,0 +1,268 @@+import { describe, expect, it } from 'vitest';++import {+ FIRST_PARTY_ALIAS_GHOST_IDS,+ resolveGhostFirstPartyPrivilege,+ type GhostFirstPartyFacts,+ type GhostFirstPartyMarketRecord,+} from '../ghostFirstPartyPrivilege.js';++const CURRENT_ORG = { organizationId: 'org-acme', pluginPrefix: 'acme' as const };++const BUNDLED_NON_OFFICIAL_IDS = [+ '163-mail',+ 'google-calendar',+ 'google-drive',+ 'google-gmail',+ 'google-sheets',+ 'icloud-mail',+ 'ios-simulator',+ 'qq-mail',+ 'yahoo-mail',+ 'world-bank-open-data',+ 'taptap-maker',+ 'x-manager',+] as const;++function facts(partial: Partial<GhostFirstPartyFacts> & Pick<GhostFirstPartyFacts, 'ghostId'>): GhostFirstPartyFacts {+ return {+ builtin: false,+ marketRecord: null,+ currentOrganization: null,+ ...partial,+ };+}++function market(+ partial: Partial<GhostFirstPartyMarketRecord> & Pick<GhostFirstPartyMarketRecord, 'scope'>,+): GhostFirstPartyMarketRecord {+ return {+ organizationId: partial.scope === 'organization' ? (partial.organizationId ?? 'org-acme') : null,+ source: 'market',+ installed: true,+ ...partial,+ };+}++describe('resolveGhostFirstPartyPrivilege', () => {+ it('gives builtin official plugins broker and host primitives even with no ledger', () => {+ expect(resolveGhostFirstPartyPrivilege(facts({ ghostId: 'xd-feishu', builtin: true }))).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: true,+ basis: 'builtin-official',+ });+ expect(resolveGhostFirstPartyPrivilege(facts({ ghostId: 'xd-atlassian', builtin: true }))).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: true,+ basis: 'builtin-official',+ });+ });++ it('denies bundled plugins that miss the static official table, including x-manager reclaimPort', () => {+ for (const ghostId of BUNDLED_NON_OFFICIAL_IDS) {+ expect(+ resolveGhostFirstPartyPrivilege(facts({ ghostId, builtin: true })),+ ghostId,+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ }+ expect(+ resolveGhostFirstPartyPrivilege(facts({ ghostId: 'x-manager', builtin: true })).hostPrimitiveEligible,+ ).toBe(false);+ });++ it('denies alias ids that are not the real builtin seed', () => {+ for (const ghostId of FIRST_PARTY_ALIAS_GHOST_IDS) {+ expect(resolveGhostFirstPartyPrivilege(facts({ ghostId }))).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-alias',+ });+ }+ expect(+ resolveGhostFirstPartyPrivilege(facts({ ghostId: 'xd-mivo', builtin: true })),+ ).toMatchObject({ basis: 'builtin-official', brokerEligible: true });+ });++ it('trusts server-market public installs only when the id hits the static table', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'cindy-art',+ marketRecord: market({ scope: 'public', organizationId: null, source: 'market' }),+ }),+ ),+ ).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: true,+ basis: 'market-public',+ });+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'google-gmail',+ marketRecord: market({ scope: 'public', organizationId: null, source: 'market' }),+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ });++ it('does not treat custom-market public scope as a trust statement', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'google-gmail',+ marketRecord: market({+ scope: 'public',+ organizationId: null,+ source: 'local-market',+ }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ });++ it('denies server-market personal scope', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ marketRecord: market({+ scope: 'personal',+ organizationId: null,+ source: 'market',+ }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ });++ it('gives current-org market plugins broker but not host primitives', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ marketRecord: market({ scope: 'organization', organizationId: 'org-acme' }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: false,+ basis: 'market-organization-current',+ });+ });++ it('denies an official-looking org plugin whose prefix does not belong to the current org', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'xd-evil',+ marketRecord: market({ scope: 'organization', organizationId: 'org-acme' }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ });++ it('denies another organization market package', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ marketRecord: market({ scope: 'organization', organizationId: 'org-other' }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-foreign-org',+ });+ });++ it('gives local / custom-market packages broker when the id matches the current org prefix', () => {+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ marketRecord: market({+ scope: 'personal',+ organizationId: null,+ source: 'local-market',+ }),+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: false,+ basis: 'local-current-org-prefix',+ });+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: true,+ hostPrimitiveEligible: false,+ basis: 'local-current-org-prefix',+ });+ });++ it('fail-closes unknown origin, missing prefix, and unmatched prefix', () => {+ expect(resolveGhostFirstPartyPrivilege(facts({ ghostId: 'mystery' }))).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'acme-feishu',+ currentOrganization: { organizationId: 'org-acme', pluginPrefix: null },+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ expect(+ resolveGhostFirstPartyPrivilege(+ facts({+ ghostId: 'xd-evil',+ currentOrganization: CURRENT_ORG,+ }),+ ),+ ).toEqual({+ brokerEligible: false,+ hostPrimitiveEligible: false,+ basis: 'denied-unknown-origin',+ });+ });+});diff --git a/apps/desktop/src/main/cindy-brain/ghostFirstPartyPrivilege.ts b/apps/desktop/src/main/cindy-brain/ghostFirstPartyPrivilege.tsnew file mode 100644index 0000000000..f87f02c2e3--- /dev/null+++ b/apps/desktop/src/main/cindy-brain/ghostFirstPartyPrivilege.ts@@ -0,0 +1,151 @@+/**+ * Host-side first-party privilege resolver.+ *+ * Collects "where this installed plugin came from" into a structured+ * conclusion. Callers decide whether to refuse install or only withhold+ * privileges; this module does not refuse loading.+ *+ * Not wired yet. Existing `isBrokerEligibleGhostId` /+ * `isFirstPartyHostPrivilegeGhostId` call sites stay on the static prefix+ * predicates until the next step.+ *+ * Input priority: first evaluable of+ * 1. builtin seed → static official table+ * 2. plugin-market ledger (source + scope + organizationId)+ * 3. neither → fail-closed, no privilege+ *+ * Discriminator is the combination of `source` and `scope`, not `scope`+ * alone. Custom / git market rows write `scope: 'public'` as a placeholder+ * (`plugin-market/service.ts`); that is not a trust statement. Server-market+ * public is only trusted when `source === 'market'`.+ *+ * A matching official prefix is never a security proof by itself. The+ * static table is only a criterion on the builtin branch and on trusted+ * server-market public installs.+ *+ * `facts.builtin` is id-based, not byte-based: it means "this id is on the+ * bundled seed roster" (`listBuiltinSeedIds` / directory name), not "these+ * bytes came from the bundled seed". Byte-level guarantees live in+ * provisioning content matching and `approveTrustedBundledInstall`. That is+ * the same strength as today's `isOfficialGhostId(id)`.+ */+import { PLUGIN_PREFIX_PATTERN, type PluginScope } from '@cindy/plugin-protocol';++import { isOfficialGhostId } from '../../shared/ghost.js';++/**+ * Ghost ids that share Host credential aliases (`GHOST_SECRET_STORAGE_ALIASES`).+ * A local package impersonating these ids must never receive first-party+ * privilege. Keep this list in sync with `shared/providerSecrets.ts`.+ */+export const FIRST_PARTY_ALIAS_GHOST_IDS = Object.freeze(['cindy-web-search', 'xd-mivo'] as const);++export type GhostFirstPartyBasis =+ | 'builtin-official'+ | 'market-public'+ | 'market-organization-current'+ | 'local-current-org-prefix'+ | 'denied-alias'+ | 'denied-foreign-org'+ | 'denied-unknown-origin';++export interface GhostFirstPartyPrivilege {+ brokerEligible: boolean;+ hostPrimitiveEligible: boolean;+ basis: GhostFirstPartyBasis;+}++export interface GhostFirstPartyMarketRecord {+ scope: PluginScope;+ organizationId: string | null;+ source: 'market' | 'legacy-adopted' | 'git-market' | 'local-market';+ installed: boolean;+}++export interface GhostFirstPartyCurrentOrganization {+ organizationId: string;+ pluginPrefix: string | null;+}++export interface GhostFirstPartyFacts {+ ghostId: string;+ /** True when the id is on the bundled seed roster (`InstalledGhost.builtin`). */+ builtin: boolean;+ marketRecord: GhostFirstPartyMarketRecord | null;+ currentOrganization: GhostFirstPartyCurrentOrganization | null;+}++function matchesCurrentOrgPrefix(+ ghostId: string,+ currentOrganization: GhostFirstPartyCurrentOrganization | null,+): boolean {+ const prefix = currentOrganization?.pluginPrefix;+ if (!prefix || !PLUGIN_PREFIX_PATTERN.test(prefix)) return false;+ return ghostId.startsWith(`${prefix}-`);+}++function isCurrentOrganizationRecord(+ record: GhostFirstPartyMarketRecord,+ currentOrganization: GhostFirstPartyCurrentOrganization | null,+): boolean {+ return (+ record.scope === 'organization' &&+ currentOrganization !== null &&+ record.organizationId === currentOrganization.organizationId+ );+}++function allow(basis: GhostFirstPartyBasis, hostPrimitiveEligible: boolean): GhostFirstPartyPrivilege {+ return { brokerEligible: true, hostPrimitiveEligible, basis };+}++function deny(basis: Extract<GhostFirstPartyBasis, `denied-${string}`>): GhostFirstPartyPrivilege {+ return { brokerEligible: false, hostPrimitiveEligible: false, basis };+}++/**+ * Pure first-party privilege conclusion from already-collected facts.+ * Does not read disk, ledger, or Electron.+ */+export function resolveGhostFirstPartyPrivilege(facts: GhostFirstPartyFacts): GhostFirstPartyPrivilege {+ if (facts.builtin) {+ return isOfficialGhostId(facts.ghostId)+ ? allow('builtin-official', true)+ : deny('denied-unknown-origin');+ }++ if ((FIRST_PARTY_ALIAS_GHOST_IDS as readonly string[]).includes(facts.ghostId)) {+ return deny('denied-alias');+ }++ const record = facts.marketRecord;+ if (record?.installed) {+ if (record.scope === 'public' && record.source === 'market') {+ return isOfficialGhostId(facts.ghostId)+ ? allow('market-public', true)+ : deny('denied-unknown-origin');+ }+ if (record.scope === 'organization') {+ if (!isCurrentOrganizationRecord(record, facts.currentOrganization)) {+ return deny('denied-foreign-org');+ }+ if (!matchesCurrentOrgPrefix(facts.ghostId, facts.currentOrganization)) {+ return deny('denied-unknown-origin');+ }+ return allow('market-organization-current', false);+ }+ if (+ (record.source === 'git-market' || record.source === 'local-market') &&+ matchesCurrentOrgPrefix(facts.ghostId, facts.currentOrganization)+ ) {+ return allow('local-current-org-prefix', false);+ }+ return deny('denied-unknown-origin');+ }++ if (matchesCurrentOrgPrefix(facts.ghostId, facts.currentOrganization)) {+ return allow('local-current-org-prefix', false);+ }++ return deny('denied-unknown-origin');+}