feat: add MCP server configuration management
Integrate MCP server settings into the admin console, including token management, tool permissions, and Coolify build log configuration. Update UI components to support standardized action groups.
This commit is contained in:
1 parent
b2669e2ad5
commit
725d3c5096
30 files changed
+5264
-2729
No files matched your search
@@ -5,8 +5,10 @@ import { resolve } from "node:path";
|
||||
import test, { after } from "node:test";
|
||||
import { NextRequest } from "next/server";
|
||||
import { GET as getHealthRoute } from "../app/api/health/route";
|
||||
import { POST as postInternalMcpRoute } from "../app/api/internal/mcp/route";
|
||||
import { SettingsManager } from "../core/adminSettings/settingsManager";
|
||||
import { DataBufferEngine, type RedisBufferClient } from "../core/data/buffer";
|
||||
import { resolveInternalIngestionSecret } from "../core/Security/security";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
@@ -643,4 +645,95 @@ test("PREPUSH-FIX-01: Nginx preserves upstream real client IP behind Traefik, Do
|
||||
assert.ok(lockJson.packages?.["node_modules/cloudcmd"], "package-lock.json must resolve node_modules/cloudcmd");
|
||||
});
|
||||
|
||||
test("FINAL-DEPLOY-FIX-01: Single npm lockfile, Dockerfile .runtime-logs ownership parity, 8-table prisma sync, and cold-boot MCP bridge hydration", async () => {
|
||||
assert.equal(existsSync(resolve(process.cwd(), "bun.lock")), false, "Stale bun.lock must stay removed");
|
||||
|
||||
for (const dfRel of ["docker/Dockerfile.web", "docker/Dockerfile.server"]) {
|
||||
const dfSrc = readFileSync(resolve(process.cwd(), dfRel), "utf8");
|
||||
assert.match(dfSrc, /mkdir -p \/app\/storage\/data \/app\/storage\/\.runtime-logs/, `${dfRel} must pre-create .runtime-logs`);
|
||||
assert.match(dfSrc, /chown -R node:node \/app\/storage\/\.runtime-logs/, `${dfRel} must ensure node:node ownership on .runtime-logs`);
|
||||
}
|
||||
|
||||
const prismaSrc = readFileSync(resolve(process.cwd(), "prisma/schema.prisma"), "utf8");
|
||||
assert.match(prismaSrc, /@@map\("mcp_settings"\)/, "prisma/schema.prisma must map mcp_settings");
|
||||
|
||||
const prevCorePool = g.__dataStackCorePool;
|
||||
const prevCoreSchema = (g as Record<string, unknown>).__dataStackCoreSchema;
|
||||
const prevHydrated = g.__dataStackSettingsHydrated;
|
||||
const prevSettings = g.__dataStackAdminSettings;
|
||||
const prevInternalSecret = process.env.DATASTACK_INTERNAL_SECRET;
|
||||
const rotatedJwt = "rotated-db-jwt-secret-minimum-32-chars-ok";
|
||||
|
||||
try {
|
||||
delete process.env.DATASTACK_INTERNAL_SECRET;
|
||||
process.env.DATABASE_URL = "postgres://datastack:test@127.0.0.1:5432/datastack";
|
||||
g.__dataStackSettingsHydrated = false;
|
||||
g.__dataStackAdminSettings = undefined;
|
||||
(g as Record<string, unknown>).__dataStackCoreSchema = Promise.resolve();
|
||||
g.__dataStackCorePool = {
|
||||
async query(sql: string) {
|
||||
if (/FROM admin_settings/i.test(sql)) {
|
||||
return {
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
server_port: 4000,
|
||||
web_port: 3000,
|
||||
max_events_per_sec: 100,
|
||||
max_payload_size_kb: 100,
|
||||
batch_flush_threshold: 500,
|
||||
batch_flush_interval_ms: 2000,
|
||||
file_rotation_mb: 100,
|
||||
jwt_secret: rotatedJwt,
|
||||
enable_idempotency: true,
|
||||
auto_prune_days: 30,
|
||||
compress_after_days: 0,
|
||||
enable_s3_backup: false,
|
||||
s3_endpoint: "",
|
||||
s3_region: "auto",
|
||||
s3_bucket: "",
|
||||
s3_prefix: "datastack/",
|
||||
s3_access_key_id: "",
|
||||
s3_secret_access_key_enc: "",
|
||||
disk_pause_threshold_pct: 95,
|
||||
enable_webhook_alerts: false,
|
||||
webhook_url: "",
|
||||
webhook_secret_enc: "",
|
||||
webhook_cooldown_sec: 300,
|
||||
enable_prometheus_metrics: false,
|
||||
prometheus_metrics_token_enc: "",
|
||||
updated_at: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
return { rowCount: 0, rows: [] };
|
||||
},
|
||||
};
|
||||
|
||||
const expectedRotatedHeader = resolveInternalIngestionSecret(rotatedJwt);
|
||||
const res = await postInternalMcpRoute(
|
||||
new NextRequest("http://localhost:3000/api/internal/mcp", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-datastack-internal-secret": expectedRotatedHeader,
|
||||
},
|
||||
body: JSON.stringify({ action: "settings_get" }),
|
||||
}),
|
||||
);
|
||||
assert.equal(res.status, 200);
|
||||
} finally {
|
||||
g.__dataStackCorePool = prevCorePool;
|
||||
(g as Record<string, unknown>).__dataStackCoreSchema = prevCoreSchema;
|
||||
g.__dataStackSettingsHydrated = prevHydrated;
|
||||
g.__dataStackAdminSettings = prevSettings;
|
||||
if (prevInternalSecret === undefined) delete process.env.DATASTACK_INTERNAL_SECRET;
|
||||
else process.env.DATASTACK_INTERNAL_SECRET = prevInternalSecret;
|
||||
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
|
||||
else process.env.DATABASE_URL = prevDbUrl;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,820 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import fs from "node:fs/promises";
|
||||
import http from "node:http";
|
||||
import { createRequire } from "node:module";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test, { after } from "node:test";
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
process.env.DATASTACK_DISABLE_LISTEN = "1";
|
||||
|
||||
import { GET as mcpSettingsGet, POST as mcpSettingsPost } from "../app/api/mcp/route";
|
||||
import { POST as internalMcpPost } from "../app/api/internal/mcp/route";
|
||||
import { POST as teamPost } from "../app/api/team/route";
|
||||
import {
|
||||
createAdminSessionToken,
|
||||
getAdminSessionSecret,
|
||||
} from "../core/auth/adminSession";
|
||||
import {
|
||||
createInitialAdmin,
|
||||
resetAdminStateForTests,
|
||||
} from "../core/auth/adminUsers";
|
||||
import { clearAuditLogsForTests } from "../core/auth/auditLog";
|
||||
import { DataBufferEngine } from "../core/data/buffer";
|
||||
import {
|
||||
MCP_PERMISSION_DEFINITIONS,
|
||||
McpSettingsManager,
|
||||
} from "../core/mcp/mcpSettings";
|
||||
import { SDKManager } from "../core/sdk/sdkManager";
|
||||
import { getInternalIngestionHeaders } from "../core/Security/security";
|
||||
import { CSVStorageManager } from "../core/Storage/csvStorage";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const ingestionServer = require("../server/index.js") as {
|
||||
app: import("express").Express;
|
||||
mcpEngine: {
|
||||
syncState: (payload: Record<string, unknown>) => void;
|
||||
hydrateFromDatabase: (pool?: unknown) => Promise<boolean>;
|
||||
getStateSnapshot: () => Record<string, unknown>;
|
||||
resetForTests: () => void;
|
||||
};
|
||||
setDbPoolForTests: (pool: unknown) => void;
|
||||
stopTimers: () => void;
|
||||
};
|
||||
const { createMcpEngine, hashMcpToken } = require("../server/mcp/mcpEngine.js") as {
|
||||
createMcpEngine: (deps: Record<string, unknown>) => {
|
||||
syncState: (payload: Record<string, unknown>) => void;
|
||||
hydrateFromDatabase: (pool?: unknown) => Promise<boolean>;
|
||||
handleHttpRequest: (req: unknown, res: unknown) => Promise<void>;
|
||||
getStateSnapshot: () => Record<string, unknown>;
|
||||
resetForTests: () => void;
|
||||
resolveSafeStoragePath: (
|
||||
rel: string,
|
||||
opts?: { allowWriteRuntimeLogs?: boolean; requireExists?: boolean },
|
||||
) => Promise<{
|
||||
sandboxRoot: string;
|
||||
sandboxReal: string;
|
||||
relativePath: string;
|
||||
fullPath: string;
|
||||
}>;
|
||||
};
|
||||
hashMcpToken: (token: string) => string;
|
||||
};
|
||||
|
||||
after(async () => {
|
||||
await DataBufferEngine.shutdown();
|
||||
ingestionServer.stopTimers();
|
||||
resetAdminStateForTests();
|
||||
McpSettingsManager.resetForTests();
|
||||
clearAuditLogsForTests();
|
||||
});
|
||||
|
||||
async function setupRbacCookies() {
|
||||
resetAdminStateForTests();
|
||||
await createInitialAdmin("owner@datastack.dev", "OwnerPass123!");
|
||||
const secret = getAdminSessionSecret();
|
||||
assert.ok(secret, "Expected admin session secret in test runtime");
|
||||
const ownerToken = await createAdminSessionToken(
|
||||
"owner@datastack.dev",
|
||||
secret,
|
||||
Date.now() + 3600_000,
|
||||
1,
|
||||
"owner",
|
||||
"owner-1",
|
||||
);
|
||||
const ownerCookie = `datastack_admin_session=${ownerToken}`;
|
||||
|
||||
const createMember = async (
|
||||
email: string,
|
||||
password: string,
|
||||
role: "operator" | "viewer",
|
||||
) => {
|
||||
const res = await teamPost(
|
||||
new NextRequest("http://localhost:3000/api/team", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
cookie: ownerCookie,
|
||||
},
|
||||
body: JSON.stringify({ email, password, role }),
|
||||
}),
|
||||
);
|
||||
assert.equal(res.status, 201);
|
||||
const json = (await res.json()) as { member: { id: string } };
|
||||
const tok = await createAdminSessionToken(
|
||||
email,
|
||||
secret,
|
||||
Date.now() + 3600_000,
|
||||
1,
|
||||
role,
|
||||
json.member.id,
|
||||
);
|
||||
return `datastack_admin_session=${tok}`;
|
||||
};
|
||||
|
||||
const operatorCookie = await createMember(
|
||||
"operator@datastack.dev",
|
||||
"OperatorPass123!",
|
||||
"operator",
|
||||
);
|
||||
const viewerCookie = await createMember(
|
||||
"viewer@datastack.dev",
|
||||
"ViewerPass123!",
|
||||
"viewer",
|
||||
);
|
||||
return { ownerCookie, operatorCookie, viewerCookie };
|
||||
}
|
||||
|
||||
test("R5-Phase-02 [MCP-AUDIT-05 / /api/mcp RBAC]: /api/mcp enforces Owner-only RBAC, auto-mints/rotates/revokes tokens, validates permissions/SSRF, and writes audit logs", async () => {
|
||||
McpSettingsManager.resetForTests();
|
||||
clearAuditLogsForTests();
|
||||
const prevDbUrl = process.env.DATABASE_URL;
|
||||
const prevDemoAdmin = process.env.DATASTACK_ALLOW_DEMO_ADMIN;
|
||||
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
|
||||
delete process.env.DATABASE_URL;
|
||||
process.env.DATASTACK_ALLOW_DEMO_ADMIN = "false";
|
||||
const syncPayloads: Array<Record<string, unknown>> = [];
|
||||
|
||||
const mockPort4000Sync = http.createServer((req, res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (c) => chunks.push(Buffer.from(c)));
|
||||
req.on("end", () => {
|
||||
if (req.url === "/internal/mcp-sync" && req.method === "POST") {
|
||||
syncPayloads.push(
|
||||
JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record<string, unknown>,
|
||||
);
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(JSON.stringify({ success: true }));
|
||||
return;
|
||||
}
|
||||
res.statusCode = 404;
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve) =>
|
||||
mockPort4000Sync.listen(0, "127.0.0.1", () => resolve()),
|
||||
);
|
||||
const syncPort = (mockPort4000Sync.address() as AddressInfo).port;
|
||||
process.env.INGESTION_INTERNAL_URL = `http://127.0.0.1:${syncPort}`;
|
||||
|
||||
try {
|
||||
const { ownerCookie, operatorCookie, viewerCookie } = await setupRbacCookies();
|
||||
|
||||
// 1. Unauthenticated /api/mcp GET & POST -> 401
|
||||
const unauthGet = await mcpSettingsGet(
|
||||
new NextRequest("http://localhost:3000/api/mcp", { method: "GET" }),
|
||||
);
|
||||
assert.equal(unauthGet.status, 401);
|
||||
|
||||
// 2. Non-owner roles (viewer & operator) -> 403 INSUFFICIENT_ROLE_PERMISSION
|
||||
for (const cookie of [viewerCookie, operatorCookie]) {
|
||||
const roleGet = await mcpSettingsGet(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "GET",
|
||||
headers: { cookie },
|
||||
}),
|
||||
);
|
||||
assert.equal(roleGet.status, 403);
|
||||
const rolePost = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ enabled: true }),
|
||||
}),
|
||||
);
|
||||
assert.equal(rolePost.status, 403);
|
||||
}
|
||||
|
||||
// 3. Owner GET returns masked public state + 17 permission definitions
|
||||
const getRes = await mcpSettingsGet(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "GET",
|
||||
headers: { cookie: ownerCookie },
|
||||
}),
|
||||
);
|
||||
assert.equal(getRes.status, 200);
|
||||
const getJson = (await getRes.json()) as {
|
||||
settings: { enabled: boolean; hasToken: boolean; endpointPath: string };
|
||||
permissionDefinitions: Array<{ key: string }>;
|
||||
runtime: { permissionCount: number };
|
||||
};
|
||||
assert.equal(getJson.settings.enabled, false);
|
||||
assert.equal(getJson.settings.hasToken, false);
|
||||
assert.equal(getJson.settings.endpointPath, "/mcp");
|
||||
assert.equal(getJson.permissionDefinitions.length, 17);
|
||||
assert.equal(getJson.runtime.permissionCount, 17);
|
||||
|
||||
// 4. Reject unknown fields, unknown permission keys, rotate+revoke conflict, and SSRF coolifyUrl
|
||||
const badFieldRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ rogueField: true }),
|
||||
}),
|
||||
);
|
||||
assert.equal(badFieldRes.status, 400);
|
||||
|
||||
const badPermRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ permissions: { hack_everything: true } }),
|
||||
}),
|
||||
);
|
||||
assert.equal(badPermRes.status, 400);
|
||||
|
||||
const ssrfRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ coolifyUrl: "http://169.254.169.254/latest" }),
|
||||
}),
|
||||
);
|
||||
assert.equal(ssrfRes.status, 400);
|
||||
assert.equal(((await ssrfRes.json()) as { code: string }).code, "SSRF_BLOCKED_HOST");
|
||||
|
||||
// 5. Enable MCP -> mints Bearer token + toggles permissions + syncs Port 4000
|
||||
const enableRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
enabled: true,
|
||||
allowedIps: "127.0.0.1",
|
||||
permissions: { clients_create: true, files_upload_create: true },
|
||||
}),
|
||||
}),
|
||||
);
|
||||
assert.equal(enableRes.status, 200);
|
||||
const enableJson = (await enableRes.json()) as {
|
||||
success: boolean;
|
||||
issuedToken?: string;
|
||||
tokenRotated: boolean;
|
||||
settings: { enabled: boolean; hasToken: boolean; tokenPreview: string };
|
||||
bridgeSync: { ok: boolean; synced: boolean };
|
||||
};
|
||||
assert.equal(enableJson.success, true);
|
||||
assert.ok(enableJson.issuedToken?.startsWith("dsmcp_"));
|
||||
assert.equal(enableJson.tokenRotated, true);
|
||||
assert.equal(enableJson.settings.enabled, true);
|
||||
assert.equal(enableJson.settings.hasToken, true);
|
||||
assert.equal(enableJson.bridgeSync.ok, true);
|
||||
assert.equal(syncPayloads.length, 1);
|
||||
|
||||
// 6. Rotate token issues new distinct token; revoke disables MCP
|
||||
const rotateRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ rotateToken: true }),
|
||||
}),
|
||||
);
|
||||
assert.equal(rotateRes.status, 200);
|
||||
const rotateJson = (await rotateRes.json()) as { issuedToken?: string };
|
||||
assert.ok(rotateJson.issuedToken);
|
||||
assert.notEqual(rotateJson.issuedToken, enableJson.issuedToken);
|
||||
|
||||
const revokeRes = await mcpSettingsPost(
|
||||
new NextRequest("http://localhost:3000/api/mcp", {
|
||||
method: "POST",
|
||||
headers: { cookie: ownerCookie, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ revokeToken: true }),
|
||||
}),
|
||||
);
|
||||
assert.equal(revokeRes.status, 200);
|
||||
const revokeJson = (await revokeRes.json()) as {
|
||||
settings: { enabled: boolean; hasToken: boolean };
|
||||
};
|
||||
assert.equal(revokeJson.settings.enabled, false);
|
||||
assert.equal(revokeJson.settings.hasToken, false);
|
||||
} finally {
|
||||
resetAdminStateForTests();
|
||||
McpSettingsManager.resetForTests();
|
||||
clearAuditLogsForTests();
|
||||
if (prevDbUrl === undefined) delete process.env.DATABASE_URL;
|
||||
else process.env.DATABASE_URL = prevDbUrl;
|
||||
if (prevDemoAdmin === undefined) delete process.env.DATASTACK_ALLOW_DEMO_ADMIN;
|
||||
else process.env.DATASTACK_ALLOW_DEMO_ADMIN = prevDemoAdmin;
|
||||
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
|
||||
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
|
||||
mockPort4000Sync.closeAllConnections?.();
|
||||
await new Promise<void>((resolve) => mockPort4000Sync.close(() => resolve()));
|
||||
}
|
||||
});
|
||||
|
||||
test("R5-Phase-02 [MCP-AUDIT-01/06/07 & ERR-R5-03/04/05/06]: Port 4000 MCP server enforces disabled/auth/IP gates, registers all 17 SDK tools, enforces permissions & path sandbox, and bridges live tools", async () => {
|
||||
const tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "datastack-r5p2-"));
|
||||
const tmpDataDir = path.join(tmpRoot, "data");
|
||||
const prevStorageDir = process.env.DATASTACK_STORAGE_DIR;
|
||||
const originalBaseDir = CSVStorageManager.baseStorageDir;
|
||||
const prevWebInternalUrl = process.env.WEB_INTERNAL_URL;
|
||||
const prevIngestionUrl = process.env.INGESTION_INTERNAL_URL;
|
||||
process.env.INGESTION_INTERNAL_URL = "http://127.0.0.1:1";
|
||||
|
||||
await fs.mkdir(tmpDataDir, { recursive: true });
|
||||
await fs.mkdir(path.join(tmpRoot, ".runtime-logs"), { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(tmpRoot, ".runtime-logs", "web.log"),
|
||||
"[web] started on 3000\n",
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(tmpDataDir, "active.csv"),
|
||||
"clientId,eventId\nc1,e1\n",
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(path.join(tmpDataDir, "active.csv.lock"), "pid:123", "utf8");
|
||||
const outsideSecret = path.join(tmpRoot, "..", `outside-${Date.now()}.txt`);
|
||||
await fs.writeFile(outsideSecret, "TOP_SECRET", "utf8");
|
||||
const symlinkPath = path.join(tmpRoot, "escape-link");
|
||||
await fs.symlink(outsideSecret, symlinkPath).catch(() => undefined);
|
||||
|
||||
// Spin up an embedded HTTP wrapper around App Router POST /api/internal/mcp
|
||||
const bridgeServer = http.createServer(async (req, res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (c) => chunks.push(Buffer.from(c)));
|
||||
req.on("end", async () => {
|
||||
const bodyStr = Buffer.concat(chunks).toString("utf8");
|
||||
const nextReq = new NextRequest("http://127.0.0.1:3000/api/internal/mcp", {
|
||||
method: req.method || "POST",
|
||||
headers: {
|
||||
"Content-Type": req.headers["content-type"] || "application/json",
|
||||
"x-datastack-internal-secret": String(
|
||||
req.headers["x-datastack-internal-secret"] || "",
|
||||
),
|
||||
},
|
||||
body: req.method === "POST" ? bodyStr : undefined,
|
||||
});
|
||||
const nextRes = await internalMcpPost(nextReq);
|
||||
res.statusCode = nextRes.status;
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(await nextRes.text());
|
||||
});
|
||||
});
|
||||
|
||||
let mcpHttpServer: http.Server | null = null;
|
||||
try {
|
||||
CSVStorageManager.baseStorageDir = tmpDataDir;
|
||||
process.env.DATASTACK_STORAGE_DIR = tmpDataDir;
|
||||
clearAuditLogsForTests();
|
||||
|
||||
await new Promise<void>((resolve) =>
|
||||
bridgeServer.listen(0, "127.0.0.1", () => resolve()),
|
||||
);
|
||||
const bridgePort = (bridgeServer.address() as AddressInfo).port;
|
||||
process.env.WEB_INTERNAL_URL = `http://127.0.0.1:${bridgePort}`;
|
||||
|
||||
const express = require("express");
|
||||
const { isClientIpAllowed } = ingestionServer as unknown as {
|
||||
isClientIpAllowed?: (ip: string, allow: string) => boolean;
|
||||
};
|
||||
const internalHeaders = getInternalIngestionHeaders();
|
||||
|
||||
const engine = createMcpEngine({
|
||||
getStorageDataDir: () => tmpDataDir,
|
||||
getDbPool: () => null,
|
||||
isClientIpAllowed:
|
||||
typeof isClientIpAllowed === "function"
|
||||
? isClientIpAllowed
|
||||
: (ip: string, list: string) => list.includes(ip),
|
||||
resolveInternalSecret: () => internalHeaders["x-datastack-internal-secret"],
|
||||
decryptVaultSecret: (v: string) => v,
|
||||
validateOutboundUrlAsync: async (urlStr: string) => {
|
||||
const u = new URL(urlStr);
|
||||
if (u.hostname === "169.254.169.254" || u.hostname === "127.0.0.1") {
|
||||
return {
|
||||
allowed: false,
|
||||
code: "SSRF_BLOCKED_HOST",
|
||||
error: `Blocked host ${u.hostname}`,
|
||||
};
|
||||
}
|
||||
return { allowed: true, url: urlStr };
|
||||
},
|
||||
flushBuffer: async () => ({ written: 2, files: ["active.csv"] }),
|
||||
});
|
||||
|
||||
const testApp = express();
|
||||
testApp.use(express.json({ limit: "5mb" }));
|
||||
testApp.all("/mcp", (req: unknown, res: unknown) => {
|
||||
void engine.handleHttpRequest(req, res);
|
||||
});
|
||||
mcpHttpServer = http.createServer(testApp);
|
||||
await new Promise<void>((resolve) =>
|
||||
mcpHttpServer!.listen(0, "127.0.0.1", () => resolve()),
|
||||
);
|
||||
const mcpPort = (mcpHttpServer.address() as AddressInfo).port;
|
||||
const mcpUrl = new URL(`http://127.0.0.1:${mcpPort}/mcp`);
|
||||
|
||||
// 1. Disabled by default -> 503 MCP_DISABLED
|
||||
const disabledRes = await fetch(mcpUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ jsonrpc: "2.0", method: "tools/list", id: 1 }),
|
||||
});
|
||||
assert.equal(disabledRes.status, 503);
|
||||
const disabledJson = (await disabledRes.json()) as {
|
||||
error?: { data?: { code?: string } };
|
||||
};
|
||||
assert.equal(disabledJson.error?.data?.code, "MCP_DISABLED");
|
||||
|
||||
// 2. Enable with token + IP allowlist
|
||||
const rawToken = `dsmcp_${"a".repeat(64)}`;
|
||||
engine.syncState({
|
||||
enabled: true,
|
||||
tokenHash: hashMcpToken(rawToken),
|
||||
allowedIps: "203.0.113.0/24",
|
||||
permissions: {
|
||||
metrics_health_read: true,
|
||||
clients_read: true,
|
||||
clients_create: false,
|
||||
files_list_read: true,
|
||||
files_download: true,
|
||||
files_upload_create: true,
|
||||
files_edit: true,
|
||||
files_delete: true,
|
||||
storage_maintenance: true,
|
||||
build_logs_read: true,
|
||||
},
|
||||
coolifyUrl: "http://127.0.0.1:9999",
|
||||
coolifyToken: "secret_pat",
|
||||
coolifyAppUuid: "app-1",
|
||||
});
|
||||
|
||||
// 3. IP blocked -> 403 MCP_IP_FORBIDDEN
|
||||
const ipBlockedRes = await fetch(mcpUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${rawToken}`,
|
||||
},
|
||||
body: JSON.stringify({ jsonrpc: "2.0", method: "tools/list", id: 2 }),
|
||||
});
|
||||
assert.equal(ipBlockedRes.status, 403);
|
||||
assert.equal(
|
||||
((await ipBlockedRes.json()) as { error?: { data?: { code?: string } } }).error?.data
|
||||
?.code,
|
||||
"MCP_IP_FORBIDDEN",
|
||||
);
|
||||
|
||||
// 4. Open allowlist to loopback & test invalid Bearer token -> 401 MCP_UNAUTHORIZED
|
||||
engine.syncState({ allowedIps: "127.0.0.1" });
|
||||
const badTokenRes = await fetch(mcpUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer dsmcp_wrong_token",
|
||||
},
|
||||
body: JSON.stringify({ jsonrpc: "2.0", method: "tools/list", id: 3 }),
|
||||
});
|
||||
assert.equal(badTokenRes.status, 401);
|
||||
assert.equal(
|
||||
((await badTokenRes.json()) as { error?: { data?: { code?: string } } }).error?.data
|
||||
?.code,
|
||||
"MCP_UNAUTHORIZED",
|
||||
);
|
||||
|
||||
// 5. Real @modelcontextprotocol/sdk Client + StreamableHTTPClientTransport session
|
||||
const mcpClient = new Client({
|
||||
name: "r5-phase02-test-client",
|
||||
version: "1.0.0",
|
||||
});
|
||||
const transport = new StreamableHTTPClientTransport(mcpUrl, {
|
||||
requestInit: {
|
||||
headers: {
|
||||
Authorization: `Bearer ${rawToken}`,
|
||||
},
|
||||
},
|
||||
});
|
||||
await mcpClient.connect(transport);
|
||||
|
||||
const listedTools = await mcpClient.listTools();
|
||||
assert.equal(listedTools.tools.length, 17);
|
||||
assert.ok(listedTools.tools.some((t) => t.name === "datastack_metrics_health"));
|
||||
assert.ok(listedTools.tools.some((t) => t.name === "datastack_files_edit"));
|
||||
assert.ok(listedTools.tools.some((t) => t.name === "datastack_storage_maintenance"));
|
||||
|
||||
// 6. Disabled permission blocks tool call (ERR-R5-04 -> MCP_PERMISSION_DENIED)
|
||||
const deniedClientCreate = await mcpClient.callTool({
|
||||
name: "datastack_clients_create",
|
||||
arguments: { clientId: "denied-client", name: "Denied" },
|
||||
});
|
||||
assert.equal(deniedClientCreate.isError, true);
|
||||
const deniedPayload = JSON.parse(
|
||||
(deniedClientCreate.content[0] as { text: string }).text,
|
||||
) as { code: string; permission: string };
|
||||
assert.equal(deniedPayload.code, "MCP_PERMISSION_DENIED");
|
||||
assert.equal(deniedPayload.permission, "clients_create");
|
||||
|
||||
// 7. Enable clients_create live & invoke -> creates client via /api/internal/mcp bridge
|
||||
engine.syncState({ permissions: { clients_create: true } });
|
||||
const testClientId = `mcp-live-${Date.now()}`;
|
||||
const createdToolRes = await mcpClient.callTool({
|
||||
name: "datastack_clients_create",
|
||||
arguments: { clientId: testClientId, name: "Live MCP Client", rateLimit: 320 },
|
||||
});
|
||||
assert.notEqual(createdToolRes.isError, true);
|
||||
const createdPayload = JSON.parse(
|
||||
(createdToolRes.content[0] as { text: string }).text,
|
||||
) as { success: boolean; client: { clientId: string; rateLimit: number; token?: string } };
|
||||
assert.equal(createdPayload.success, true);
|
||||
assert.equal(createdPayload.client.clientId, testClientId);
|
||||
assert.equal(createdPayload.client.rateLimit, 320);
|
||||
assert.ok(createdPayload.client.token);
|
||||
|
||||
// 8. Sandbox path confinement: traversal, symlink, *.lock, and .runtime-logs write protection
|
||||
const traversalRes = await mcpClient.callTool({
|
||||
name: "datastack_files_download",
|
||||
arguments: { path: "../../etc/passwd" },
|
||||
});
|
||||
assert.equal(traversalRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((traversalRes.content[0] as { text: string }).text) as { code: string }).code,
|
||||
"PATH_OUTSIDE_ROOT",
|
||||
);
|
||||
|
||||
if (existsSync(symlinkPath)) {
|
||||
const symlinkRes = await mcpClient.callTool({
|
||||
name: "datastack_files_download",
|
||||
arguments: { path: "escape-link" },
|
||||
});
|
||||
assert.equal(symlinkRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((symlinkRes.content[0] as { text: string }).text) as { code: string }).code,
|
||||
"SYMLINK_FORBIDDEN",
|
||||
);
|
||||
}
|
||||
|
||||
const lockReadRes = await mcpClient.callTool({
|
||||
name: "datastack_files_download",
|
||||
arguments: { path: "data/active.csv.lock" },
|
||||
});
|
||||
assert.equal(lockReadRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((lockReadRes.content[0] as { text: string }).text) as { code: string }).code,
|
||||
"LOCKFILE_PROTECTED",
|
||||
);
|
||||
|
||||
const lockedFileUploadRes = await mcpClient.callTool({
|
||||
name: "datastack_files_upload",
|
||||
arguments: { path: "data/active.csv", content: "overwrite", overwrite: true },
|
||||
});
|
||||
assert.equal(lockedFileUploadRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((lockedFileUploadRes.content[0] as { text: string }).text) as { code: string })
|
||||
.code,
|
||||
"LOCKFILE_PROTECTED",
|
||||
);
|
||||
|
||||
const logWriteRes = await mcpClient.callTool({
|
||||
name: "datastack_files_upload",
|
||||
arguments: { path: ".runtime-logs/web.log", content: "tamper", overwrite: true },
|
||||
});
|
||||
assert.equal(logWriteRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((logWriteRes.content[0] as { text: string }).text) as { code: string }).code,
|
||||
"PROTECTED_LOG_DIR",
|
||||
);
|
||||
|
||||
// 9. Legitimate files upload -> edit -> download -> list -> delete inside sandbox
|
||||
const uploadRes = await mcpClient.callTool({
|
||||
name: "datastack_files_upload",
|
||||
arguments: { path: "notes/hello.txt", content: "alpha beta" },
|
||||
});
|
||||
assert.notEqual(uploadRes.isError, true);
|
||||
|
||||
const editRes = await mcpClient.callTool({
|
||||
name: "datastack_files_edit",
|
||||
arguments: { path: "notes/hello.txt", search: "beta", replace: "gamma" },
|
||||
});
|
||||
assert.notEqual(editRes.isError, true);
|
||||
|
||||
const dlRes = await mcpClient.callTool({
|
||||
name: "datastack_files_download",
|
||||
arguments: { path: "notes/hello.txt" },
|
||||
});
|
||||
assert.notEqual(dlRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((dlRes.content[0] as { text: string }).text) as { content: string }).content,
|
||||
"alpha gamma",
|
||||
);
|
||||
|
||||
const listRes = await mcpClient.callTool({
|
||||
name: "datastack_files_list",
|
||||
arguments: { path: "data" },
|
||||
});
|
||||
assert.notEqual(listRes.isError, true);
|
||||
const listParsed = JSON.parse((listRes.content[0] as { text: string }).text) as {
|
||||
entries: Array<{ name: string }>;
|
||||
};
|
||||
assert.ok(listParsed.entries.some((e) => e.name === "active.csv"));
|
||||
assert.equal(
|
||||
listParsed.entries.some((e) => e.name.endsWith(".lock")),
|
||||
false,
|
||||
);
|
||||
|
||||
const delFileRes = await mcpClient.callTool({
|
||||
name: "datastack_files_delete",
|
||||
arguments: { path: "notes/hello.txt" },
|
||||
});
|
||||
assert.notEqual(delFileRes.isError, true);
|
||||
|
||||
// 10. Metrics, Storage flush (calls local flush + web bridge flush), and SSRF check on Coolify logs
|
||||
const metricsRes = await mcpClient.callTool({
|
||||
name: "datastack_metrics_health",
|
||||
arguments: {},
|
||||
});
|
||||
assert.notEqual(metricsRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((metricsRes.content[0] as { text: string }).text) as { success: boolean }).success,
|
||||
true,
|
||||
);
|
||||
|
||||
const flushRes = await mcpClient.callTool({
|
||||
name: "datastack_storage_maintenance",
|
||||
arguments: { action: "flush" },
|
||||
});
|
||||
assert.notEqual(flushRes.isError, true);
|
||||
const flushPayload = JSON.parse((flushRes.content[0] as { text: string }).text) as {
|
||||
success: boolean;
|
||||
ingestionFlushed: number;
|
||||
};
|
||||
assert.equal(flushPayload.success, true);
|
||||
assert.equal(flushPayload.ingestionFlushed, 2);
|
||||
|
||||
const coolifySsrfRes = await mcpClient.callTool({
|
||||
name: "datastack_build_logs",
|
||||
arguments: { includeCoolify: true },
|
||||
});
|
||||
assert.equal(coolifySsrfRes.isError, true);
|
||||
assert.equal(
|
||||
(JSON.parse((coolifySsrfRes.content[0] as { text: string }).text) as { code: string }).code,
|
||||
"SSRF_BLOCKED_HOST",
|
||||
);
|
||||
|
||||
await transport.close().catch(() => undefined);
|
||||
await mcpClient.close().catch(() => undefined);
|
||||
await SDKManager.deleteClientWithStatsAsync(testClientId, { purgeFiles: true });
|
||||
} finally {
|
||||
CSVStorageManager.baseStorageDir = originalBaseDir;
|
||||
if (prevStorageDir === undefined) delete process.env.DATASTACK_STORAGE_DIR;
|
||||
else process.env.DATASTACK_STORAGE_DIR = prevStorageDir;
|
||||
if (prevWebInternalUrl === undefined) delete process.env.WEB_INTERNAL_URL;
|
||||
else process.env.WEB_INTERNAL_URL = prevWebInternalUrl;
|
||||
if (prevIngestionUrl === undefined) delete process.env.INGESTION_INTERNAL_URL;
|
||||
else process.env.INGESTION_INTERNAL_URL = prevIngestionUrl;
|
||||
clearAuditLogsForTests();
|
||||
if (mcpHttpServer) {
|
||||
mcpHttpServer.closeAllConnections?.();
|
||||
await new Promise<void>((resolve) => mcpHttpServer!.close(() => resolve()));
|
||||
}
|
||||
bridgeServer.closeAllConnections?.();
|
||||
await new Promise<void>((resolve) => bridgeServer.close(() => resolve()));
|
||||
await fs.rm(outsideSecret, { force: true }).catch(() => undefined);
|
||||
await fs.rm(tmpRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("R5-Phase-02 [Port 4000 Routes & Proxy Configs]: server/index.js exposes /internal/mcp-sync & /mcp and Nginx/Next/Compose/Env route /mcp cleanly", async () => {
|
||||
try {
|
||||
const internalHeaders = getInternalIngestionHeaders();
|
||||
const expressAppServer = http.createServer(ingestionServer.app);
|
||||
await new Promise<void>((resolve) =>
|
||||
expressAppServer.listen(0, "127.0.0.1", () => resolve()),
|
||||
);
|
||||
const port = (expressAppServer.address() as AddressInfo).port;
|
||||
|
||||
try {
|
||||
// 1. /internal/mcp-sync rejects missing internal secret -> 401
|
||||
const unauthSync = await fetch(`http://127.0.0.1:${port}/internal/mcp-sync`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ enabled: true }),
|
||||
});
|
||||
assert.equal(unauthSync.status, 401);
|
||||
|
||||
// 2. /internal/mcp-sync with valid internal secret updates live mcpEngine state
|
||||
const tok = `dsmcp_${"b".repeat(64)}`;
|
||||
const authSync = await fetch(`http://127.0.0.1:${port}/internal/mcp-sync`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-datastack-internal-secret": internalHeaders["x-datastack-internal-secret"],
|
||||
},
|
||||
body: JSON.stringify({
|
||||
enabled: true,
|
||||
tokenHash: hashMcpToken(tok),
|
||||
tokenPreview: "dsmcp_bbbb...bbbb",
|
||||
allowedIps: "127.0.0.1",
|
||||
}),
|
||||
});
|
||||
assert.equal(authSync.status, 200);
|
||||
assert.equal(ingestionServer.mcpEngine.getStateSnapshot().enabled, true);
|
||||
|
||||
// 3. /mcp endpoint responds to MCP initialize JSON-RPC call cleanly
|
||||
const mcpInit = await fetch(`http://127.0.0.1:${port}/mcp`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${tok}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "initialize",
|
||||
params: {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {},
|
||||
clientInfo: { name: "curl-smoke", version: "1.0.0" },
|
||||
},
|
||||
}),
|
||||
});
|
||||
assert.equal(mcpInit.status, 200);
|
||||
const initJson = (await mcpInit.json()) as {
|
||||
result?: { serverInfo?: { name?: string } };
|
||||
};
|
||||
assert.equal(initJson.result?.serverInfo?.name, "datastack-mcp");
|
||||
|
||||
// 4. hydrateFromDatabase syncs DB mcp_settings row cleanly
|
||||
ingestionServer.setDbPoolForTests({
|
||||
async query(sql: string) {
|
||||
if (/FROM\s+mcp_settings/i.test(sql)) {
|
||||
return {
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
enabled: false,
|
||||
token_hash: "",
|
||||
token_preview: "",
|
||||
allowed_ips: "10.0.0.0/8",
|
||||
permissions_json: JSON.stringify({ clients_create: true }),
|
||||
coolify_url: "https://coolify.example.com",
|
||||
coolify_token_enc: "",
|
||||
coolify_app_uuid: "uuid-99",
|
||||
updated_at: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
return { rowCount: 0, rows: [] };
|
||||
},
|
||||
});
|
||||
assert.equal(await ingestionServer.mcpEngine.hydrateFromDatabase(), true);
|
||||
const snap = ingestionServer.mcpEngine.getStateSnapshot() as {
|
||||
enabled: boolean;
|
||||
allowedIps: string;
|
||||
permissions: Record<string, boolean>;
|
||||
};
|
||||
assert.equal(snap.enabled, false);
|
||||
assert.equal(snap.allowedIps, "10.0.0.0/8");
|
||||
assert.equal(snap.permissions.clients_create, true);
|
||||
} finally {
|
||||
ingestionServer.mcpEngine.resetForTests();
|
||||
ingestionServer.setDbPoolForTests(null);
|
||||
expressAppServer.closeAllConnections?.();
|
||||
await new Promise<void>((resolve) => expressAppServer.close(() => resolve()));
|
||||
}
|
||||
|
||||
// 5. Verify Nginx, NextConfig, Compose, Env, and MCP_PERMISSION_DEFINITIONS 1:1 count
|
||||
assert.equal(MCP_PERMISSION_DEFINITIONS.length, 17);
|
||||
|
||||
const coolifyNginx = readFileSync(
|
||||
path.resolve(process.cwd(), "docker/coolify-nginx.conf"),
|
||||
"utf8",
|
||||
);
|
||||
assert.match(
|
||||
coolifyNginx,
|
||||
/location\s+\/mcp\s*\{[\s\S]*?proxy_pass\s+\$socketio_upstream;[\s\S]*?proxy_buffering\s+off;/,
|
||||
);
|
||||
|
||||
const localNginx = readFileSync(
|
||||
path.resolve(process.cwd(), "nginx/default.conf"),
|
||||
"utf8",
|
||||
);
|
||||
assert.match(
|
||||
localNginx,
|
||||
/location\s+\/mcp\s*\{[\s\S]*?proxy_pass\s+http:\/\/socketio_upstream;[\s\S]*?proxy_buffering\s+off;/,
|
||||
);
|
||||
|
||||
const nextConfig = readFileSync(path.resolve(process.cwd(), "next.config.ts"), "utf8");
|
||||
assert.match(nextConfig, /source:\s*['"]\/mcp['"]/);
|
||||
assert.match(nextConfig, /destination:\s*`\$\{ingestionBase\}\/mcp`/);
|
||||
|
||||
const compose = readFileSync(
|
||||
path.resolve(process.cwd(), "docker/docker-compose.yml"),
|
||||
"utf8",
|
||||
);
|
||||
assert.match(compose, /WEB_INTERNAL_URL:\s*"http:\/\/web:3000"/);
|
||||
|
||||
const envEx = readFileSync(path.resolve(process.cwd(), ".env.example"), "utf8");
|
||||
assert.match(envEx, /WEB_INTERNAL_URL=/);
|
||||
} finally {
|
||||
ingestionServer.stopTimers();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,202 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
import { MCP_PERMISSION_DEFINITIONS } from "../core/mcp/mcpSettings";
|
||||
import {
|
||||
applyCategoryPermissionToggle,
|
||||
applyMcpPermissionPreset,
|
||||
buildMcpClientConfigSnippet,
|
||||
buildMcpEndpointUrl,
|
||||
buildMcpSavePayload,
|
||||
countEnabledMcpPermissions,
|
||||
DEFAULT_PUBLIC_MCP_SETTINGS,
|
||||
describeMcpBridgeSync,
|
||||
groupMcpPermissionsByCategory,
|
||||
isMcpDraftDirty,
|
||||
MCP_CLIENT_TARGETS,
|
||||
type McpDraftState,
|
||||
type McpPermissionDefinitionView,
|
||||
type PublicMcpSettingsView,
|
||||
} from "../lib/mcpUiHelpers";
|
||||
|
||||
const DEFINITIONS = MCP_PERMISSION_DEFINITIONS as readonly McpPermissionDefinitionView[];
|
||||
|
||||
test("R5-Phase-03 [lib/mcpUiHelpers.ts]: URL builder, AI client JSON snippets, permission grouping/presets, bridge status, and diff payload", () => {
|
||||
// 1. Endpoint URL builder
|
||||
assert.equal(
|
||||
buildMcpEndpointUrl("https://datastack.example.com/", "/mcp"),
|
||||
"https://datastack.example.com/mcp",
|
||||
);
|
||||
assert.equal(buildMcpEndpointUrl("", "mcp"), "/mcp");
|
||||
|
||||
// 2. Client config snippets (4 targets, valid JSON, token vs placeholder)
|
||||
assert.equal(MCP_CLIENT_TARGETS.length, 4);
|
||||
for (const target of MCP_CLIENT_TARGETS) {
|
||||
const rawPlaceholder = buildMcpClientConfigSnippet(target.id, {
|
||||
endpointUrl: "https://telemetry.example.com/mcp",
|
||||
});
|
||||
const parsedPlaceholder = JSON.parse(rawPlaceholder) as Record<string, unknown>;
|
||||
assert.ok(parsedPlaceholder);
|
||||
assert.match(rawPlaceholder, /Bearer YOUR_MCP_BEARER_TOKEN/);
|
||||
|
||||
const rawToken = buildMcpClientConfigSnippet(target.id, {
|
||||
endpointUrl: "https://telemetry.example.com/mcp",
|
||||
token: "dsmcp_1234567890abcdef",
|
||||
});
|
||||
const parsedToken = JSON.parse(rawToken) as Record<string, unknown>;
|
||||
assert.ok(parsedToken);
|
||||
assert.match(rawToken, /Bearer dsmcp_1234567890abcdef/);
|
||||
|
||||
if (target.id === "vscode") {
|
||||
const servers = parsedToken.servers as Record<string, { type: string; url: string }>;
|
||||
assert.equal(servers.datastack.type, "http");
|
||||
assert.equal(servers.datastack.url, "https://telemetry.example.com/mcp");
|
||||
} else if (target.id === "windsurf") {
|
||||
const servers = parsedToken.mcpServers as Record<string, { serverUrl: string }>;
|
||||
assert.equal(servers.datastack.serverUrl, "https://telemetry.example.com/mcp");
|
||||
} else {
|
||||
const servers = parsedToken.mcpServers as Record<string, { url: string }>;
|
||||
assert.equal(servers.datastack.url, "https://telemetry.example.com/mcp");
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Permission grouping across all 17 definitions & 4 categories
|
||||
const defaultPerms = applyMcpPermissionPreset({}, DEFINITIONS, "defaults");
|
||||
const groups = groupMcpPermissionsByCategory(DEFINITIONS, defaultPerms);
|
||||
assert.equal(groups.length, 4);
|
||||
assert.deepEqual(
|
||||
groups.map((g) => [g.category, g.totalCount]),
|
||||
[
|
||||
["Observability & Logs", 5],
|
||||
["Clients & SDKs", 4],
|
||||
["Files & Storage", 5],
|
||||
["Admin & Maintenance", 3],
|
||||
],
|
||||
);
|
||||
const defaultCounts = countEnabledMcpPermissions(defaultPerms, DEFINITIONS);
|
||||
assert.equal(defaultCounts.total, 17);
|
||||
assert.equal(defaultCounts.enabled, 9);
|
||||
|
||||
// 4. Presets & category toggle
|
||||
const readOnlyPerms = applyMcpPermissionPreset(defaultPerms, DEFINITIONS, "read_only");
|
||||
assert.equal(readOnlyPerms.clients_create, false);
|
||||
assert.equal(readOnlyPerms.files_delete, false);
|
||||
assert.equal(readOnlyPerms.metrics_health_read, true);
|
||||
assert.equal(readOnlyPerms.files_download, true);
|
||||
|
||||
const clientEnabled = applyCategoryPermissionToggle(
|
||||
readOnlyPerms,
|
||||
DEFINITIONS,
|
||||
"Clients & SDKs",
|
||||
true,
|
||||
);
|
||||
assert.equal(clientEnabled.clients_create, true);
|
||||
assert.equal(clientEnabled.clients_update, true);
|
||||
assert.equal(clientEnabled.clients_revoke_delete, true);
|
||||
assert.equal(clientEnabled.files_delete, false);
|
||||
|
||||
const allOff = applyMcpPermissionPreset(clientEnabled, DEFINITIONS, "all_off");
|
||||
assert.equal(countEnabledMcpPermissions(allOff, DEFINITIONS).enabled, 0);
|
||||
|
||||
// 5. Bridge sync summaries
|
||||
assert.equal(describeMcpBridgeSync(null).level, "idle");
|
||||
assert.equal(describeMcpBridgeSync({ attempted: true, ok: true, synced: true }).level, "success");
|
||||
assert.equal(
|
||||
describeMcpBridgeSync({ attempted: true, ok: false, status: 401 }).level,
|
||||
"error",
|
||||
);
|
||||
assert.equal(
|
||||
describeMcpBridgeSync({ attempted: true, ok: false, error: "ECONNREFUSED" }).level,
|
||||
"warning",
|
||||
);
|
||||
|
||||
// 6. Save payload diffing
|
||||
const saved: PublicMcpSettingsView = {
|
||||
...DEFAULT_PUBLIC_MCP_SETTINGS,
|
||||
enabled: false,
|
||||
allowedIps: "127.0.0.1",
|
||||
permissions: { ...defaultPerms },
|
||||
coolifyUrl: "",
|
||||
hasCoolifyToken: true,
|
||||
coolifyTokenMask: "••••1234",
|
||||
coolifyAppUuid: "app-1",
|
||||
};
|
||||
const cleanDraft: McpDraftState = {
|
||||
enabled: false,
|
||||
allowedIps: "127.0.0.1",
|
||||
permissions: { ...defaultPerms },
|
||||
coolifyUrl: "",
|
||||
coolifyTokenInput: "",
|
||||
clearCoolifyToken: false,
|
||||
coolifyAppUuid: "app-1",
|
||||
};
|
||||
assert.equal(isMcpDraftDirty(cleanDraft, saved, DEFINITIONS), false);
|
||||
assert.deepEqual(buildMcpSavePayload(cleanDraft, saved, DEFINITIONS), {});
|
||||
|
||||
const editedDraft: McpDraftState = {
|
||||
...cleanDraft,
|
||||
enabled: true,
|
||||
permissions: { ...defaultPerms, clients_create: true },
|
||||
clearCoolifyToken: true,
|
||||
};
|
||||
assert.equal(isMcpDraftDirty(editedDraft, saved, DEFINITIONS), true);
|
||||
assert.deepEqual(buildMcpSavePayload(editedDraft, saved, DEFINITIONS), {
|
||||
enabled: true,
|
||||
permissions: { clients_create: true },
|
||||
clearCoolifyToken: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("R5-Phase-03 [Routes, Sidebar, Navbar & UI Architecture]: /vcon/mcp, McpWorkspace, Sidebar, and Navbar comply with project single-source-of-truth & security rules", () => {
|
||||
const pagePath = path.resolve(process.cwd(), "app/vcon/mcp/page.tsx");
|
||||
assert.equal(existsSync(pagePath), true);
|
||||
const pageSrc = readFileSync(pagePath, "utf8");
|
||||
assert.match(pageSrc, /McpWorkspace/);
|
||||
|
||||
const sidebarSrc = readFileSync(
|
||||
path.resolve(process.cwd(), "components/layout/Sidebar.tsx"),
|
||||
"utf8",
|
||||
);
|
||||
assert.match(sidebarSrc, /name:\s*['"]MCP Server['"]/);
|
||||
assert.match(sidebarSrc, /href:\s*['"]\/vcon\/mcp['"]/);
|
||||
|
||||
const navbarSrc = readFileSync(
|
||||
path.resolve(process.cwd(), "components/layout/Navbar.tsx"),
|
||||
"utf8",
|
||||
);
|
||||
assert.match(navbarSrc, /['"]\/vcon\/mcp['"]:\s*['"]MCP Server['"]/);
|
||||
|
||||
const workspaceSrc = readFileSync(
|
||||
path.resolve(process.cwd(), "components/sections/McpWorkspace.tsx"),
|
||||
"utf8",
|
||||
);
|
||||
// Uses shared single-source-of-truth UI primitives
|
||||
for (const symbol of [
|
||||
"PageShell",
|
||||
"StatStrip",
|
||||
"SettingsTabs",
|
||||
"SettingsPanel",
|
||||
"SettingsGroup",
|
||||
"SettingsField",
|
||||
"SettingsFooter",
|
||||
"Panel",
|
||||
"Button",
|
||||
"Input",
|
||||
"FormSwitch",
|
||||
"CodeBlock",
|
||||
"Modal",
|
||||
]) {
|
||||
assert.ok(workspaceSrc.includes(symbol), `Expected ${symbol} inside McpWorkspace.tsx`);
|
||||
}
|
||||
|
||||
// Enforces Owner RBAC guard without caching raw issuedToken to localStorage/vconCache
|
||||
assert.match(workspaceSrc, /isOwner/);
|
||||
assert.match(workspaceSrc, /mcp-non-owner-banner/);
|
||||
assert.match(workspaceSrc, /mcp-issued-token-banner/);
|
||||
assert.equal(/localStorage/.test(workspaceSrc), false);
|
||||
assert.equal(/setCached\([^)]*issuedToken/.test(workspaceSrc), false);
|
||||
assert.equal(/\brounded-/.test(workspaceSrc), false);
|
||||
assert.equal(/\bshadow-/.test(workspaceSrc), false);
|
||||
});
|
||||
Reference in new issue
Block a user