@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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { logger } from "../../logging";
|
|
3
|
+
import {
|
|
4
|
+
createRequestHolderVerifier,
|
|
5
|
+
resolveDidKeyHolder,
|
|
6
|
+
} from "../request-holder";
|
|
7
|
+
import {
|
|
8
|
+
documentUrl,
|
|
9
|
+
hostedResolver,
|
|
10
|
+
newHolder,
|
|
11
|
+
newWebHolder,
|
|
12
|
+
SERVER,
|
|
13
|
+
signedCall,
|
|
14
|
+
} from "./holder-fixtures";
|
|
15
|
+
|
|
16
|
+
describe("createRequestHolderVerifier", () => {
|
|
17
|
+
const params = {
|
|
18
|
+
name: "read",
|
|
19
|
+
arguments: { id: 1 },
|
|
20
|
+
delegationToken: "a.b.c",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
it("proves the did:key holder whose key signed the call", async () => {
|
|
24
|
+
const holder = newHolder();
|
|
25
|
+
const verify = createRequestHolderVerifier({ audience: SERVER });
|
|
26
|
+
|
|
27
|
+
expect(await verify(await signedCall(holder, params))).toEqual({
|
|
28
|
+
ok: true,
|
|
29
|
+
did: holder.did,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("proves no holder for a call without a request proof", async () => {
|
|
34
|
+
const verify = createRequestHolderVerifier({ audience: SERVER });
|
|
35
|
+
|
|
36
|
+
expect(
|
|
37
|
+
await verify({
|
|
38
|
+
method: "tools/call",
|
|
39
|
+
params: { ...params, _meta: { agentDid: "did:key:z" } },
|
|
40
|
+
})
|
|
41
|
+
).toEqual({ ok: false, code: "holder_proof_required" });
|
|
42
|
+
expect(await verify({ method: "tools/call" })).toEqual({
|
|
43
|
+
ok: false,
|
|
44
|
+
code: "holder_proof_required",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("refuses a call whose params changed after signing", async () => {
|
|
49
|
+
const verify = createRequestHolderVerifier({ audience: SERVER });
|
|
50
|
+
const call = await signedCall(newHolder(), params);
|
|
51
|
+
|
|
52
|
+
const result = await verify({
|
|
53
|
+
...call,
|
|
54
|
+
params: { ...call.params, delegationToken: "x.y.z" },
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
expect(result).toEqual({ ok: false, code: "proof_invalid" });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("refuses a proof addressed to another server", async () => {
|
|
61
|
+
const verify = createRequestHolderVerifier({ audience: SERVER });
|
|
62
|
+
|
|
63
|
+
const result = await verify(
|
|
64
|
+
await signedCall(newHolder(), params, "did:web:other.example")
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result).toEqual({ ok: false, code: "proof_invalid" });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("spends each proof once", async () => {
|
|
71
|
+
const verify = createRequestHolderVerifier({ audience: SERVER });
|
|
72
|
+
const call = await signedCall(newHolder(), params);
|
|
73
|
+
|
|
74
|
+
expect((await verify(call)).ok).toBe(true);
|
|
75
|
+
expect(await verify(call)).toEqual({ ok: false, code: "proof_invalid" });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("resolveDidKeyHolder", () => {
|
|
80
|
+
it("resolves a did:key holder's own key id to its key", async () => {
|
|
81
|
+
const holder = newHolder();
|
|
82
|
+
|
|
83
|
+
expect(await resolveDidKeyHolder(holder.kid)).toMatchObject({
|
|
84
|
+
kty: "OKP",
|
|
85
|
+
crv: "Ed25519",
|
|
86
|
+
x: holder.privateJwk.x,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it.each([
|
|
91
|
+
["another fragment", (did: string) => `${did}#key-1`],
|
|
92
|
+
["no fragment", (did: string) => did],
|
|
93
|
+
["a did:web key", () => "did:web:agent.example#key-1"],
|
|
94
|
+
])("resolves no key for %s", async (_label, kidOf) => {
|
|
95
|
+
await expect(resolveDidKeyHolder(kidOf(newHolder().did))).rejects.toThrow(
|
|
96
|
+
/did:key/
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("createRequestHolderVerifier with a did:web holder", () => {
|
|
102
|
+
const params = { name: "read", arguments: { id: 1 } };
|
|
103
|
+
|
|
104
|
+
afterEach(() => {
|
|
105
|
+
vi.restoreAllMocks();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("proves the holder by a key its document lists for authentication", async () => {
|
|
109
|
+
const holder = newWebHolder("listed");
|
|
110
|
+
const { resolver } = hostedResolver(
|
|
111
|
+
new Map([[documentUrl("listed"), holder.document]])
|
|
112
|
+
);
|
|
113
|
+
const verify = createRequestHolderVerifier({
|
|
114
|
+
audience: SERVER,
|
|
115
|
+
didWebResolver: resolver,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(await verify(await signedCall(holder, params))).toEqual({
|
|
119
|
+
ok: true,
|
|
120
|
+
did: holder.did,
|
|
121
|
+
});
|
|
122
|
+
expect(
|
|
123
|
+
await verify(
|
|
124
|
+
await signedCall({ ...holder, kid: `${holder.did}#key-1-jwk` }, params)
|
|
125
|
+
)
|
|
126
|
+
).toEqual({ ok: true, did: holder.did });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("reads the document once while it is cached", async () => {
|
|
130
|
+
const holder = newWebHolder("cached");
|
|
131
|
+
const { resolver, reads } = hostedResolver(
|
|
132
|
+
new Map([[documentUrl("cached"), holder.document]])
|
|
133
|
+
);
|
|
134
|
+
const verify = createRequestHolderVerifier({
|
|
135
|
+
audience: SERVER,
|
|
136
|
+
didWebResolver: resolver,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect((await verify(await signedCall(holder, params))).ok).toBe(true);
|
|
140
|
+
expect((await verify(await signedCall(holder, params))).ok).toBe(true);
|
|
141
|
+
expect(reads).toEqual([documentUrl("cached")]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("refuses a key the holder's document does not list", async () => {
|
|
145
|
+
const holder = newWebHolder("unlisted");
|
|
146
|
+
const { resolver } = hostedResolver(
|
|
147
|
+
new Map([[documentUrl("unlisted"), holder.document]])
|
|
148
|
+
);
|
|
149
|
+
const verify = createRequestHolderVerifier({
|
|
150
|
+
audience: SERVER,
|
|
151
|
+
didWebResolver: resolver,
|
|
152
|
+
});
|
|
153
|
+
const stranger = newHolder();
|
|
154
|
+
|
|
155
|
+
// Another key, under a method id the document does not have.
|
|
156
|
+
expect(
|
|
157
|
+
await verify(
|
|
158
|
+
await signedCall(
|
|
159
|
+
{ ...stranger, did: holder.did, kid: `${holder.did}#key-2` },
|
|
160
|
+
params
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
).toEqual({ ok: false, code: "proof_invalid" });
|
|
164
|
+
// Another key, under the id of a method the document does list.
|
|
165
|
+
expect(
|
|
166
|
+
await verify(
|
|
167
|
+
await signedCall(
|
|
168
|
+
{ ...stranger, did: holder.did, kid: holder.kid },
|
|
169
|
+
params
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
).toEqual({ ok: false, code: "proof_invalid" });
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("refuses a key the document lists only for assertions", async () => {
|
|
176
|
+
const holder = newWebHolder("assertions");
|
|
177
|
+
const { resolver } = hostedResolver(
|
|
178
|
+
new Map([
|
|
179
|
+
[documentUrl("assertions"), { ...holder.document, authentication: [] }],
|
|
180
|
+
])
|
|
181
|
+
);
|
|
182
|
+
const verify = createRequestHolderVerifier({
|
|
183
|
+
audience: SERVER,
|
|
184
|
+
didWebResolver: resolver,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(await verify(await signedCall(holder, params))).toEqual({
|
|
188
|
+
ok: false,
|
|
189
|
+
code: "proof_invalid",
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("refuses a holder whose document cannot be read, and logs why", async () => {
|
|
194
|
+
const warn = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
|
195
|
+
const holder = newWebHolder("missing");
|
|
196
|
+
const { resolver } = hostedResolver(new Map());
|
|
197
|
+
const verify = createRequestHolderVerifier({
|
|
198
|
+
audience: SERVER,
|
|
199
|
+
didWebResolver: resolver,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
expect(await verify(await signedCall(holder, params))).toEqual({
|
|
203
|
+
ok: false,
|
|
204
|
+
code: "proof_invalid",
|
|
205
|
+
});
|
|
206
|
+
expect(warn).toHaveBeenCalledWith(
|
|
207
|
+
expect.stringContaining(`${holder.did}: unavailable`)
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -240,3 +240,82 @@ describe("ToolContextBuilder — delegation auth-type enforcement (B3)", () => {
|
|
|
240
240
|
});
|
|
241
241
|
});
|
|
242
242
|
});
|
|
243
|
+
|
|
244
|
+
describe("ToolContextBuilder — stored IdP token needs a satisfying delegation (finding A)", () => {
|
|
245
|
+
// Defence in depth: a delegation minted under the user's DID without their
|
|
246
|
+
// sign-in must not unlock the IdP token stored for that DID.
|
|
247
|
+
const oauthTool: ToolProtection = {
|
|
248
|
+
requiresDelegation: true,
|
|
249
|
+
requiredScopes: ["read:user"],
|
|
250
|
+
authorization: { type: "oauth2", provider: "github" },
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
let builder: ToolContextBuilder;
|
|
254
|
+
|
|
255
|
+
beforeEach(() => {
|
|
256
|
+
builder = new ToolContextBuilder({
|
|
257
|
+
tokenResolver: {
|
|
258
|
+
resolveTokenDataFromDid: vi.fn().mockResolvedValue({
|
|
259
|
+
access_token: "victim-idp-token",
|
|
260
|
+
tokenUsage: "bearer",
|
|
261
|
+
}),
|
|
262
|
+
} as unknown as IdpTokenResolver,
|
|
263
|
+
configService: {
|
|
264
|
+
getOAuthConfig: vi.fn(),
|
|
265
|
+
} as unknown as OAuthConfigService,
|
|
266
|
+
providerResolver: {
|
|
267
|
+
resolveProvider: vi.fn().mockResolvedValue("github"),
|
|
268
|
+
} as unknown as ProviderResolver,
|
|
269
|
+
projectId: "proj-123",
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it.each([
|
|
274
|
+
["a none delegation", "none"],
|
|
275
|
+
["a password delegation", "password"],
|
|
276
|
+
["a legacy delegation with no type", undefined],
|
|
277
|
+
])(
|
|
278
|
+
"re-challenges instead of handing out the token for %s",
|
|
279
|
+
async (_label, delegationType) => {
|
|
280
|
+
await expect(
|
|
281
|
+
builder.buildContext(
|
|
282
|
+
"gated-tool",
|
|
283
|
+
"did:key:z6MkVictim",
|
|
284
|
+
"session-123",
|
|
285
|
+
"delegation-token",
|
|
286
|
+
oauthTool,
|
|
287
|
+
delegationType
|
|
288
|
+
)
|
|
289
|
+
).rejects.toThrow(OAuthRequiredError);
|
|
290
|
+
}
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
it("hands out the token for an oauth2 delegation", async () => {
|
|
294
|
+
const result = await builder.buildContext(
|
|
295
|
+
"gated-tool",
|
|
296
|
+
"did:key:z6MkUser",
|
|
297
|
+
"session-123",
|
|
298
|
+
"delegation-token",
|
|
299
|
+
oauthTool,
|
|
300
|
+
"oauth2"
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
expect(result?.idpToken).toBe("victim-idp-token");
|
|
304
|
+
expect(result?.idpHeaders).toEqual({
|
|
305
|
+
Authorization: "Bearer victim-idp-token",
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("keeps the existing behaviour when no delegation is presented", async () => {
|
|
310
|
+
const result = await builder.buildContext(
|
|
311
|
+
"gated-tool",
|
|
312
|
+
"did:key:z6MkUser",
|
|
313
|
+
"session-123",
|
|
314
|
+
undefined,
|
|
315
|
+
oauthTool,
|
|
316
|
+
undefined
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
expect(result?.idpToken).toBe("victim-idp-token");
|
|
320
|
+
});
|
|
321
|
+
});
|
|
@@ -114,7 +114,10 @@ describe("ToolContextBuilder", () => {
|
|
|
114
114
|
"did:key:z6MkUser123",
|
|
115
115
|
"session-123",
|
|
116
116
|
"delegation-token",
|
|
117
|
-
toolProtection
|
|
117
|
+
toolProtection,
|
|
118
|
+
// A presented delegation must satisfy the tool's type before the
|
|
119
|
+
// stored IdP token is handed out (finding A defence in depth).
|
|
120
|
+
"oauth2"
|
|
118
121
|
);
|
|
119
122
|
|
|
120
123
|
expect(result).toBeDefined();
|
|
@@ -430,9 +433,3 @@ describe("ToolContextBuilder", () => {
|
|
|
430
433
|
});
|
|
431
434
|
});
|
|
432
435
|
});
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The served tool-protection revision: the hash of exactly what Checkpoint
|
|
3
|
+
* served, which a native challenge registration reports and Checkpoint
|
|
4
|
+
* recomputes. It is computed from the served `tools` object as received,
|
|
5
|
+
* before any local config is merged in.
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
import { canonicalize } from "json-canonicalize";
|
|
10
|
+
|
|
11
|
+
import { InMemoryToolProtectionCache } from "../../cache/tool-protection-cache.js";
|
|
12
|
+
import {
|
|
13
|
+
ToolProtectionService,
|
|
14
|
+
servedToolProtectionRevision,
|
|
15
|
+
} from "../tool-protection.service.js";
|
|
16
|
+
|
|
17
|
+
const SERVED_TOOLS = {
|
|
18
|
+
place_order: {
|
|
19
|
+
requiresDelegation: true,
|
|
20
|
+
requiredScopes: ["orders:write"],
|
|
21
|
+
authorization: { type: "none" },
|
|
22
|
+
},
|
|
23
|
+
browse_catalog: { requiresDelegation: false, requiredScopes: [] },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
vi.unstubAllGlobals();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("servedToolProtectionRevision", () => {
|
|
31
|
+
it("is the SHA-256 hex of the RFC 8785 canonical JSON", async () => {
|
|
32
|
+
const expected = createHash("sha256")
|
|
33
|
+
.update(canonicalize(SERVED_TOOLS))
|
|
34
|
+
.digest("hex");
|
|
35
|
+
expect(await servedToolProtectionRevision(SERVED_TOOLS)).toBe(expected);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("does not depend on key order", async () => {
|
|
39
|
+
const reordered = {
|
|
40
|
+
browse_catalog: { requiredScopes: [], requiresDelegation: false },
|
|
41
|
+
place_order: {
|
|
42
|
+
authorization: { type: "none" },
|
|
43
|
+
requiredScopes: ["orders:write"],
|
|
44
|
+
requiresDelegation: true,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
expect(await servedToolProtectionRevision(reordered)).toBe(
|
|
48
|
+
await servedToolProtectionRevision(SERVED_TOOLS)
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("ToolProtectionService.getToolProtectionConfig", () => {
|
|
54
|
+
function service(localConfig?: Record<string, unknown>) {
|
|
55
|
+
return new ToolProtectionService(
|
|
56
|
+
{
|
|
57
|
+
apiUrl: "https://checkpoint.test",
|
|
58
|
+
apiKey: "sk_test_key",
|
|
59
|
+
projectId: "project-1",
|
|
60
|
+
failSafeBehavior: "deny-all",
|
|
61
|
+
...(localConfig
|
|
62
|
+
? { localConfig: { toolProtections: localConfig as never } }
|
|
63
|
+
: {}),
|
|
64
|
+
},
|
|
65
|
+
new InMemoryToolProtectionCache()
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
it("records the revision of the served tools, as received, before the local merge", async () => {
|
|
70
|
+
vi.stubGlobal(
|
|
71
|
+
"fetch",
|
|
72
|
+
vi.fn(async () =>
|
|
73
|
+
Response.json({
|
|
74
|
+
success: true,
|
|
75
|
+
data: { config: { toolProtection: { tools: SERVED_TOOLS } } },
|
|
76
|
+
})
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
const config = await service({
|
|
80
|
+
local_only: { requiresDelegation: true, requiredScopes: ["x:y"] },
|
|
81
|
+
}).getToolProtectionConfig("did:key:z6MkAgent");
|
|
82
|
+
|
|
83
|
+
expect(config.servedRevision).toBe(
|
|
84
|
+
await servedToolProtectionRevision(SERVED_TOOLS)
|
|
85
|
+
);
|
|
86
|
+
// The local tool is evaluated, but it is not part of what Checkpoint served.
|
|
87
|
+
expect(Object.keys(config.toolProtections)).toContain("local_only");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("records no revision for a config Checkpoint did not serve", async () => {
|
|
91
|
+
// A network failure falls back to the fail-safe deny-all config.
|
|
92
|
+
vi.stubGlobal(
|
|
93
|
+
"fetch",
|
|
94
|
+
vi.fn(async () => {
|
|
95
|
+
throw new TypeError("network down");
|
|
96
|
+
})
|
|
97
|
+
);
|
|
98
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
99
|
+
const config = await service().getToolProtectionConfig("did:key:z6MkAgent");
|
|
100
|
+
expect(config.servedRevision).toBeUndefined();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
} from "@kya-os/contracts/checkpoint-api";
|
|
29
29
|
import { CheckpointAPIError } from "@kya-os/contracts/checkpoint-api";
|
|
30
30
|
import type { FetchProvider } from "../providers/base";
|
|
31
|
+
import { normalizeAuthType } from "./auth-type-satisfies.js";
|
|
31
32
|
|
|
32
33
|
// Type for error response
|
|
33
34
|
type CheckpointAPIErrorResponse = {
|
|
@@ -1026,16 +1027,21 @@ export function authorizationMatches(
|
|
|
1026
1027
|
return true;
|
|
1027
1028
|
}
|
|
1028
1029
|
|
|
1029
|
-
//
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1030
|
+
// Checkpoint grants can use the legacy `oauth` spelling while current tool
|
|
1031
|
+
// configuration uses `oauth2`. Both must enforce the same provider boundary;
|
|
1032
|
+
// checking only `oauth` would also let two `oauth2` values skip that check.
|
|
1033
|
+
const delegationIsOAuth = normalizeAuthType(delegationAuth.type) === "oauth2";
|
|
1034
|
+
const toolRequiresOAuth = normalizeAuthType(toolAuth.type) === "oauth2";
|
|
1035
|
+
if (delegationIsOAuth || toolRequiresOAuth) {
|
|
1036
|
+
if (!delegationIsOAuth || !toolRequiresOAuth) return false;
|
|
1034
1037
|
// If tool doesn't specify a provider, any provider is acceptable
|
|
1035
1038
|
if (!toolAuth.provider) return true;
|
|
1036
1039
|
return delegationAuth.provider === toolAuth.provider;
|
|
1037
1040
|
}
|
|
1038
1041
|
|
|
1042
|
+
// Preserve the existing comparison for non-OAuth authorization types.
|
|
1043
|
+
if (delegationAuth.type !== toolAuth.type) return false;
|
|
1044
|
+
|
|
1039
1045
|
// For verifiable_credential, compare credentialType
|
|
1040
1046
|
if (delegationAuth.type === "verifiable_credential") {
|
|
1041
1047
|
// If tool doesn't specify a credential type, any type is acceptable
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A request-proof guard that reads a holder's key only for a proof its own
|
|
3
|
+
* claims pass.
|
|
4
|
+
*
|
|
5
|
+
* Through 1.25.0, `@kya-os/mcp/card`'s guard read the key a proof names
|
|
6
|
+
* before it checked the proof's audience, request hash and window, and for a
|
|
7
|
+
* did:web holder that is a network read of a document the caller chose.
|
|
8
|
+
* 1.26.0 decides those claims first itself; this guard keeps the order
|
|
9
|
+
* without relying on that. A proof is first put to the same guard with no key
|
|
10
|
+
* to read. That guard refuses every proof, and its reasons beside the missing
|
|
11
|
+
* key are what the proof's own claims decide: its form, that its key is its
|
|
12
|
+
* holder's, its audience, its request hash and its window. A proof refused
|
|
13
|
+
* there reads no document and spends no nonce, and no rule is copied from the
|
|
14
|
+
* library.
|
|
15
|
+
*
|
|
16
|
+
* @module services/claims-first-guard
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { DifCard } from "../runtime/dif-core";
|
|
20
|
+
|
|
21
|
+
type RequireProof = DifCard["requireProof"];
|
|
22
|
+
type ProofDeps = Parameters<RequireProof>[0];
|
|
23
|
+
type ProofGuard = ReturnType<RequireProof>;
|
|
24
|
+
type ProofRefusal = Extract<Awaited<ReturnType<ProofGuard>>, { ok: false }>;
|
|
25
|
+
|
|
26
|
+
const NO_KEY_READ = "No key is read before the proof's own claims hold";
|
|
27
|
+
/** The guard's reason for a proof whose key its seam did not give. */
|
|
28
|
+
const KEY_UNRESOLVED = "key_unresolvable";
|
|
29
|
+
|
|
30
|
+
/** `deps` with no key to read and no nonce to spend. */
|
|
31
|
+
function withNoKey(deps: ProofDeps): ProofDeps {
|
|
32
|
+
// Over `deps`, not a copy of it: its other members may be inherited.
|
|
33
|
+
const keyless: ProofDeps = Object.create(deps);
|
|
34
|
+
keyless.resolveKey = async () => {
|
|
35
|
+
throw new Error(NO_KEY_READ);
|
|
36
|
+
};
|
|
37
|
+
keyless.resolveDidKeys = undefined;
|
|
38
|
+
keyless.consumeNonceIfFresh = async () => false;
|
|
39
|
+
return keyless;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether the keyless guard refused a proof for want of its key alone, so
|
|
44
|
+
* that everything its own claims decide holds.
|
|
45
|
+
*/
|
|
46
|
+
function refusedOnlyForItsKey(reasons: readonly string[] | undefined): boolean {
|
|
47
|
+
return (
|
|
48
|
+
reasons !== undefined &&
|
|
49
|
+
reasons.length > 0 &&
|
|
50
|
+
reasons.every((reason) => reason === KEY_UNRESOLVED)
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** `refusal` without the key the keyless guard never tried to read. */
|
|
55
|
+
function withoutKeyReason(refusal: ProofRefusal): ProofRefusal {
|
|
56
|
+
const { reasons } = refusal.error;
|
|
57
|
+
if (!reasons) return refusal;
|
|
58
|
+
return {
|
|
59
|
+
...refusal,
|
|
60
|
+
error: {
|
|
61
|
+
...refusal.error,
|
|
62
|
+
reasons: reasons.filter((reason) => reason !== KEY_UNRESOLVED),
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `requireProof(deps)`, reading a key only for a proof whose own claims
|
|
69
|
+
* pass. A proof its claims refuse is answered with the library's refusal,
|
|
70
|
+
* its reasons as the library gives them.
|
|
71
|
+
*
|
|
72
|
+
* @param requireProof `@kya-os/mcp/card`'s guard builder
|
|
73
|
+
* @param deps what the guard verifies with; its key seams are called only
|
|
74
|
+
* for a proof that passed every check that needs no key
|
|
75
|
+
*/
|
|
76
|
+
export function claimsFirstGuard(
|
|
77
|
+
requireProof: RequireProof,
|
|
78
|
+
deps: ProofDeps
|
|
79
|
+
): ProofGuard {
|
|
80
|
+
const keyed = requireProof(deps);
|
|
81
|
+
const keyless = requireProof(withNoKey(deps));
|
|
82
|
+
return async (request, meta) => {
|
|
83
|
+
const unread = await keyless(request, meta);
|
|
84
|
+
if (!unread.ok && !refusedOnlyForItsKey(unread.error.reasons)) {
|
|
85
|
+
return withoutKeyReason(unread);
|
|
86
|
+
}
|
|
87
|
+
return keyed(request, meta);
|
|
88
|
+
};
|
|
89
|
+
}
|