93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import {
|
|
getMissingCapabilityGrantNames,
|
|
isCapabilityGrantScopeSatisfied,
|
|
} from "./capability-grants.js";
|
|
import type { PluginManifest } from "./manifest.js";
|
|
|
|
function manifest(capabilities: PluginManifest["capabilities"]): PluginManifest {
|
|
return {
|
|
schemaVersion: 1,
|
|
id: "de.hivegrid.test",
|
|
version: "1.0.0",
|
|
name: "Test",
|
|
entry: "plugin.wasm",
|
|
wasmSha256: "0".repeat(64),
|
|
signature: "0".repeat(128),
|
|
signedBy: "0".repeat(64),
|
|
allowedHosts: [],
|
|
capabilities,
|
|
hooks: [{ name: "run", export: "run" }],
|
|
};
|
|
}
|
|
|
|
describe("capability grants", () => {
|
|
test("accepts exact or narrower declared scope", () => {
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: { allowedHosts: ["api.example.com", "hooks.example.com"] },
|
|
grantedScopeJson: { allowedHosts: ["api.example.com"] },
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: { keyPattern: "payment_*", tablePrefix: "plugin_" },
|
|
grantedScopeJson: { keyPattern: "payment_*", tablePrefix: "plugin_" },
|
|
})
|
|
).toBe(true);
|
|
});
|
|
|
|
test("rejects broadened or malformed scopes", () => {
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: { allowedHosts: ["api.example.com"] },
|
|
grantedScopeJson: { allowedHosts: ["api.example.com", "internal.example"] },
|
|
})
|
|
).toBe(false);
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: null,
|
|
grantedScopeJson: { tablePrefix: "tenant_" },
|
|
})
|
|
).toBe(false);
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: { allowedHosts: ["api.example.com"] },
|
|
grantedScopeJson: "all",
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
test("rejects empty host grants for scoped outbound capabilities", () => {
|
|
expect(
|
|
isCapabilityGrantScopeSatisfied({
|
|
declaredScope: { allowedHosts: ["api.example.com"] },
|
|
grantedScopeJson: { allowedHosts: [] },
|
|
})
|
|
).toBe(false);
|
|
|
|
const missing = getMissingCapabilityGrantNames({
|
|
manifest: manifest([{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } }]),
|
|
grants: [{ capabilityName: "http.outbound", scopeJson: { allowedHosts: [] } }],
|
|
});
|
|
|
|
expect(missing).toEqual(["http.outbound"]);
|
|
});
|
|
|
|
test("reports missing grants and stale scopes from a manifest", () => {
|
|
const missing = getMissingCapabilityGrantNames({
|
|
manifest: manifest([
|
|
{ name: "log" },
|
|
{ name: "http.outbound", scope: { allowedHosts: ["api.example.com"] } },
|
|
{ name: "audit" },
|
|
]),
|
|
grants: [
|
|
{ capabilityName: "log", scopeJson: null },
|
|
{ capabilityName: "http.outbound", scopeJson: { allowedHosts: ["api.example.com", "internal.example"] } },
|
|
],
|
|
});
|
|
|
|
expect(missing).toEqual(["http.outbound", "audit"]);
|
|
});
|
|
});
|