@kya-os/mcp-i-runtime 1.2.2 → 1.2.5

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.
@@ -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 `verifyProof` / `verifyProofDetached` to it, instead of carrying a
11
- * duplicate of the verification logic. The public surface
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
- * Behavior parity: DIF's `verifyProof` runs the identical pipeline (structure →
18
- * nonce replay → timestamp skew → canonical-payload reconstruction → JWS
19
- * signature) for the two-argument call this façade makes; the content-binding
20
- * fail-closed check only runs when an `expected` request/response is supplied,
21
- * which this façade does not pass. The one pre-C2 default that differs (local
22
- * timestamp skew defaulted to 300s vs DIF's 120s) is preserved explicitly when
23
- * composing the DIF verifier.
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. Delegates to the DIF `ProofVerifier`, which
109
- * reconstructs the canonical payload from `proof.meta` and checks the JWS
110
- * signature, nonce replay, and timestamp skew.
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
- return verifier.verifyProof(
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
- // Canonicalize the reconstructed payload using the same function as proof generation
292
- // CRITICAL: Must use json-canonicalize canonicalize() to match proof.ts exactly
293
- return canonicalize(payload);
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
  }