@kya-os/mcp-i-runtime 1.2.4 → 1.2.6
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/types.d.ts +1 -1
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/config/remote-config.d.ts +3 -3
- package/dist/config/remote-config.js +4 -4
- package/dist/config/remote-config.js.map +1 -1
- package/dist/identity/user-did-manager.d.ts +4 -4
- package/dist/identity/user-did-manager.js +5 -5
- package/dist/identity/user-did-manager.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +22 -0
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/base.d.ts +17 -11
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +58 -28
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/delegation-denial.d.ts +43 -0
- package/dist/runtime/delegation-denial.d.ts.map +1 -0
- package/dist/runtime/delegation-denial.js +83 -0
- package/dist/runtime/delegation-denial.js.map +1 -0
- package/dist/runtime/verified-delegation.d.ts +30 -0
- package/dist/runtime/verified-delegation.d.ts.map +1 -0
- package/dist/runtime/verified-delegation.js +62 -0
- package/dist/runtime/verified-delegation.js.map +1 -0
- package/dist/services/access-control.service.d.ts +2 -2
- package/dist/services/access-control.service.js +4 -4
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/delegation-error-formatter.d.ts +15 -0
- package/dist/services/delegation-error-formatter.d.ts.map +1 -1
- package/dist/services/delegation-error-formatter.js +27 -1
- package/dist/services/delegation-error-formatter.js.map +1 -1
- package/dist/services/oauth-config.service.d.ts +4 -4
- package/dist/services/oauth-config.service.d.ts.map +1 -1
- package/dist/services/oauth-config.service.js +8 -8
- package/dist/services/oauth-config.service.js.map +1 -1
- package/dist/services/oauth-provider-registry.d.ts +3 -3
- package/dist/services/oauth-provider-registry.js +4 -4
- package/dist/services/oauth-provider-registry.js.map +1 -1
- package/dist/services/oauth-service.d.ts +8 -8
- package/dist/services/oauth-service.d.ts.map +1 -1
- package/dist/services/oauth-service.js +8 -8
- package/dist/services/oauth-service.js.map +1 -1
- package/dist/services/oauth-token-retrieval.service.d.ts +6 -6
- package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -1
- package/dist/services/oauth-token-retrieval.service.js +5 -5
- package/dist/services/provider-resolver.d.ts +1 -1
- package/dist/services/provider-resolver.js +4 -4
- package/dist/services/provider-resolver.js.map +1 -1
- package/dist/services/session-registration.service.d.ts +4 -4
- package/dist/services/session-registration.service.d.ts.map +1 -1
- package/dist/services/session-registration.service.js +5 -5
- package/dist/services/session-registration.service.js.map +1 -1
- package/dist/services/tool-protection.service.d.ts +13 -13
- package/dist/services/tool-protection.service.js +16 -16
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/tool-protection.d.ts +11 -4
- package/dist/types/tool-protection.d.ts.map +1 -1
- package/dist/types/tool-protection.js +8 -1
- package/dist/types/tool-protection.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/index.test.ts +12 -0
- package/src/__tests__/integration/full-flow.test.ts +3 -3
- package/src/__tests__/runtime/audit-contracts-compat.test.ts +54 -0
- package/src/__tests__/runtime/audit.test.ts +172 -0
- package/src/__tests__/runtime/delegation-denial.test.ts +115 -0
- package/src/__tests__/runtime/native-holder-enforcement.test.ts +107 -0
- package/src/__tests__/runtime/process-tool-call-security.test.ts +323 -6
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +40 -42
- package/src/__tests__/runtime/verified-delegation.test.ts +129 -0
- package/src/__tests__/services/agentshield-integration.test.ts +3 -3
- package/src/__tests__/services/oauth-service-secret-resolution.test.ts +1 -1
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +2 -2
- package/src/__tests__/services/tool-protection.service.test.ts +1 -1
- package/src/__tests__/utils/tool-protection-runtime.ts +25 -0
- package/src/auth/types.ts +1 -1
- package/src/config/remote-config.ts +4 -4
- package/src/identity/user-did-manager.ts +5 -5
- package/src/index.ts +15 -0
- package/src/runtime/audit.ts +26 -0
- package/src/runtime/base.ts +76 -31
- package/src/runtime/delegation-denial.ts +107 -0
- package/src/runtime/verified-delegation.ts +66 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +8 -8
- package/src/services/__tests__/delegation-error-formatter.test.ts +156 -0
- package/src/services/__tests__/oauth-config.service.test.ts +2 -2
- package/src/services/__tests__/oauth-provider-registry.test.ts +1 -1
- package/src/services/__tests__/storage.service.test.ts +21 -16
- package/src/services/access-control.service.ts +4 -4
- package/src/services/delegation-error-formatter.ts +42 -1
- package/src/services/oauth-config.service.d.ts +4 -4
- package/src/services/oauth-config.service.ts +9 -9
- package/src/services/oauth-provider-registry.d.ts +2 -2
- package/src/services/oauth-provider-registry.ts +4 -4
- package/src/services/oauth-service.ts +10 -10
- package/src/services/oauth-token-retrieval.service.ts +8 -8
- package/src/services/provider-resolver.ts +4 -4
- package/src/services/session-registration.service.ts +6 -6
- package/src/services/tool-protection.service.ts +17 -17
- package/src/types/tool-protection.ts +13 -4
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delegation denial mapping
|
|
3
|
+
*
|
|
4
|
+
* When the verifier refuses a presented delegation, clients and the audit
|
|
5
|
+
* ledger need to know why (a revoked grant must not look like a missing one).
|
|
6
|
+
* Upstream codes pass through a fixed allowlist onto DelegationDenialCode.
|
|
7
|
+
* The upstream `message` and `details` are never read: `details.reason` can
|
|
8
|
+
* hold free text an admin typed when revoking.
|
|
9
|
+
*
|
|
10
|
+
* @module @kya-os/mcp-i-runtime/runtime/delegation-denial
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { DelegationDenialCode } from "@kya-os/contracts/config";
|
|
14
|
+
|
|
15
|
+
export type { DelegationDenialCode };
|
|
16
|
+
|
|
17
|
+
/** A verifier refusal, safe to show to clients and to record in audit. */
|
|
18
|
+
export interface DelegationDenial {
|
|
19
|
+
code: DelegationDenialCode;
|
|
20
|
+
/** Always DEFAULT_DENIAL_MESSAGES[code]; never upstream text. */
|
|
21
|
+
message: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Product-neutral default text per denial code. Each one ends with
|
|
26
|
+
* "Nothing was executed." so a client can tell the user the tool had no effect.
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_DENIAL_MESSAGES: Readonly<
|
|
29
|
+
Record<DelegationDenialCode, string>
|
|
30
|
+
> = Object.freeze({
|
|
31
|
+
delegation_revoked:
|
|
32
|
+
"This authorization was revoked. Nothing was executed.",
|
|
33
|
+
delegation_expired:
|
|
34
|
+
"This authorization has expired. Nothing was executed.",
|
|
35
|
+
delegation_not_yet_valid:
|
|
36
|
+
"This authorization is not active yet. Nothing was executed.",
|
|
37
|
+
delegation_invalid:
|
|
38
|
+
"This authorization is no longer valid. Nothing was executed.",
|
|
39
|
+
agent_mismatch:
|
|
40
|
+
"This authorization was issued to a different agent. Nothing was executed.",
|
|
41
|
+
user_mismatch:
|
|
42
|
+
"This authorization belongs to a different user. Nothing was executed.",
|
|
43
|
+
native_binding_mismatch:
|
|
44
|
+
"This authorization does not match the agent key presented with this request. Nothing was executed.",
|
|
45
|
+
budget_exceeded:
|
|
46
|
+
"This authorization has used all of its allowed calls. Nothing was executed.",
|
|
47
|
+
insufficient_scope:
|
|
48
|
+
"This authorization does not cover this action. Nothing was executed.",
|
|
49
|
+
policy_denied:
|
|
50
|
+
"This request is outside the access policy of this authorization. Nothing was executed.",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Upstream verifier `error.code` to denial code. A Map, not an object
|
|
54
|
+
// literal, so inherited keys such as "constructor" can never match.
|
|
55
|
+
// Codes left out (billing, limit_exceeded, revocation_check_failed,
|
|
56
|
+
// project_not_found, anything new) map to undefined: the client then sees
|
|
57
|
+
// today's message, because those are not a statement about this grant.
|
|
58
|
+
const UPSTREAM_DENIAL_CODES: ReadonlyMap<string, DelegationDenialCode> =
|
|
59
|
+
new Map<string, DelegationDenialCode>([
|
|
60
|
+
["delegation_revoked", "delegation_revoked"],
|
|
61
|
+
["delegation_expired", "delegation_expired"],
|
|
62
|
+
["delegation_not_yet_valid", "delegation_not_yet_valid"],
|
|
63
|
+
["delegation_inactive", "delegation_invalid"],
|
|
64
|
+
["delegation_not_found", "delegation_invalid"],
|
|
65
|
+
["invalid_token", "delegation_invalid"],
|
|
66
|
+
["invalid_jwt", "delegation_invalid"],
|
|
67
|
+
["agent_mismatch", "agent_mismatch"],
|
|
68
|
+
["user_mismatch", "user_mismatch"],
|
|
69
|
+
["max_calls_exceeded", "budget_exceeded"],
|
|
70
|
+
["insufficient_scopes", "insufficient_scope"],
|
|
71
|
+
["origin_not_allowed", "policy_denied"],
|
|
72
|
+
["origin_required", "policy_denied"],
|
|
73
|
+
["ip_not_whitelisted", "policy_denied"],
|
|
74
|
+
["client_ip_required", "policy_denied"],
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
function denialFor(code: DelegationDenialCode): DelegationDenial {
|
|
78
|
+
return { code, message: DEFAULT_DENIAL_MESSAGES[code] };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Map an upstream verifier code to a denial, or undefined when the code is
|
|
83
|
+
* not on the allowlist.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* toDelegationDenial("delegation_revoked");
|
|
88
|
+
* // { code: "delegation_revoked", message: "This authorization was revoked. Nothing was executed." }
|
|
89
|
+
* toDelegationDenial("project_not_found"); // undefined
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function toDelegationDenial(
|
|
93
|
+
upstreamCode?: string
|
|
94
|
+
): DelegationDenial | undefined {
|
|
95
|
+
if (upstreamCode === undefined) return undefined;
|
|
96
|
+
const code = UPSTREAM_DENIAL_CODES.get(upstreamCode);
|
|
97
|
+
return code === undefined ? undefined : denialFor(code);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The denial for the runtime's own check that a verified grant matches the
|
|
102
|
+
* holder-of-key admitted on this request (native rail only). No upstream code
|
|
103
|
+
* maps here, because only the runtime makes that comparison.
|
|
104
|
+
*/
|
|
105
|
+
export function nativeBindingDenial(): DelegationDenial {
|
|
106
|
+
return denialFor("native_binding_mismatch");
|
|
107
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verified delegation mapping
|
|
3
|
+
*
|
|
4
|
+
* Turns the verifier's answer for THIS tool call into the VerifiedDelegation
|
|
5
|
+
* that protected handlers receive. Handlers enforce grant limits from this
|
|
6
|
+
* value, so it is built only from a successful verify response and never from
|
|
7
|
+
* the stored token, tool arguments or session state.
|
|
8
|
+
*
|
|
9
|
+
* @module @kya-os/mcp-i-runtime/runtime/verified-delegation
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { VerifiedDelegation } from "@kya-os/contracts/config";
|
|
13
|
+
import type { VerifyDelegationResponse } from "@kya-os/contracts/agentshield-api";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Build a VerifiedDelegation from a verify response.
|
|
17
|
+
*
|
|
18
|
+
* Returns undefined when the verifier did not accept the delegation, or when
|
|
19
|
+
* it returned no credential (KV and memory verifiers), because there is then
|
|
20
|
+
* nothing verified to hand to the handler.
|
|
21
|
+
*
|
|
22
|
+
* The result is a deep copy: a handler that mutates it cannot change the
|
|
23
|
+
* verify response the runtime still holds.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const verified = toVerifiedDelegation(verificationResult.data);
|
|
28
|
+
* // verified?.constraints?.crisp is what Checkpoint echoed for this call
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function toVerifiedDelegation(
|
|
32
|
+
data: VerifyDelegationResponse
|
|
33
|
+
): VerifiedDelegation | undefined {
|
|
34
|
+
if (data.valid !== true) return undefined;
|
|
35
|
+
|
|
36
|
+
const credential = data.credential;
|
|
37
|
+
if (!credential) return undefined;
|
|
38
|
+
|
|
39
|
+
const constraints = credential.constraints
|
|
40
|
+
? structuredClone(credential.constraints)
|
|
41
|
+
: undefined;
|
|
42
|
+
const expiresAt = positiveSeconds(constraints?.not_after);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
...(data.delegation_id === undefined ? {} : { id: data.delegation_id }),
|
|
46
|
+
...(credential.agent_did === undefined
|
|
47
|
+
? {}
|
|
48
|
+
: { agentDid: credential.agent_did }),
|
|
49
|
+
...(credential.user_did === undefined
|
|
50
|
+
? {}
|
|
51
|
+
: { userDid: credential.user_did }),
|
|
52
|
+
// Only a real array of scopes is authority; anything else means none.
|
|
53
|
+
scopes: Array.isArray(credential.scopes) ? [...credential.scopes] : [],
|
|
54
|
+
...(constraints === undefined ? {} : { constraints }),
|
|
55
|
+
...(credential.authorization?.type === undefined
|
|
56
|
+
? {}
|
|
57
|
+
: { authorizationType: credential.authorization.type }),
|
|
58
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Checkpoint stores `not_after: 0` for "no expiry", so only a positive number
|
|
63
|
+
// is a real deadline.
|
|
64
|
+
function positiveSeconds(value: unknown): number | undefined {
|
|
65
|
+
return typeof value === "number" && value > 0 ? value : undefined;
|
|
66
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* These tests verify that the proof submission response format matches
|
|
5
5
|
* the schema defined in @kya-os/contracts, preventing regressions.
|
|
6
6
|
*
|
|
7
|
-
* CRITICAL: These tests ensure API parity between MCP-I and
|
|
7
|
+
* CRITICAL: These tests ensure API parity between MCP-I and Checkpoint/Bouncer.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
@@ -28,7 +28,7 @@ describe("Proof Submission Response Validation", () => {
|
|
|
28
28
|
});
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
describe("Wrapped Response Format (
|
|
31
|
+
describe("Wrapped Response Format (Checkpoint API)", () => {
|
|
32
32
|
it("should validate wrapped response with success field in data", async () => {
|
|
33
33
|
const request: ProofSubmissionRequest = {
|
|
34
34
|
session_id: "test-session",
|
|
@@ -50,7 +50,7 @@ describe("Proof Submission Response Validation", () => {
|
|
|
50
50
|
],
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
// Simulate
|
|
53
|
+
// Simulate Checkpoint API wrapped response
|
|
54
54
|
const wrappedResponse = {
|
|
55
55
|
success: true,
|
|
56
56
|
data: {
|
|
@@ -272,8 +272,8 @@ describe("Proof Submission Response Validation", () => {
|
|
|
272
272
|
expect(result.success).toBe(true);
|
|
273
273
|
});
|
|
274
274
|
|
|
275
|
-
it("should validate schema directly matches
|
|
276
|
-
// This test ensures the schema matches what
|
|
275
|
+
it("should validate schema directly matches Checkpoint response format", () => {
|
|
276
|
+
// This test ensures the schema matches what Checkpoint actually returns
|
|
277
277
|
const agentShieldResponseData = {
|
|
278
278
|
success: true,
|
|
279
279
|
accepted: 1,
|
|
@@ -295,7 +295,7 @@ describe("Proof Submission Response Validation", () => {
|
|
|
295
295
|
}
|
|
296
296
|
});
|
|
297
297
|
|
|
298
|
-
it("should validate schema with errors array matches
|
|
298
|
+
it("should validate schema with errors array matches Checkpoint format", () => {
|
|
299
299
|
const agentShieldResponseData = {
|
|
300
300
|
success: true,
|
|
301
301
|
accepted: 0,
|
|
@@ -332,7 +332,7 @@ describe("Proof Submission Response Validation", () => {
|
|
|
332
332
|
|
|
333
333
|
describe("Regression Prevention", () => {
|
|
334
334
|
it("should prevent regression: missing success field in data", () => {
|
|
335
|
-
// This test ensures we catch if
|
|
335
|
+
// This test ensures we catch if Checkpoint changes response format
|
|
336
336
|
const responseWithoutSuccess = {
|
|
337
337
|
accepted: 1,
|
|
338
338
|
rejected: 0,
|
|
@@ -419,7 +419,7 @@ describe("Proof Submission Response Validation", () => {
|
|
|
419
419
|
],
|
|
420
420
|
};
|
|
421
421
|
|
|
422
|
-
// Simulate the exact response format from
|
|
422
|
+
// Simulate the exact response format from Checkpoint API
|
|
423
423
|
// This matches the format seen in production logs
|
|
424
424
|
const wrappedResponse = {
|
|
425
425
|
success: true,
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
createDelegationErrorFormatter,
|
|
17
17
|
} from "../delegation-error-formatter";
|
|
18
18
|
import type { ClientMessagesConfig } from "../delegation-error-formatter";
|
|
19
|
+
import { toDelegationDenial } from "../../runtime/delegation-denial";
|
|
19
20
|
|
|
20
21
|
describe("DelegationErrorFormatter", () => {
|
|
21
22
|
describe("default behavior (no config)", () => {
|
|
@@ -459,6 +460,161 @@ describe("DelegationErrorFormatter", () => {
|
|
|
459
460
|
});
|
|
460
461
|
});
|
|
461
462
|
|
|
463
|
+
describe("verifier denials", () => {
|
|
464
|
+
const consentUrl = "https://example.com/consent?resume=abc";
|
|
465
|
+
const revoked = toDelegationDenial("delegation_revoked");
|
|
466
|
+
// Today's default output, pinned so a denial without configuration
|
|
467
|
+
// provably changes nothing.
|
|
468
|
+
const todaysMessage = `Authorization required for "place_order".
|
|
469
|
+
|
|
470
|
+
Authorization URL: ${consentUrl}
|
|
471
|
+
|
|
472
|
+
Share this URL with the user so they can authorize in their browser, then retry.`;
|
|
473
|
+
|
|
474
|
+
it("puts the configured denial line first and keeps the consent URL", () => {
|
|
475
|
+
const formatter = new DelegationErrorFormatter({
|
|
476
|
+
default: {
|
|
477
|
+
denialMessages: {
|
|
478
|
+
delegation_revoked:
|
|
479
|
+
"Your approval for {toolName} was revoked in the dashboard.",
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
const result = formatter.format({
|
|
485
|
+
toolName: "place_order",
|
|
486
|
+
consentUrl,
|
|
487
|
+
denial: revoked,
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
expect(result.message).toBe(
|
|
491
|
+
`Your approval for place_order was revoked in the dashboard.\n\n${todaysMessage}`
|
|
492
|
+
);
|
|
493
|
+
expect(result.consentUrl).toBe(consentUrl);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it("prefers the client's denial message over the default template's", () => {
|
|
497
|
+
const formatter = new DelegationErrorFormatter({
|
|
498
|
+
default: {
|
|
499
|
+
denialMessages: { delegation_revoked: "Default: revoked." },
|
|
500
|
+
},
|
|
501
|
+
clients: {
|
|
502
|
+
claude: {
|
|
503
|
+
denialMessages: { delegation_revoked: "Claude: revoked." },
|
|
504
|
+
},
|
|
505
|
+
},
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
const claudeResult = formatter.format({
|
|
509
|
+
toolName: "place_order",
|
|
510
|
+
consentUrl,
|
|
511
|
+
clientId: "claude",
|
|
512
|
+
denial: revoked,
|
|
513
|
+
});
|
|
514
|
+
const cursorResult = formatter.format({
|
|
515
|
+
toolName: "place_order",
|
|
516
|
+
consentUrl,
|
|
517
|
+
clientId: "cursor",
|
|
518
|
+
denial: revoked,
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
expect(claudeResult.message.startsWith("Claude: revoked.\n\n")).toBe(true);
|
|
522
|
+
expect(cursorResult.message.startsWith("Default: revoked.\n\n")).toBe(true);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("falls back to the default template per code when the client template lacks that code", () => {
|
|
526
|
+
const formatter = new DelegationErrorFormatter({
|
|
527
|
+
default: {
|
|
528
|
+
denialMessages: { delegation_revoked: "Default: revoked." },
|
|
529
|
+
},
|
|
530
|
+
clients: {
|
|
531
|
+
claude: {
|
|
532
|
+
denialMessages: { agent_mismatch: "Claude: wrong agent." },
|
|
533
|
+
},
|
|
534
|
+
},
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const result = formatter.format({
|
|
538
|
+
toolName: "place_order",
|
|
539
|
+
consentUrl,
|
|
540
|
+
clientId: "claude",
|
|
541
|
+
denial: revoked,
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
expect(result.message.startsWith("Default: revoked.\n\n")).toBe(true);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it("is byte-identical to today when a denial has no configured message", () => {
|
|
548
|
+
const formatter = new DelegationErrorFormatter({
|
|
549
|
+
default: { denialMessages: { agent_mismatch: "Wrong agent." } },
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
const withDenial = formatter.format({
|
|
553
|
+
toolName: "place_order",
|
|
554
|
+
consentUrl,
|
|
555
|
+
denial: revoked,
|
|
556
|
+
});
|
|
557
|
+
const withoutDenial = formatter.format({
|
|
558
|
+
toolName: "place_order",
|
|
559
|
+
consentUrl,
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
expect(withDenial.message).toBe(todaysMessage);
|
|
563
|
+
expect(withoutDenial.message).toBe(todaysMessage);
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
it("is byte-identical to today with no config at all", () => {
|
|
567
|
+
const result = new DelegationErrorFormatter().format({
|
|
568
|
+
toolName: "place_order",
|
|
569
|
+
consentUrl,
|
|
570
|
+
denial: revoked,
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
expect(result.message).toBe(todaysMessage);
|
|
574
|
+
expect(result.message).not.toContain(revoked!.message);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("prepends the denial line to the no-URL message too", () => {
|
|
578
|
+
const formatter = new DelegationErrorFormatter({
|
|
579
|
+
default: { denialMessages: { delegation_revoked: "Revoked." } },
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
const result = formatter.format({
|
|
583
|
+
toolName: "place_order",
|
|
584
|
+
denial: revoked,
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
expect(result.message).toBe(
|
|
588
|
+
`Revoked.\n\nAuthorization required for "place_order" but no consent URL is available.
|
|
589
|
+
|
|
590
|
+
Please contact the administrator to configure delegation for this tool.`
|
|
591
|
+
);
|
|
592
|
+
expect(result.consentUrl).toBeUndefined();
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
it("still strips markdown links, including in the denial line, for clients without markdown", () => {
|
|
596
|
+
const formatter = new DelegationErrorFormatter({
|
|
597
|
+
default: {
|
|
598
|
+
message: "[Authorize {toolName}]({consentUrl})",
|
|
599
|
+
denialMessages: {
|
|
600
|
+
delegation_revoked: "Revoked. [Review grants](https://example.com/grants)",
|
|
601
|
+
},
|
|
602
|
+
supportsMarkdown: false,
|
|
603
|
+
},
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
const result = formatter.format({
|
|
607
|
+
toolName: "place_order",
|
|
608
|
+
consentUrl,
|
|
609
|
+
denial: revoked,
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
expect(result.message).toBe(
|
|
613
|
+
`Revoked. Review grants: https://example.com/grants\n\nAuthorize place_order: ${consentUrl}`
|
|
614
|
+
);
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
|
|
462
618
|
describe("formatOAuth", () => {
|
|
463
619
|
describe("default behavior (no config)", () => {
|
|
464
620
|
it("should format OAuth message with URL using default template", () => {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Tests for OAuthConfigService
|
|
3
3
|
*
|
|
4
4
|
* Covers:
|
|
5
|
-
* - OAuth config fetching from
|
|
5
|
+
* - OAuth config fetching from Checkpoint API
|
|
6
6
|
* - Caching behavior
|
|
7
7
|
* - Error handling
|
|
8
8
|
* - snake_case to camelCase mapping
|
|
@@ -175,7 +175,7 @@ describe("OAuthConfigService", () => {
|
|
|
175
175
|
// OAuthService.resolveClientSecret could not resolve a confidential
|
|
176
176
|
// provider's secret from secure storage (it keys off
|
|
177
177
|
// `metadata.clientSecretName`). It then fell back to the inline
|
|
178
|
-
// `clientSecret` — which the
|
|
178
|
+
// `clientSecret` — which the Checkpoint /providers response
|
|
179
179
|
// intentionally OMITS — so the worker sent an empty secret and the
|
|
180
180
|
// OAuth token exchange failed with "client_id and/or client_secret
|
|
181
181
|
// incorrect".
|
|
@@ -45,7 +45,7 @@ describe("OAuthProviderRegistry", () => {
|
|
|
45
45
|
});
|
|
46
46
|
|
|
47
47
|
describe("loadFromAgentShield", () => {
|
|
48
|
-
it("should load providers from
|
|
48
|
+
it("should load providers from Checkpoint config", async () => {
|
|
49
49
|
await registry.loadFromAgentShield("test-project");
|
|
50
50
|
|
|
51
51
|
expect(mockConfigService.getOAuthConfig).toHaveBeenCalledWith(
|
|
@@ -6,6 +6,23 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { describe, it, expect, vi } from 'vitest';
|
|
9
|
+
|
|
10
|
+
// Hoist-safe redis mock (vitest hoists vi.mock above the imports, so the
|
|
11
|
+
// client must come from vi.hoisted). The nested vi.mock this replaces was
|
|
12
|
+
// hoisted here anyway; vitest >=4.1.10 rejects the misleading placement.
|
|
13
|
+
const mockRedisClient = vi.hoisted(() => ({
|
|
14
|
+
connect: vi.fn().mockResolvedValue(undefined),
|
|
15
|
+
ping: vi.fn().mockResolvedValue('PONG'),
|
|
16
|
+
get: vi.fn(),
|
|
17
|
+
set: vi.fn(),
|
|
18
|
+
setEx: vi.fn(),
|
|
19
|
+
del: vi.fn(),
|
|
20
|
+
exists: vi.fn(),
|
|
21
|
+
keys: vi.fn().mockResolvedValue([]),
|
|
22
|
+
}));
|
|
23
|
+
vi.mock('redis', () => ({
|
|
24
|
+
createClient: vi.fn().mockReturnValue(mockRedisClient),
|
|
25
|
+
}));
|
|
9
26
|
import {
|
|
10
27
|
createStorageProviders,
|
|
11
28
|
StorageKeyHelpers,
|
|
@@ -43,22 +60,6 @@ describe('StorageService', () => {
|
|
|
43
60
|
});
|
|
44
61
|
|
|
45
62
|
it('should prefer Redis over KV when both are configured', async () => {
|
|
46
|
-
const mockRedisClient = {
|
|
47
|
-
connect: vi.fn().mockResolvedValue(undefined),
|
|
48
|
-
ping: vi.fn().mockResolvedValue('PONG'),
|
|
49
|
-
get: vi.fn(),
|
|
50
|
-
set: vi.fn(),
|
|
51
|
-
setEx: vi.fn(),
|
|
52
|
-
del: vi.fn(),
|
|
53
|
-
exists: vi.fn(),
|
|
54
|
-
keys: vi.fn().mockResolvedValue([]),
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// Mock redis import
|
|
58
|
-
vi.mock('redis', () => ({
|
|
59
|
-
createClient: vi.fn().mockReturnValue(mockRedisClient),
|
|
60
|
-
}));
|
|
61
|
-
|
|
62
63
|
const config: StorageServiceConfig = {
|
|
63
64
|
redisUrl: 'redis://localhost:6379',
|
|
64
65
|
kvNamespace: {
|
|
@@ -81,6 +82,10 @@ describe('StorageService', () => {
|
|
|
81
82
|
});
|
|
82
83
|
|
|
83
84
|
it('should fall back to memory when Redis connection fails', async () => {
|
|
85
|
+
// The module-level redis mock connects successfully by default; this
|
|
86
|
+
// case needs the connection itself to fail.
|
|
87
|
+
mockRedisClient.connect.mockRejectedValueOnce(new Error('connection refused'));
|
|
88
|
+
mockRedisClient.ping.mockRejectedValueOnce(new Error('connection refused'));
|
|
84
89
|
const config: StorageServiceConfig = {
|
|
85
90
|
redisUrl: 'redis://invalid:6379',
|
|
86
91
|
fallbackToMemory: true,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Access Control API Service
|
|
3
3
|
*
|
|
4
4
|
* Brand-neutral service for interacting with access-control APIs
|
|
5
|
-
* (currently
|
|
5
|
+
* (currently Checkpoint/bouncer, but designed to be provider-agnostic).
|
|
6
6
|
*
|
|
7
7
|
* This service provides:
|
|
8
8
|
* - Delegation verification
|
|
@@ -98,7 +98,7 @@ function generateCorrelationId(): string {
|
|
|
98
98
|
/**
|
|
99
99
|
* Access Control API Service
|
|
100
100
|
*
|
|
101
|
-
* Handles all interactions with the access-control API (
|
|
101
|
+
* Handles all interactions with the access-control API (Checkpoint/bouncer).
|
|
102
102
|
* Designed to be brand-neutral and work with any access-control provider.
|
|
103
103
|
*/
|
|
104
104
|
export class AccessControlApiService {
|
|
@@ -258,9 +258,9 @@ export class AccessControlApiService {
|
|
|
258
258
|
(e) => e.path.includes("scopes") && e.message === "Required"
|
|
259
259
|
);
|
|
260
260
|
if (scopesError && !("scopes" in cleanedRequestBody)) {
|
|
261
|
-
// This is a known Zod schema issue in
|
|
261
|
+
// This is a known Zod schema issue in Checkpoint - scopes is optional but Zod treats it as required
|
|
262
262
|
// Skip validation for this case since the field is correctly omitted
|
|
263
|
-
// TODO: Remove this workaround once
|
|
263
|
+
// TODO: Remove this workaround once Checkpoint fixes their schema
|
|
264
264
|
} else {
|
|
265
265
|
this.config.logger(`[AccessControl] Validation failed:`, {
|
|
266
266
|
errors: validationResult.error.errors,
|
|
@@ -17,6 +17,8 @@ import type {
|
|
|
17
17
|
ClientMessagesConfig,
|
|
18
18
|
} from "@kya-os/contracts/config";
|
|
19
19
|
|
|
20
|
+
import type { DelegationDenial } from "../runtime/delegation-denial.js";
|
|
21
|
+
|
|
20
22
|
// Re-export types for convenience
|
|
21
23
|
export type { ClientMessageTemplate, ClientMessagesConfig };
|
|
22
24
|
|
|
@@ -32,6 +34,11 @@ export interface DelegationErrorFormatOptions {
|
|
|
32
34
|
scopes?: string[];
|
|
33
35
|
/** Client ID (e.g., "claude", "chatgpt", "cursor") - used for client-specific messages */
|
|
34
36
|
clientId?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Why the verifier refused a presented delegation. Changes the message
|
|
39
|
+
* only when a `denialMessages` entry is configured for its code.
|
|
40
|
+
*/
|
|
41
|
+
denial?: DelegationDenial;
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
/**
|
|
@@ -132,7 +139,7 @@ export class DelegationErrorFormatter {
|
|
|
132
139
|
* @returns Formatted message and potentially modified consent URL
|
|
133
140
|
*/
|
|
134
141
|
format(options: DelegationErrorFormatOptions): DelegationErrorResult {
|
|
135
|
-
const { toolName, consentUrl, scopes, clientId } = options;
|
|
142
|
+
const { toolName, consentUrl, scopes, clientId, denial } = options;
|
|
136
143
|
|
|
137
144
|
// Get the appropriate template for this client
|
|
138
145
|
const template = this.getTemplate(clientId);
|
|
@@ -163,9 +170,43 @@ export class DelegationErrorFormatter {
|
|
|
163
170
|
});
|
|
164
171
|
}
|
|
165
172
|
|
|
173
|
+
const denialLine = this.formatDenialLine(denial, clientId, toolName, template);
|
|
174
|
+
if (denialLine !== undefined) {
|
|
175
|
+
message = `${denialLine}\n\n${message}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
166
178
|
return { message, consentUrl: finalUrl };
|
|
167
179
|
}
|
|
168
180
|
|
|
181
|
+
/**
|
|
182
|
+
* The configured line for a verifier denial, or undefined when none is
|
|
183
|
+
* configured for its code (the message then stays exactly as today).
|
|
184
|
+
*
|
|
185
|
+
* Resolved per code rather than through getTemplate's shallow merge, so a
|
|
186
|
+
* client template that configures other codes does not hide the default
|
|
187
|
+
* template's line for this one.
|
|
188
|
+
*/
|
|
189
|
+
private formatDenialLine(
|
|
190
|
+
denial: DelegationDenial | undefined,
|
|
191
|
+
clientId: string | undefined,
|
|
192
|
+
toolName: string,
|
|
193
|
+
template: ClientMessageTemplate
|
|
194
|
+
): string | undefined {
|
|
195
|
+
if (!denial) return undefined;
|
|
196
|
+
|
|
197
|
+
const clientLine = clientId
|
|
198
|
+
? this.config?.clients?.[clientId]?.denialMessages?.[denial.code]
|
|
199
|
+
: undefined;
|
|
200
|
+
const configuredLine =
|
|
201
|
+
clientLine ?? this.config?.default?.denialMessages?.[denial.code];
|
|
202
|
+
if (configuredLine === undefined) return undefined;
|
|
203
|
+
|
|
204
|
+
const line = this.interpolate(configuredLine, { toolName });
|
|
205
|
+
return template.supportsMarkdown === false
|
|
206
|
+
? this.stripMarkdownLinks(line)
|
|
207
|
+
: line;
|
|
208
|
+
}
|
|
209
|
+
|
|
169
210
|
/**
|
|
170
211
|
* Format an OAuth error message
|
|
171
212
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OAuth Config Service
|
|
3
3
|
*
|
|
4
|
-
* Fetches and caches OAuth provider configurations from
|
|
4
|
+
* Fetches and caches OAuth provider configurations from Checkpoint API.
|
|
5
5
|
* Provides caching with TTL to reduce API calls.
|
|
6
6
|
*
|
|
7
7
|
* @package @kya-os/mcp-i-core
|
|
@@ -10,7 +10,7 @@ import type { FetchProvider } from "../providers/base.js";
|
|
|
10
10
|
import type { OAuthConfig } from "@kya-os/contracts/config";
|
|
11
11
|
import type { OAuthConfigCache } from "../cache/oauth-config-cache.js";
|
|
12
12
|
export interface OAuthConfigServiceConfig {
|
|
13
|
-
/** Base URL for
|
|
13
|
+
/** Base URL for Checkpoint API (e.g., "https://kya.vouched.id") */
|
|
14
14
|
baseUrl: string;
|
|
15
15
|
/** API key for authentication */
|
|
16
16
|
apiKey: string;
|
|
@@ -24,7 +24,7 @@ export interface OAuthConfigServiceConfig {
|
|
|
24
24
|
logger?: (message: string, data?: unknown) => void;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
|
-
* Service for fetching OAuth provider configurations from
|
|
27
|
+
* Service for fetching OAuth provider configurations from Checkpoint API
|
|
28
28
|
*/
|
|
29
29
|
export declare class OAuthConfigService {
|
|
30
30
|
private config;
|
|
@@ -32,7 +32,7 @@ export declare class OAuthConfigService {
|
|
|
32
32
|
/**
|
|
33
33
|
* Get OAuth configuration for a project
|
|
34
34
|
*
|
|
35
|
-
* Fetches from
|
|
35
|
+
* Fetches from Checkpoint API: GET /api/v1/bouncer/projects/{projectId}/providers
|
|
36
36
|
* Caches responses with TTL to reduce API calls.
|
|
37
37
|
*
|
|
38
38
|
* @param projectId - Project ID to fetch config for
|