@kya-os/mcp-i-runtime 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/auth/handshake.d.ts +23 -43
- package/dist/auth/handshake.d.ts.map +1 -1
- package/dist/auth/handshake.js +51 -82
- package/dist/auth/handshake.js.map +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/resume-token.d.ts +52 -0
- package/dist/auth/resume-token.d.ts.map +1 -0
- package/dist/auth/resume-token.js +89 -0
- package/dist/auth/resume-token.js.map +1 -0
- package/dist/auth/types.d.ts +15 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/dist/proof/companion.d.ts +25 -0
- package/dist/proof/companion.d.ts.map +1 -0
- package/dist/proof/companion.js +14 -0
- package/dist/proof/companion.js.map +1 -0
- package/dist/proof/generator.d.ts +22 -9
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +20 -27
- package/dist/proof/generator.js.map +1 -1
- package/dist/proof/index.d.ts +2 -1
- package/dist/proof/index.d.ts.map +1 -1
- package/dist/proof/index.js +3 -1
- package/dist/proof/index.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +78 -18
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/base.d.ts +6 -1
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +65 -17
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/dif-core.d.ts +8 -0
- package/dist/runtime/dif-core.d.ts.map +1 -1
- package/dist/runtime/dif-core.js +10 -0
- package/dist/runtime/dif-core.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +12 -5
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/claims-first-guard.d.ts +33 -0
- package/dist/services/claims-first-guard.d.ts.map +1 -0
- package/dist/services/claims-first-guard.js +77 -0
- package/dist/services/claims-first-guard.js.map +1 -0
- package/dist/services/holder-keys.d.ts +95 -0
- package/dist/services/holder-keys.d.ts.map +1 -0
- package/dist/services/holder-keys.js +281 -0
- package/dist/services/holder-keys.js.map +1 -0
- package/dist/services/holder-read-slots.d.ts +42 -0
- package/dist/services/holder-read-slots.d.ts.map +1 -0
- package/dist/services/holder-read-slots.js +82 -0
- package/dist/services/holder-read-slots.js.map +1 -0
- package/dist/services/proof-verifier.d.ts +13 -35
- package/dist/services/proof-verifier.d.ts.map +1 -1
- package/dist/services/proof-verifier.js +16 -74
- package/dist/services/proof-verifier.js.map +1 -1
- package/dist/services/request-holder.d.ts +78 -0
- package/dist/services/request-holder.d.ts.map +1 -0
- package/dist/services/request-holder.js +81 -0
- package/dist/services/request-holder.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -1
- package/dist/services/tool-context-builder.js +16 -4
- package/dist/services/tool-context-builder.js.map +1 -1
- package/dist/services/tool-protection.service.d.ts +7 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +34 -15
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/tool-protection.d.ts +94 -0
- package/dist/types/tool-protection.d.ts.map +1 -1
- package/dist/types/tool-protection.js +113 -1
- package/dist/types/tool-protection.js.map +1 -1
- package/eslint.config.js +20 -0
- package/package.json +8 -2
- package/src/__tests__/runtime/amount-limit-enforcement.test.ts +269 -0
- package/src/__tests__/runtime/amount-limit-result.test.ts +61 -0
- package/src/__tests__/runtime/audit-contracts-compat.test.ts +20 -5
- package/src/__tests__/runtime/audit.test.ts +363 -53
- package/src/__tests__/runtime/process-tool-call-security.test.ts +97 -13
- package/src/__tests__/runtime/provider-selection.test.ts +4 -6
- package/src/__tests__/runtime/route-interception.test.ts +215 -214
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +89 -6
- package/src/__tests__/services/tool-protection-amount.test.ts +107 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +71 -4
- package/src/__tests__/utils/tool-protection-runtime.ts +3 -0
- package/src/auth/__tests__/verify-or-hints-presented.test.ts +155 -0
- package/src/auth/handshake.ts +80 -125
- package/src/auth/index.ts +1 -0
- package/src/auth/resume-token.ts +134 -0
- package/src/auth/types.ts +20 -2
- package/src/index.ts +40 -1
- package/src/proof/__tests__/proof-generator.test.ts +80 -13
- package/src/proof/companion.ts +35 -0
- package/src/proof/generator.ts +51 -33
- package/src/proof/index.ts +3 -0
- package/src/runtime/__tests__/ext-apps-consent.test.ts +87 -2
- package/src/runtime/audit.ts +170 -65
- package/src/runtime/base.ts +95 -31
- package/src/runtime/dif-core.ts +16 -0
- package/src/services/__tests__/access-control.service.test.ts +44 -0
- package/src/services/__tests__/claims-first-guard.test.ts +93 -0
- package/src/services/__tests__/holder-fixtures.ts +135 -0
- package/src/services/__tests__/holder-keys.test.ts +257 -0
- package/src/services/__tests__/holder-read-slots.test.ts +117 -0
- package/src/services/__tests__/holder-reads.test.ts +284 -0
- package/src/services/__tests__/proof-verifier.test.ts +286 -195
- package/src/services/__tests__/request-holder.test.ts +210 -0
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +79 -0
- package/src/services/__tests__/tool-context-builder.test.ts +4 -7
- package/src/services/__tests__/tool-protection-served-revision.test.ts +102 -0
- package/src/services/access-control.service.ts +11 -5
- package/src/services/claims-first-guard.ts +89 -0
- package/src/services/holder-keys.ts +379 -0
- package/src/services/holder-read-slots.ts +85 -0
- package/src/services/proof-verifier.ts +18 -79
- package/src/services/request-holder.ts +146 -0
- package/src/services/tool-context-builder.ts +19 -4
- package/src/services/tool-protection.service.ts +62 -19
- package/src/types/tool-protection.ts +161 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request holder: the DID whose key a tools/call proves its caller holds.
|
|
3
|
+
*
|
|
4
|
+
* The proof is the call's request proof, `_meta["org.kya-os/request-proof"]`
|
|
5
|
+
* (SPEC-ENTITY-CARD §8, SPEC-MCP-EXTENSION §6), verified fail closed by
|
|
6
|
+
* `@kya-os/mcp/card`'s `requireProof`: its request hash covers the call's
|
|
7
|
+
* method and params without `_meta`, its audience is this server's DID, its
|
|
8
|
+
* nonce is spent once, and it is signed by the key the holder DID names. This
|
|
9
|
+
* is the check the Cloudflare adapter's request admission makes
|
|
10
|
+
* (mcp-i-cloudflare `security/mcp-holder-admission`). A did:key holder's key
|
|
11
|
+
* is resolved locally with no network; a did:web holder's keys are the ones
|
|
12
|
+
* its DID document lists for authentication (./holder-keys).
|
|
13
|
+
*
|
|
14
|
+
* A proof names its holder before anything verifies it, so the holder's
|
|
15
|
+
* document is read only for a proof its own claims do not already refuse
|
|
16
|
+
* (./claims-first-guard).
|
|
17
|
+
*
|
|
18
|
+
* A verified holder is identity only: it grants nothing until a delegation
|
|
19
|
+
* the call presents is verified for it.
|
|
20
|
+
*
|
|
21
|
+
* @module services/request-holder
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { loadDifCard, loadDifCore, type DifCard } from "../runtime/dif-core";
|
|
25
|
+
import { claimsFirstGuard } from "./claims-first-guard";
|
|
26
|
+
import {
|
|
27
|
+
withDidWebHolders,
|
|
28
|
+
type DidWebHolderOptions,
|
|
29
|
+
type ProofPublicJwk,
|
|
30
|
+
} from "./holder-keys";
|
|
31
|
+
|
|
32
|
+
type VerifyProofDeps = Parameters<DifCard["requireProof"]>[0];
|
|
33
|
+
type ProofGateCode = Extract<
|
|
34
|
+
Awaited<ReturnType<ReturnType<DifCard["requireProof"]>>>,
|
|
35
|
+
{ ok: false }
|
|
36
|
+
>["error"]["code"];
|
|
37
|
+
|
|
38
|
+
/** Seconds of clock skew a request proof's validity window is widened by. */
|
|
39
|
+
export const REQUEST_PROOF_SKEW_SEC = 30;
|
|
40
|
+
|
|
41
|
+
/** Why a call proved no holder: it carried no request proof, or a bad one. */
|
|
42
|
+
export type RequestHolderRefusal = "holder_proof_required" | ProofGateCode;
|
|
43
|
+
|
|
44
|
+
/** The holder a call proved, or why it proved none. */
|
|
45
|
+
export type RequestHolderResult =
|
|
46
|
+
| { ok: true; did: string }
|
|
47
|
+
| { ok: false; code: RequestHolderRefusal };
|
|
48
|
+
|
|
49
|
+
/** How a server checks its calls' request proofs, beside their audience. */
|
|
50
|
+
export interface RequestProofOptions extends DidWebHolderOptions {
|
|
51
|
+
/**
|
|
52
|
+
* The atomic test-and-set that spends a proof's nonce:
|
|
53
|
+
* `(nonce, did, minTtlSec) => Promise<boolean>`, true when the nonce was
|
|
54
|
+
* fresh. Default: one in-memory cache per verifier, so one per process. A
|
|
55
|
+
* proof lives at most 60 seconds and is accepted within 30 seconds of
|
|
56
|
+
* clock skew, so behind several instances that each keep their own cache,
|
|
57
|
+
* a captured proof is accepted once by each, for up to 90 seconds after
|
|
58
|
+
* it was signed. Share one store across the instances that serve one
|
|
59
|
+
* server DID, as the Cloudflare adapter shares a Durable Object.
|
|
60
|
+
*/
|
|
61
|
+
consumeNonceIfFresh?: VerifyProofDeps["consumeNonceIfFresh"];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface RequestHolderOptions extends RequestProofOptions {
|
|
65
|
+
/** This server's DID: a proof addressed to any other is refused. */
|
|
66
|
+
audience: string;
|
|
67
|
+
/** Clock in epoch milliseconds, for tests. */
|
|
68
|
+
now?: () => number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A tools/call as received: its method and params, `_meta` included. */
|
|
72
|
+
export interface HolderRequest {
|
|
73
|
+
method: string;
|
|
74
|
+
params?: Record<string, unknown>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type RequestHolderVerifier = (
|
|
78
|
+
request: HolderRequest
|
|
79
|
+
) => Promise<RequestHolderResult>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The Ed25519 key a did:key holder names by its own key id, resolved with no
|
|
83
|
+
* network. Any other kid, or any other DID method, has no key here.
|
|
84
|
+
*/
|
|
85
|
+
export async function resolveDidKeyHolder(
|
|
86
|
+
kid: string
|
|
87
|
+
): Promise<ProofPublicJwk> {
|
|
88
|
+
const {
|
|
89
|
+
didKeyFragment,
|
|
90
|
+
extractPublicKeyFromDidKey,
|
|
91
|
+
isEd25519DidKey,
|
|
92
|
+
publicKeyToJwk,
|
|
93
|
+
} = await loadDifCore();
|
|
94
|
+
const did = kid.split("#")[0] ?? "";
|
|
95
|
+
const publicKey =
|
|
96
|
+
isEd25519DidKey(did) && kid === `${did}#${didKeyFragment(did)}`
|
|
97
|
+
? extractPublicKeyFromDidKey(did)
|
|
98
|
+
: null;
|
|
99
|
+
if (!publicKey) {
|
|
100
|
+
throw new Error("Only an Ed25519 did:key holder's own key is accepted");
|
|
101
|
+
}
|
|
102
|
+
return publicKeyToJwk(publicKey) as ProofPublicJwk;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Build the verifier of the holder a tools/call proves. One verifier keeps
|
|
107
|
+
* one nonce cache, so build it once per server.
|
|
108
|
+
*/
|
|
109
|
+
export function createRequestHolderVerifier(
|
|
110
|
+
options: RequestHolderOptions
|
|
111
|
+
): RequestHolderVerifier {
|
|
112
|
+
let guard: ReturnType<DifCard["requireProof"]> | undefined;
|
|
113
|
+
const holderKeys = withDidWebHolders(
|
|
114
|
+
{
|
|
115
|
+
resolveKey: resolveDidKeyHolder,
|
|
116
|
+
resolveDidKeys: async (did) => {
|
|
117
|
+
const { didKeyFragment } = await loadDifCore();
|
|
118
|
+
return [await resolveDidKeyHolder(`${did}#${didKeyFragment(did)}`)];
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
options
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const guardFor = (card: DifCard) =>
|
|
125
|
+
(guard ??= claimsFirstGuard(card.requireProof, {
|
|
126
|
+
expectedAudience: options.audience,
|
|
127
|
+
skewSec: REQUEST_PROOF_SKEW_SEC,
|
|
128
|
+
...(options.now ? { now: options.now } : {}),
|
|
129
|
+
resolveKey: holderKeys.resolveKey,
|
|
130
|
+
resolveDidKeys: holderKeys.resolveDidKeys,
|
|
131
|
+
consumeNonceIfFresh:
|
|
132
|
+
options.consumeNonceIfFresh ?? new card.InMemoryNonceCache().consume,
|
|
133
|
+
}));
|
|
134
|
+
|
|
135
|
+
return async ({ method, params = {} }) => {
|
|
136
|
+
const { _meta, ...signed } = params;
|
|
137
|
+
const card = await loadDifCard();
|
|
138
|
+
if (card.readCardProof(_meta) === undefined) {
|
|
139
|
+
return { ok: false, code: "holder_proof_required" };
|
|
140
|
+
}
|
|
141
|
+
const verdict = await guardFor(card)({ method, params: signed }, _meta);
|
|
142
|
+
return verdict.ok
|
|
143
|
+
? { ok: true, did: verdict.did }
|
|
144
|
+
: { ok: false, code: verdict.error.code };
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -134,8 +134,23 @@ export class ToolContextBuilder {
|
|
|
134
134
|
toolProtection.requiredScopes
|
|
135
135
|
);
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
// Reaching this OAuth context builder means the tool is neither
|
|
138
|
+
// consent-only (`none`) nor password (see the comment below), so the
|
|
139
|
+
// effective requirement is OAuth.
|
|
140
|
+
const requiredAuthType = toolProtection.authorization?.type ?? "oauth2";
|
|
141
|
+
|
|
142
|
+
// SECURITY (defence in depth): a presented delegation must satisfy the
|
|
143
|
+
// tool's required type before the user's stored IdP token is handed to the
|
|
144
|
+
// tool, the same rule as the delegation-only path below. Otherwise a
|
|
145
|
+
// delegation minted under the user's DID without their sign-in (an
|
|
146
|
+
// anonymous `none` one, say) would unlock their token. Not presenting a
|
|
147
|
+
// delegation keeps the existing behaviour.
|
|
148
|
+
const presentedDelegationAllowed =
|
|
149
|
+
!delegationToken ||
|
|
150
|
+
authTypeSatisfies(delegationAuthorizationType, requiredAuthType);
|
|
151
|
+
|
|
152
|
+
if (!tokenData || !presentedDelegationAllowed) {
|
|
153
|
+
// No usable IDP token - check if delegation token exists
|
|
139
154
|
// If delegation exists, tool can still execute without IDP token
|
|
140
155
|
// (the tool handler should check for idpToken if it needs external API access)
|
|
141
156
|
//
|
|
@@ -150,8 +165,8 @@ export class ToolContextBuilder {
|
|
|
150
165
|
// mismatched delegation (e.g. an anonymous `none` consent-only delegation,
|
|
151
166
|
// or a legacy record with no persisted type → undefined) is rejected. On
|
|
152
167
|
// mismatch we do NOT return the delegation-only context; we fall through
|
|
153
|
-
// to OAuthRequiredError so the tool re-challenges.
|
|
154
|
-
|
|
168
|
+
// to OAuthRequiredError so the tool re-challenges. `requiredAuthType`
|
|
169
|
+
// is computed above.
|
|
155
170
|
if (
|
|
156
171
|
delegationToken &&
|
|
157
172
|
authTypeSatisfies(delegationAuthorizationType, requiredAuthType)
|
|
@@ -106,10 +106,36 @@
|
|
|
106
106
|
* @package @kya-os/mcp-i-core
|
|
107
107
|
*/
|
|
108
108
|
|
|
109
|
-
import type {
|
|
110
|
-
|
|
109
|
+
import type {
|
|
110
|
+
ToolProtection,
|
|
111
|
+
ToolProtectionConfig,
|
|
112
|
+
ToolProtectionServiceConfig,
|
|
113
|
+
} from "../types/tool-protection.js";
|
|
114
|
+
import { unavailableToolProtection } from "../types/tool-protection.js";
|
|
115
|
+
import type {
|
|
116
|
+
AuthorizationRequirement,
|
|
117
|
+
ToolAmountConfig,
|
|
118
|
+
} from "@kya-os/contracts/tool-protection";
|
|
111
119
|
import type { ToolProtectionCache } from "../cache/tool-protection-cache.js";
|
|
112
120
|
import { InMemoryToolProtectionCache } from "../cache/tool-protection-cache.js";
|
|
121
|
+
import { canonicalize } from "json-canonicalize";
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The revision of the tool protection Checkpoint served: SHA-256 (lowercase
|
|
125
|
+
* hex) of the RFC 8785 canonical JSON of `config.toolProtection.tools`,
|
|
126
|
+
* exactly as received. Checkpoint recomputes the same from what it serves.
|
|
127
|
+
*/
|
|
128
|
+
export async function servedToolProtectionRevision(
|
|
129
|
+
tools: unknown
|
|
130
|
+
): Promise<string> {
|
|
131
|
+
const digest = await crypto.subtle.digest(
|
|
132
|
+
"SHA-256",
|
|
133
|
+
new TextEncoder().encode(canonicalize(tools))
|
|
134
|
+
);
|
|
135
|
+
return [...new Uint8Array(digest)]
|
|
136
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
137
|
+
.join("");
|
|
138
|
+
}
|
|
113
139
|
|
|
114
140
|
/**
|
|
115
141
|
* Tool protection data structure in API responses
|
|
@@ -130,6 +156,7 @@ interface ToolProtectionData {
|
|
|
130
156
|
issuer?: string;
|
|
131
157
|
credentialType?: string;
|
|
132
158
|
};
|
|
159
|
+
amount?: ToolAmountConfig;
|
|
133
160
|
}
|
|
134
161
|
|
|
135
162
|
/**
|
|
@@ -235,6 +262,17 @@ export class ToolProtectionService {
|
|
|
235
262
|
return null;
|
|
236
263
|
}
|
|
237
264
|
|
|
265
|
+
private keepLocalAmount(
|
|
266
|
+
toolName: string,
|
|
267
|
+
apiConfig: ToolProtection
|
|
268
|
+
): ToolProtection {
|
|
269
|
+
const localAmount =
|
|
270
|
+
this.config.localConfig?.toolProtections?.[toolName]?.amount;
|
|
271
|
+
return apiConfig.amount || !localAmount
|
|
272
|
+
? apiConfig
|
|
273
|
+
: { ...apiConfig, amount: localAmount };
|
|
274
|
+
}
|
|
275
|
+
|
|
238
276
|
/**
|
|
239
277
|
* Get tool protection configuration for the agent
|
|
240
278
|
*
|
|
@@ -370,6 +408,9 @@ export class ToolProtectionService {
|
|
|
370
408
|
...((localConfig as ToolProtection).authorization && {
|
|
371
409
|
authorization: (localConfig as ToolProtection).authorization,
|
|
372
410
|
}),
|
|
411
|
+
...((localConfig as ToolProtection).amount && {
|
|
412
|
+
amount: (localConfig as ToolProtection).amount,
|
|
413
|
+
}),
|
|
373
414
|
};
|
|
374
415
|
|
|
375
416
|
mergedToolProtections[toolName] = validConfig;
|
|
@@ -387,7 +428,10 @@ export class ToolProtectionService {
|
|
|
387
428
|
|
|
388
429
|
// Step 2: Apply API config (OVERWRITES local config - remote wins)
|
|
389
430
|
for (const [toolName, apiConfig] of Object.entries(toolProtections)) {
|
|
390
|
-
mergedToolProtections[toolName] =
|
|
431
|
+
mergedToolProtections[toolName] = this.keepLocalAmount(
|
|
432
|
+
toolName,
|
|
433
|
+
apiConfig
|
|
434
|
+
);
|
|
391
435
|
if (
|
|
392
436
|
this.config.debug &&
|
|
393
437
|
this.config.localConfig?.toolProtections?.[toolName]
|
|
@@ -404,6 +448,9 @@ export class ToolProtectionService {
|
|
|
404
448
|
|
|
405
449
|
const config: ToolProtectionConfig = {
|
|
406
450
|
toolProtections: mergedToolProtections,
|
|
451
|
+
servedRevision: await servedToolProtectionRevision(
|
|
452
|
+
response.data?.config?.toolProtection?.tools ?? {}
|
|
453
|
+
),
|
|
407
454
|
};
|
|
408
455
|
|
|
409
456
|
// 3. Cache the response
|
|
@@ -543,21 +590,10 @@ export class ToolProtectionService {
|
|
|
543
590
|
}
|
|
544
591
|
);
|
|
545
592
|
|
|
546
|
-
//
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
requiresDelegation: true,
|
|
551
|
-
requiredScopes: [],
|
|
552
|
-
},
|
|
553
|
-
},
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
// Cache deny-all config to avoid repeated API calls
|
|
557
|
-
const ttl = this.config.cacheTtl ?? 300000; // Default 5 minutes
|
|
558
|
-
await this.cache.set(cacheKey, denyAllConfig, ttl);
|
|
559
|
-
|
|
560
|
-
return denyAllConfig;
|
|
593
|
+
// Protection is unavailable: every tool resolves to the stand-in,
|
|
594
|
+
// which the runtime refuses. It isn't cached, so the next lookup
|
|
595
|
+
// reads the API again and the first config that arrives replaces it.
|
|
596
|
+
return { toolProtections: { "*": unavailableToolProtection() } };
|
|
561
597
|
} else {
|
|
562
598
|
// failSafeBehavior === 'allow-all' (insecure, not recommended)
|
|
563
599
|
console.warn(
|
|
@@ -810,6 +846,7 @@ export class ToolProtectionService {
|
|
|
810
846
|
...(oauthProvider && { oauthProvider }),
|
|
811
847
|
...(riskLevel && { riskLevel }),
|
|
812
848
|
...(authorization && { authorization }),
|
|
849
|
+
...(toolConfig.amount && { amount: toolConfig.amount }),
|
|
813
850
|
};
|
|
814
851
|
}
|
|
815
852
|
|
|
@@ -920,6 +957,9 @@ export class ToolProtectionService {
|
|
|
920
957
|
...((localConfig as ToolProtection).authorization && {
|
|
921
958
|
authorization: (localConfig as ToolProtection).authorization,
|
|
922
959
|
}),
|
|
960
|
+
...((localConfig as ToolProtection).amount && {
|
|
961
|
+
amount: (localConfig as ToolProtection).amount,
|
|
962
|
+
}),
|
|
923
963
|
};
|
|
924
964
|
mergedToolProtections[toolName] = validConfig;
|
|
925
965
|
}
|
|
@@ -928,7 +968,10 @@ export class ToolProtectionService {
|
|
|
928
968
|
|
|
929
969
|
// Step 2: Apply API config (OVERWRITES local config - remote wins)
|
|
930
970
|
for (const [toolName, apiConfig] of Object.entries(toolProtections)) {
|
|
931
|
-
mergedToolProtections[toolName] =
|
|
971
|
+
mergedToolProtections[toolName] = this.keepLocalAmount(
|
|
972
|
+
toolName,
|
|
973
|
+
apiConfig
|
|
974
|
+
);
|
|
932
975
|
}
|
|
933
976
|
|
|
934
977
|
// Construct fresh config (ToolProtectionConfig type)
|
|
@@ -14,6 +14,12 @@ import type {
|
|
|
14
14
|
ToolProtectionMap,
|
|
15
15
|
} from "@kya-os/contracts/tool-protection";
|
|
16
16
|
import type { DelegationDenial } from "../runtime/delegation-denial.js";
|
|
17
|
+
import { markToolRefusal } from "@kya-os/contracts/config";
|
|
18
|
+
import {
|
|
19
|
+
describeAmountLimitViolation,
|
|
20
|
+
type AmountLimitCode,
|
|
21
|
+
type AmountLimitViolation,
|
|
22
|
+
} from "@kya-os/contracts/delegation";
|
|
17
23
|
|
|
18
24
|
// Re-export for backward compatibility
|
|
19
25
|
export type { ToolProtection };
|
|
@@ -27,6 +33,33 @@ export interface ToolProtectionConfig {
|
|
|
27
33
|
* Map of tool names to their protection settings
|
|
28
34
|
*/
|
|
29
35
|
toolProtections: ToolProtectionMap;
|
|
36
|
+
/**
|
|
37
|
+
* The revision of the tool protection Checkpoint served for this config:
|
|
38
|
+
* SHA-256 (lowercase hex) of the RFC 8785 canonical JSON of the served
|
|
39
|
+
* `config.toolProtection.tools` object, exactly as received. Checkpoint
|
|
40
|
+
* recomputes it from what it serves. Absent for local, fallback and
|
|
41
|
+
* deny-all configs, which Checkpoint did not serve.
|
|
42
|
+
*/
|
|
43
|
+
servedRevision?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The wildcard protection the service stands in with when no config can be
|
|
48
|
+
* read and none is cached. Every tool resolves to it, and the runtime refuses
|
|
49
|
+
* it until a config arrives. It is never cached.
|
|
50
|
+
*/
|
|
51
|
+
export interface UnavailableToolProtection extends ToolProtection {
|
|
52
|
+
readonly unavailable: true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function unavailableToolProtection(): UnavailableToolProtection {
|
|
56
|
+
return { requiresDelegation: true, requiredScopes: [], unavailable: true };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isUnavailableToolProtection(
|
|
60
|
+
protection: ToolProtection
|
|
61
|
+
): protection is UnavailableToolProtection {
|
|
62
|
+
return (protection as { unavailable?: unknown }).unavailable === true;
|
|
30
63
|
}
|
|
31
64
|
|
|
32
65
|
/**
|
|
@@ -163,6 +196,134 @@ export class DelegationRequiredError extends Error {
|
|
|
163
196
|
}
|
|
164
197
|
}
|
|
165
198
|
|
|
199
|
+
export const TOOL_PROTECTION_UNENFORCEABLE =
|
|
200
|
+
"tool_protection_unenforceable" as const;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* A protected tool was called where its protection can't be enforced: the
|
|
204
|
+
* deployment lacks what the protection service needs, the protection names no
|
|
205
|
+
* scope to check a delegation against, or no protection config can be read.
|
|
206
|
+
* Refused before the handler runs.
|
|
207
|
+
*/
|
|
208
|
+
export class ToolProtectionUnenforceableError extends Error {
|
|
209
|
+
readonly code = TOOL_PROTECTION_UNENFORCEABLE;
|
|
210
|
+
|
|
211
|
+
constructor(
|
|
212
|
+
readonly toolName: string,
|
|
213
|
+
/**
|
|
214
|
+
* What enforcement lacks: the deployment's CHECKPOINT_API_KEY and/or
|
|
215
|
+
* TOOL_PROTECTION_KV, the protection's requiredScopes, or the
|
|
216
|
+
* toolProtectionConfig itself.
|
|
217
|
+
*/
|
|
218
|
+
readonly missing: readonly string[],
|
|
219
|
+
message?: string
|
|
220
|
+
) {
|
|
221
|
+
super(
|
|
222
|
+
message ??
|
|
223
|
+
`Tool "${toolName}" is protected, but this deployment can't enforce its ` +
|
|
224
|
+
`protection without ${missing.join(" and ")}. Configure ` +
|
|
225
|
+
`${missing.length === 1 ? "it" : "them"} to call this tool.`
|
|
226
|
+
);
|
|
227
|
+
this.name = "ToolProtectionUnenforceableError";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
toJSON() {
|
|
231
|
+
return {
|
|
232
|
+
error: this.code,
|
|
233
|
+
message: this.message,
|
|
234
|
+
toolName: this.toolName,
|
|
235
|
+
missing: [...this.missing],
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// By shape, like the other refusal checks: the error may come from another
|
|
241
|
+
// copy of this package.
|
|
242
|
+
export function isToolProtectionUnenforceableError(
|
|
243
|
+
error: unknown
|
|
244
|
+
): error is ToolProtectionUnenforceableError {
|
|
245
|
+
return (
|
|
246
|
+
error instanceof Error &&
|
|
247
|
+
error.name === "ToolProtectionUnenforceableError" &&
|
|
248
|
+
(error as { code?: unknown }).code === TOOL_PROTECTION_UNENFORCEABLE
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Whether a protection's required scopes name at least one scope. With none
|
|
254
|
+
* there is nothing to check a delegation against, so the protection can't be
|
|
255
|
+
* enforced: an empty list never counts as satisfied.
|
|
256
|
+
*/
|
|
257
|
+
export function namesRequiredScope(
|
|
258
|
+
requiredScopes: readonly unknown[] | undefined
|
|
259
|
+
): boolean {
|
|
260
|
+
return (
|
|
261
|
+
Array.isArray(requiredScopes) &&
|
|
262
|
+
requiredScopes.some((scope) => typeof scope === "string" && scope !== "")
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The refusal for a protected tool whose protection names no scope. */
|
|
267
|
+
export function unscopedToolProtectionError(
|
|
268
|
+
toolName: string
|
|
269
|
+
): ToolProtectionUnenforceableError {
|
|
270
|
+
return new ToolProtectionUnenforceableError(
|
|
271
|
+
toolName,
|
|
272
|
+
["requiredScopes"],
|
|
273
|
+
`Tool "${toolName}" is protected, but its protection names no required ` +
|
|
274
|
+
`scope, so no delegation can be checked against it. Give it a scope to ` +
|
|
275
|
+
`call this tool.`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The refusal while no tool protection config can be read. */
|
|
280
|
+
export function unavailableToolProtectionError(
|
|
281
|
+
toolName: string
|
|
282
|
+
): ToolProtectionUnenforceableError {
|
|
283
|
+
return new ToolProtectionUnenforceableError(
|
|
284
|
+
toolName,
|
|
285
|
+
["toolProtectionConfig"],
|
|
286
|
+
`Tool "${toolName}" can't be called while its protection is unavailable: ` +
|
|
287
|
+
`the tool protection config can't be read and none is cached. It is ` +
|
|
288
|
+
`refused until the config can be read.`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export class AmountLimitError extends Error {
|
|
293
|
+
public readonly code: AmountLimitCode;
|
|
294
|
+
|
|
295
|
+
constructor(
|
|
296
|
+
public readonly toolName: string,
|
|
297
|
+
public readonly violation: AmountLimitViolation
|
|
298
|
+
) {
|
|
299
|
+
super(describeAmountLimitViolation(toolName, violation));
|
|
300
|
+
this.name = "AmountLimitError";
|
|
301
|
+
this.code = violation.code;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
toJSON() {
|
|
305
|
+
const { code, ...detail } = this.violation;
|
|
306
|
+
return {
|
|
307
|
+
error: code,
|
|
308
|
+
message: this.message,
|
|
309
|
+
toolName: this.toolName,
|
|
310
|
+
...detail,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function amountRefusalCode(code: AmountLimitCode): string {
|
|
316
|
+
return code.toUpperCase();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function amountLimitToolResult(error: AmountLimitError) {
|
|
320
|
+
return {
|
|
321
|
+
content: [{ type: "text" as const, text: JSON.stringify(error) }],
|
|
322
|
+
isError: true as const,
|
|
323
|
+
_meta: markToolRefusal(amountRefusalCode(error.code)),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
166
327
|
/**
|
|
167
328
|
* Context for an intercepted tool call that needs authorization
|
|
168
329
|
*/
|