import { describe, expect, test } from "vitest"; import { isCapabilityCallWithinScope } from "./capability-scope.js"; describe("capability call scope", () => { test("allows exact HTTPS outbound hosts only", () => { const scope = { allowedHosts: ["api.example.com"] }; expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://api.example.com/v1" }], scope })).toBe(true); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://evil.example.com/v1" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "http://api.example.com/v1" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://api.example.com.evil.test/v1" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://api.example.com:8443/v1" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "not a url" }], scope })).toBe(false); }); test("requires structured outbound request arguments and a scoped host grant", () => { expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: ["https://api.example.com/v1"], scope: { allowedHosts: ["api.example.com"] } })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://api.example.com/v1" }], scope: null })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "http.outbound", callArgs: [{ url: "https://api.example.com/v1" }, { retry: true }], scope: { allowedHosts: ["api.example.com"] } })).toBe(false); }); test("matches secret keys with full-string glob patterns", () => { const scope = { keyPattern: "stripe_*" }; expect(isCapabilityCallWithinScope({ capability: "secret.read", callArgs: [{ key: "stripe_live_key" }], scope })).toBe(true); expect(isCapabilityCallWithinScope({ capability: "secret.read", callArgs: [{ key: "tenant/stripe_live_key" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "secret.read", callArgs: [{ key: "github_token" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "secret.read", callArgs: [{ key: "stripe_live_key" }], scope: null })).toBe(false); }); test("limits database calls to tenant-safe table prefixes and rejects raw query shapes", () => { const scope = { tablePrefix: "plugin_" }; expect(isCapabilityCallWithinScope({ capability: "db.read", callArgs: [{ table: "plugin_orders" }], scope })).toBe(true); expect(isCapabilityCallWithinScope({ capability: "db.write", callArgs: [{ table: "plugin_orders" }], scope })).toBe(true); expect(isCapabilityCallWithinScope({ capability: "db.read", callArgs: [{ table: "tenant_users" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "db.write", callArgs: [{ table: "plugin_orders", sql: "delete from tenant_users" }], scope })).toBe(false); expect(isCapabilityCallWithinScope({ capability: "db.read", callArgs: [{ table: "plugin_orders" }], scope: null })).toBe(false); }); test("does not scope low-risk host bindings that carry their own structured contracts", () => { expect(isCapabilityCallWithinScope({ capability: "log", callArgs: ["hello"], scope: null })).toBe(true); expect(isCapabilityCallWithinScope({ capability: "audit", callArgs: [{ action: "plugin.test" }], scope: null })).toBe(true); }); });