@kya-os/mcp-i-runtime 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/auth/handshake.d.ts +23 -43
- package/dist/auth/handshake.d.ts.map +1 -1
- package/dist/auth/handshake.js +51 -82
- package/dist/auth/handshake.js.map +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/resume-token.d.ts +52 -0
- package/dist/auth/resume-token.d.ts.map +1 -0
- package/dist/auth/resume-token.js +89 -0
- package/dist/auth/resume-token.js.map +1 -0
- package/dist/auth/types.d.ts +15 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/dist/proof/companion.d.ts +25 -0
- package/dist/proof/companion.d.ts.map +1 -0
- package/dist/proof/companion.js +14 -0
- package/dist/proof/companion.js.map +1 -0
- package/dist/proof/generator.d.ts +22 -9
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +20 -27
- package/dist/proof/generator.js.map +1 -1
- package/dist/proof/index.d.ts +2 -1
- package/dist/proof/index.d.ts.map +1 -1
- package/dist/proof/index.js +3 -1
- package/dist/proof/index.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +78 -18
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/base.d.ts +6 -1
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +65 -17
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/dif-core.d.ts +8 -0
- package/dist/runtime/dif-core.d.ts.map +1 -1
- package/dist/runtime/dif-core.js +10 -0
- package/dist/runtime/dif-core.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +12 -5
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/claims-first-guard.d.ts +33 -0
- package/dist/services/claims-first-guard.d.ts.map +1 -0
- package/dist/services/claims-first-guard.js +77 -0
- package/dist/services/claims-first-guard.js.map +1 -0
- package/dist/services/holder-keys.d.ts +95 -0
- package/dist/services/holder-keys.d.ts.map +1 -0
- package/dist/services/holder-keys.js +281 -0
- package/dist/services/holder-keys.js.map +1 -0
- package/dist/services/holder-read-slots.d.ts +42 -0
- package/dist/services/holder-read-slots.d.ts.map +1 -0
- package/dist/services/holder-read-slots.js +82 -0
- package/dist/services/holder-read-slots.js.map +1 -0
- package/dist/services/proof-verifier.d.ts +13 -35
- package/dist/services/proof-verifier.d.ts.map +1 -1
- package/dist/services/proof-verifier.js +16 -74
- package/dist/services/proof-verifier.js.map +1 -1
- package/dist/services/request-holder.d.ts +78 -0
- package/dist/services/request-holder.d.ts.map +1 -0
- package/dist/services/request-holder.js +81 -0
- package/dist/services/request-holder.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -1
- package/dist/services/tool-context-builder.js +16 -4
- package/dist/services/tool-context-builder.js.map +1 -1
- package/dist/services/tool-protection.service.d.ts +7 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +34 -15
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/tool-protection.d.ts +94 -0
- package/dist/types/tool-protection.d.ts.map +1 -1
- package/dist/types/tool-protection.js +113 -1
- package/dist/types/tool-protection.js.map +1 -1
- package/eslint.config.js +20 -0
- package/package.json +8 -2
- package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
- package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
- package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
- package/src/__tests__/runtime/audit.test.ts +363 -53
- package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
- package/src/__tests__/runtime/provider-selection.test.ts +4 -6
- package/src/__tests__/runtime/route-interception.test.ts +215 -214
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
- package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
- package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
- package/src/auth/__tests__/verify-or-hints-presented.test.ts +155 -0
- package/src/auth/handshake.ts +80 -125
- package/src/auth/index.ts +1 -0
- package/src/auth/resume-token.ts +134 -0
- package/src/auth/types.ts +20 -2
- package/src/index.ts +40 -1
- package/src/proof/__tests__/proof-generator.test.ts +80 -13
- package/src/proof/companion.ts +35 -0
- package/src/proof/generator.ts +51 -33
- package/src/proof/index.ts +3 -0
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +170 -65
- package/src/runtime/base.ts +95 -31
- package/src/runtime/dif-core.ts +16 -0
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/claims-first-guard.test.ts +93 -0
- package/src/services/__tests__/holder-fixtures.ts +135 -0
- package/src/services/__tests__/holder-keys.test.ts +257 -0
- package/src/services/__tests__/holder-read-slots.test.ts +117 -0
- package/src/services/__tests__/holder-reads.test.ts +284 -0
- package/src/services/__tests__/proof-verifier.test.ts +286 -195
- package/src/services/__tests__/request-holder.test.ts +210 -0
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
- package/src/services/__tests__/tool-context-builder.test.ts +4 -7
- package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
- package/src/services/access-control.service.ts +11 -5
- package/src/services/claims-first-guard.ts +89 -0
- package/src/services/holder-keys.ts +379 -0
- package/src/services/holder-read-slots.ts +85 -0
- package/src/services/proof-verifier.ts +18 -79
- package/src/services/request-holder.ts +146 -0
- package/src/services/tool-context-builder.ts +19 -4
- package/src/services/tool-protection.service.ts +62 -19
- package/src/types/tool-protection.ts +161 -0
|
@@ -8,7 +8,12 @@ import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
|
8
8
|
import { MCPIRuntimeBase } from "../../runtime/base";
|
|
9
9
|
import { ProviderRuntimeConfig } from "../../config";
|
|
10
10
|
import { createToolProtectionRuntime } from "../utils/tool-protection-runtime";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
DelegationRequiredError,
|
|
13
|
+
isToolProtectionUnenforceableError,
|
|
14
|
+
type ToolProtectionUnenforceableError,
|
|
15
|
+
unavailableToolProtection,
|
|
16
|
+
} from "../../types/tool-protection";
|
|
12
17
|
import type { VerifyDelegationAPIResponse } from "@kya-os/contracts/checkpoint-api";
|
|
13
18
|
|
|
14
19
|
describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
@@ -681,7 +686,7 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
681
686
|
} catch (error: any) {
|
|
682
687
|
expect(error).toBeInstanceOf(DelegationRequiredError);
|
|
683
688
|
expect(error.consentUrl).toBeDefined();
|
|
684
|
-
expect(error.consentUrl).toContain("
|
|
689
|
+
expect(error.consentUrl).toContain("https://auth.example.com/consent?");
|
|
685
690
|
expect(error.consentUrl).toContain("tool=protectedTool");
|
|
686
691
|
expect(error.consentUrl).toContain("scopes=files%3Awrite");
|
|
687
692
|
}
|
|
@@ -900,20 +905,30 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
900
905
|
nonce: "test-nonce",
|
|
901
906
|
};
|
|
902
907
|
|
|
903
|
-
it("
|
|
908
|
+
it("refuses a protected tool whose protection names no scope, before any consent", async () => {
|
|
904
909
|
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
905
910
|
requiresDelegation: true,
|
|
906
911
|
requiredScopes: [],
|
|
907
912
|
});
|
|
908
913
|
|
|
909
|
-
await
|
|
910
|
-
|
|
914
|
+
const error = await runtime
|
|
915
|
+
.processToolCall(
|
|
911
916
|
"protectedTool",
|
|
912
917
|
{ arg: "value" },
|
|
913
918
|
mockHandler,
|
|
914
919
|
session
|
|
915
920
|
)
|
|
916
|
-
|
|
921
|
+
.catch((e: unknown) => e);
|
|
922
|
+
|
|
923
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
924
|
+
expect(
|
|
925
|
+
(error as ToolProtectionUnenforceableError).toJSON()
|
|
926
|
+
).toMatchObject({
|
|
927
|
+
error: "tool_protection_unenforceable",
|
|
928
|
+
toolName: "protectedTool",
|
|
929
|
+
missing: ["requiredScopes"],
|
|
930
|
+
});
|
|
931
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
917
932
|
});
|
|
918
933
|
|
|
919
934
|
it("should handle multiple required scopes", async () => {
|
|
@@ -960,6 +975,74 @@ describe("MCPIRuntimeBase - Tool Protection Enforcement", () => {
|
|
|
960
975
|
}
|
|
961
976
|
});
|
|
962
977
|
|
|
978
|
+
it.each([
|
|
979
|
+
["an empty list", []],
|
|
980
|
+
["only empty strings", ["", ""]],
|
|
981
|
+
["no list at all", undefined],
|
|
982
|
+
])(
|
|
983
|
+
"refuses a presented credential for a protection naming %s, without asking the verifier",
|
|
984
|
+
async (_label, requiredScopes) => {
|
|
985
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
986
|
+
requiresDelegation: true,
|
|
987
|
+
requiredScopes,
|
|
988
|
+
});
|
|
989
|
+
mockAccessControlService.verifyDelegation.mockResolvedValue({
|
|
990
|
+
success: true,
|
|
991
|
+
data: {
|
|
992
|
+
valid: true,
|
|
993
|
+
credential: {
|
|
994
|
+
agent_did: "did:key:zmock123",
|
|
995
|
+
scopes: ["anything"],
|
|
996
|
+
issued_at: Date.now(),
|
|
997
|
+
created_at: Date.now(),
|
|
998
|
+
},
|
|
999
|
+
},
|
|
1000
|
+
} as VerifyDelegationAPIResponse);
|
|
1001
|
+
|
|
1002
|
+
const error = await runtime
|
|
1003
|
+
.processToolCall("protectedTool", { arg: "value" }, mockHandler, {
|
|
1004
|
+
...session,
|
|
1005
|
+
delegationToken: "valid-delegation-token",
|
|
1006
|
+
})
|
|
1007
|
+
.catch((e: unknown) => e);
|
|
1008
|
+
|
|
1009
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
1010
|
+
expect((error as ToolProtectionUnenforceableError).missing).toEqual([
|
|
1011
|
+
"requiredScopes",
|
|
1012
|
+
]);
|
|
1013
|
+
expect(
|
|
1014
|
+
mockAccessControlService.verifyDelegation
|
|
1015
|
+
).not.toHaveBeenCalled();
|
|
1016
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
|
|
1020
|
+
it("refuses every call while the service stands in for unavailable protection", async () => {
|
|
1021
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue(
|
|
1022
|
+
unavailableToolProtection()
|
|
1023
|
+
);
|
|
1024
|
+
|
|
1025
|
+
for (const delegationToken of [undefined, "valid-delegation-token"]) {
|
|
1026
|
+
const error = await runtime
|
|
1027
|
+
.processToolCall("anyTool", { arg: "value" }, mockHandler, {
|
|
1028
|
+
...session,
|
|
1029
|
+
...(delegationToken ? { delegationToken } : {}),
|
|
1030
|
+
})
|
|
1031
|
+
.catch((e: unknown) => e);
|
|
1032
|
+
|
|
1033
|
+
expect(isToolProtectionUnenforceableError(error)).toBe(true);
|
|
1034
|
+
expect(
|
|
1035
|
+
(error as ToolProtectionUnenforceableError).toJSON()
|
|
1036
|
+
).toMatchObject({
|
|
1037
|
+
error: "tool_protection_unenforceable",
|
|
1038
|
+
toolName: "anyTool",
|
|
1039
|
+
missing: ["toolProtectionConfig"],
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
expect(mockAccessControlService.verifyDelegation).not.toHaveBeenCalled();
|
|
1043
|
+
expect(mockHandler).not.toHaveBeenCalled();
|
|
1044
|
+
});
|
|
1045
|
+
|
|
963
1046
|
it("should handle handler errors independently of protection", async () => {
|
|
964
1047
|
mockToolProtectionService.checkToolProtection.mockResolvedValue(null);
|
|
965
1048
|
const errorHandler = vi
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
import { ToolProtectionService } from "../../services/tool-protection.service";
|
|
3
|
+
import { InMemoryToolProtectionCache } from "../../cache/tool-protection-cache";
|
|
4
|
+
|
|
5
|
+
const agentDid = "did:key:zTestAgent123";
|
|
6
|
+
const amount = { argument: "amount", currency: "USD" };
|
|
7
|
+
const remoteAmount = { argument: "order.total", currency: { argument: "ccy" } };
|
|
8
|
+
|
|
9
|
+
const respond = (mockFetch: ReturnType<typeof vi.fn>, tools: unknown) => {
|
|
10
|
+
const body = {
|
|
11
|
+
success: true,
|
|
12
|
+
data: { config: { toolProtection: { source: "agentshield", tools } } },
|
|
13
|
+
};
|
|
14
|
+
mockFetch.mockResolvedValue({
|
|
15
|
+
ok: true,
|
|
16
|
+
json: async () => body,
|
|
17
|
+
text: async () => JSON.stringify(body),
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
describe("ToolProtectionService amount config", () => {
|
|
22
|
+
let mockFetch: ReturnType<typeof vi.fn>;
|
|
23
|
+
let service: ToolProtectionService;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
mockFetch = vi.fn();
|
|
27
|
+
(globalThis as Record<string, unknown>).fetch = mockFetch;
|
|
28
|
+
service = new ToolProtectionService(
|
|
29
|
+
{
|
|
30
|
+
projectId: "p",
|
|
31
|
+
apiUrl: "https://kya.vouched.id",
|
|
32
|
+
apiKey: "k",
|
|
33
|
+
localConfig: {
|
|
34
|
+
toolProtections: {
|
|
35
|
+
pay: {
|
|
36
|
+
requiresDelegation: true,
|
|
37
|
+
requiredScopes: ["payments:send"],
|
|
38
|
+
amount,
|
|
39
|
+
},
|
|
40
|
+
plain: { requiresDelegation: true, requiredScopes: ["a:b"] },
|
|
41
|
+
both: {
|
|
42
|
+
requiresDelegation: true,
|
|
43
|
+
requiredScopes: ["payments:send"],
|
|
44
|
+
amount,
|
|
45
|
+
},
|
|
46
|
+
keep: {
|
|
47
|
+
requiresDelegation: true,
|
|
48
|
+
requiredScopes: ["payments:send"],
|
|
49
|
+
amount,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
new InMemoryToolProtectionCache()
|
|
55
|
+
);
|
|
56
|
+
respond(mockFetch, {
|
|
57
|
+
remote: {
|
|
58
|
+
requiresDelegation: true,
|
|
59
|
+
requiredScopes: ["payments:refund"],
|
|
60
|
+
amount: remoteAmount,
|
|
61
|
+
},
|
|
62
|
+
remotePlain: { requiresDelegation: true, requiredScopes: [] },
|
|
63
|
+
both: {
|
|
64
|
+
requiresDelegation: true,
|
|
65
|
+
requiredScopes: ["payments:send"],
|
|
66
|
+
amount: remoteAmount,
|
|
67
|
+
},
|
|
68
|
+
keep: { requiresDelegation: true, requiredScopes: ["payments:send"] },
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("keeps a local amount config and reads a remote one", async () => {
|
|
73
|
+
const { toolProtections } = await service.getToolProtectionConfig(agentDid);
|
|
74
|
+
expect(toolProtections.pay.amount).toEqual(amount);
|
|
75
|
+
expect(toolProtections.remote.amount).toEqual(remoteAmount);
|
|
76
|
+
expect(toolProtections.plain).not.toHaveProperty("amount");
|
|
77
|
+
expect(toolProtections.remotePlain).not.toHaveProperty("amount");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("lets a remote amount win and keeps the local one when the remote entry omits it", async () => {
|
|
81
|
+
const { toolProtections } = await service.getToolProtectionConfig(agentDid);
|
|
82
|
+
expect(toolProtections.both.amount).toEqual(remoteAmount);
|
|
83
|
+
expect(toolProtections.keep.amount).toEqual(amount);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("keeps a local amount config when the cache is cleared and refreshed", async () => {
|
|
87
|
+
const { config } = await service.clearAndRefresh(agentDid);
|
|
88
|
+
expect(config.toolProtections.pay.amount).toEqual(amount);
|
|
89
|
+
expect(config.toolProtections.remote.amount).toEqual(remoteAmount);
|
|
90
|
+
expect(config.toolProtections.plain).not.toHaveProperty("amount");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("applies the same remote-wins and local-kept rules on clear and refresh", async () => {
|
|
94
|
+
const { config } = await service.clearAndRefresh(agentDid);
|
|
95
|
+
expect(config.toolProtections.both.amount).toEqual(remoteAmount);
|
|
96
|
+
expect(config.toolProtections.keep.amount).toEqual(amount);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("hands the amount config to the caller through checkToolProtection", async () => {
|
|
100
|
+
await expect(
|
|
101
|
+
service.checkToolProtection("pay", agentDid)
|
|
102
|
+
).resolves.toMatchObject({ amount });
|
|
103
|
+
await expect(
|
|
104
|
+
service.checkToolProtection("keep", agentDid)
|
|
105
|
+
).resolves.toMatchObject({ amount });
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
12
12
|
import { ToolProtectionService } from "../../services/tool-protection.service";
|
|
13
13
|
import { InMemoryToolProtectionCache } from "../../cache/tool-protection-cache";
|
|
14
|
-
import
|
|
14
|
+
import {
|
|
15
|
+
isUnavailableToolProtection,
|
|
16
|
+
type ToolProtectionConfig,
|
|
17
|
+
} from "../../types/tool-protection";
|
|
15
18
|
import type { ToolProtection } from "@kya-os/contracts/tool-protection";
|
|
16
19
|
|
|
17
20
|
/**
|
|
@@ -265,9 +268,73 @@ describe("ToolProtectionService Fail-Safe Behaviors", () => {
|
|
|
265
268
|
|
|
266
269
|
const config = await service.getToolProtectionConfig(agentDid);
|
|
267
270
|
|
|
268
|
-
//
|
|
269
|
-
|
|
270
|
-
|
|
271
|
+
// Every tool resolves to the protection-unavailable stand-in
|
|
272
|
+
const protection = await service.checkToolProtection(
|
|
273
|
+
"any-tool",
|
|
274
|
+
agentDid
|
|
275
|
+
);
|
|
276
|
+
expect(protection).not.toBeNull();
|
|
277
|
+
expect(isUnavailableToolProtection(protection!)).toBe(true);
|
|
278
|
+
expect(Object.keys(config.toolProtections)).toEqual(["*"]);
|
|
279
|
+
expect(isUnavailableToolProtection(config.toolProtections["*"])).toBe(
|
|
280
|
+
true
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("never caches the stand-in, so the first config that arrives replaces it", async () => {
|
|
285
|
+
service = new ToolProtectionService(
|
|
286
|
+
{ projectId, apiUrl, apiKey, failSafeBehavior: "deny-all" },
|
|
287
|
+
cache
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
mockFetch.mockRejectedValue(new Error("Network error"));
|
|
291
|
+
const unavailable = await service.checkToolProtection("tool-a", agentDid);
|
|
292
|
+
expect(isUnavailableToolProtection(unavailable!)).toBe(true);
|
|
293
|
+
expect(
|
|
294
|
+
await cache.get(`config:tool-protections:${projectId}`)
|
|
295
|
+
).toBeNull();
|
|
296
|
+
|
|
297
|
+
// The API answers again: the next lookup reads it.
|
|
298
|
+
createMockFetchResponse(mockFetch, {
|
|
299
|
+
"tool-a": { requiresDelegation: true, requiredScopes: ["read"] },
|
|
300
|
+
});
|
|
301
|
+
const served = await service.checkToolProtection("tool-a", agentDid);
|
|
302
|
+
expect(served).toMatchObject({
|
|
303
|
+
requiresDelegation: true,
|
|
304
|
+
requiredScopes: ["read"],
|
|
305
|
+
});
|
|
306
|
+
expect(isUnavailableToolProtection(served!)).toBe(false);
|
|
307
|
+
expect(mockFetch).toHaveBeenCalledTimes(2);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("still prefers a stale cache over the stand-in", async () => {
|
|
311
|
+
const staleCache = new StaleCacheTestImpl();
|
|
312
|
+
await staleCache.set(
|
|
313
|
+
`config:tool-protections:${projectId}`,
|
|
314
|
+
{
|
|
315
|
+
toolProtections: {
|
|
316
|
+
"tool-a": { requiresDelegation: true, requiredScopes: ["read"] },
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
100
|
|
320
|
+
);
|
|
321
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
322
|
+
|
|
323
|
+
service = new ToolProtectionService(
|
|
324
|
+
{
|
|
325
|
+
projectId,
|
|
326
|
+
apiUrl,
|
|
327
|
+
apiKey,
|
|
328
|
+
allowStaleCache: true,
|
|
329
|
+
maxStaleCacheAge: 60000,
|
|
330
|
+
},
|
|
331
|
+
staleCache
|
|
332
|
+
);
|
|
333
|
+
mockFetch.mockRejectedValue(new Error("Network error"));
|
|
334
|
+
|
|
335
|
+
const protection = await service.checkToolProtection("tool-a", agentDid);
|
|
336
|
+
expect(protection).toMatchObject({ requiredScopes: ["read"] });
|
|
337
|
+
expect(isUnavailableToolProtection(protection!)).toBe(false);
|
|
271
338
|
});
|
|
272
339
|
|
|
273
340
|
it("should allow-all when configured and API fails", async () => {
|
|
@@ -12,6 +12,9 @@ export async function createToolProtectionRuntime() {
|
|
|
12
12
|
environment: "development",
|
|
13
13
|
session: { timestampSkewSeconds: 120, ttlMinutes: 30 },
|
|
14
14
|
toolProtectionService: mockToolProtectionService as any,
|
|
15
|
+
delegation: {
|
|
16
|
+
authorization: { authorizationUrl: "https://auth.example.com/consent" },
|
|
17
|
+
} as any,
|
|
15
18
|
};
|
|
16
19
|
const runtime = new MCPIRuntimeBase(config);
|
|
17
20
|
(runtime as any).accessControlService = mockAccessControlService;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
MemoryResumeTokenStore,
|
|
4
|
+
verifyOrHints,
|
|
5
|
+
type AuthHandshakeConfig,
|
|
6
|
+
} from "../handshake";
|
|
7
|
+
import type { DelegationVerifier } from "../types";
|
|
8
|
+
|
|
9
|
+
const HOLDER = "did:key:z6MkHolder";
|
|
10
|
+
const SCOPES = ["files:read"];
|
|
11
|
+
|
|
12
|
+
function configWith(verifier: DelegationVerifier): AuthHandshakeConfig {
|
|
13
|
+
return {
|
|
14
|
+
delegationVerifier: verifier,
|
|
15
|
+
resumeTokenStore: new MemoryResumeTokenStore(),
|
|
16
|
+
bouncer: { authorizationUrl: "https://consent.example/consent" },
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const credential = {
|
|
21
|
+
agent_did: HOLDER,
|
|
22
|
+
user_did: "did:web:user.example",
|
|
23
|
+
scopes: SCOPES,
|
|
24
|
+
authorization: { type: "none" as const },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
describe("verifyOrHints on a presented delegation", () => {
|
|
28
|
+
it("asks for authorization when the call presents none, and looks nothing up", async () => {
|
|
29
|
+
const verifier = {
|
|
30
|
+
verify: vi.fn(),
|
|
31
|
+
verifyPresented: vi.fn(),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const result = await verifyOrHints(
|
|
35
|
+
HOLDER,
|
|
36
|
+
SCOPES,
|
|
37
|
+
configWith(verifier),
|
|
38
|
+
undefined,
|
|
39
|
+
{ presented: {} }
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(result.authorized).toBe(false);
|
|
43
|
+
expect(result.reason).toBe("No delegation presented");
|
|
44
|
+
expect(result.authError?.authorizationUrl).toContain(
|
|
45
|
+
encodeURIComponent(HOLDER)
|
|
46
|
+
);
|
|
47
|
+
expect(verifier.verify).not.toHaveBeenCalled();
|
|
48
|
+
expect(verifier.verifyPresented).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("refuses when the verifier cannot check a presented credential", async () => {
|
|
52
|
+
const verifier = { verify: vi.fn() };
|
|
53
|
+
|
|
54
|
+
const result = await verifyOrHints(
|
|
55
|
+
HOLDER,
|
|
56
|
+
SCOPES,
|
|
57
|
+
configWith(verifier),
|
|
58
|
+
undefined,
|
|
59
|
+
{ presented: { credential: "a.b.c" } }
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
expect(result.authorized).toBe(false);
|
|
63
|
+
expect(result.reason).toMatch(/cannot verify a presented delegation/);
|
|
64
|
+
expect(verifier.verify).not.toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("hands the credential and the proven holder to verifyPresented", async () => {
|
|
68
|
+
const verifier = {
|
|
69
|
+
verify: vi.fn(),
|
|
70
|
+
verifyPresented: vi.fn().mockResolvedValue({ valid: true, credential }),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const result = await verifyOrHints(
|
|
74
|
+
HOLDER,
|
|
75
|
+
SCOPES,
|
|
76
|
+
configWith(verifier),
|
|
77
|
+
undefined,
|
|
78
|
+
{ presented: { credential: "a.b.c" } }
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(verifier.verifyPresented).toHaveBeenCalledWith(
|
|
82
|
+
"a.b.c",
|
|
83
|
+
HOLDER,
|
|
84
|
+
SCOPES
|
|
85
|
+
);
|
|
86
|
+
// A credential-only decision is authority on the presented path.
|
|
87
|
+
expect(result.authorized).toBe(true);
|
|
88
|
+
expect(result.credential).toBe(credential);
|
|
89
|
+
expect(verifier.verify).not.toHaveBeenCalled();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("refuses a presented credential the verifier rejects", async () => {
|
|
93
|
+
const verifier = {
|
|
94
|
+
verify: vi.fn(),
|
|
95
|
+
verifyPresented: vi
|
|
96
|
+
.fn()
|
|
97
|
+
.mockResolvedValue({ valid: false, reason: "Not this holder's" }),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const result = await verifyOrHints(
|
|
101
|
+
HOLDER,
|
|
102
|
+
SCOPES,
|
|
103
|
+
configWith(verifier),
|
|
104
|
+
undefined,
|
|
105
|
+
{ presented: { credential: "a.b.c" } }
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
expect(result.authorized).toBe(false);
|
|
109
|
+
expect(result.reason).toBe("Not this holder's");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("still needs a stored record when it looks one up for the agent", async () => {
|
|
113
|
+
const verifier = {
|
|
114
|
+
verify: vi.fn().mockResolvedValue({ valid: true, credential }),
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const result = await verifyOrHints(HOLDER, SCOPES, configWith(verifier));
|
|
118
|
+
|
|
119
|
+
expect(result.authorized).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("verifyOrHints without a presented delegation", () => {
|
|
124
|
+
it("warns once that it authorizes on a DID the caller only names", async () => {
|
|
125
|
+
// The warning is said once per process, so this reads a module of its own.
|
|
126
|
+
vi.resetModules();
|
|
127
|
+
const fresh = await import("../handshake");
|
|
128
|
+
const { logger } = await import("../../logging");
|
|
129
|
+
const warn = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
|
130
|
+
const verifier = {
|
|
131
|
+
verify: vi.fn().mockResolvedValue({ valid: true, credential }),
|
|
132
|
+
verifyPresented: vi.fn().mockResolvedValue({ valid: true, credential }),
|
|
133
|
+
};
|
|
134
|
+
const config = {
|
|
135
|
+
...configWith(verifier),
|
|
136
|
+
resumeTokenStore: new fresh.MemoryResumeTokenStore(),
|
|
137
|
+
};
|
|
138
|
+
const lookups = () =>
|
|
139
|
+
warn.mock.calls.filter(([message]) =>
|
|
140
|
+
String(message).includes("options.presented")
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
await fresh.verifyOrHints(HOLDER, SCOPES, config, undefined, {
|
|
144
|
+
presented: { credential: "a.b.c" },
|
|
145
|
+
});
|
|
146
|
+
expect(lookups()).toHaveLength(0);
|
|
147
|
+
|
|
148
|
+
await fresh.verifyOrHints(HOLDER, SCOPES, config);
|
|
149
|
+
await fresh.verifyOrHints(HOLDER, SCOPES, config);
|
|
150
|
+
|
|
151
|
+
expect(verifier.verify).toHaveBeenCalledTimes(2);
|
|
152
|
+
expect(lookups()).toHaveLength(1);
|
|
153
|
+
warn.mockRestore();
|
|
154
|
+
});
|
|
155
|
+
});
|