@kya-os/mcp-i-runtime 1.3.2 → 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 +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -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 +16 -5
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +13 -3
- 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 +12 -0
- package/dist/runtime/audit.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/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/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/eslint.config.js +20 -0
- package/package.json +8 -2
- package/src/__tests__/runtime/audit.test.ts +108 -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 +25 -0
- package/src/proof/__tests__/proof-generator.test.ts +48 -4
- package/src/proof/companion.ts +35 -0
- package/src/proof/generator.ts +35 -8
- package/src/proof/index.ts +3 -0
- package/src/runtime/audit.ts +15 -3
- package/src/runtime/dif-core.ts +16 -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 +8 -5
- package/src/services/__tests__/request-holder.test.ts +210 -0
- 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/request-holder.ts +146 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resume tokens: what a needs-authorization answer carries for the client
|
|
3
|
+
* to resume with once the user has authorized, and the store that keeps
|
|
4
|
+
* them. Platform-agnostic, like ./handshake, which mints them.
|
|
5
|
+
*
|
|
6
|
+
* @module auth/resume-token
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resume token store interface
|
|
11
|
+
*/
|
|
12
|
+
export interface ResumeTokenStore {
|
|
13
|
+
create(
|
|
14
|
+
agentDid: string,
|
|
15
|
+
scopes: string[],
|
|
16
|
+
metadata?: Record<string, unknown>
|
|
17
|
+
): Promise<string>;
|
|
18
|
+
|
|
19
|
+
get(token: string): Promise<{
|
|
20
|
+
agentDid: string;
|
|
21
|
+
scopes: string[];
|
|
22
|
+
createdAt: number;
|
|
23
|
+
expiresAt: number;
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
} | null>;
|
|
26
|
+
|
|
27
|
+
fulfill(token: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Mint a capability-bearing resume token with CSPRNG entropy.
|
|
32
|
+
*
|
|
33
|
+
* Format: `${prefix}_${hex(16 random bytes)}` (128 bits). Uses Web Crypto
|
|
34
|
+
* `getRandomValues` (Node 18+, Cloudflare Workers, browsers), falling back to
|
|
35
|
+
* `randomUUID`: both CSPRNG. Replaces the prior `Date.now()_Math.random()`
|
|
36
|
+
* form, which leaked creation time and had weak uniqueness/guessability for a
|
|
37
|
+
* token that grants delegated access. No Node-specific imports (Buffer-free),
|
|
38
|
+
* so it stays safe on every fetch-capable runtime.
|
|
39
|
+
*/
|
|
40
|
+
export function generateResumeToken(prefix = "rt"): string {
|
|
41
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
42
|
+
const bytes = new Uint8Array(16);
|
|
43
|
+
crypto.getRandomValues(bytes);
|
|
44
|
+
let hex = "";
|
|
45
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
46
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
47
|
+
}
|
|
48
|
+
return `${prefix}_${hex}`;
|
|
49
|
+
}
|
|
50
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
51
|
+
return `${prefix}_${crypto.randomUUID().replace(/-/g, "")}`;
|
|
52
|
+
}
|
|
53
|
+
throw new Error("CSPRNG unavailable: cannot mint resume token");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Simple in-memory resume token store (for testing/development).
|
|
58
|
+
* Not suitable for multi-instance deployments.
|
|
59
|
+
*/
|
|
60
|
+
export class MemoryResumeTokenStore implements ResumeTokenStore {
|
|
61
|
+
private tokens = new Map<
|
|
62
|
+
string,
|
|
63
|
+
{
|
|
64
|
+
agentDid: string;
|
|
65
|
+
scopes: string[];
|
|
66
|
+
createdAt: number;
|
|
67
|
+
expiresAt: number;
|
|
68
|
+
metadata?: Record<string, unknown>;
|
|
69
|
+
fulfilled: boolean;
|
|
70
|
+
}
|
|
71
|
+
>();
|
|
72
|
+
private ttl: number;
|
|
73
|
+
|
|
74
|
+
constructor(ttlMs = 600_000) {
|
|
75
|
+
this.ttl = ttlMs;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async create(
|
|
79
|
+
agentDid: string,
|
|
80
|
+
scopes: string[],
|
|
81
|
+
metadata?: Record<string, unknown>
|
|
82
|
+
): Promise<string> {
|
|
83
|
+
const token = generateResumeToken("rt");
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
|
|
86
|
+
this.tokens.set(token, {
|
|
87
|
+
agentDid,
|
|
88
|
+
scopes,
|
|
89
|
+
createdAt: now,
|
|
90
|
+
expiresAt: now + this.ttl,
|
|
91
|
+
metadata,
|
|
92
|
+
fulfilled: false,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return token;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async get(token: string): Promise<{
|
|
99
|
+
agentDid: string;
|
|
100
|
+
scopes: string[];
|
|
101
|
+
createdAt: number;
|
|
102
|
+
expiresAt: number;
|
|
103
|
+
metadata?: Record<string, unknown>;
|
|
104
|
+
} | null> {
|
|
105
|
+
const data = this.tokens.get(token);
|
|
106
|
+
if (!data) return null;
|
|
107
|
+
|
|
108
|
+
if (Date.now() > data.expiresAt) {
|
|
109
|
+
this.tokens.delete(token);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (data.fulfilled) return null;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
agentDid: data.agentDid,
|
|
117
|
+
scopes: data.scopes,
|
|
118
|
+
createdAt: data.createdAt,
|
|
119
|
+
expiresAt: data.expiresAt,
|
|
120
|
+
metadata: data.metadata,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async fulfill(token: string): Promise<void> {
|
|
125
|
+
const data = this.tokens.get(token);
|
|
126
|
+
if (data) {
|
|
127
|
+
data.fulfilled = true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
clear(): void {
|
|
132
|
+
this.tokens.clear();
|
|
133
|
+
}
|
|
134
|
+
}
|
package/src/auth/types.ts
CHANGED
|
@@ -52,6 +52,12 @@ export interface VerifyDelegationResult {
|
|
|
52
52
|
/** Reason for invalid result */
|
|
53
53
|
reason?: string;
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The code the verifier refused with, when it names one (Checkpoint's
|
|
57
|
+
* `error.code`, such as `holder_proof_required`).
|
|
58
|
+
*/
|
|
59
|
+
code?: string;
|
|
60
|
+
|
|
55
61
|
/** Whether result came from cache */
|
|
56
62
|
cached?: boolean;
|
|
57
63
|
}
|
|
@@ -61,8 +67,20 @@ export interface VerifyDelegationResult {
|
|
|
61
67
|
* Concrete implementations live in packages/mcp-i (CheckpointAPIDelegationVerifier, etc.).
|
|
62
68
|
*/
|
|
63
69
|
export interface DelegationVerifier {
|
|
64
|
-
verify(
|
|
65
|
-
|
|
70
|
+
verify(agentDid: string, scopes: string[]): Promise<VerifyDelegationResult>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Verify the delegation credential a call presents, for the holder that
|
|
74
|
+
* call proved it holds the key of. Valid only when the credential is
|
|
75
|
+
* authority this verifier accepts, names `holderDid` as its subject and
|
|
76
|
+
* grants every scope in `scopes`. Nothing is looked up for the agent.
|
|
77
|
+
*
|
|
78
|
+
* Optional so existing verifiers still compile; verifyOrHints refuses a
|
|
79
|
+
* presented delegation a verifier without it cannot check.
|
|
80
|
+
*/
|
|
81
|
+
verifyPresented?(
|
|
82
|
+
credential: string,
|
|
83
|
+
holderDid: string,
|
|
66
84
|
scopes: string[]
|
|
67
85
|
): Promise<VerifyDelegationResult>;
|
|
68
86
|
}
|
package/src/index.ts
CHANGED
|
@@ -108,6 +108,27 @@ export type { Ed25519JWK, ParsedJWS } from "./services/crypto.service";
|
|
|
108
108
|
// Proof Verifier Service
|
|
109
109
|
export { ProofVerifier } from "./services/proof-verifier";
|
|
110
110
|
|
|
111
|
+
// Request holder: the DID whose key a tools/call proves its caller holds
|
|
112
|
+
export {
|
|
113
|
+
didWebHolderKeys,
|
|
114
|
+
withDidWebHolders,
|
|
115
|
+
type DidWebHolderOptions,
|
|
116
|
+
type HolderKeySeams,
|
|
117
|
+
type ProofPublicJwk,
|
|
118
|
+
} from "./services/holder-keys";
|
|
119
|
+
export { claimsFirstGuard } from "./services/claims-first-guard";
|
|
120
|
+
export {
|
|
121
|
+
createRequestHolderVerifier,
|
|
122
|
+
resolveDidKeyHolder,
|
|
123
|
+
REQUEST_PROOF_SKEW_SEC,
|
|
124
|
+
type HolderRequest,
|
|
125
|
+
type RequestHolderOptions,
|
|
126
|
+
type RequestHolderRefusal,
|
|
127
|
+
type RequestProofOptions,
|
|
128
|
+
type RequestHolderResult,
|
|
129
|
+
type RequestHolderVerifier,
|
|
130
|
+
} from "./services/request-holder";
|
|
131
|
+
|
|
111
132
|
export type {
|
|
112
133
|
ProofVerificationResult,
|
|
113
134
|
ProofVerifierConfig,
|
|
@@ -370,6 +391,9 @@ export {
|
|
|
370
391
|
type ToolRequest,
|
|
371
392
|
type ToolResponse,
|
|
372
393
|
type ProofOptions,
|
|
394
|
+
type ProofCompanion,
|
|
395
|
+
type GeneratedProof,
|
|
396
|
+
presentValues,
|
|
373
397
|
} from "./proof";
|
|
374
398
|
|
|
375
399
|
// Session management (platform-agnostic protocol reference â Phase 2)
|
|
@@ -388,6 +412,7 @@ export {
|
|
|
388
412
|
MemoryResumeTokenStore,
|
|
389
413
|
generateResumeToken,
|
|
390
414
|
type AuthHandshakeConfig,
|
|
415
|
+
type VerifyOrHintsOptions,
|
|
391
416
|
type VerifyOrHintsResult,
|
|
392
417
|
type AgentReputation,
|
|
393
418
|
type ResumeTokenStore,
|
|
@@ -179,13 +179,31 @@ describe("ProofGenerator", () => {
|
|
|
179
179
|
expect(proof.meta.did).toBe(mockIdentity.did);
|
|
180
180
|
expect(proof.meta.kid).toBe(mockIdentity.kid);
|
|
181
181
|
expect(proof.meta.ts).toBeTypeOf("number");
|
|
182
|
-
expect(proof.meta.nonce).
|
|
182
|
+
expect(proof.meta.nonce).toMatch(/^[A-Za-z0-9_-]{22}$/);
|
|
183
183
|
expect(proof.meta.audience).toBe(mockSession.audience);
|
|
184
184
|
expect(proof.meta.sessionId).toBe(mockSession.sessionId);
|
|
185
185
|
expect(proof.meta.requestHash).toBeTruthy();
|
|
186
186
|
expect(proof.meta.responseHash).toBeTruthy();
|
|
187
187
|
});
|
|
188
188
|
|
|
189
|
+
it("signs each proof in a session with a nonce of its own", async () => {
|
|
190
|
+
// A replay check (Checkpoint's, KTA's, @kya-os/mcp's ProofVerifier)
|
|
191
|
+
// spends a proof's nonce once per signer, so a nonce a session shares
|
|
192
|
+
// would refuse every proof after the session's first.
|
|
193
|
+
const request: ToolRequest = { method: "test-tool", params: {} };
|
|
194
|
+
const response: ToolResponse = { data: "ok" };
|
|
195
|
+
|
|
196
|
+
const proofs = await Promise.all(
|
|
197
|
+
[1, 2, 3].map(() =>
|
|
198
|
+
proofGenerator.generateProof(request, response, mockSession)
|
|
199
|
+
)
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const nonces = proofs.map((proof) => proof.meta.nonce);
|
|
203
|
+
expect(new Set(nonces).size).toBe(3);
|
|
204
|
+
expect(nonces).not.toContain(mockSession.nonce);
|
|
205
|
+
});
|
|
206
|
+
|
|
189
207
|
it("should include optional fields when provided", async () => {
|
|
190
208
|
const request: ToolRequest = {
|
|
191
209
|
method: "test-tool",
|
|
@@ -333,7 +351,7 @@ describe("ProofGenerator", () => {
|
|
|
333
351
|
expect(payload).not.toHaveProperty("outcome");
|
|
334
352
|
});
|
|
335
353
|
|
|
336
|
-
it("signs the authorization outcome and
|
|
354
|
+
it("signs the authorization outcome and returns toolName/submissionOutcome beside the proof, never on meta", async () => {
|
|
337
355
|
const request: ToolRequest = {
|
|
338
356
|
method: "test-tool",
|
|
339
357
|
params: { input: "hello" },
|
|
@@ -357,8 +375,34 @@ describe("ProofGenerator", () => {
|
|
|
357
375
|
|
|
358
376
|
expect(payload.outcome).toBe("allowed");
|
|
359
377
|
expect(payload).not.toHaveProperty("toolName");
|
|
360
|
-
expect(
|
|
361
|
-
expect(proof.meta
|
|
378
|
+
expect(payload).not.toHaveProperty("submissionOutcome");
|
|
379
|
+
expect(proof.meta).not.toHaveProperty("toolName");
|
|
380
|
+
expect(proof.meta).not.toHaveProperty("submissionOutcome");
|
|
381
|
+
expect(proof.toolName).toBe("get_weather");
|
|
382
|
+
expect(proof.submissionOutcome).toBe("failure");
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("returns a bare { jws, meta } when nothing rides beside it", async () => {
|
|
386
|
+
const proof = await proofGenerator.generateProof(
|
|
387
|
+
{ method: "test-tool", params: {} },
|
|
388
|
+
{ data: {} },
|
|
389
|
+
mockSession
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
expect(Object.keys(proof).sort()).toEqual(["jws", "meta"]);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("never writes an empty optional claim to meta", async () => {
|
|
396
|
+
const proof = await proofGenerator.generateProof(
|
|
397
|
+
{ method: "test-tool", params: {} },
|
|
398
|
+
{ data: {} },
|
|
399
|
+
mockSession,
|
|
400
|
+
{ scopeId: "", clientDid: "", delegationRef: undefined }
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
expect(proof.meta).not.toHaveProperty("scopeId");
|
|
404
|
+
expect(proof.meta).not.toHaveProperty("clientDid");
|
|
405
|
+
expect(proof.meta).not.toHaveProperty("delegationRef");
|
|
362
406
|
});
|
|
363
407
|
});
|
|
364
408
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What rides beside a generated proof, unsigned: shared by every runtime's
|
|
3
|
+
* proof generator, the reference one here (./generator) and Cloudflare's.
|
|
4
|
+
*
|
|
5
|
+
* @module proof/companion
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { DetachedProof } from "@kya-os/contracts/proof";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Values that ride beside a proof, unsigned. They are never written to
|
|
12
|
+
* `meta`, whose members a reader takes for the signed claims they mirror.
|
|
13
|
+
*/
|
|
14
|
+
export interface ProofCompanion {
|
|
15
|
+
/** The tool the proof is for, as a proof submission names it. */
|
|
16
|
+
toolName?: string;
|
|
17
|
+
/** Whether the tool call itself succeeded, as a proof submission says. */
|
|
18
|
+
submissionOutcome?: "success" | "failure" | "denied";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A detached proof as the generator returns it: `{ jws, meta }`, plus any
|
|
23
|
+
* {@link ProofCompanion} values the caller passed, beside `meta`.
|
|
24
|
+
*/
|
|
25
|
+
export type GeneratedProof<P extends object = DetachedProof> = P &
|
|
26
|
+
ProofCompanion;
|
|
27
|
+
|
|
28
|
+
/** The members of `values` that have one: an empty value is never written. */
|
|
29
|
+
export function presentValues<T extends object>(values: T): Partial<T> {
|
|
30
|
+
return Object.fromEntries(
|
|
31
|
+
Object.entries(values).filter(
|
|
32
|
+
([, value]) => value !== undefined && value !== null && value !== ""
|
|
33
|
+
)
|
|
34
|
+
) as Partial<T>;
|
|
35
|
+
}
|
package/src/proof/generator.ts
CHANGED
|
@@ -18,7 +18,15 @@ import {
|
|
|
18
18
|
import { SessionContext } from "@kya-os/contracts/handshake";
|
|
19
19
|
import { CryptoProvider } from "../providers/base";
|
|
20
20
|
import { CryptoService, type Ed25519JWK } from "../services/crypto.service";
|
|
21
|
+
import { base64urlEncodeFromBytes } from "../utils/base64";
|
|
21
22
|
import { loadDifCore } from "../runtime/dif-core";
|
|
23
|
+
import {
|
|
24
|
+
presentValues,
|
|
25
|
+
type GeneratedProof,
|
|
26
|
+
type ProofCompanion,
|
|
27
|
+
} from "./companion";
|
|
28
|
+
|
|
29
|
+
export type { GeneratedProof, ProofCompanion } from "./companion";
|
|
22
30
|
|
|
23
31
|
/**
|
|
24
32
|
* Minimal agent identity required by ProofGenerator.
|
|
@@ -52,17 +60,26 @@ export interface ToolResponse {
|
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
/**
|
|
55
|
-
* Proof generation options
|
|
63
|
+
* Proof generation options. Each claim is signed when it has a value; an
|
|
64
|
+
* empty one (undefined or "") is never written.
|
|
56
65
|
*/
|
|
57
|
-
export interface ProofOptions {
|
|
66
|
+
export interface ProofOptions extends ProofCompanion {
|
|
58
67
|
/** Authorization decision this proof attests to (signed). */
|
|
59
68
|
outcome?: ProofMeta["outcome"];
|
|
60
69
|
scopeId?: string;
|
|
61
70
|
delegationRef?: string;
|
|
62
71
|
clientDid?: string;
|
|
63
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Not a proof claim. Returned beside the proof as
|
|
74
|
+
* `proof.toolName`, never on `meta`. Prefer naming the tool where the
|
|
75
|
+
* proof is submitted: `ProofBatchQueue.enqueue({ proof, toolName, outcome })`.
|
|
76
|
+
*/
|
|
64
77
|
toolName?: string;
|
|
65
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Not a proof claim. Returned beside the proof as
|
|
80
|
+
* `proof.submissionOutcome`, never on `meta`. Prefer
|
|
81
|
+
* `ProofBatchQueue.enqueue({ proof, toolName, outcome })`.
|
|
82
|
+
*/
|
|
66
83
|
submissionOutcome?: "success" | "failure" | "denied";
|
|
67
84
|
}
|
|
68
85
|
|
|
@@ -88,24 +105,34 @@ export class ProofGenerator {
|
|
|
88
105
|
response: ToolResponse,
|
|
89
106
|
session: SessionContext,
|
|
90
107
|
options: ProofOptions = {}
|
|
91
|
-
): Promise<
|
|
108
|
+
): Promise<GeneratedProof> {
|
|
92
109
|
const hashes = await this.generateCanonicalHashes(request, response);
|
|
110
|
+
const { toolName, submissionOutcome, ...claims } = options;
|
|
93
111
|
|
|
94
112
|
const meta: ProofMeta = {
|
|
95
113
|
did: this.identity.did,
|
|
96
114
|
kid: this.identity.kid,
|
|
97
115
|
ts: Math.floor(Date.now() / 1000),
|
|
98
|
-
nonce:
|
|
116
|
+
// A nonce of this proof's own, as @kya-os/mcp's generator mints it: a
|
|
117
|
+
// replay check spends a nonce once per signer, so the session's
|
|
118
|
+
// handshake nonce would refuse every proof after its first.
|
|
119
|
+
nonce: base64urlEncodeFromBytes(
|
|
120
|
+
await this.cryptoProvider.randomBytes(16)
|
|
121
|
+
),
|
|
99
122
|
audience: session.audience,
|
|
100
123
|
sessionId: session.sessionId,
|
|
101
124
|
requestHash: hashes.requestHash,
|
|
102
125
|
responseHash: hashes.responseHash,
|
|
103
|
-
...
|
|
126
|
+
...presentValues(claims),
|
|
104
127
|
};
|
|
105
128
|
|
|
106
129
|
const jws = await this.generateJWS(meta);
|
|
107
130
|
|
|
108
|
-
return {
|
|
131
|
+
return {
|
|
132
|
+
jws,
|
|
133
|
+
meta,
|
|
134
|
+
...presentValues({ toolName, submissionOutcome }),
|
|
135
|
+
};
|
|
109
136
|
}
|
|
110
137
|
|
|
111
138
|
/**
|
package/src/proof/index.ts
CHANGED
package/src/runtime/audit.ts
CHANGED
|
@@ -6,9 +6,14 @@ import type {
|
|
|
6
6
|
AuthorizationEvidence,
|
|
7
7
|
PartyRef,
|
|
8
8
|
} from "@kya-os/mcp/audit" with { "resolution-mode": "import" };
|
|
9
|
-
//
|
|
10
|
-
// the denial code list, so the known codes come from
|
|
11
|
-
|
|
9
|
+
// The denial code is type-only on purpose: package.json accepts contracts
|
|
10
|
+
// releases that predate the denial code list, so the known codes come from
|
|
11
|
+
// this package instead. The refusal marker reader shipped in contracts 1.13.0,
|
|
12
|
+
// so every contracts release package.json accepts has it.
|
|
13
|
+
import {
|
|
14
|
+
readToolRefusal,
|
|
15
|
+
type DelegationDenialCode,
|
|
16
|
+
} from "@kya-os/contracts/config";
|
|
12
17
|
import { DEFAULT_DENIAL_MESSAGES } from "./delegation-denial.js";
|
|
13
18
|
import { AmountLimitError } from "../types/tool-protection.js";
|
|
14
19
|
|
|
@@ -635,6 +640,13 @@ export function terminalForToolResult(result: unknown): RuntimeToolTerminal {
|
|
|
635
640
|
reasonCode: "AUTHORIZATION_REQUIRED",
|
|
636
641
|
};
|
|
637
642
|
}
|
|
643
|
+
// A tool's policy refusal (over a spend cap, a product out of scope) is a
|
|
644
|
+
// denial with the tool's own code, not a tool error. Read before isError,
|
|
645
|
+
// as Checkpoint and the activity telemetry do, so all three agree.
|
|
646
|
+
const refusal = readToolRefusal(result);
|
|
647
|
+
if (refusal) {
|
|
648
|
+
return { phase: "denied", outcome: "denied", reasonCode: refusal.code };
|
|
649
|
+
}
|
|
638
650
|
if (result.isError !== true)
|
|
639
651
|
return { phase: "completed", outcome: "succeeded" };
|
|
640
652
|
const metadata = isRecord(result._meta) ? result._meta : undefined;
|
package/src/runtime/dif-core.ts
CHANGED
|
@@ -37,3 +37,19 @@ let cached: Promise<DifCore> | undefined;
|
|
|
37
37
|
export function loadDifCore(): Promise<DifCore> {
|
|
38
38
|
return (cached ??= import("@kya-os/mcp"));
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
/** The `@kya-os/mcp/card` module: Entity Card and per-request proofs. */
|
|
42
|
+
export type DifCard = typeof import("@kya-os/mcp/card", {
|
|
43
|
+
with: { "resolution-mode": "import" }
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
let cachedCard: Promise<DifCard> | undefined;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve `@kya-os/mcp/card` through the same cached dynamic import as
|
|
50
|
+
* {@link loadDifCore}: its request-proof guard (`requireProof`) and nonce
|
|
51
|
+
* cache verify which key a call proves it holds.
|
|
52
|
+
*/
|
|
53
|
+
export function loadDifCard(): Promise<DifCard> {
|
|
54
|
+
return (cachedCard ??= import("@kya-os/mcp/card"));
|
|
55
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guard both runtimes verify a request proof with: a holder's key is
|
|
3
|
+
* read only for a proof whose own claims pass.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { requireProof } from "@kya-os/mcp/card";
|
|
7
|
+
import { claimsFirstGuard } from "../claims-first-guard";
|
|
8
|
+
import { resolveDidKeyHolder } from "../request-holder";
|
|
9
|
+
import { SERVER, newHolder, signedCall } from "./holder-fixtures";
|
|
10
|
+
|
|
11
|
+
const params = { name: "read", arguments: { id: 1 } };
|
|
12
|
+
|
|
13
|
+
function guarded() {
|
|
14
|
+
const resolveKey = vi.fn(resolveDidKeyHolder);
|
|
15
|
+
const consumeNonceIfFresh = vi.fn(async () => true);
|
|
16
|
+
// The audience is inherited, as a deployment's own dependencies may be.
|
|
17
|
+
const resolveDidKeys = vi.fn(async (did: string) => [
|
|
18
|
+
await resolveDidKeyHolder(`${did}#${did.slice("did:key:".length)}`),
|
|
19
|
+
]);
|
|
20
|
+
const deps = Object.assign(Object.create({ expectedAudience: SERVER }), {
|
|
21
|
+
resolveKey,
|
|
22
|
+
resolveDidKeys,
|
|
23
|
+
consumeNonceIfFresh,
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
guard: claimsFirstGuard(requireProof, deps),
|
|
27
|
+
resolveKey,
|
|
28
|
+
resolveDidKeys,
|
|
29
|
+
consumeNonceIfFresh,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function proved(audience = SERVER) {
|
|
34
|
+
const holder = newHolder();
|
|
35
|
+
const { method, params: sent } = await signedCall(holder, params, audience);
|
|
36
|
+
const { _meta, ...signed } = sent;
|
|
37
|
+
return { holder, request: { method, params: signed }, meta: _meta };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("claimsFirstGuard", () => {
|
|
41
|
+
it("verifies a proof its claims pass with its key, and spends its nonce once", async () => {
|
|
42
|
+
const { guard, resolveKey, consumeNonceIfFresh } = guarded();
|
|
43
|
+
const { holder, request, meta } = await proved();
|
|
44
|
+
|
|
45
|
+
expect(await guard(request, meta)).toMatchObject({
|
|
46
|
+
ok: true,
|
|
47
|
+
did: holder.did,
|
|
48
|
+
});
|
|
49
|
+
expect(resolveKey).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(consumeNonceIfFresh).toHaveBeenCalledTimes(1);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("refuses a proof for another audience with the library's reason, and reads no key", async () => {
|
|
54
|
+
const { guard, resolveKey, resolveDidKeys, consumeNonceIfFresh } =
|
|
55
|
+
guarded();
|
|
56
|
+
const { request, meta } = await proved("did:web:another.example");
|
|
57
|
+
|
|
58
|
+
expect(await guard(request, meta)).toMatchObject({
|
|
59
|
+
ok: false,
|
|
60
|
+
error: { code: "proof_invalid", reasons: ["audience_mismatch"] },
|
|
61
|
+
});
|
|
62
|
+
expect(resolveKey).not.toHaveBeenCalled();
|
|
63
|
+
expect(resolveDidKeys).not.toHaveBeenCalled();
|
|
64
|
+
expect(consumeNonceIfFresh).not.toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("refuses a proof over other params, and reads no key", async () => {
|
|
68
|
+
const { guard, resolveKey } = guarded();
|
|
69
|
+
const { request, meta } = await proved();
|
|
70
|
+
|
|
71
|
+
const verdict = await guard(
|
|
72
|
+
{ ...request, params: { ...request.params, arguments: { id: 2 } } },
|
|
73
|
+
meta
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
expect(verdict).toMatchObject({
|
|
77
|
+
ok: false,
|
|
78
|
+
error: { reasons: ["request_hash_mismatch"] },
|
|
79
|
+
});
|
|
80
|
+
expect(resolveKey).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("answers a call with no proof as the library does", async () => {
|
|
84
|
+
const { guard, resolveKey } = guarded();
|
|
85
|
+
const { request } = await proved();
|
|
86
|
+
|
|
87
|
+
expect(await guard(request, {})).toMatchObject({
|
|
88
|
+
ok: false,
|
|
89
|
+
error: { code: "proof_missing" },
|
|
90
|
+
});
|
|
91
|
+
expect(resolveKey).not.toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
});
|