moving
This commit is contained in:
@@ -12,14 +12,14 @@ import type { FastifyInstance, FastifyReply } from 'fastify';
|
||||
import { existsSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { MarketplaceDB, MarketplaceInstaller, MarketplaceSync, SecurityGate, ENTERPRISE_PACKS, PACKAGE_CATEGORIES, recategorizeAll, isCiscoScannerAvailable, resolveSkillSource, SkillSourceError } from '@waggle/marketplace';
|
||||
import type { InstallationType, SearchSort, ScanResult, MarketplacePackage, FetchFn } from '@waggle/marketplace';
|
||||
import { MarketplaceDB, MarketplaceInstaller, MarketplaceSync, SecurityGate, ENTERPRISE_PACKS, PACKAGE_CATEGORIES, recategorizeAll, isCiscoScannerAvailable, resolveSkillSource, SkillSourceError, createMarketplaceMcpProvenance } from '@waggle/marketplace';
|
||||
import type { InstallationType, SearchSort, ScanResult, MarketplacePackage, MarketplaceMcpProvenance, MarketplaceApprovalIdentity, FetchFn } from '@waggle/marketplace';
|
||||
import { validateSkillMd } from '@waggle/sdk';
|
||||
import { safeFetch, assertUrlAllowed, scanForInjection } from '@waggle/agent';
|
||||
import { getKvarkConfig } from '../../kvark/kvark-config.js';
|
||||
import { emitNotification } from './notifications.js';
|
||||
import { requireTier } from '../../middleware/assert-tier.js';
|
||||
import { removeMcpServerEntry } from '../mcp-config.js';
|
||||
import { loadMcpConfig, removeMcpServerEntry } from '../mcp-config.js';
|
||||
import { enqueueHeldAction } from '../held-action-executor.js';
|
||||
import { isMarketplaceBackgroundSyncDisabled } from '../marketplace-background-sync.js';
|
||||
|
||||
@@ -226,6 +226,9 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
settings?: Record<string, string>;
|
||||
force?: boolean;
|
||||
forceInsecure?: boolean;
|
||||
expectedInstallType?: InstallationType;
|
||||
expectedMcpProvenance?: MarketplaceMcpProvenance;
|
||||
expectedApprovalIdentity?: MarketplaceApprovalIdentity;
|
||||
};
|
||||
|
||||
if (!body.packageId) {
|
||||
@@ -238,18 +241,64 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
return reply.code(404).send({ error: `Package ID ${body.packageId} not found` });
|
||||
}
|
||||
|
||||
const gate = new SecurityGate({
|
||||
enable_gen_trust_hub: false,
|
||||
enable_cisco_scanner: false,
|
||||
enable_mcp_guardian: false,
|
||||
enable_heuristics: true,
|
||||
});
|
||||
if (
|
||||
body.expectedMcpProvenance
|
||||
&& body.expectedInstallType
|
||||
&& pkg.waggle_install_type !== body.expectedInstallType
|
||||
) {
|
||||
const error = 'Marketplace MCP package changed during installation; retry from the refreshed catalog';
|
||||
return reply.code(409).send({
|
||||
success: false,
|
||||
error,
|
||||
message: error,
|
||||
errorCode: 'PACKAGE_IDENTITY_CHANGED',
|
||||
});
|
||||
}
|
||||
|
||||
if (body.expectedMcpProvenance) {
|
||||
let actualMcpProvenance: MarketplaceMcpProvenance | undefined;
|
||||
try {
|
||||
const mcpConfig = pkg.install_manifest?.mcp_config;
|
||||
if (!mcpConfig) throw new Error('Marketplace MCP snapshot has no configuration.');
|
||||
actualMcpProvenance = createMarketplaceMcpProvenance(
|
||||
db.getSource(pkg.source_id),
|
||||
{ name: pkg.name, version: pkg.version },
|
||||
mcpConfig,
|
||||
);
|
||||
} catch {
|
||||
// A delegated install treats an invalid fresh snapshot as the same
|
||||
// retryable identity conflict as a valid-but-different snapshot.
|
||||
}
|
||||
if (
|
||||
!actualMcpProvenance
|
||||
|| !MarketplaceInstaller.mcpProvenanceMatches(
|
||||
actualMcpProvenance,
|
||||
body.expectedMcpProvenance,
|
||||
)
|
||||
) {
|
||||
const error = 'Marketplace MCP package changed during installation; retry from the refreshed catalog';
|
||||
return reply.code(409).send({
|
||||
success: false,
|
||||
error,
|
||||
message: error,
|
||||
errorCode: 'PACKAGE_IDENTITY_CHANGED',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let scanResult: ScanResult | undefined;
|
||||
try {
|
||||
scanResult = await gate.scan(pkg);
|
||||
} catch {
|
||||
// Scan failure should not block installation — proceed with warning
|
||||
if (!body.expectedMcpProvenance) {
|
||||
const gate = new SecurityGate({
|
||||
enable_gen_trust_hub: false,
|
||||
enable_cisco_scanner: false,
|
||||
enable_mcp_guardian: false,
|
||||
enable_heuristics: true,
|
||||
});
|
||||
try {
|
||||
scanResult = await gate.scan(pkg);
|
||||
} catch {
|
||||
// Scan failure should not block installation — proceed with warning
|
||||
}
|
||||
}
|
||||
|
||||
if (scanResult) {
|
||||
@@ -377,6 +426,9 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
settings: body.settings,
|
||||
force: body.force,
|
||||
forceInsecure: body.forceInsecure,
|
||||
expectedInstallType: body.expectedInstallType,
|
||||
expectedMcpProvenance: body.expectedMcpProvenance,
|
||||
expectedApprovalIdentity: body.expectedApprovalIdentity,
|
||||
});
|
||||
|
||||
// Update security status in DB after successful install. Prefer the
|
||||
@@ -454,6 +506,9 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
|
||||
// Attach security scan info to the response
|
||||
const response: Record<string, unknown> = { ...result };
|
||||
if (result.errorCode === 'PACKAGE_IDENTITY_CHANGED') {
|
||||
response.error = result.message;
|
||||
}
|
||||
if (scanResult) {
|
||||
response.security = {
|
||||
severity: scanResult.overall_severity,
|
||||
@@ -466,7 +521,7 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
};
|
||||
}
|
||||
|
||||
return reply.code(result.success ? 200 : 422).send(response);
|
||||
return reply.code(result.success ? 200 : result.errorCode === 'PACKAGE_IDENTITY_CHANGED' ? 409 : 422).send(response);
|
||||
});
|
||||
|
||||
// ── POST /api/marketplace/uninstall ─────────────────────────────────
|
||||
@@ -482,28 +537,50 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
|
||||
return reply.code(400).send({ error: 'packageId is required' });
|
||||
}
|
||||
|
||||
const installer = new MarketplaceInstaller(db, undefined, guardedFetch);
|
||||
const result = await installer.uninstall(body.packageId);
|
||||
const pkg = db.getPackage(body.packageId);
|
||||
if (pkg?.waggle_install_type === 'mcp') {
|
||||
const manifest = pkg.install_manifest as { mcp_config?: { name?: string } } | null;
|
||||
const serverName = manifest?.mcp_config?.name || pkg.name;
|
||||
const dataDir = fastify.localConfig?.dataDir ?? '';
|
||||
const runtime = (fastify.agentState as {
|
||||
mcpRuntime?: { getServer(n: string): unknown; removeServer(n: string): Promise<void> };
|
||||
} | undefined)?.mcpRuntime;
|
||||
|
||||
// Phase 4 (S08): an MCP uninstall must ALSO leave the live runtime and the
|
||||
// server's persisted <dataDir>/.mcp.json — the installer only edits its
|
||||
// own WAGGLE_DATA_DIR/~/.waggle copy, so without this the "uninstalled"
|
||||
// server keeps running and resurrects at every boot via the C4 loader.
|
||||
if (result.success) {
|
||||
try {
|
||||
const pkg = db.getPackage(body.packageId);
|
||||
if (pkg?.waggle_install_type === 'mcp') {
|
||||
const manifest = pkg.install_manifest as { mcp_config?: { name?: string } } | null;
|
||||
const serverName = manifest?.mcp_config?.name || pkg.name;
|
||||
const runtime = (fastify.agentState as { mcpRuntime?: { getServer(n: string): unknown; removeServer(n: string): Promise<void> } } | undefined)?.mcpRuntime;
|
||||
if (runtime?.getServer(serverName)) await runtime.removeServer(serverName);
|
||||
removeMcpServerEntry(fastify.localConfig?.dataDir ?? '', serverName);
|
||||
// Remove the boot source before touching the live process: a failed
|
||||
// shutdown must never leave a server able to resurrect after restart.
|
||||
removeMcpServerEntry(dataDir, serverName);
|
||||
if (loadMcpConfig(dataDir).mcpServers[serverName]) {
|
||||
throw new Error('Canonical MCP configuration still contains the server');
|
||||
}
|
||||
if (runtime?.getServer(serverName)) await runtime.removeServer(serverName);
|
||||
if (runtime?.getServer(serverName)) {
|
||||
throw new Error('MCP runtime still contains the server');
|
||||
}
|
||||
} catch (err) {
|
||||
fastify.log.warn({ err, packageId: body.packageId }, 'MCP runtime/config cleanup on uninstall failed (non-blocking)');
|
||||
const residualState = {
|
||||
installed: db.isInstalled(body.packageId),
|
||||
runtimeRegistered: Boolean(runtime?.getServer(serverName)),
|
||||
bootConfigured: Boolean(loadMcpConfig(dataDir).mcpServers[serverName]),
|
||||
};
|
||||
fastify.log.warn({ err, packageId: body.packageId, residualState }, 'MCP revocation incomplete');
|
||||
return reply.code(503).send({
|
||||
success: false,
|
||||
packageId: pkg.id,
|
||||
packageName: pkg.name,
|
||||
installType: 'mcp',
|
||||
installPath: pkg.waggle_install_path,
|
||||
message: `MCP revocation incomplete: ${(err as Error).message}`,
|
||||
errors: [(err as Error).message],
|
||||
errorCode: 'MCP_REVOCATION_INCOMPLETE',
|
||||
residualState,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const installer = new MarketplaceInstaller(db, undefined, guardedFetch);
|
||||
const result = await installer.uninstall(body.packageId);
|
||||
|
||||
return reply.code(result.success ? 200 : 422).send(result);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user