@kya-os/mcp-i-runtime 1.2.1 → 1.2.4
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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/proof/generator.d.ts +7 -1
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +10 -1
- package/dist/proof/generator.js.map +1 -1
- package/dist/services/auth-type-satisfies.d.ts +41 -0
- package/dist/services/auth-type-satisfies.d.ts.map +1 -1
- package/dist/services/auth-type-satisfies.js +53 -0
- package/dist/services/auth-type-satisfies.js.map +1 -1
- package/dist/services/proof-verifier.d.ts +29 -12
- package/dist/services/proof-verifier.d.ts.map +1 -1
- package/dist/services/proof-verifier.js +50 -17
- 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 +13 -0
- package/dist/services/tool-context-builder.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/proof/__tests__/proof-generator.test.ts +65 -0
- package/src/proof/generator.ts +12 -1
- package/src/services/__tests__/auth-type-satisfies.test.ts +62 -1
- package/src/services/__tests__/proof-verifier.test.ts +182 -0
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +75 -0
- package/src/services/auth-type-satisfies.ts +55 -0
- package/src/services/proof-verifier.ts +52 -17
- package/src/services/tool-context-builder.ts +22 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kya-os/mcp-i-runtime",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Shared product-runtime layer for the MCP-I framework (Node + Cloudflare adapters). Protocol types/values live in @kya-os/mcp (DIF).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Dylan Hobbs",
|
|
@@ -59,10 +59,10 @@
|
|
|
59
59
|
"prepublishOnly": "npm run build && node ../create-mcpi-app/scripts/validate-no-workspace.js"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@kya-os/contracts": "^1.
|
|
62
|
+
"@kya-os/contracts": "^1.12.1",
|
|
63
63
|
"@kya-os/mcp": "1.11.0",
|
|
64
64
|
"jose": "^5.6.3",
|
|
65
|
-
"json-canonicalize": "
|
|
65
|
+
"json-canonicalize": "2.0.0",
|
|
66
66
|
"zod": "^3.25.67"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -145,6 +145,8 @@ export type { ToolContextBuilderConfig } from "./services/tool-context-builder";
|
|
|
145
145
|
export {
|
|
146
146
|
authTypeSatisfies,
|
|
147
147
|
normalizeAuthType,
|
|
148
|
+
isUnsatisfiableAuthType,
|
|
149
|
+
MINTABLE_AUTH_TYPES,
|
|
148
150
|
} from "./services/auth-type-satisfies";
|
|
149
151
|
|
|
150
152
|
// OAuth Provider Registry (Phase 2)
|
|
@@ -272,6 +272,71 @@ describe("ProofGenerator", () => {
|
|
|
272
272
|
|
|
273
273
|
expect(payload.clientDid).toBe("did:key:zClientPayload");
|
|
274
274
|
});
|
|
275
|
+
|
|
276
|
+
it("encodes did, kid, and audience as literal payload fields, since verifiers check those exact property names, not the JWT-standard sub/iss/aud aliases", async () => {
|
|
277
|
+
const request: ToolRequest = {
|
|
278
|
+
method: "test-tool",
|
|
279
|
+
params: { input: "hello" },
|
|
280
|
+
};
|
|
281
|
+
const response: ToolResponse = { data: { output: "world" } };
|
|
282
|
+
|
|
283
|
+
const proof = await proofGenerator.generateProof(
|
|
284
|
+
request,
|
|
285
|
+
response,
|
|
286
|
+
mockSession
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const payload = JSON.parse(
|
|
290
|
+
Buffer.from(proof.jws.split(".")[1], "base64url").toString()
|
|
291
|
+
) as { did?: string; kid?: string; audience?: string };
|
|
292
|
+
|
|
293
|
+
expect(payload.did).toBe(mockIdentity.did);
|
|
294
|
+
expect(payload.kid).toBe(mockIdentity.kid);
|
|
295
|
+
expect(payload.audience).toBe(mockSession.audience);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("omits toolName and outcome from the JWS payload when not provided", async () => {
|
|
299
|
+
const request: ToolRequest = {
|
|
300
|
+
method: "test-tool",
|
|
301
|
+
params: { input: "hello" },
|
|
302
|
+
};
|
|
303
|
+
const response: ToolResponse = { data: { output: "world" } };
|
|
304
|
+
|
|
305
|
+
const proof = await proofGenerator.generateProof(
|
|
306
|
+
request,
|
|
307
|
+
response,
|
|
308
|
+
mockSession
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
const payload = JSON.parse(
|
|
312
|
+
Buffer.from(proof.jws.split(".")[1], "base64url").toString()
|
|
313
|
+
) as Record<string, unknown>;
|
|
314
|
+
|
|
315
|
+
expect(payload).not.toHaveProperty("toolName");
|
|
316
|
+
expect(payload).not.toHaveProperty("outcome");
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("encodes toolName as a literal payload field, and renames submissionOutcome to the literal payload key outcome, since know-that-ai's verifier checks those exact property names", async () => {
|
|
320
|
+
const request: ToolRequest = {
|
|
321
|
+
method: "test-tool",
|
|
322
|
+
params: { input: "hello" },
|
|
323
|
+
};
|
|
324
|
+
const response: ToolResponse = { data: { output: "world" } };
|
|
325
|
+
|
|
326
|
+
const proof = await proofGenerator.generateProof(
|
|
327
|
+
request,
|
|
328
|
+
response,
|
|
329
|
+
mockSession,
|
|
330
|
+
{ toolName: "get_weather", submissionOutcome: "success" }
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const payload = JSON.parse(
|
|
334
|
+
Buffer.from(proof.jws.split(".")[1], "base64url").toString()
|
|
335
|
+
) as { toolName?: string; outcome?: string };
|
|
336
|
+
|
|
337
|
+
expect(payload.toolName).toBe("get_weather");
|
|
338
|
+
expect(payload.outcome).toBe("success");
|
|
339
|
+
});
|
|
275
340
|
});
|
|
276
341
|
|
|
277
342
|
describe("Proof Verification", () => {
|
package/src/proof/generator.ts
CHANGED
|
@@ -57,6 +57,8 @@ export interface ProofOptions {
|
|
|
57
57
|
scopeId?: string;
|
|
58
58
|
delegationRef?: string;
|
|
59
59
|
clientDid?: string;
|
|
60
|
+
toolName?: string;
|
|
61
|
+
submissionOutcome?: "success" | "failure" | "denied";
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
/**
|
|
@@ -144,7 +146,11 @@ export class ProofGenerator {
|
|
|
144
146
|
* Generate Ed25519 JWS in compact format (header.payload.signature).
|
|
145
147
|
* Requirement: 5.3
|
|
146
148
|
*
|
|
147
|
-
*
|
|
149
|
+
* Includes both the JWT-standard claims (aud, sub, iss) and the literal
|
|
150
|
+
* did/kid/audience field names verifiers check for -- a verifier that
|
|
151
|
+
* compares payload fields against proof metadata by exact property name
|
|
152
|
+
* (did, kid, nonce, ts, audience) would otherwise see them all as
|
|
153
|
+
* undefined, since none of the JWT-standard aliases match those names.
|
|
148
154
|
*/
|
|
149
155
|
private async generateJWS(meta: ProofMeta): Promise<string> {
|
|
150
156
|
try {
|
|
@@ -155,6 +161,9 @@ export class ProofGenerator {
|
|
|
155
161
|
aud: meta.audience,
|
|
156
162
|
sub: meta.did,
|
|
157
163
|
iss: meta.did,
|
|
164
|
+
did: meta.did,
|
|
165
|
+
kid: this.identity.kid,
|
|
166
|
+
audience: meta.audience,
|
|
158
167
|
requestHash: meta.requestHash,
|
|
159
168
|
responseHash: meta.responseHash,
|
|
160
169
|
ts: meta.ts,
|
|
@@ -163,6 +172,8 @@ export class ProofGenerator {
|
|
|
163
172
|
...(meta.scopeId && { scopeId: meta.scopeId }),
|
|
164
173
|
...(meta.delegationRef && { delegationRef: meta.delegationRef }),
|
|
165
174
|
...(meta.clientDid && { clientDid: meta.clientDid }),
|
|
175
|
+
...(meta.toolName && { toolName: meta.toolName }),
|
|
176
|
+
...(meta.submissionOutcome && { outcome: meta.submissionOutcome }),
|
|
166
177
|
};
|
|
167
178
|
|
|
168
179
|
const jwt = await new SignJWT(payload)
|
|
@@ -17,7 +17,12 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { describe, it, expect } from "vitest";
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
authTypeSatisfies,
|
|
22
|
+
normalizeAuthType,
|
|
23
|
+
isUnsatisfiableAuthType,
|
|
24
|
+
MINTABLE_AUTH_TYPES,
|
|
25
|
+
} from "../auth-type-satisfies";
|
|
21
26
|
|
|
22
27
|
describe("normalizeAuthType", () => {
|
|
23
28
|
it("returns undefined for undefined", () => {
|
|
@@ -230,3 +235,59 @@ describe("authTypeSatisfies", () => {
|
|
|
230
235
|
}
|
|
231
236
|
});
|
|
232
237
|
});
|
|
238
|
+
|
|
239
|
+
describe("isUnsatisfiableAuthType", () => {
|
|
240
|
+
// Diagnostic predicate only — it must never influence an authorization
|
|
241
|
+
// decision. It exists so the enforcement gates can distinguish "re-auth
|
|
242
|
+
// fixes this" from "no mint can ever satisfy this, you are in a loop".
|
|
243
|
+
|
|
244
|
+
it("is false for the types a mint can actually produce", () => {
|
|
245
|
+
expect(isUnsatisfiableAuthType("password")).toBe(false);
|
|
246
|
+
expect(isUnsatisfiableAuthType("oauth2")).toBe(false);
|
|
247
|
+
expect(isUnsatisfiableAuthType("none")).toBe(false);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("folds deprecated aliases before deciding", () => {
|
|
251
|
+
// `oauth` normalizes to `oauth2`, which IS mintable — so this must not be
|
|
252
|
+
// reported as unsatisfiable.
|
|
253
|
+
expect(isUnsatisfiableAuthType("oauth")).toBe(false);
|
|
254
|
+
// `credential` normalizes to `verifiable_credential`, which is NOT.
|
|
255
|
+
expect(isUnsatisfiableAuthType("credential")).toBe(true);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("is true for declarable-but-never-minted types", () => {
|
|
259
|
+
for (const type of [
|
|
260
|
+
"mdl",
|
|
261
|
+
"idv",
|
|
262
|
+
"verifiable_credential",
|
|
263
|
+
"webauthn",
|
|
264
|
+
"siwe",
|
|
265
|
+
]) {
|
|
266
|
+
expect(isUnsatisfiableAuthType(type)).toBe(true);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("is true for apikey as a bare string, which is safe because api-key tools never reach this gate", () => {
|
|
271
|
+
// Documented explicitly so a future reader does not "fix" this: api-key
|
|
272
|
+
// tools are resolved by a separate vault branch that returns before the
|
|
273
|
+
// delegation gate, so the predicate is never consulted for them.
|
|
274
|
+
expect(isUnsatisfiableAuthType("apikey")).toBe(true);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("is false for nullish/empty input — no requirement is not an unsatisfiable requirement", () => {
|
|
278
|
+
expect(isUnsatisfiableAuthType(undefined)).toBe(false);
|
|
279
|
+
expect(isUnsatisfiableAuthType(null)).toBe(false);
|
|
280
|
+
expect(isUnsatisfiableAuthType("")).toBe(false);
|
|
281
|
+
expect(isUnsatisfiableAuthType(" ")).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("agrees with authTypeSatisfies: nothing mintable can satisfy an unsatisfiable requirement", () => {
|
|
285
|
+
// The property that makes the diagnostic true rather than a guess.
|
|
286
|
+
for (const required of ["mdl", "idv", "verifiable_credential", "webauthn", "siwe"]) {
|
|
287
|
+
expect(isUnsatisfiableAuthType(required)).toBe(true);
|
|
288
|
+
for (const minted of MINTABLE_AUTH_TYPES) {
|
|
289
|
+
expect(authTypeSatisfies(minted, required)).toBe(false);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
});
|
|
@@ -22,6 +22,10 @@ import {
|
|
|
22
22
|
ProofVerificationError,
|
|
23
23
|
PROOF_VERIFICATION_ERROR_CODES,
|
|
24
24
|
} from '../errors.js';
|
|
25
|
+
import { ProofGenerator } from '../../proof/generator.js';
|
|
26
|
+
// Cross-package relative import for a real Node crypto provider in tests —
|
|
27
|
+
// same pattern already used by proof/__tests__/proof-generator.test.ts.
|
|
28
|
+
import { NodeCryptoProvider } from '../../../../mcp-i/src/providers/node-providers';
|
|
25
29
|
|
|
26
30
|
describe('ProofVerifier Security', () => {
|
|
27
31
|
let proofVerifier: ProofVerifier;
|
|
@@ -256,6 +260,69 @@ describe('ProofVerifier Security', () => {
|
|
|
256
260
|
expect(canonical1).toBe(canonical2);
|
|
257
261
|
});
|
|
258
262
|
|
|
263
|
+
it('omits toolName and outcome from the reconstructed payload when meta carries neither', () => {
|
|
264
|
+
const meta = {
|
|
265
|
+
did: 'did:test',
|
|
266
|
+
kid: 'kid',
|
|
267
|
+
ts: 123,
|
|
268
|
+
nonce: 'nonce',
|
|
269
|
+
audience: 'aud',
|
|
270
|
+
sessionId: 'session',
|
|
271
|
+
requestHash: 'sha256:' + 'a'.repeat(64),
|
|
272
|
+
responseHash: 'sha256:' + 'b'.repeat(64),
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const canonical = JSON.parse(proofVerifier.buildCanonicalPayload(meta));
|
|
276
|
+
|
|
277
|
+
expect(canonical).not.toHaveProperty('toolName');
|
|
278
|
+
expect(canonical).not.toHaveProperty('outcome');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('reconstructs the same fields a real ProofGenerator actually signs — regression guard for generator/verifier payload drift', async () => {
|
|
282
|
+
const cryptoProvider = new NodeCryptoProvider();
|
|
283
|
+
const keyPair = await cryptoProvider.generateKeyPair();
|
|
284
|
+
const identity = {
|
|
285
|
+
did: 'did:web:example.com:agents:sync-test',
|
|
286
|
+
kid: 'did:web:example.com:agents:sync-test#key-1',
|
|
287
|
+
privateKey: keyPair.privateKey,
|
|
288
|
+
publicKey: keyPair.publicKey,
|
|
289
|
+
};
|
|
290
|
+
const generator = new ProofGenerator(identity, cryptoProvider);
|
|
291
|
+
|
|
292
|
+
const proof = await generator.generateProof(
|
|
293
|
+
{ method: 'test-tool', params: { input: 'hello' } },
|
|
294
|
+
{ data: { output: 'world' } },
|
|
295
|
+
{
|
|
296
|
+
sessionId: 'sess-sync-test',
|
|
297
|
+
audience: 'did:web:business.example',
|
|
298
|
+
nonce: 'nonce-sync-test',
|
|
299
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
300
|
+
createdAt: Math.floor(Date.now() / 1000),
|
|
301
|
+
lastActivity: Math.floor(Date.now() / 1000),
|
|
302
|
+
ttlMinutes: 30,
|
|
303
|
+
identityState: 'anonymous',
|
|
304
|
+
} as never,
|
|
305
|
+
{ toolName: 'test-tool', submissionOutcome: 'success' }
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const actualPayload = JSON.parse(
|
|
309
|
+
Buffer.from(proof.jws.split('.')[1]!, 'base64url').toString('utf8')
|
|
310
|
+
);
|
|
311
|
+
const reconstructedPayload = JSON.parse(
|
|
312
|
+
proofVerifier.buildCanonicalPayload(proof.meta)
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
// Field-level equality (not byte/order equality — canonicalization
|
|
316
|
+
// sorts keys, which the signed JWS payload does not). If the
|
|
317
|
+
// reconstruction ever drifts from what generator.ts actually signs,
|
|
318
|
+
// this fails long before it reaches a live 403. Covers toolName and
|
|
319
|
+
// the submissionOutcome -> outcome rename too, since those are the
|
|
320
|
+
// two newest fields most likely to drift out of lockstep.
|
|
321
|
+
expect(reconstructedPayload).toEqual(actualPayload);
|
|
322
|
+
expect(reconstructedPayload.toolName).toBe('test-tool');
|
|
323
|
+
expect(reconstructedPayload.outcome).toBe('success');
|
|
324
|
+
});
|
|
325
|
+
|
|
259
326
|
it('should handle detached JWS reconstruction', async () => {
|
|
260
327
|
const header = { alg: 'EdDSA', kid: 'did:key:z123#key-1' };
|
|
261
328
|
const headerB64 = btoa(JSON.stringify(header))
|
|
@@ -419,6 +486,121 @@ describe('ProofVerifier Security', () => {
|
|
|
419
486
|
});
|
|
420
487
|
});
|
|
421
488
|
|
|
489
|
+
describe('End-to-end verifyProof() with real crypto — regression guard for DIF payload drift', () => {
|
|
490
|
+
// Uses a REAL NodeCryptoProvider (unlike the mocked cryptoProvider used
|
|
491
|
+
// elsewhere in this file, whose verify() always returns true and so
|
|
492
|
+
// could never catch a signature mismatch). This is the only way to
|
|
493
|
+
// prove verifyProof() itself -- not just buildCanonicalPayload() --
|
|
494
|
+
// still accepts a proof signed with the literal did/kid/audience/
|
|
495
|
+
// toolName/outcome fields once the DIF-delegated verifier is involved.
|
|
496
|
+
function makeRealVerifier() {
|
|
497
|
+
const cryptoProvider = new NodeCryptoProvider();
|
|
498
|
+
const clockProvider: ClockProvider = {
|
|
499
|
+
now: () => Date.now(),
|
|
500
|
+
isWithinSkew: () => true,
|
|
501
|
+
hasExpired: () => false,
|
|
502
|
+
calculateExpiry: (ttlSeconds: number) => Date.now() + ttlSeconds * 1000,
|
|
503
|
+
format: (d: number) => new Date(d).toISOString(),
|
|
504
|
+
};
|
|
505
|
+
const nonceCacheProvider: NonceCacheProvider = {
|
|
506
|
+
has: async () => false,
|
|
507
|
+
add: async () => {},
|
|
508
|
+
cleanup: async () => {},
|
|
509
|
+
destroy: async () => {},
|
|
510
|
+
};
|
|
511
|
+
const fetchProvider: FetchProvider = {
|
|
512
|
+
resolveDID: async () => null,
|
|
513
|
+
fetchStatusList: async () => null,
|
|
514
|
+
fetchDelegationChain: async () => null,
|
|
515
|
+
fetch: async () => {
|
|
516
|
+
throw new Error('not used');
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
const verifier = new ProofVerifier({
|
|
520
|
+
cryptoProvider,
|
|
521
|
+
clockProvider,
|
|
522
|
+
nonceCacheProvider,
|
|
523
|
+
fetchProvider,
|
|
524
|
+
});
|
|
525
|
+
return { cryptoProvider, verifier };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function toJwk(publicKeyBase64: string, kid: string): Ed25519JWK {
|
|
529
|
+
const bytes = Buffer.from(publicKeyBase64, 'base64');
|
|
530
|
+
const x = bytes
|
|
531
|
+
.toString('base64')
|
|
532
|
+
.replace(/\+/g, '-')
|
|
533
|
+
.replace(/\//g, '_')
|
|
534
|
+
.replace(/=/g, '');
|
|
535
|
+
return { kty: 'OKP', crv: 'Ed25519', x, kid };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
it('accepts a proof signed with the literal did/kid/audience/toolName/outcome fields', async () => {
|
|
539
|
+
const { cryptoProvider, verifier } = makeRealVerifier();
|
|
540
|
+
const keyPair = await cryptoProvider.generateKeyPair();
|
|
541
|
+
const identity = {
|
|
542
|
+
did: 'did:web:example.com:agents:e2e-test',
|
|
543
|
+
kid: 'did:web:example.com:agents:e2e-test#key-1',
|
|
544
|
+
privateKey: keyPair.privateKey,
|
|
545
|
+
publicKey: keyPair.publicKey,
|
|
546
|
+
};
|
|
547
|
+
const generator = new ProofGenerator(identity, cryptoProvider);
|
|
548
|
+
|
|
549
|
+
const proof = await generator.generateProof(
|
|
550
|
+
{ method: 'test-tool', params: { input: 'hello' } },
|
|
551
|
+
{ data: { output: 'world' } },
|
|
552
|
+
{
|
|
553
|
+
sessionId: 'sess-e2e-test',
|
|
554
|
+
audience: 'did:web:business.example',
|
|
555
|
+
nonce: 'nonce-e2e-test',
|
|
556
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
557
|
+
createdAt: Math.floor(Date.now() / 1000),
|
|
558
|
+
lastActivity: Math.floor(Date.now() / 1000),
|
|
559
|
+
ttlMinutes: 30,
|
|
560
|
+
identityState: 'anonymous',
|
|
561
|
+
} as never,
|
|
562
|
+
{ toolName: 'get_weather', submissionOutcome: 'success' }
|
|
563
|
+
);
|
|
564
|
+
|
|
565
|
+
const publicKeyJwk = toJwk(identity.publicKey, identity.kid);
|
|
566
|
+
const result = await verifier.verifyProof(proof, publicKeyJwk);
|
|
567
|
+
|
|
568
|
+
expect(result.valid).toBe(true);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it('also accepts a proof signed WITHOUT toolName/outcome (PR651-era fields only)', async () => {
|
|
572
|
+
const { cryptoProvider, verifier } = makeRealVerifier();
|
|
573
|
+
const keyPair = await cryptoProvider.generateKeyPair();
|
|
574
|
+
const identity = {
|
|
575
|
+
did: 'did:web:example.com:agents:e2e-test-2',
|
|
576
|
+
kid: 'did:web:example.com:agents:e2e-test-2#key-1',
|
|
577
|
+
privateKey: keyPair.privateKey,
|
|
578
|
+
publicKey: keyPair.publicKey,
|
|
579
|
+
};
|
|
580
|
+
const generator = new ProofGenerator(identity, cryptoProvider);
|
|
581
|
+
|
|
582
|
+
const proof = await generator.generateProof(
|
|
583
|
+
{ method: 'test-tool', params: { input: 'hello' } },
|
|
584
|
+
{ data: { output: 'world' } },
|
|
585
|
+
{
|
|
586
|
+
sessionId: 'sess-e2e-test-2',
|
|
587
|
+
audience: 'did:web:business.example',
|
|
588
|
+
nonce: 'nonce-e2e-test-2',
|
|
589
|
+
timestamp: Math.floor(Date.now() / 1000),
|
|
590
|
+
createdAt: Math.floor(Date.now() / 1000),
|
|
591
|
+
lastActivity: Math.floor(Date.now() / 1000),
|
|
592
|
+
ttlMinutes: 30,
|
|
593
|
+
identityState: 'anonymous',
|
|
594
|
+
} as never
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
const publicKeyJwk = toJwk(identity.publicKey, identity.kid);
|
|
598
|
+
const result = await verifier.verifyProof(proof, publicKeyJwk);
|
|
599
|
+
|
|
600
|
+
expect(result.valid).toBe(true);
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
422
604
|
describe('fetchPublicKeyFromDID', () => {
|
|
423
605
|
it('should fetch public key from DID document', async () => {
|
|
424
606
|
const jwk = await proofVerifier.fetchPublicKeyFromDID('did:key:z123', 'key-1');
|
|
@@ -164,4 +164,79 @@ describe("ToolContextBuilder — delegation auth-type enforcement (B3)", () => {
|
|
|
164
164
|
)
|
|
165
165
|
).rejects.toThrow(OAuthRequiredError);
|
|
166
166
|
});
|
|
167
|
+
|
|
168
|
+
describe("unsatisfiable-requirement diagnostic", () => {
|
|
169
|
+
// A tool whose required type no mint can produce re-challenges forever.
|
|
170
|
+
// The gate cannot fix that (strict equality is the locked decision), but it
|
|
171
|
+
// must SAY so — otherwise the operator sees an inexplicable auth loop.
|
|
172
|
+
|
|
173
|
+
const mdlTool: ToolProtection = {
|
|
174
|
+
requiresDelegation: true,
|
|
175
|
+
requiredScopes: ["read:user"],
|
|
176
|
+
authorization: { type: "mdl" } as ToolProtection["authorization"],
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
function buildWithLogger(logger: (msg: string, data?: unknown) => void) {
|
|
180
|
+
return new ToolContextBuilder({
|
|
181
|
+
tokenResolver: mockTokenResolver,
|
|
182
|
+
configService: mockConfigService,
|
|
183
|
+
providerResolver: mockProviderResolver,
|
|
184
|
+
projectId: "proj-123",
|
|
185
|
+
logger,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
it("logs a MISCONFIGURATION line when the required type can never be minted", async () => {
|
|
190
|
+
const lines: string[] = [];
|
|
191
|
+
const builderWithLog = buildWithLogger((msg) => lines.push(msg));
|
|
192
|
+
|
|
193
|
+
await expect(
|
|
194
|
+
builderWithLog.buildContext(
|
|
195
|
+
"gated-tool",
|
|
196
|
+
"did:key:z6MkUser",
|
|
197
|
+
"session-123",
|
|
198
|
+
"delegation-token-oauth",
|
|
199
|
+
mdlTool,
|
|
200
|
+
"oauth2"
|
|
201
|
+
)
|
|
202
|
+
).rejects.toThrow(OAuthRequiredError);
|
|
203
|
+
|
|
204
|
+
expect(lines.some((l) => l.includes("MISCONFIGURATION"))).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("does NOT log it for an ordinary mismatch that re-authenticating can fix", async () => {
|
|
208
|
+
const lines: string[] = [];
|
|
209
|
+
const builderWithLog = buildWithLogger((msg) => lines.push(msg));
|
|
210
|
+
|
|
211
|
+
// oauth2-gated tool presented a `none` delegation: a real mismatch, but
|
|
212
|
+
// the user CAN resolve it by authenticating via OAuth.
|
|
213
|
+
await expect(
|
|
214
|
+
builderWithLog.buildContext(
|
|
215
|
+
"gated-tool",
|
|
216
|
+
"did:key:z6MkUser",
|
|
217
|
+
"session-123",
|
|
218
|
+
"delegation-token-anon",
|
|
219
|
+
oauthTool,
|
|
220
|
+
"none"
|
|
221
|
+
)
|
|
222
|
+
).rejects.toThrow(OAuthRequiredError);
|
|
223
|
+
|
|
224
|
+
expect(lines.some((l) => l.includes("MISCONFIGURATION"))).toBe(false);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("still REJECTS — the diagnostic must not relax the decision", async () => {
|
|
228
|
+
// Guards against a future "unsatisfiable, so let it through" "fix",
|
|
229
|
+
// which would reopen the bypass the gate exists to close.
|
|
230
|
+
await expect(
|
|
231
|
+
buildWithLogger(() => {}).buildContext(
|
|
232
|
+
"gated-tool",
|
|
233
|
+
"did:key:z6MkUser",
|
|
234
|
+
"session-123",
|
|
235
|
+
"delegation-token-oauth",
|
|
236
|
+
mdlTool,
|
|
237
|
+
"oauth2"
|
|
238
|
+
)
|
|
239
|
+
).rejects.toThrow(OAuthRequiredError);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
167
242
|
});
|
|
@@ -132,3 +132,58 @@ export function authTypeSatisfies(
|
|
|
132
132
|
|
|
133
133
|
return normalizedDelegationType === normalizedRequiredType;
|
|
134
134
|
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The normalized authorization types the mint side can actually produce today.
|
|
138
|
+
*
|
|
139
|
+
* Every delegation is minted with one of these three:
|
|
140
|
+
* - `password` — the credential clickwrap, which rewrites `provider_type` to
|
|
141
|
+
* PASSWORD for EVERY credential provider (`consent.service.ts`);
|
|
142
|
+
* - `oauth2` — the OAuth callback, which persists it as a literal;
|
|
143
|
+
* - `none` — the pure consent-only branch.
|
|
144
|
+
*
|
|
145
|
+
* This is deliberately about what is MINTED, not what is declarable: the
|
|
146
|
+
* `AUTHORIZATION_TYPES` contract also allows `mdl`, `idv`,
|
|
147
|
+
* `verifiable_credential`, `webauthn` and `siwe`.
|
|
148
|
+
*/
|
|
149
|
+
export const MINTABLE_AUTH_TYPES: ReadonlySet<string> = new Set([
|
|
150
|
+
"password",
|
|
151
|
+
"oauth2",
|
|
152
|
+
"none",
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Whether a tool's required authorization type is one that no mint can ever
|
|
157
|
+
* produce — meaning the delegation gate can never be satisfied and the tool
|
|
158
|
+
* will re-challenge forever rather than once.
|
|
159
|
+
*
|
|
160
|
+
* Under strict normalized equality (the locked decision), a requirement
|
|
161
|
+
* outside {@link MINTABLE_AUTH_TYPES} is unsatisfiable by construction: no
|
|
162
|
+
* delegation carrying such a type is ever written, so `authTypeSatisfies`
|
|
163
|
+
* cannot return true for it. Before the auth-type gate existed these tools
|
|
164
|
+
* authorized on mere delegation presence, so this is a behaviour change that
|
|
165
|
+
* presents to a user as an inexplicable auth loop.
|
|
166
|
+
*
|
|
167
|
+
* This exists purely so the enforcement gates can SAY so in their logs. It
|
|
168
|
+
* makes no authorization decision and must never be used to relax one —
|
|
169
|
+
* treating "unsatisfiable" as "allow" would reopen exactly the bypass the
|
|
170
|
+
* gate closes.
|
|
171
|
+
*
|
|
172
|
+
* `apikey` is intentionally NOT special-cased here: api-key tools are
|
|
173
|
+
* resolved by a separate vault branch that returns before either delegation
|
|
174
|
+
* gate, so this predicate is never consulted for them.
|
|
175
|
+
*
|
|
176
|
+
* @param requiredType - The tool's required authorization type.
|
|
177
|
+
* @returns `true` if the requirement can never be met by any minted
|
|
178
|
+
* delegation. Nullish/unknown input returns `false` — absence of a
|
|
179
|
+
* requirement is not an unsatisfiable requirement.
|
|
180
|
+
*/
|
|
181
|
+
export function isUnsatisfiableAuthType(
|
|
182
|
+
requiredType: string | undefined | null
|
|
183
|
+
): boolean {
|
|
184
|
+
const normalized = normalizeAuthType(requiredType);
|
|
185
|
+
if (normalized === undefined) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return !MINTABLE_AUTH_TYPES.has(normalized);
|
|
189
|
+
}
|
|
@@ -7,20 +7,26 @@
|
|
|
7
7
|
* C2 (#2916): the verification pipeline is re-homed onto the DIF
|
|
8
8
|
* `@kya-os/mcp` `ProofVerifier` — this class is now a thin façade that lazily
|
|
9
9
|
* composes the DIF verifier (via the `loadDifCore()` ESM-bridge seam) and
|
|
10
|
-
* delegates
|
|
11
|
-
* duplicate of
|
|
10
|
+
* delegates the structure/nonce-replay/timestamp-skew/signature pipeline to
|
|
11
|
+
* it, instead of carrying a duplicate of that logic. The public surface
|
|
12
12
|
* (`verifyProof`, `verifyProofDetached`, `buildCanonicalPayload`,
|
|
13
13
|
* `fetchPublicKeyFromDID`, the result/config shapes) is preserved so callers —
|
|
14
14
|
* `runtime/base.ts` and the runtime subclasses that inject a `ProofVerifier` —
|
|
15
15
|
* are unchanged.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
17
|
+
* `verifyProof` calls DIF's `verifyProofDetached` (not DIF's `verifyProof`),
|
|
18
|
+
* passing a canonical payload built by this façade's own
|
|
19
|
+
* `buildCanonicalPayload` rather than letting DIF reconstruct it internally.
|
|
20
|
+
* `@kya-os/mcp` is a separately-versioned, externally-published dependency,
|
|
21
|
+
* so its own reconstruction does not know about fields this repo's signers
|
|
22
|
+
* add to the payload (see `buildCanonicalPayload`'s doc comment) — supplying
|
|
23
|
+
* the payload ourselves keeps the crypto/replay/skew pipeline delegated to
|
|
24
|
+
* DIF while staying correct regardless of DIF's release cadence. One
|
|
25
|
+
* consequence: the content-binding fail-closed check (DIF's `verifyProof`
|
|
26
|
+
* third `expected` argument) is not reachable through `verifyProofDetached`,
|
|
27
|
+
* but this façade never passed that argument through anyway. The one
|
|
28
|
+
* pre-C2 default that differs (local timestamp skew defaulted to 300s vs
|
|
29
|
+
* DIF's 120s) is preserved explicitly when composing the DIF verifier.
|
|
24
30
|
*/
|
|
25
31
|
|
|
26
32
|
import { type Ed25519JWK } from "./crypto.service.js";
|
|
@@ -29,7 +35,6 @@ import { ClockProvider } from "../providers/base.js";
|
|
|
29
35
|
import { NonceCacheProvider } from "../providers/base.js";
|
|
30
36
|
import { FetchProvider } from "../providers/base.js";
|
|
31
37
|
import { type DetachedProof } from "@kya-os/contracts/proof";
|
|
32
|
-
import { canonicalize } from "json-canonicalize";
|
|
33
38
|
import {
|
|
34
39
|
ProofVerificationError,
|
|
35
40
|
PROOF_VERIFICATION_ERROR_CODES,
|
|
@@ -105,9 +110,20 @@ export class ProofVerifier {
|
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
/**
|
|
108
|
-
* Verify a DetachedProof.
|
|
109
|
-
*
|
|
110
|
-
*
|
|
113
|
+
* Verify a DetachedProof. Checks the JWS signature, nonce replay, and
|
|
114
|
+
* timestamp skew via the DIF `ProofVerifier` pipeline.
|
|
115
|
+
*
|
|
116
|
+
* Deliberately calls the DIF verifier's `verifyProofDetached()` with a
|
|
117
|
+
* canonical payload built by this class's own `buildCanonicalPayload()`,
|
|
118
|
+
* rather than the DIF verifier's `verifyProof()` (which would reconstruct
|
|
119
|
+
* the payload using ITS OWN internal builder). `@kya-os/mcp` is a
|
|
120
|
+
* separately-versioned, externally-published dependency; its internal
|
|
121
|
+
* builder does not know about the literal did/kid/audience/toolName/
|
|
122
|
+
* outcome fields this repo's signers add to the payload, so proofs signed
|
|
123
|
+
* here would fail `@kya-os/mcp`'s own reconstruction even though the
|
|
124
|
+
* signature is genuinely valid. Supplying the payload ourselves keeps the
|
|
125
|
+
* nonce-replay/timestamp-skew/signature pipeline delegated to DIF while
|
|
126
|
+
* sidestepping its stale reconstruction.
|
|
111
127
|
*
|
|
112
128
|
* @param proof - The proof to verify
|
|
113
129
|
* @param publicKeyJwk - Ed25519 public key in JWK format (from DID document)
|
|
@@ -118,8 +134,10 @@ export class ProofVerifier {
|
|
|
118
134
|
publicKeyJwk: Ed25519JWK
|
|
119
135
|
): Promise<ProofVerificationResult> {
|
|
120
136
|
const verifier = await this.getDifVerifier();
|
|
121
|
-
|
|
137
|
+
const canonicalPayload = this.buildCanonicalPayload(proof.meta);
|
|
138
|
+
return verifier.verifyProofDetached(
|
|
122
139
|
proof,
|
|
140
|
+
canonicalPayload,
|
|
123
141
|
publicKeyJwk
|
|
124
142
|
) as Promise<ProofVerificationResult>;
|
|
125
143
|
}
|
|
@@ -275,6 +293,14 @@ export class ProofVerifier {
|
|
|
275
293
|
sub: meta.did, // Subject (agent DID)
|
|
276
294
|
iss: meta.did, // Issuer (agent DID - self-issued)
|
|
277
295
|
|
|
296
|
+
// Literal field names some verifiers (e.g. knowthat.ai) check by
|
|
297
|
+
// exact property, rather than the JWT-standard aliases above -- must
|
|
298
|
+
// stay in lockstep with generator.ts's payload, or reconstruction
|
|
299
|
+
// here will byte-diverge from what was actually signed.
|
|
300
|
+
did: meta.did,
|
|
301
|
+
kid: meta.kid,
|
|
302
|
+
audience: meta.audience,
|
|
303
|
+
|
|
278
304
|
// Custom MCP-I proof claims
|
|
279
305
|
requestHash: meta.requestHash,
|
|
280
306
|
responseHash: meta.responseHash,
|
|
@@ -286,10 +312,19 @@ export class ProofVerifier {
|
|
|
286
312
|
...(meta.scopeId && { scopeId: meta.scopeId }),
|
|
287
313
|
...(meta.delegationRef && { delegationRef: meta.delegationRef }),
|
|
288
314
|
...(meta.clientDid && { clientDid: meta.clientDid }),
|
|
315
|
+
...(meta.toolName && { toolName: meta.toolName }),
|
|
316
|
+
...(meta.submissionOutcome && { outcome: meta.submissionOutcome }),
|
|
289
317
|
};
|
|
290
318
|
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
|
|
319
|
+
// generator.ts signs this payload via jose's SignJWT, which serializes
|
|
320
|
+
// with a plain JSON.stringify() in object-insertion order (see
|
|
321
|
+
// JWTClaimsBuilder.data() in the jose source) -- NOT RFC 8785 JCS.
|
|
322
|
+
// canonicalize() sorts keys alphabetically, which byte-diverges from
|
|
323
|
+
// what was actually signed even when every field VALUE matches (this
|
|
324
|
+
// silently broke DIF-delegated verification: see the regression tests
|
|
325
|
+
// above). The object literal above already mirrors generator.ts's
|
|
326
|
+
// field order exactly, so a plain JSON.stringify() here reproduces the
|
|
327
|
+
// exact bytes that were signed.
|
|
328
|
+
return JSON.stringify(payload);
|
|
294
329
|
}
|
|
295
330
|
}
|