1060 lines
39 KiB
TypeScript
1060 lines
39 KiB
TypeScript
import { describe, expect, test, vi, beforeEach } from "vitest";
|
|
import { PLUGIN_CAPABILITY_NAMES } from "@hivegrid/plugin-runtime/capabilities";
|
|
import { readRepoFile } from "~/utils/testing/repoPaths";
|
|
|
|
const listMock = vi.fn();
|
|
const enableMock = vi.fn();
|
|
const disableMock = vi.fn();
|
|
const listGrantsMock = vi.fn();
|
|
const listGrantsForPluginsMock = vi.fn();
|
|
const grantMock = vi.fn();
|
|
const revokeMock = vi.fn();
|
|
const verifyPermissionMock = vi.fn().mockResolvedValue(undefined);
|
|
const getUserPermissionMock = vi.fn().mockResolvedValue({ userPermission: "app.plugins.manage" });
|
|
const csrfMock = vi.fn().mockResolvedValue(undefined);
|
|
const getUserInfoMock = vi.fn().mockResolvedValue({ userId: "u-1" });
|
|
const getUserMock = vi.fn().mockResolvedValue({ email: "admin@example.com" });
|
|
const getTenantIdMock = vi.fn().mockResolvedValue("t-1");
|
|
|
|
vi.mock("~/utils/services/.server/urlService", () => ({
|
|
getTenantIdFromUrl: (...args: unknown[]) => getTenantIdMock(...args),
|
|
}));
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
|
verifyUserHasPermission: (...args: unknown[]) => verifyPermissionMock(...args),
|
|
getUserPermission: (...args: unknown[]) => getUserPermissionMock(...args),
|
|
}));
|
|
vi.mock("~/utils/session.server", () => ({
|
|
getUserInfo: (...args: unknown[]) => getUserInfoMock(...args),
|
|
validateCSRFToken: (...args: unknown[]) => csrfMock(...args),
|
|
}));
|
|
vi.mock("~/utils/db/users.db.server", () => ({
|
|
getUser: (...args: unknown[]) => getUserMock(...args),
|
|
}));
|
|
vi.mock("~/modules/plugins/services/.server/PluginLifecycleService", () => {
|
|
class PluginLifecycleError extends Error {
|
|
constructor(
|
|
public code: string,
|
|
message: string
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
return {
|
|
PluginLifecycleError,
|
|
listPlugins: (...args: unknown[]) => listMock(...args),
|
|
enablePlugin: (...args: unknown[]) => enableMock(...args),
|
|
disablePlugin: (...args: unknown[]) => disableMock(...args),
|
|
listPluginCapabilityGrants: (...args: unknown[]) => listGrantsMock(...args),
|
|
listPluginCapabilityGrantsForPlugins: (...args: unknown[]) => listGrantsForPluginsMock(...args),
|
|
grantPluginCapabilities: (...args: unknown[]) => grantMock(...args),
|
|
revokePluginCapabilities: (...args: unknown[]) => revokeMock(...args),
|
|
isCapabilityGrantScopeSatisfied: () => true,
|
|
};
|
|
});
|
|
|
|
beforeEach(() => {
|
|
listMock.mockReset();
|
|
enableMock.mockReset();
|
|
disableMock.mockReset();
|
|
listGrantsMock.mockReset();
|
|
listGrantsMock.mockResolvedValue([]);
|
|
listGrantsForPluginsMock.mockReset();
|
|
listGrantsForPluginsMock.mockResolvedValue([]);
|
|
grantMock.mockReset();
|
|
revokeMock.mockReset();
|
|
verifyPermissionMock.mockClear();
|
|
getUserPermissionMock.mockReset();
|
|
getUserPermissionMock.mockResolvedValue({ userPermission: "app.plugins.manage" });
|
|
csrfMock.mockClear();
|
|
getUserInfoMock.mockClear();
|
|
getUserMock.mockClear();
|
|
});
|
|
|
|
function readPluginCapabilityLabels(locale: "de" | "en") {
|
|
const catalog = JSON.parse(readRepoFile(`apps/web/public/locales/${locale}/translations.json`)) as {
|
|
settings?: { plugins?: { capabilityNames?: Record<string, string> } };
|
|
};
|
|
return catalog.settings?.plugins?.capabilityNames ?? {};
|
|
}
|
|
|
|
function interpolateTranslation(template: string, options?: Record<string, unknown>) {
|
|
return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) => String(options?.[key] ?? `{{${key}}}`));
|
|
}
|
|
|
|
function createCapabilityLabelTranslator(labels: Record<string, string>) {
|
|
const calls: string[] = [];
|
|
const t = vi.fn((key: string, options?: Record<string, unknown>) => {
|
|
calls.push(key);
|
|
const labelKey = key.replace("settings.plugins.capabilityNames.", "");
|
|
const template = labels[labelKey];
|
|
if (template) {
|
|
return interpolateTranslation(template, options);
|
|
}
|
|
return String(options?.defaultValue ?? key);
|
|
});
|
|
return { calls, t };
|
|
}
|
|
|
|
describe("plugins settings route", () => {
|
|
function getCapabilityReviewDigest(
|
|
plugin: {
|
|
pluginId: string;
|
|
version: string;
|
|
manifest: { capabilities?: Array<{ name: string; scope?: unknown }>; hooks?: Array<{ name: string; export: string }> };
|
|
},
|
|
capabilityNames: string[]
|
|
) {
|
|
return import("~/modules/plugins/utils/capabilityReviewDigest").then(({ createCapabilityReviewDigest }) =>
|
|
createCapabilityReviewDigest(plugin as never, capabilityNames)
|
|
);
|
|
}
|
|
|
|
test("loader returns list with manifest preview", async () => {
|
|
listMock.mockResolvedValue([
|
|
{
|
|
id: "pl-1",
|
|
pluginId: "com.acme.hi",
|
|
version: "1.0.0",
|
|
enabled: true,
|
|
manifest: { capabilities: [{ name: "log" }], id: "com.acme.hi" },
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
},
|
|
]);
|
|
listGrantsForPluginsMock.mockResolvedValue([
|
|
{
|
|
id: "grant-1",
|
|
tenantId: "t-1",
|
|
pluginId: "com.acme.hi",
|
|
capabilityName: "log",
|
|
scopeJson: null,
|
|
grantedAt: new Date("2026-01-02T00:00:00.000Z"),
|
|
grantedByUserId: "u-1",
|
|
},
|
|
]);
|
|
const { loader } = await import("./plugins.js");
|
|
const response = await loader({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins"),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
const data = await (response as Response).json();
|
|
expect(data.items).toHaveLength(1);
|
|
expect(data.items[0].capabilities).toEqual([
|
|
{
|
|
name: "log",
|
|
scope: null,
|
|
granted: true,
|
|
grantedAt: "2026-01-02T00:00:00.000Z",
|
|
grantedScopeJson: null,
|
|
metadata: { risk: "low", productionAvailable: true },
|
|
},
|
|
]);
|
|
expect(data.items[0].missingCapabilities).toEqual([]);
|
|
expect(data.items[0].capabilityReviewDigest).toBeTruthy();
|
|
expect(data.canManage).toBe(true);
|
|
expect(data.canAudit).toBe(true);
|
|
expect(verifyPermissionMock).toHaveBeenCalledWith(expect.any(Request), "app.plugins.use", "t-1");
|
|
expect(getUserPermissionMock).toHaveBeenCalledWith({ userId: "u-1", permissionName: "app.plugins.manage", tenantId: "t-1" });
|
|
expect(getUserPermissionMock).toHaveBeenCalledWith({ userId: "u-1", permissionName: "app.settings.auditTrails.view", tenantId: "t-1" });
|
|
expect(listGrantsForPluginsMock).toHaveBeenCalledWith({ tenantId: "t-1", pluginIds: ["com.acme.hi"] });
|
|
expect(listGrantsMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("formats known plugin capability names through i18n labels", async () => {
|
|
const { formatPluginCapabilityLabel } = await import("./plugins.js");
|
|
const t = vi.fn((key: string, options?: Record<string, unknown>) =>
|
|
key === "settings.plugins.capabilityNames.http_outbound"
|
|
? "Outbound HTTP"
|
|
: key === "settings.plugins.consent.hookLabel"
|
|
? `Hook: ${String(options?.hook)}`
|
|
: key === "settings.plugins.capabilityNames.unknown"
|
|
? `Unknown (${String(options?.capability)})`
|
|
: String(options?.defaultValue ?? key)
|
|
);
|
|
|
|
expect(formatPluginCapabilityLabel("http.outbound", t)).toBe("Outbound HTTP");
|
|
expect(formatPluginCapabilityLabel("hook:workflow.executed", t)).toBe("Hook: workflow.executed");
|
|
expect(formatPluginCapabilityLabel("custom.capability", t)).toBe("Unknown (custom.capability)");
|
|
});
|
|
|
|
test("formats every registry plugin capability through en and de locale labels without unknown fallback", async () => {
|
|
const { formatPluginCapabilityLabel } = await import("./plugins.js");
|
|
|
|
for (const locale of ["en", "de"] as const) {
|
|
const labels = readPluginCapabilityLabels(locale);
|
|
const expectedKeys = PLUGIN_CAPABILITY_NAMES.map((capability) => capability.replaceAll(".", "_"));
|
|
const { calls, t } = createCapabilityLabelTranslator(labels);
|
|
|
|
expect(Object.keys(labels), `${locale} plugin capability labels`).toEqual(expect.arrayContaining([...expectedKeys, "unknown"]));
|
|
|
|
for (const capability of PLUGIN_CAPABILITY_NAMES) {
|
|
const labelKey = capability.replaceAll(".", "_");
|
|
const unknownLabel = interpolateTranslation(labels.unknown, { capability });
|
|
const formatted = formatPluginCapabilityLabel(capability, t);
|
|
|
|
expect(formatted, `${locale}:${capability}`).toBe(labels[labelKey]);
|
|
expect(formatted, `${locale}:${capability}`).not.toBe(unknownLabel);
|
|
}
|
|
|
|
expect(calls, `${locale} known capability fallback`).not.toContain("settings.plugins.capabilityNames.unknown");
|
|
}
|
|
});
|
|
|
|
test("loader stops before management lookup and plugin listing when use permission is denied", async () => {
|
|
const denial = new Response("Forbidden", { status: 403 });
|
|
verifyPermissionMock.mockRejectedValueOnce(denial);
|
|
const { loader } = await import("./plugins.js");
|
|
|
|
await expect(
|
|
loader({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins"),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
})
|
|
).rejects.toBe(denial);
|
|
|
|
expect(getUserPermissionMock).not.toHaveBeenCalled();
|
|
expect(listMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action enables plugins via form data", async () => {
|
|
enableMock.mockResolvedValue({ id: "pl-1", enabled: true });
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "enable");
|
|
form.set("id", "pl-1");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
expect(response.status).toBe(200);
|
|
expect(enableMock).toHaveBeenCalledOnce();
|
|
expect(csrfMock).toHaveBeenCalledWith(expect.any(Request), { formData: expect.any(FormData) });
|
|
expect(verifyPermissionMock).toHaveBeenCalledWith(expect.any(Request), "app.plugins.manage", "t-1");
|
|
});
|
|
|
|
test("action grants missing capabilities and enables plugins", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } }],
|
|
id: "com.acme.outbound",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
grantMock.mockResolvedValue([]);
|
|
enableMock.mockResolvedValue({ id: "pl-1", enabled: true });
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
const capabilityReviewDigest = await getCapabilityReviewDigest(plugin, ["http.outbound"]);
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", capabilityReviewDigest);
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(grantMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginId: "com.acme.outbound",
|
|
grants: [
|
|
{
|
|
capabilityName: "http.outbound",
|
|
scopeJson: { allowedHosts: ["api.example.com"] },
|
|
},
|
|
],
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: null },
|
|
capabilityReviewDigest,
|
|
});
|
|
expect(enableMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginRowId: "pl-1",
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: null },
|
|
});
|
|
});
|
|
|
|
test("action rejects grant submissions with undeclared consent names", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } }],
|
|
id: "com.acme.outbound",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
const capabilityReviewDigest = await getCapabilityReviewDigest(plugin, ["http.outbound"]);
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.append("capabilities[]", "secret.read");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", capabilityReviewDigest);
|
|
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(409);
|
|
await expect(response.json()).resolves.toEqual({
|
|
ok: false,
|
|
message: "Plugin capabilities changed. Review the latest manifest before approving.",
|
|
});
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("loader exposes manifest hooks as explicit consent items", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.workflow-hook",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [],
|
|
hooks: [{ name: "workflow.executed", export: "onWorkflowExecuted" }],
|
|
id: "com.acme.workflow-hook",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
listGrantsForPluginsMock.mockResolvedValue([]);
|
|
const { loader } = await import("./plugins.js");
|
|
|
|
const response = await loader({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins"),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
const data = await (response as Response).json();
|
|
|
|
expect(data.items[0].capabilities).toEqual([
|
|
{
|
|
name: "hook:workflow.executed",
|
|
scope: null,
|
|
granted: false,
|
|
grantedAt: null,
|
|
grantedScopeJson: null,
|
|
metadata: { risk: "medium", productionAvailable: true },
|
|
},
|
|
]);
|
|
expect(data.items[0].missingCapabilities).toEqual(["hook:workflow.executed"]);
|
|
expect(data.items[0].capabilityReviewDigest).toBe(await getCapabilityReviewDigest(plugin, ["hook:workflow.executed"]));
|
|
});
|
|
|
|
test("action grants hook consent before enabling hook-only plugins", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.workflow-hook",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [],
|
|
hooks: [{ name: "workflow.executed", export: "onWorkflowExecuted" }],
|
|
id: "com.acme.workflow-hook",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
grantMock.mockResolvedValue([]);
|
|
enableMock.mockResolvedValue({ id: "pl-1", enabled: true });
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
const capabilityReviewDigest = await getCapabilityReviewDigest(plugin, ["hook:workflow.executed"]);
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "hook:workflow.executed");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", capabilityReviewDigest);
|
|
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(grantMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginId: "com.acme.workflow-hook",
|
|
grants: [{ capabilityName: "hook:workflow.executed", scopeJson: null }],
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: null },
|
|
capabilityReviewDigest,
|
|
});
|
|
expect(enableMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginRowId: "pl-1",
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: null },
|
|
});
|
|
});
|
|
|
|
test("action surfaces unavailable host capability lifecycle failures during grant review", async () => {
|
|
const { PluginLifecycleError } = await import("~/modules/plugins/services/.server/PluginLifecycleService");
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } }],
|
|
id: "com.acme.outbound",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
grantMock.mockRejectedValue(new PluginLifecycleError("capability_unavailable", "http.outbound unavailable"));
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
const capabilityReviewDigest = await getCapabilityReviewDigest(plugin, ["http.outbound"]);
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", capabilityReviewDigest);
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
await expect(response.json()).resolves.toEqual({
|
|
ok: false,
|
|
message: "One or more requested plugin capabilities are unavailable in this environment.",
|
|
});
|
|
expect(response.status).toBe(403);
|
|
expect(grantMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginId: "com.acme.outbound",
|
|
grants: [
|
|
{
|
|
capabilityName: "http.outbound",
|
|
scopeJson: { allowedHosts: ["api.example.com"] },
|
|
},
|
|
],
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: null },
|
|
capabilityReviewDigest,
|
|
});
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects capability grants that have not been explicitly reviewed", async () => {
|
|
listMock.mockResolvedValue([
|
|
{
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: { capabilities: [{ name: "http.outbound" }], id: "com.acme.outbound" },
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
},
|
|
]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grant");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilityReviewDigest", "digest");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects stale capability review digests", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.1",
|
|
enabled: false,
|
|
manifest: { capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } }], id: "com.acme.outbound" },
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grant");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", "stale-digest");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(409);
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects partial capability review digests for broader grant submissions", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.1",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [
|
|
{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } },
|
|
{ name: "log", scope: null },
|
|
],
|
|
id: "com.acme.outbound",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.append("capabilities[]", "log");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set(
|
|
"capabilityReviewDigest",
|
|
await getCapabilityReviewDigest(
|
|
{
|
|
pluginId: plugin.pluginId,
|
|
version: plugin.version,
|
|
manifest: { capabilities: plugin.manifest.capabilities },
|
|
},
|
|
["http.outbound"]
|
|
)
|
|
);
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(409);
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects grant-and-enable submissions that review only a subset of missing capabilities", async () => {
|
|
const plugin = {
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.1",
|
|
enabled: false,
|
|
manifest: {
|
|
capabilities: [
|
|
{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } },
|
|
{ name: "log", scope: null },
|
|
],
|
|
id: "com.acme.outbound",
|
|
},
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
};
|
|
listMock.mockResolvedValue([plugin]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set(
|
|
"capabilityReviewDigest",
|
|
await getCapabilityReviewDigest(
|
|
{
|
|
pluginId: plugin.pluginId,
|
|
version: plugin.version,
|
|
manifest: { capabilities: plugin.manifest.capabilities },
|
|
},
|
|
["http.outbound"]
|
|
)
|
|
);
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(409);
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects empty capability grant submissions", async () => {
|
|
listMock.mockResolvedValue([
|
|
{
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: false,
|
|
manifest: { capabilities: [{ name: "http.outbound" }], id: "com.acme.outbound" },
|
|
installedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
},
|
|
]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grant");
|
|
form.set("id", "pl-1");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set(
|
|
"capabilityReviewDigest",
|
|
await getCapabilityReviewDigest(
|
|
{
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
manifest: { capabilities: [{ name: "http.outbound" }] },
|
|
},
|
|
["http.outbound"]
|
|
)
|
|
);
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects malformed capability grant form payloads before plugin lookup", async () => {
|
|
const { action } = await import("./plugins.js");
|
|
const cases = [Array.from({ length: 65 }, (_, index) => `capability.${index}`), [" http.outbound"], ["x".repeat(129)], ["http.outbound", "http.outbound"]];
|
|
|
|
for (const capabilities of cases) {
|
|
listMock.mockClear();
|
|
grantMock.mockClear();
|
|
enableMock.mockClear();
|
|
|
|
const form = new FormData();
|
|
form.set("op", "grant");
|
|
form.set("id", "pl-1");
|
|
for (const capability of capabilities) {
|
|
form.append("capabilities[]", capability);
|
|
}
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", "digest");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(listMock).not.toHaveBeenCalled();
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
test("action rejects oversized plugin settings form payloads before CSRF", async () => {
|
|
const { action } = await import("./plugins.js");
|
|
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: "op=enable&id=pl-1",
|
|
headers: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
"content-length": String(129 * 1024),
|
|
},
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(csrfMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
expect(disableMock).not.toHaveBeenCalled();
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(revokeMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action stops before CSRF and lifecycle mutation when manage permission is denied", async () => {
|
|
const denial = new Response("Forbidden", { status: 403 });
|
|
verifyPermissionMock.mockRejectedValueOnce(denial);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "enable");
|
|
form.set("id", "pl-1");
|
|
|
|
await expect(
|
|
action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
})
|
|
).rejects.toBe(denial);
|
|
|
|
expect(csrfMock).not.toHaveBeenCalled();
|
|
expect(getUserInfoMock).not.toHaveBeenCalled();
|
|
expect(getUserMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
expect(disableMock).not.toHaveBeenCalled();
|
|
expect(revokeMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action does not let app.plugins.use-only users grant capabilities and enable plugins", async () => {
|
|
const denial = new Response("Forbidden", { status: 403 });
|
|
verifyPermissionMock.mockRejectedValueOnce(denial);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "grantAndEnable");
|
|
form.set("id", "pl-1");
|
|
form.append("capabilities[]", "http.outbound");
|
|
form.set("capabilitiesReviewed", "yes");
|
|
form.set("capabilityReviewDigest", "digest");
|
|
|
|
await expect(
|
|
action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
})
|
|
).rejects.toBe(denial);
|
|
|
|
expect(verifyPermissionMock).toHaveBeenCalledWith(expect.any(Request), "app.plugins.manage", "t-1");
|
|
expect(csrfMock).not.toHaveBeenCalled();
|
|
expect(listMock).not.toHaveBeenCalled();
|
|
expect(grantMock).not.toHaveBeenCalled();
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("loader allows read-only plugin users without management controls", async () => {
|
|
getUserPermissionMock.mockResolvedValue({ userPermission: undefined });
|
|
listMock.mockResolvedValue([]);
|
|
const { loader } = await import("./plugins.js");
|
|
const response = await loader({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins"),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
const data = await (response as Response).json();
|
|
expect(data.canManage).toBe(false);
|
|
expect(data.canAudit).toBe(false);
|
|
});
|
|
|
|
test("action disables plugins via form data", async () => {
|
|
disableMock.mockResolvedValue({ id: "pl-1", enabled: false });
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "disable");
|
|
form.set("id", "pl-1");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
"x-forwarded-for": "203.0.113.10, 198.51.100.2",
|
|
},
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(disableMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginRowId: "pl-1",
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: "203.0.113.10" },
|
|
});
|
|
});
|
|
|
|
test("action revokes plugin capabilities via form data", async () => {
|
|
listMock.mockResolvedValue([
|
|
{
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: true,
|
|
manifest: { id: "com.acme.outbound" },
|
|
},
|
|
]);
|
|
revokeMock.mockResolvedValue({ revokedCapabilities: ["http.outbound"], autoDisabled: true });
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "revoke");
|
|
form.set("id", "pl-1");
|
|
form.set("pluginId", "com.acme.outbound");
|
|
form.append("capabilityNames[]", "http.outbound");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
"x-forwarded-for": "203.0.113.10, 198.51.100.2",
|
|
},
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(revokeMock).toHaveBeenCalledWith({
|
|
tenantId: "t-1",
|
|
pluginId: "com.acme.outbound",
|
|
capabilityNames: ["http.outbound"],
|
|
actor: { userId: "u-1", email: "admin@example.com", ip: "203.0.113.10" },
|
|
});
|
|
});
|
|
|
|
test("action rejects capability revoke when submitted plugin id does not match the tenant plugin row", async () => {
|
|
listMock.mockResolvedValue([
|
|
{
|
|
id: "pl-1",
|
|
pluginId: "com.acme.outbound",
|
|
version: "1.0.0",
|
|
enabled: true,
|
|
manifest: { id: "com.acme.outbound" },
|
|
},
|
|
]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "revoke");
|
|
form.set("id", "pl-1");
|
|
form.set("pluginId", "com.acme.other");
|
|
form.append("capabilityNames[]", "http.outbound");
|
|
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(409);
|
|
expect(revokeMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects capability revoke when the tenant plugin row no longer exists", async () => {
|
|
listMock.mockResolvedValue([]);
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "revoke");
|
|
form.set("id", "pl-missing");
|
|
form.set("pluginId", "com.acme.outbound");
|
|
form.append("capabilityNames[]", "http.outbound");
|
|
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(revokeMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects malformed capability revoke form payloads before lifecycle mutation", async () => {
|
|
const { action } = await import("./plugins.js");
|
|
const cases = [Array.from({ length: 65 }, (_, index) => `capability.${index}`), [" http.outbound"], ["x".repeat(129)], ["http.outbound", "http.outbound"]];
|
|
|
|
for (const capabilityNames of cases) {
|
|
revokeMock.mockClear();
|
|
|
|
const form = new FormData();
|
|
form.set("op", "revoke");
|
|
form.set("id", "pl-1");
|
|
form.set("pluginId", "com.acme.outbound");
|
|
for (const capabilityName of capabilityNames) {
|
|
form.append("capabilityNames[]", capabilityName);
|
|
}
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded", "accept-language": "en" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(revokeMock).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
test.each([
|
|
{
|
|
code: "capability_consent_missing",
|
|
status: 409,
|
|
expected: ["Required plugin capability consent is missing.", "Die erforderliche Freigabe für Plugin-Berechtigungen fehlt."],
|
|
},
|
|
{
|
|
code: "capability_unavailable",
|
|
status: 403,
|
|
expected: [
|
|
"One or more requested plugin capabilities are unavailable in this environment.",
|
|
"Mindestens eine angeforderte Plugin-Berechtigung ist in dieser Umgebung nicht verfügbar.",
|
|
],
|
|
},
|
|
{
|
|
code: "not_found",
|
|
status: 404,
|
|
expected: ["Plugin not found.", "Plugin nicht gefunden."],
|
|
},
|
|
{
|
|
code: "production_guard_violation",
|
|
status: 403,
|
|
expected: [
|
|
"Plugin runtime production guard blocked this lifecycle operation.",
|
|
"Der Produktionsschutz der Plugin-Laufzeit hat diesen Vorgang blockiert.",
|
|
],
|
|
},
|
|
{
|
|
code: "tenant_install_limit_exceeded",
|
|
status: 429,
|
|
expected: ["Plugin install limit for this account has been reached.", "Das Plugin-Installationslimit für dieses Konto ist erreicht."],
|
|
},
|
|
])("action localizes plugin lifecycle error $code and maps its HTTP status", async ({ code, status, expected }) => {
|
|
const { PluginLifecycleError } = await import("~/modules/plugins/services/.server/PluginLifecycleService");
|
|
enableMock.mockRejectedValue(new PluginLifecycleError(code, "lifecycle failed"));
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "enable");
|
|
form.set("id", "pl-1");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
const data = await response.json();
|
|
expect(response.status).toBe(status);
|
|
expect(expected).toContain(data.message);
|
|
});
|
|
|
|
test("action rejects unknown operations", async () => {
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "rotate");
|
|
form.set("id", "pl-1");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(enableMock).not.toHaveBeenCalled();
|
|
expect(disableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("action rejects missing id", async () => {
|
|
const { action } = await import("./plugins.js");
|
|
const form = new FormData();
|
|
form.set("op", "enable");
|
|
const response = await action({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins", {
|
|
method: "POST",
|
|
body: new URLSearchParams(Array.from(form.entries()) as [string, string][]),
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { tenant: "t-1" },
|
|
context: {},
|
|
});
|
|
expect(response.status).toBe(400);
|
|
});
|
|
});
|