@kya-os/mcp-i-runtime 1.3.1 → 1.3.2
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/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/proof/generator.d.ts +8 -6
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +7 -24
- package/dist/proof/generator.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +66 -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/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/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/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/package.json +2 -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 +255 -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/index.ts +15 -1
- package/src/proof/__tests__/proof-generator.test.ts +35 -12
- package/src/proof/generator.ts +18 -27
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +155 -62
- package/src/runtime/base.ts +95 -31
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/proof-verifier.test.ts +283 -195
- 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/proof-verifier.ts +18 -79
- 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
|
@@ -15,7 +15,9 @@ import { toDelegationDenial } from "../../runtime/delegation-denial";
|
|
|
15
15
|
import type { AccessControlApiService } from "../../services/access-control.service";
|
|
16
16
|
import type { ToolProtectionService } from "../../services/tool-protection.service";
|
|
17
17
|
|
|
18
|
-
function auditConfig(
|
|
18
|
+
function auditConfig(
|
|
19
|
+
record: (event: AuditTrailEventInput) => Promise<unknown>
|
|
20
|
+
): RuntimeAuditConfig {
|
|
19
21
|
return {
|
|
20
22
|
sourceId: "deployment:test:mcp:0",
|
|
21
23
|
context: {
|
|
@@ -36,7 +38,9 @@ function auditConfig(record: (event: AuditTrailEventInput) => Promise<unknown>):
|
|
|
36
38
|
};
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
function runtimeWithAudit(
|
|
41
|
+
function runtimeWithAudit(
|
|
42
|
+
record: (event: AuditTrailEventInput) => Promise<unknown>
|
|
43
|
+
): MCPIRuntimeBase {
|
|
40
44
|
const config: ProviderRuntimeConfig = {
|
|
41
45
|
...createMockProviders(),
|
|
42
46
|
environment: "development",
|
|
@@ -72,7 +76,7 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
72
76
|
"payments.send",
|
|
73
77
|
{ secret: "never-audited" },
|
|
74
78
|
handler,
|
|
75
|
-
{ id: "raw-session", nonce: "nonce", audience: "client" }
|
|
79
|
+
{ id: "raw-session", nonce: "nonce", audience: "client" }
|
|
76
80
|
);
|
|
77
81
|
|
|
78
82
|
expect(result).toMatchObject({
|
|
@@ -95,7 +99,9 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
95
99
|
"invocation-1:started",
|
|
96
100
|
"invocation-1:completed",
|
|
97
101
|
]);
|
|
98
|
-
expect(
|
|
102
|
+
expect(
|
|
103
|
+
events.every((event) => event.correlationId === "correlation-1")
|
|
104
|
+
).toBe(true);
|
|
99
105
|
expect(JSON.stringify(events)).not.toContain("never-audited");
|
|
100
106
|
expect(JSON.stringify(events)).not.toContain("raw-session");
|
|
101
107
|
});
|
|
@@ -110,12 +116,13 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
110
116
|
throw new Error("side effect failed");
|
|
111
117
|
});
|
|
112
118
|
|
|
113
|
-
await expect(
|
|
114
|
-
"payments.send",
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
await expect(
|
|
120
|
+
runtime.processToolCall("payments.send", {}, handler, {
|
|
121
|
+
id: "session",
|
|
122
|
+
nonce: "nonce",
|
|
123
|
+
audience: "client",
|
|
124
|
+
})
|
|
125
|
+
).rejects.toThrow("side effect failed");
|
|
119
126
|
|
|
120
127
|
expect(handler).toHaveBeenCalledTimes(1);
|
|
121
128
|
expect(events.map((event) => event.eventType)).toEqual([
|
|
@@ -142,7 +149,9 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
142
149
|
auditTrail: config,
|
|
143
150
|
});
|
|
144
151
|
|
|
145
|
-
await runtime.processToolCall("payments.send", {}, async () => ({
|
|
152
|
+
await runtime.processToolCall("payments.send", {}, async () => ({
|
|
153
|
+
ok: true,
|
|
154
|
+
}));
|
|
146
155
|
|
|
147
156
|
expect(events.map((event) => event.eventId)).toHaveLength(2);
|
|
148
157
|
expect(events[0].eventId).not.toBe(events[1].eventId);
|
|
@@ -158,11 +167,13 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
158
167
|
});
|
|
159
168
|
const handler = vi.fn(async () => ({ ok: true }));
|
|
160
169
|
|
|
161
|
-
await expect(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
170
|
+
await expect(
|
|
171
|
+
runtime.processToolCall("dangerous", {}, handler, {
|
|
172
|
+
id: "session",
|
|
173
|
+
nonce: "nonce",
|
|
174
|
+
audience: "client",
|
|
175
|
+
})
|
|
176
|
+
).rejects.toThrow("recorder unavailable");
|
|
166
177
|
expect(handler).not.toHaveBeenCalled();
|
|
167
178
|
});
|
|
168
179
|
|
|
@@ -173,10 +184,15 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
173
184
|
return pendingResult(event, String(events.length));
|
|
174
185
|
});
|
|
175
186
|
|
|
176
|
-
await runtime.processToolCall(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
187
|
+
await runtime.processToolCall(
|
|
188
|
+
"protected",
|
|
189
|
+
{},
|
|
190
|
+
async () => ({
|
|
191
|
+
structuredContent: { type: "consent_required" },
|
|
192
|
+
_meta: { ui: { resourceUri: "ui://kya-os-consent/authorize" } },
|
|
193
|
+
}),
|
|
194
|
+
{ id: "session", nonce: "nonce", audience: "client" }
|
|
195
|
+
);
|
|
180
196
|
|
|
181
197
|
expect(events.map((event) => event.eventType)).toEqual([
|
|
182
198
|
"tool.call.started",
|
|
@@ -235,8 +251,10 @@ describe("canonical runtime audit lifecycle", () => {
|
|
|
235
251
|
});
|
|
236
252
|
const handler = vi.fn(async () => "side-effect-complete");
|
|
237
253
|
|
|
238
|
-
await expect(
|
|
239
|
-
"
|
|
254
|
+
await expect(
|
|
255
|
+
runtime.processToolCall("payments.send", {}, handler)
|
|
256
|
+
).rejects.toThrow(
|
|
257
|
+
"Required terminal audit delivery failed after tool execution"
|
|
240
258
|
);
|
|
241
259
|
expect(handler).toHaveBeenCalledTimes(1);
|
|
242
260
|
expect(events.map((event) => event.eventType)).toEqual([
|
|
@@ -266,13 +284,14 @@ describe("verifier denials in the audit ledger", () => {
|
|
|
266
284
|
auditTrail: auditConfig(record),
|
|
267
285
|
toolProtectionService,
|
|
268
286
|
});
|
|
269
|
-
(
|
|
270
|
-
|
|
287
|
+
(
|
|
288
|
+
runtime as unknown as { accessControlService: AccessControlApiService }
|
|
289
|
+
).accessControlService = accessControlService;
|
|
271
290
|
await runtime.initialize();
|
|
272
291
|
return runtime;
|
|
273
292
|
}
|
|
274
293
|
|
|
275
|
-
it("records a revoked grant as tool.call.
|
|
294
|
+
it("records a revoked grant as tool.call.challenged with DELEGATION_REVOKED", async () => {
|
|
276
295
|
const events: AuditTrailEventInput[] = [];
|
|
277
296
|
const runtime = await protectedRuntimeWithAudit(
|
|
278
297
|
async (event) => {
|
|
@@ -298,12 +317,12 @@ describe("verifier denials in the audit ledger", () => {
|
|
|
298
317
|
expect(handler).not.toHaveBeenCalled();
|
|
299
318
|
expect(events.map((event) => event.eventType)).toEqual([
|
|
300
319
|
"tool.call.started",
|
|
301
|
-
"tool.call.
|
|
320
|
+
"tool.call.challenged",
|
|
302
321
|
]);
|
|
303
322
|
expect(events[1]).toMatchObject({
|
|
304
|
-
outcome: "
|
|
323
|
+
outcome: "challenged",
|
|
305
324
|
reason: { code: "DELEGATION_REVOKED" },
|
|
306
|
-
details: { phase: "
|
|
325
|
+
details: { phase: "challenged" },
|
|
307
326
|
});
|
|
308
327
|
expect(JSON.stringify(events)).not.toContain("secret");
|
|
309
328
|
});
|
|
@@ -335,10 +354,10 @@ describe("verifier denials in the audit ledger", () => {
|
|
|
335
354
|
expect(handler).not.toHaveBeenCalled();
|
|
336
355
|
expect(events.map((event) => event.eventType)).toEqual([
|
|
337
356
|
"tool.call.started",
|
|
338
|
-
"tool.call.
|
|
357
|
+
"tool.call.challenged",
|
|
339
358
|
]);
|
|
340
359
|
expect(events[1]).toMatchObject({
|
|
341
|
-
outcome: "
|
|
360
|
+
outcome: "challenged",
|
|
342
361
|
reason: { code: "DELEGATION_REVOKED" },
|
|
343
362
|
});
|
|
344
363
|
expect(JSON.stringify(events)).not.toContain("NATIVE_BINDING_MISMATCH");
|
|
@@ -372,7 +391,7 @@ describe("verifier denials in the audit ledger", () => {
|
|
|
372
391
|
});
|
|
373
392
|
});
|
|
374
393
|
|
|
375
|
-
it("maps a
|
|
394
|
+
it("maps a refused grant a new grant could fix to the challenged phase", () => {
|
|
376
395
|
const error = new DelegationRequiredError(
|
|
377
396
|
"orders.place",
|
|
378
397
|
["orders:place"],
|
|
@@ -383,8 +402,8 @@ describe("verifier denials in the audit ledger", () => {
|
|
|
383
402
|
);
|
|
384
403
|
|
|
385
404
|
expect(terminalForToolError(error)).toEqual({
|
|
386
|
-
phase: "
|
|
387
|
-
outcome: "
|
|
405
|
+
phase: "challenged",
|
|
406
|
+
outcome: "challenged",
|
|
388
407
|
reasonCode: "AGENT_MISMATCH",
|
|
389
408
|
});
|
|
390
409
|
});
|
|
@@ -442,22 +461,202 @@ describe("HMAC audit references", () => {
|
|
|
442
461
|
expect(serialized).not.toContain("request-secret");
|
|
443
462
|
expect(serialized).not.toContain("flow-secret");
|
|
444
463
|
});
|
|
464
|
+
|
|
465
|
+
describe("actor attribution", () => {
|
|
466
|
+
const PROVED_HOLDER = "did:key:zProvedHolder";
|
|
467
|
+
|
|
468
|
+
function hmacFactory() {
|
|
469
|
+
return createHmacRuntimeAuditContextFactory({
|
|
470
|
+
secret: "0123456789abcdef0123456789abcdef",
|
|
471
|
+
keyId: "audit-ref-2026-01",
|
|
472
|
+
randomUUID: () => "fallback-invocation",
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Captured from the factory before the proved holder was considered, so
|
|
477
|
+
// a session without one must keep producing exactly these contexts.
|
|
478
|
+
const BASELINE_REFS = {
|
|
479
|
+
invocationId:
|
|
480
|
+
"invocation:3d83efbffba8e7e6d16375f1bca7769a995ec6d534250b35d1674e22e326600d",
|
|
481
|
+
correlationId:
|
|
482
|
+
"correlation:f91d71e462c8ce56c29ad3f285063e76df2417a55678720d62f33c5e52bb846c",
|
|
483
|
+
session: {
|
|
484
|
+
ref: {
|
|
485
|
+
kind: "keyed_commitment",
|
|
486
|
+
value:
|
|
487
|
+
"sha256:170dd72f62ad13ec4a1a3535afaa1b09f0ba8d8d00c9fd874455c5da67713fe0",
|
|
488
|
+
keyId: "audit-ref-2026-01",
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
function callWith(session: Record<string, unknown>) {
|
|
494
|
+
return {
|
|
495
|
+
toolName: "payments.send",
|
|
496
|
+
args: {},
|
|
497
|
+
session: {
|
|
498
|
+
id: "session-secret",
|
|
499
|
+
auditInvocationId: "request-secret",
|
|
500
|
+
...session,
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
it("attributes the call to the proved holder over caller-asserted DIDs", async () => {
|
|
506
|
+
const context = await hmacFactory().create(
|
|
507
|
+
callWith({
|
|
508
|
+
verifiedInvokerDid: PROVED_HOLDER,
|
|
509
|
+
clientDid: "did:key:zClient",
|
|
510
|
+
userDid: "did:key:zUser",
|
|
511
|
+
})
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
expect(context.actor).toEqual({ kind: "public_did", did: PROVED_HOLDER });
|
|
515
|
+
expect(JSON.stringify(context)).not.toContain("zClient");
|
|
516
|
+
expect(JSON.stringify(context)).not.toContain("zUser");
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("without a proved holder, keeps today's context with clientDid as the actor", async () => {
|
|
520
|
+
const context = await hmacFactory().create(
|
|
521
|
+
callWith({ clientDid: "did:key:zClient", userDid: "did:key:zUser" })
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
expect(context).toEqual({
|
|
525
|
+
...BASELINE_REFS,
|
|
526
|
+
actor: { kind: "public_did", did: "did:key:zClient" },
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it("without a proved holder or clientDid, keeps today's context with userDid as the actor", async () => {
|
|
531
|
+
const context = await hmacFactory().create(
|
|
532
|
+
callWith({ userDid: "did:key:zUser" })
|
|
533
|
+
);
|
|
534
|
+
|
|
535
|
+
expect(context).toEqual({
|
|
536
|
+
...BASELINE_REFS,
|
|
537
|
+
actor: { kind: "public_did", did: "did:key:zUser" },
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
it("never takes the actor from a caller-asserted agentDid", async () => {
|
|
542
|
+
const context = await hmacFactory().create(
|
|
543
|
+
callWith({ agentDid: "did:key:zAsserted" })
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
expect(context).toEqual(BASELINE_REFS);
|
|
547
|
+
expect(context).not.toHaveProperty("actor");
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("keeps the did: prefix check for the proved holder", async () => {
|
|
551
|
+
const context = await hmacFactory().create(
|
|
552
|
+
callWith({
|
|
553
|
+
verifiedInvokerDid: "not-a-did",
|
|
554
|
+
clientDid: "did:key:zClient",
|
|
555
|
+
})
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
// A malformed holder never falls back to the caller-asserted clientDid.
|
|
559
|
+
expect(context).not.toHaveProperty("actor");
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
it("ignores a blank proved holder like any other blank reference", async () => {
|
|
563
|
+
const context = await hmacFactory().create(
|
|
564
|
+
callWith({ verifiedInvokerDid: " ", clientDid: "did:key:zClient" })
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
expect(context.actor).toEqual({
|
|
568
|
+
kind: "public_did",
|
|
569
|
+
did: "did:key:zClient",
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("records the presenter, not the grant's agent, when a holder presents another agent's grant", async () => {
|
|
574
|
+
const grantAgent = "did:key:zGrantAgentA";
|
|
575
|
+
const presenter = "did:key:zPresenterB";
|
|
576
|
+
const events: AuditTrailEventInput[] = [];
|
|
577
|
+
const verifyDelegation = vi.fn().mockResolvedValue({
|
|
578
|
+
data: {
|
|
579
|
+
valid: false,
|
|
580
|
+
error: {
|
|
581
|
+
code: "agent_mismatch",
|
|
582
|
+
message: "grant belongs to another agent",
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
});
|
|
586
|
+
const runtime = new MCPIRuntimeBase({
|
|
587
|
+
...createMockProviders(),
|
|
588
|
+
environment: "production",
|
|
589
|
+
auditTrail: {
|
|
590
|
+
...auditConfig(async (event) => {
|
|
591
|
+
events.push(event);
|
|
592
|
+
return pendingResult(event, String(events.length));
|
|
593
|
+
}),
|
|
594
|
+
context: hmacFactory(),
|
|
595
|
+
},
|
|
596
|
+
toolProtectionService: {
|
|
597
|
+
checkToolProtection: vi.fn().mockResolvedValue({
|
|
598
|
+
requiresDelegation: true,
|
|
599
|
+
requiredScopes: ["orders:place"],
|
|
600
|
+
}),
|
|
601
|
+
} as unknown as ToolProtectionService,
|
|
602
|
+
});
|
|
603
|
+
(
|
|
604
|
+
runtime as unknown as { accessControlService: AccessControlApiService }
|
|
605
|
+
).accessControlService = {
|
|
606
|
+
verifyDelegation,
|
|
607
|
+
} as unknown as AccessControlApiService;
|
|
608
|
+
await runtime.initialize();
|
|
609
|
+
const handler = vi.fn(async () => ({ ok: true }));
|
|
610
|
+
|
|
611
|
+
await expect(
|
|
612
|
+
runtime.processToolCall("orders.place", {}, handler, {
|
|
613
|
+
id: "session",
|
|
614
|
+
nonce: "nonce",
|
|
615
|
+
audience: "client",
|
|
616
|
+
delegationToken: "grant-for-agent-a",
|
|
617
|
+
verifiedInvokerDid: presenter,
|
|
618
|
+
// What a caller can assert on its own; none of it is proved.
|
|
619
|
+
clientDid: grantAgent,
|
|
620
|
+
agentDid: grantAgent,
|
|
621
|
+
})
|
|
622
|
+
).rejects.toThrow(DelegationRequiredError);
|
|
623
|
+
|
|
624
|
+
expect(handler).not.toHaveBeenCalled();
|
|
625
|
+
expect(verifyDelegation.mock.calls[0][0]).toMatchObject({
|
|
626
|
+
agent_did: presenter,
|
|
627
|
+
});
|
|
628
|
+
expect(events.map((event) => event.eventType)).toEqual([
|
|
629
|
+
"tool.call.started",
|
|
630
|
+
"tool.call.challenged",
|
|
631
|
+
]);
|
|
632
|
+
expect(events[1]).toMatchObject({
|
|
633
|
+
outcome: "challenged",
|
|
634
|
+
reason: { code: "AGENT_MISMATCH" },
|
|
635
|
+
actor: { kind: "public_did", did: presenter },
|
|
636
|
+
});
|
|
637
|
+
expect(events[0].actor).toEqual({ kind: "public_did", did: presenter });
|
|
638
|
+
expect(JSON.stringify(events)).not.toContain(grantAgent);
|
|
639
|
+
});
|
|
640
|
+
});
|
|
445
641
|
});
|
|
446
642
|
|
|
447
643
|
describe("canonical consent and delegation events", () => {
|
|
448
644
|
it("records consent without copying request payload fields", async () => {
|
|
449
645
|
const events: AuditTrailEventInput[] = [];
|
|
450
|
-
await recordRuntimeConsentAudit(
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
646
|
+
await recordRuntimeConsentAudit(
|
|
647
|
+
auditConfig(async (event) => {
|
|
648
|
+
events.push(event);
|
|
649
|
+
return pendingResult(event);
|
|
650
|
+
}),
|
|
651
|
+
{
|
|
652
|
+
phase: "credential_failed",
|
|
653
|
+
outcome: "failed",
|
|
654
|
+
sessionId: "raw-session-secret",
|
|
655
|
+
userDid: "did:key:zUser",
|
|
656
|
+
reasonCode: "CREDENTIAL_AUTH_FAILED",
|
|
657
|
+
consentRef: "consent-reference",
|
|
658
|
+
}
|
|
659
|
+
);
|
|
461
660
|
|
|
462
661
|
expect(events).toHaveLength(1);
|
|
463
662
|
expect(events[0]).toMatchObject({
|
|
@@ -476,15 +675,18 @@ describe("canonical consent and delegation events", () => {
|
|
|
476
675
|
|
|
477
676
|
it("records delegation issuance as a separate event family", async () => {
|
|
478
677
|
const events: AuditTrailEventInput[] = [];
|
|
479
|
-
await recordRuntimeDelegationAudit(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
678
|
+
await recordRuntimeDelegationAudit(
|
|
679
|
+
auditConfig(async (event) => {
|
|
680
|
+
events.push(event);
|
|
681
|
+
return pendingResult(event);
|
|
682
|
+
}),
|
|
683
|
+
{
|
|
684
|
+
phase: "issued",
|
|
685
|
+
outcome: "succeeded",
|
|
686
|
+
sessionId: "session-secret",
|
|
687
|
+
delegationRef: "delegation-123",
|
|
688
|
+
}
|
|
689
|
+
);
|
|
488
690
|
|
|
489
691
|
expect(events[0]).toMatchObject({
|
|
490
692
|
eventType: "delegation.issued",
|
|
@@ -90,6 +90,9 @@ describe("processToolCall Security", () => {
|
|
|
90
90
|
ttlMinutes: 30,
|
|
91
91
|
},
|
|
92
92
|
toolProtectionService: mockToolProtectionService,
|
|
93
|
+
delegation: {
|
|
94
|
+
authorization: { authorizationUrl: "https://auth.example.com/consent" },
|
|
95
|
+
} as any,
|
|
93
96
|
};
|
|
94
97
|
|
|
95
98
|
runtime = new MCPIRuntimeBase(config);
|
|
@@ -292,6 +295,87 @@ describe("processToolCall Security", () => {
|
|
|
292
295
|
expect(toolHandler).toHaveBeenCalled();
|
|
293
296
|
expect(result).toEqual({ result: "success" });
|
|
294
297
|
});
|
|
298
|
+
|
|
299
|
+
it("logs a refused identifier's shape, never the email or customer id", async () => {
|
|
300
|
+
const consoleSpy = vi
|
|
301
|
+
.spyOn(console, "error")
|
|
302
|
+
.mockImplementation(() => {});
|
|
303
|
+
mockAccessControlService.verifyDelegation = vi.fn().mockResolvedValue({
|
|
304
|
+
data: {
|
|
305
|
+
valid: true,
|
|
306
|
+
delegation_id: "test-delegation",
|
|
307
|
+
credential: {
|
|
308
|
+
scopes: ["tool:execute"],
|
|
309
|
+
user_identifier: "shopper@merchant.test",
|
|
310
|
+
user_id: "cust-1001",
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
const session = {
|
|
315
|
+
id: "test-session",
|
|
316
|
+
userDid: "did:key:zSessionUser123",
|
|
317
|
+
agentDid: "did:key:zmock123",
|
|
318
|
+
delegationToken: "valid-token",
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
await expect(
|
|
322
|
+
runtime.processToolCall(
|
|
323
|
+
"protected-tool",
|
|
324
|
+
{ arg: "value" },
|
|
325
|
+
vi.fn(),
|
|
326
|
+
session
|
|
327
|
+
)
|
|
328
|
+
).rejects.toThrow(DelegationRequiredError);
|
|
329
|
+
|
|
330
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
331
|
+
expect.stringContaining("User identifier validation FAILED"),
|
|
332
|
+
expect.objectContaining({
|
|
333
|
+
delegationIdentifierFormat: "email_or_other",
|
|
334
|
+
hasDelegationUserId: true,
|
|
335
|
+
})
|
|
336
|
+
);
|
|
337
|
+
const logged = JSON.stringify(consoleSpy.mock.calls);
|
|
338
|
+
expect(logged).not.toContain("shopper@");
|
|
339
|
+
expect(logged).not.toContain("cust-1001");
|
|
340
|
+
consoleSpy.mockRestore();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("logs an identifier without a session user by its shape, never its value", async () => {
|
|
344
|
+
const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
345
|
+
mockAccessControlService.verifyDelegation = vi.fn().mockResolvedValue({
|
|
346
|
+
data: {
|
|
347
|
+
valid: true,
|
|
348
|
+
delegation_id: "test-delegation",
|
|
349
|
+
credential: {
|
|
350
|
+
scopes: ["tool:execute"],
|
|
351
|
+
user_identifier: "shopper@merchant.test",
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
const session = {
|
|
356
|
+
id: "test-session",
|
|
357
|
+
agentDid: "did:key:zmock123",
|
|
358
|
+
delegationToken: "valid-token",
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
await runtime.processToolCall(
|
|
362
|
+
"protected-tool",
|
|
363
|
+
{ arg: "value" },
|
|
364
|
+
vi.fn().mockResolvedValue({ result: "success" }),
|
|
365
|
+
session
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
369
|
+
expect.stringContaining(
|
|
370
|
+
"Delegation has user_identifier but session missing userDid"
|
|
371
|
+
),
|
|
372
|
+
expect.objectContaining({
|
|
373
|
+
delegationIdentifierFormat: "email_or_other",
|
|
374
|
+
})
|
|
375
|
+
);
|
|
376
|
+
expect(JSON.stringify(consoleSpy.mock.calls)).not.toContain("shopper@");
|
|
377
|
+
consoleSpy.mockRestore();
|
|
378
|
+
});
|
|
295
379
|
});
|
|
296
380
|
|
|
297
381
|
describe("Missing Delegation Handling", () => {
|
|
@@ -339,7 +423,7 @@ describe("processToolCall Security", () => {
|
|
|
339
423
|
const delegationError = error as DelegationRequiredError;
|
|
340
424
|
expect(delegationError.consentUrl).toBeDefined();
|
|
341
425
|
expect(delegationError.consentUrl).toContain(
|
|
342
|
-
"
|
|
426
|
+
"https://auth.example.com/consent?"
|
|
343
427
|
);
|
|
344
428
|
expect(delegationError.resumeToken).toBeDefined();
|
|
345
429
|
expect(delegationError.resumeToken).toMatch(/^resume_/);
|
|
@@ -526,12 +610,14 @@ describe("processToolCall Security", () => {
|
|
|
526
610
|
|
|
527
611
|
const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
528
612
|
|
|
529
|
-
await expect(
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
613
|
+
await expect(
|
|
614
|
+
runtime.processToolCall(
|
|
615
|
+
"protected-tool",
|
|
616
|
+
{ arg: "value" },
|
|
617
|
+
toolHandler,
|
|
618
|
+
session
|
|
619
|
+
)
|
|
620
|
+
).rejects.toThrow(DelegationRequiredError);
|
|
535
621
|
|
|
536
622
|
expect(toolHandler).not.toHaveBeenCalled();
|
|
537
623
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
@@ -1198,12 +1284,10 @@ describe("processToolCall Security", () => {
|
|
|
1198
1284
|
const toolHandler = vi.fn().mockResolvedValue({ result: "success" });
|
|
1199
1285
|
|
|
1200
1286
|
const error = await runtime
|
|
1201
|
-
.processToolCall(
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
{ ...session, ...sessionOverrides }
|
|
1206
|
-
)
|
|
1287
|
+
.processToolCall("protected-tool", { arg: "value" }, toolHandler, {
|
|
1288
|
+
...session,
|
|
1289
|
+
...sessionOverrides,
|
|
1290
|
+
})
|
|
1207
1291
|
.then(
|
|
1208
1292
|
() => undefined,
|
|
1209
1293
|
(thrown: unknown) => thrown
|
|
@@ -45,6 +45,9 @@ describe("Provider Selection in Consent URLs", () => {
|
|
|
45
45
|
config = {
|
|
46
46
|
...mockProviders,
|
|
47
47
|
toolProtectionService: mockToolProtectionService as any,
|
|
48
|
+
delegation: {
|
|
49
|
+
authorization: { authorizationUrl: "https://auth.example.com/consent" },
|
|
50
|
+
} as any,
|
|
48
51
|
clock: new MockClockProvider(),
|
|
49
52
|
};
|
|
50
53
|
|
|
@@ -167,12 +170,7 @@ describe("Provider Selection in Consent URLs", () => {
|
|
|
167
170
|
});
|
|
168
171
|
|
|
169
172
|
try {
|
|
170
|
-
await runtime.processToolCall(
|
|
171
|
-
"apiCall",
|
|
172
|
-
{},
|
|
173
|
-
mockHandler,
|
|
174
|
-
session
|
|
175
|
-
);
|
|
173
|
+
await runtime.processToolCall("apiCall", {}, mockHandler, session);
|
|
176
174
|
expect.fail("Should have thrown DelegationRequiredError");
|
|
177
175
|
} catch (error: any) {
|
|
178
176
|
expect(error).toBeInstanceOf(DelegationRequiredError);
|