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): 补齐标准化插件清单校验路径

david70cf58b2cd5c
diff --git a/apps/desktop/src/main/cindy-brain/GhostManager.ts b/apps/desktop/src/main/cindy-brain/GhostManager.ts
index f83c578141..20ff728b52 100644
--- a/apps/desktop/src/main/cindy-brain/GhostManager.ts
+++ b/apps/desktop/src/main/cindy-brain/GhostManager.ts
@@ -23,6 +23,7 @@ import {
resolveGhostManifestLocale,
validateGhostManifest,
validateGhostManifestLocaleResource,
+ validateNormalizedGhostManifest,
withGhostResolvedLocale,
type GhostManifest,
type GhostManifestLocaleResource,
@@ -3557,7 +3558,7 @@ export class GhostManager {
});
const sourceManifestRaw = JSON.parse(fs.readFileSync(sourceManifestPath, 'utf8')) as unknown;
const sourceManifest = validateGhostManifest(sourceManifestRaw);
- const expectedManifest = validateGhostManifest(manifest);
+ const expectedManifest = validateNormalizedGhostManifest(manifest);
if (
!sourceManifest.ok ||
!expectedManifest.ok ||
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 4c91d9d73b..b26b8b4d23 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/GhostManager.test.ts
@@ -3220,12 +3220,13 @@ describe('GhostManager · Host approval receipt', () => {
const writeBundledSource = async (
manifest: InstalledGhost['manifest'],
files: Record<string, string> = { 'main.js': '// bundled seed' },
+ sourceManifest: unknown = manifest,
): Promise<{ sourceDir: string }> => {
const sourceDir = path.join(workDir, 'bundled-seeds', manifest.id);
await fs.promises.rm(sourceDir, { recursive: true, force: true });
await fs.promises.mkdir(sourceDir, { recursive: true });
trustedBundledIds.add(manifest.id);
- await fs.promises.writeFile(path.join(sourceDir, 'ghost.json'), JSON.stringify(manifest));
+ await fs.promises.writeFile(path.join(sourceDir, 'ghost.json'), JSON.stringify(sourceManifest));
for (const [relativePath, content] of Object.entries(files)) {
const target = path.join(sourceDir, relativePath);
await fs.promises.mkdir(path.dirname(target), { recursive: true });
@@ -3836,6 +3837,34 @@ describe('GhostManager · Host approval receipt', () => {
).toContain('Approved instructions');
});
+ it('bundled approval accepts a normalized setup manifest while validating source author syntax', async () => {
+ const sourceManifest = setupKvManifest();
+ await manager.install(
+ await makeCindy('setup.cindy', sourceManifest, {
+ 'main.js': '// installed setup plugin',
+ 'settings.html': '<!doctype html>',
+ }),
+ );
+ const listed = manager.list()[0];
+ const source = await writeBundledSource(
+ listed.manifest,
+ {
+ 'main.js': '// bundled setup plugin',
+ 'settings.html': '<!doctype html>',
+ },
+ sourceManifest,
+ );
+
+ await expect(
+ manager.approveTrustedBundledInstall(listed.manifest, listed.enabled, source),
+ ).resolves.toBe(true);
+ expect(manager.list()[0].manifest.setup).toEqual({
+ requires: [
+ { anyOf: [{ kind: 'kv', key: 'repoDir', label: '本机 cindy 项目目录' }] },
+ ],
+ });
+ });
+
it('snapshot repair persists a one-way disable before the compatibility marker disappears', async () => {
const manifest = skillManifest();
await manager.install(await makeCindy('skill.cindy', manifest, skillFiles()));
diff --git a/apps/desktop/src/main/plugin-market/__tests__/service.test.ts b/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
index 9e092709e2..49c1e0e277 100644
--- a/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
+++ b/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
@@ -7,7 +7,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import {
diffGhostPermissionItems,
ghostPermissionBaselineKey,
+ validateGhostManifest,
type GhostInstallApproval,
+ type GhostManifest,
} from '../../../shared/ghost.js';
const runtime = vi.hoisted(() => ({
@@ -146,6 +148,41 @@ function manifest(
};
}
+function setupKvManifest(id = 'cindy-test', version = '1.0.0') {
+ return {
+ ...manifest(id, version),
+ settingsHtml: 'settings.html',
+ setup: {
+ requires: [{ anyOf: [{ kv: 'repoDir', label: '本机 cindy 项目目录' }] }],
+ },
+ };
+}
+
+function setupSecretManifest(id = 'cindy-test', version = '1.0.0') {
+ return {
+ ...manifest(id, version),
+ slots: ['notify', 'network'],
+ settingsHtml: 'settings.html',
+ network: {
+ hosts: ['api.example.com'],
+ secrets: [
+ {
+ key: 'api_key',
+ label: 'API key',
+ inject: { header: 'Authorization', format: 'Bearer {value}' },
+ },
+ ],
+ },
+ setup: { requires: [{ anyOf: ['secret:api_key'] }] },
+ };
+}
+
+function normalizedManifest(raw: unknown): GhostManifest {
+ const validated = validateGhostManifest(raw);
+ if (!validated.ok) throw new Error(validated.reason);
+ return validated.manifest;
+}
+
function summary(overrides: Partial<VisiblePluginSummary> = {}): VisiblePluginSummary {
return {
id: PLUGIN_ID,
@@ -506,6 +543,40 @@ describe('PluginMarketService migration and defaultInstall', () => {
});
});
+ it('installs a default package whose permission cap contains normalized setup requirements', async () => {
+ const item = summary({ defaultInstall: true });
+ const rawManifest = setupKvManifest();
+ const approvedManifest = normalizedManifest(rawManifest);
+ const h = harness([item]);
+ h.api.detail.mockResolvedValueOnce({
+ ...item,
+ currentRelease: { ...item.currentRelease, manifest: rawManifest },
+ } as unknown as VisiblePluginDetail);
+ runtime.install.mockImplementation(async () => {
+ const ghost = {
+ manifest: { ...approvedManifest },
+ dir: '/userData/cindy-brain/cindy-test',
+ enabled: true,
+ };
+ runtime.ghosts = [ghost];
+ return ghost;
+ });
+
+ await expect(h.service.snapshot()).resolves.toMatchObject({
+ items: [{ installState: 'installed', enabled: true }],
+ });
+ expect(runtime.install).toHaveBeenCalledWith(
+ expect.stringMatching(/\.cindy$/),
+ expect.objectContaining({
+ permissionPolicy: {
+ mode: 'cap',
+ manifest: approvedManifest,
+ sourceType: 'server',
+ },
+ }),
+ );
+ });
+
it('returns a Renderer snapshot before a default install download finishes', async () => {
const item = summary({ defaultInstall: true });
const installDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-market-deferred-'));
@@ -657,6 +728,36 @@ describe('PluginMarketService migration and defaultInstall', () => {
expect(ghost?.enabled).toBe(true);
});
+ it('manual market install accepts the normalized setup manifest returned by detail', async () => {
+ const item = summary();
+ const rawManifest = setupSecretManifest();
+ const reviewedManifest = normalizedManifest(rawManifest);
+ const h = harness([item]);
+ h.api.detail.mockResolvedValueOnce({
+ ...item,
+ currentRelease: { ...item.currentRelease, manifest: rawManifest },
+ } as unknown as VisiblePluginDetail);
+ runtime.install.mockResolvedValue({
+ manifest: reviewedManifest,
+ dir: '/userData/cindy-brain/cindy-test',
+ enabled: true,
+ });
+
+ await expect(
+ h.service.install(item.id, {
+ ...reviewedInstallOptions(item),
+ expectedManifest: reviewedManifest,
+ }),
+ ).resolves.toMatchObject({
+ ghost: {
+ manifest: {
+ setup: { requires: [{ anyOf: [{ kind: 'secret', key: 'api_key' }] }] },
+ },
+ },
+ });
+ expect(runtime.install).toHaveBeenCalledOnce();
+ });
+
it('installs the explicitly selected official entry when another entry shares its ghostId', async () => {
const selected = summary();
const other = summary({ id: `c${'b'.repeat(24)}`, name: 'Other Listing' });
@@ -1327,6 +1428,56 @@ describe('PluginMarketService migration and defaultInstall', () => {
});
});
+ it('silently upgrades a default package whose reviewed manifest contains normalized setup', async () => {
+ const item = summary({
+ scope: 'organization',
+ organizationId: 'org-1',
+ defaultInstall: true,
+ currentRelease: {
+ ...summary().currentRelease,
+ id: 'release-2',
+ version: '2.0.0',
+ },
+ });
+ const oldManifest = manifest(item.ghostId, '1.0.0');
+ const rawManifest = setupSecretManifest(item.ghostId, '2.0.0');
+ const upgradedManifest = normalizedManifest(rawManifest);
+ const installDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-market-setup-upgrade-'));
+ roots.push(installDir);
+ fs.writeFileSync(path.join(installDir, 'ghost.json'), JSON.stringify(oldManifest));
+ runtime.ghosts = [{ manifest: oldManifest, dir: installDir, enabled: true }];
+ const h = harness([item]);
+ h.ledger.upsertInstallation({
+ ...recordForTest(item),
+ releaseId: 'release-1',
+ version: '1.0.0',
+ manifestDigest: ghostManifestDigest(oldManifest),
+ });
+ h.api.detail.mockResolvedValueOnce({
+ ...item,
+ currentRelease: { ...item.currentRelease, manifest: rawManifest },
+ } as unknown as VisiblePluginDetail);
+ runtime.install.mockImplementationOnce(async () => {
+ const ghost = { manifest: { ...upgradedManifest }, dir: installDir, enabled: true };
+ runtime.ghosts = [ghost];
+ return ghost;
+ });
+
+ await expect(h.service.snapshot()).resolves.toMatchObject({
+ items: [{ installState: 'installed', version: '2.0.0' }],
+ });
+ expect(runtime.install).toHaveBeenCalledWith(
+ expect.stringMatching(/\.cindy$/),
+ expect.objectContaining({
+ permissionPolicy: {
+ mode: 'cap',
+ manifest: upgradedManifest,
+ sourceType: 'server',
+ },
+ }),
+ );
+ });
+
it('integration: snapshot upgrades an organization defaultInstall release and consumes its notice', async () => {
const item = summary({
scope: 'organization',
diff --git a/apps/desktop/src/main/plugin-market/service.ts b/apps/desktop/src/main/plugin-market/service.ts
index f53f0f609d..cb424a21df 100644
--- a/apps/desktop/src/main/plugin-market/service.ts
+++ b/apps/desktop/src/main/plugin-market/service.ts
@@ -21,6 +21,7 @@ import {
isSafeGhostRelativePath,
isOfficialGhostId,
validateGhostManifest,
+ validateNormalizedGhostManifest,
type GhostManifest,
type InstalledGhost,
} from '../../shared/ghost.js';
@@ -917,7 +918,7 @@ export class PluginMarketService {
// install.ts:184-191 的打包前比对同向(但此处比对的是预览清单而非
// 实际打包字节,名字/版本/作者差异不在此处检测)。
if (options.expectedManifest !== undefined) {
- const reviewed = validateGhostManifest(options.expectedManifest);
+ const reviewed = validateNormalizedGhostManifest(options.expectedManifest);
// 畸形 payload 会造成 ghostPermissionBaselineKey crash(slots.includes
// 等字段解引用),先验证再比对。验证失败时直接拒——审阅过的清单连基本
// 结构都不对,不能信任。
@@ -1629,14 +1630,14 @@ export class PluginMarketService {
}
const reviewedManifest = options.reviewedManifest
- ? validateGhostManifest(options.reviewedManifest)
+ ? validateNormalizedGhostManifest(options.reviewedManifest)
: null;
if (reviewedManifest && !reviewedManifest.ok) {
throwIpcError('GHOST_FILE_INVALID', 'This Plugin manifest is not supported');
}
const permissionCap =
options.permissionPolicy?.mode === 'cap'
- ? validateGhostManifest(options.permissionPolicy.manifest)
+ ? validateNormalizedGhostManifest(options.permissionPolicy.manifest)
: null;
if (permissionCap && !permissionCap.ok) {
throwIpcError('GHOST_FILE_INVALID', 'This Plugin manifest is not supported');