@kya-os/mcp-i-runtime 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -3
- package/dist/index.js.map +1 -1
- package/dist/services/auth-type-satisfies.d.ts +134 -0
- package/dist/services/auth-type-satisfies.d.ts.map +1 -0
- package/dist/services/auth-type-satisfies.js +180 -0
- package/dist/services/auth-type-satisfies.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +6 -1
- package/dist/services/tool-context-builder.d.ts.map +1 -1
- package/dist/services/tool-context-builder.js +48 -3
- package/dist/services/tool-context-builder.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +11 -0
- package/src/services/__tests__/auth-type-satisfies.test.ts +293 -0
- package/src/services/__tests__/tool-context-builder-authtype.test.ts +242 -0
- package/src/services/auth-type-satisfies.ts +189 -0
- package/src/services/tool-context-builder.ts +85 -18
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth-Type Satisfaction Check
|
|
3
|
+
*
|
|
4
|
+
* Pure comparison used by the delegation-enforcement gate (security fix:
|
|
5
|
+
* consent-approve auth-gate, 2026-07-29, Task B3) to decide whether a
|
|
6
|
+
* delegation's authorization type satisfies a tool's required authorization
|
|
7
|
+
* type.
|
|
8
|
+
*
|
|
9
|
+
* Background: a gated MCP tool call is authorized by presenting a
|
|
10
|
+
* delegation. A bug let a delegation of ANY auth type satisfy a tool that
|
|
11
|
+
* requires a specific type — e.g. an anonymous consent-only (`none`)
|
|
12
|
+
* delegation wrongly satisfied a `password`-gated tool, because call-time
|
|
13
|
+
* enforcement accepted any present delegation without checking its
|
|
14
|
+
* `authorization.type`. This module implements the comparison that closes
|
|
15
|
+
* that gap. Wiring it into the enforcement sites (`agent.ts`,
|
|
16
|
+
* `tool-context-builder.ts`) is a later task (B3) — this module is
|
|
17
|
+
* intentionally standalone and unused by any gate yet.
|
|
18
|
+
*
|
|
19
|
+
* Decision (locked by product owner, 2026-07-29): STRICT NORMALIZED
|
|
20
|
+
* EQUALITY. A delegation type satisfies a required type iff, after folding
|
|
21
|
+
* deprecated aliases, they are equal. There is no "stronger satisfies
|
|
22
|
+
* weaker" lattice — `oauth`/`oauth2` and `password` are NOT interchangeable,
|
|
23
|
+
* and a `password` delegation does NOT satisfy a `none`-gated tool (or vice
|
|
24
|
+
* versa).
|
|
25
|
+
*
|
|
26
|
+
* @module @kya-os/mcp-i-runtime/services/auth-type-satisfies
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Fold deprecated authorization-type aliases to their canonical form and
|
|
31
|
+
* normalize casing/whitespace.
|
|
32
|
+
*
|
|
33
|
+
* Alias rules mirror `normalizeAuthorizationType` in
|
|
34
|
+
* `packages/contracts/src/tool-protection/index.ts` (~774): `oauth` folds to
|
|
35
|
+
* `oauth2`, `credential` folds to `verifiable_credential`. That function
|
|
36
|
+
* operates on a full `AuthorizationRequirement` object and emits a
|
|
37
|
+
* deprecation warning on every call; this is a bare-string equivalent for
|
|
38
|
+
* use at the enforcement gate, where only the stored/required type strings
|
|
39
|
+
* — not the full requirement object — are available, and where a warning
|
|
40
|
+
* log on every tool call would be noise. (Not reusing the alias mapping
|
|
41
|
+
* directly from `@kya-os/contracts` here — it is not exposed as a
|
|
42
|
+
* standalone string-keyed map, only baked into that function's control
|
|
43
|
+
* flow — so the two rules are mirrored inline per the task's fallback
|
|
44
|
+
* guidance.)
|
|
45
|
+
*
|
|
46
|
+
* Canonical types (`AUTHORIZATION_TYPES`, contracts/src/tool-protection/index.ts
|
|
47
|
+
* ~88-106) are stored as lowercase string consts (e.g. "password", "oauth2",
|
|
48
|
+
* "none"), so normalization here only needs to trim and lowercase for
|
|
49
|
+
* defensive consistency — it does not need to handle differently-cased
|
|
50
|
+
* input from any known caller today.
|
|
51
|
+
*
|
|
52
|
+
* @param type - Raw authorization type string, or nullish if unknown/missing.
|
|
53
|
+
* @returns The normalized type string, or `undefined` if `type` is nullish or
|
|
54
|
+
* empty/whitespace-only after trimming.
|
|
55
|
+
*/
|
|
56
|
+
export function normalizeAuthType(
|
|
57
|
+
type: string | undefined | null
|
|
58
|
+
): string | undefined {
|
|
59
|
+
if (type === undefined || type === null) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const trimmed = type.trim().toLowerCase();
|
|
64
|
+
if (trimmed === "") {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Deprecated alias -> canonical (mirrors normalizeAuthorizationType).
|
|
69
|
+
if (trimmed === "oauth") {
|
|
70
|
+
return "oauth2";
|
|
71
|
+
}
|
|
72
|
+
if (trimmed === "credential") {
|
|
73
|
+
return "verifiable_credential";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return trimmed;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Decide whether a delegation's authorization type satisfies a tool's
|
|
81
|
+
* required authorization type.
|
|
82
|
+
*
|
|
83
|
+
* Strict normalized equality (locked decision, see module docs above): both
|
|
84
|
+
* types are normalized (alias-folded, trimmed, lowercased) and then compared
|
|
85
|
+
* for exact equality. There is no cross-type satisfaction — a `password`
|
|
86
|
+
* delegation does NOT satisfy an `oauth2`-gated tool, and (by the same
|
|
87
|
+
* strict rule) does NOT satisfy a `none`-gated tool either.
|
|
88
|
+
*
|
|
89
|
+
* Note on `none`-required tools: per strict equality, only a `none`
|
|
90
|
+
* delegation satisfies a `none` requirement. In practice, a tool requiring
|
|
91
|
+
* `none` (consent-only) is not expected to reach this call today — it is
|
|
92
|
+
* rejected earlier, before the delegation-presence gate, by
|
|
93
|
+
* `ConsentOnlyModeError` (see `provider-resolver.ts`) — so this function is
|
|
94
|
+
* expected to be invoked only for TYPED requirements (`password`, `oauth2`,
|
|
95
|
+
* `apikey`, etc.). Strict equality is still implemented faithfully for
|
|
96
|
+
* `none` in case that assumption ever changes.
|
|
97
|
+
*
|
|
98
|
+
* Fail-safe rules (both nullish cases return `false`, never `true`):
|
|
99
|
+
* - If `delegationType` is nullish (missing on a legacy stored record that
|
|
100
|
+
* predates persisting `authorizationType`, or simply absent), this
|
|
101
|
+
* returns `false` regardless of `requiredType` — an unknown/unrecorded
|
|
102
|
+
* delegation type never satisfies a typed requirement.
|
|
103
|
+
* - If `requiredType` is nullish (or normalizes to empty), there is no
|
|
104
|
+
* requirement to compare against. Callers are not expected to invoke this
|
|
105
|
+
* with a nullish `requiredType` (a tool with no authorization requirement
|
|
106
|
+
* doesn't reach this gate), but this fails safe to `false` rather than
|
|
107
|
+
* treating "no requirement" as automatically satisfied.
|
|
108
|
+
*
|
|
109
|
+
* @param delegationType - The delegation's stored/asserted authorization
|
|
110
|
+
* type (e.g. from `getDelegationFromStorage`), or nullish if unknown.
|
|
111
|
+
* @param requiredType - The tool's required authorization type (e.g.
|
|
112
|
+
* `protection.authorization.type`), or nullish.
|
|
113
|
+
* @returns `true` iff both types are present and equal after normalization.
|
|
114
|
+
*/
|
|
115
|
+
export function authTypeSatisfies(
|
|
116
|
+
delegationType: string | undefined | null,
|
|
117
|
+
requiredType: string | undefined | null
|
|
118
|
+
): boolean {
|
|
119
|
+
const normalizedDelegationType = normalizeAuthType(delegationType);
|
|
120
|
+
if (normalizedDelegationType === undefined) {
|
|
121
|
+
// Fail-safe: an unknown/missing delegation type never satisfies a
|
|
122
|
+
// requirement, typed or not.
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const normalizedRequiredType = normalizeAuthType(requiredType);
|
|
127
|
+
if (normalizedRequiredType === undefined) {
|
|
128
|
+
// Fail-safe: no requirement to satisfy is not treated as "anything
|
|
129
|
+
// satisfies it".
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return normalizedDelegationType === normalizedRequiredType;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The normalized authorization types the mint side can actually produce today.
|
|
138
|
+
*
|
|
139
|
+
* Every delegation is minted with one of these three:
|
|
140
|
+
* - `password` — the credential clickwrap, which rewrites `provider_type` to
|
|
141
|
+
* PASSWORD for EVERY credential provider (`consent.service.ts`);
|
|
142
|
+
* - `oauth2` — the OAuth callback, which persists it as a literal;
|
|
143
|
+
* - `none` — the pure consent-only branch.
|
|
144
|
+
*
|
|
145
|
+
* This is deliberately about what is MINTED, not what is declarable: the
|
|
146
|
+
* `AUTHORIZATION_TYPES` contract also allows `mdl`, `idv`,
|
|
147
|
+
* `verifiable_credential`, `webauthn` and `siwe`.
|
|
148
|
+
*/
|
|
149
|
+
export const MINTABLE_AUTH_TYPES: ReadonlySet<string> = new Set([
|
|
150
|
+
"password",
|
|
151
|
+
"oauth2",
|
|
152
|
+
"none",
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Whether a tool's required authorization type is one that no mint can ever
|
|
157
|
+
* produce — meaning the delegation gate can never be satisfied and the tool
|
|
158
|
+
* will re-challenge forever rather than once.
|
|
159
|
+
*
|
|
160
|
+
* Under strict normalized equality (the locked decision), a requirement
|
|
161
|
+
* outside {@link MINTABLE_AUTH_TYPES} is unsatisfiable by construction: no
|
|
162
|
+
* delegation carrying such a type is ever written, so `authTypeSatisfies`
|
|
163
|
+
* cannot return true for it. Before the auth-type gate existed these tools
|
|
164
|
+
* authorized on mere delegation presence, so this is a behaviour change that
|
|
165
|
+
* presents to a user as an inexplicable auth loop.
|
|
166
|
+
*
|
|
167
|
+
* This exists purely so the enforcement gates can SAY so in their logs. It
|
|
168
|
+
* makes no authorization decision and must never be used to relax one —
|
|
169
|
+
* treating "unsatisfiable" as "allow" would reopen exactly the bypass the
|
|
170
|
+
* gate closes.
|
|
171
|
+
*
|
|
172
|
+
* `apikey` is intentionally NOT special-cased here: api-key tools are
|
|
173
|
+
* resolved by a separate vault branch that returns before either delegation
|
|
174
|
+
* gate, so this predicate is never consulted for them.
|
|
175
|
+
*
|
|
176
|
+
* @param requiredType - The tool's required authorization type.
|
|
177
|
+
* @returns `true` if the requirement can never be met by any minted
|
|
178
|
+
* delegation. Nullish/unknown input returns `false` — absence of a
|
|
179
|
+
* requirement is not an unsatisfiable requirement.
|
|
180
|
+
*/
|
|
181
|
+
export function isUnsatisfiableAuthType(
|
|
182
|
+
requiredType: string | undefined | null
|
|
183
|
+
): boolean {
|
|
184
|
+
const normalized = normalizeAuthType(requiredType);
|
|
185
|
+
if (normalized === undefined) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return !MINTABLE_AUTH_TYPES.has(normalized);
|
|
189
|
+
}
|
|
@@ -21,6 +21,11 @@ import {
|
|
|
21
21
|
ConsentOnlyModeError,
|
|
22
22
|
CredentialAuthModeError,
|
|
23
23
|
} from "./provider-resolver.js";
|
|
24
|
+
import {
|
|
25
|
+
authTypeSatisfies,
|
|
26
|
+
isUnsatisfiableAuthType,
|
|
27
|
+
MINTABLE_AUTH_TYPES,
|
|
28
|
+
} from "./auth-type-satisfies.js";
|
|
24
29
|
|
|
25
30
|
export interface ToolContextBuilderConfig {
|
|
26
31
|
/** IDP token resolver for resolving tokens from User DID */
|
|
@@ -69,6 +74,11 @@ export class ToolContextBuilder {
|
|
|
69
74
|
* @param sessionId - Session ID (optional)
|
|
70
75
|
* @param delegationToken - Delegation token (optional)
|
|
71
76
|
* @param toolProtection - Tool protection configuration (optional)
|
|
77
|
+
* @param delegationAuthorizationType - The stored authorization type of the
|
|
78
|
+
* presented delegation (from `getDelegationFromStorage`), or undefined for
|
|
79
|
+
* legacy records that predate persisting it. Used by the delegation-only
|
|
80
|
+
* fallback below to reject a delegation whose type does not satisfy the
|
|
81
|
+
* tool's required type (security fix: consent-approve auth-gate, Task B3).
|
|
72
82
|
* @returns Tool execution context or undefined if not needed
|
|
73
83
|
*/
|
|
74
84
|
async buildContext(
|
|
@@ -76,7 +86,8 @@ export class ToolContextBuilder {
|
|
|
76
86
|
userDid: string | undefined,
|
|
77
87
|
sessionId: string | undefined,
|
|
78
88
|
delegationToken: string | undefined,
|
|
79
|
-
toolProtection: ToolProtection | null
|
|
89
|
+
toolProtection: ToolProtection | null,
|
|
90
|
+
delegationAuthorizationType?: string
|
|
80
91
|
): Promise<ToolExecutionContext | undefined> {
|
|
81
92
|
// Only build context if tool requires OAuth/credentials
|
|
82
93
|
if (!toolProtection?.requiredScopes?.length || !userDid) {
|
|
@@ -127,14 +138,34 @@ export class ToolContextBuilder {
|
|
|
127
138
|
// IDP token not available - check if delegation token exists
|
|
128
139
|
// If delegation exists, tool can still execute without IDP token
|
|
129
140
|
// (the tool handler should check for idpToken if it needs external API access)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
141
|
+
//
|
|
142
|
+
// SECURITY (consent-approve auth-gate, Task B3): a delegation only
|
|
143
|
+
// authorizes this tool if its OWN authorization type satisfies the tool's
|
|
144
|
+
// required type. Reaching this OAuth context builder means the tool is
|
|
145
|
+
// neither consent-only (`none`) nor password — those throw
|
|
146
|
+
// ConsentOnlyModeError / CredentialAuthModeError inside resolveProvider,
|
|
147
|
+
// BEFORE this point — so the effective requirement is OAuth. Fall back to
|
|
148
|
+
// "oauth2" only when the tool omits an explicit authorization.type (legacy
|
|
149
|
+
// oauthProvider-only config); this keeps such tools working while a
|
|
150
|
+
// mismatched delegation (e.g. an anonymous `none` consent-only delegation,
|
|
151
|
+
// or a legacy record with no persisted type → undefined) is rejected. On
|
|
152
|
+
// mismatch we do NOT return the delegation-only context; we fall through
|
|
153
|
+
// to OAuthRequiredError so the tool re-challenges.
|
|
154
|
+
const requiredAuthType = toolProtection.authorization?.type ?? "oauth2";
|
|
155
|
+
if (
|
|
156
|
+
delegationToken &&
|
|
157
|
+
authTypeSatisfies(delegationAuthorizationType, requiredAuthType)
|
|
158
|
+
) {
|
|
159
|
+
this.config.logger(
|
|
160
|
+
"[ToolContextBuilder] IDP token not found, but delegation exists - returning minimal context",
|
|
161
|
+
{
|
|
162
|
+
toolName,
|
|
163
|
+
userDid: userDid.substring(0, 20) + "...",
|
|
164
|
+
provider,
|
|
165
|
+
scopes: toolProtection.requiredScopes,
|
|
166
|
+
note: "Tool can execute with delegation-only authorization",
|
|
167
|
+
}
|
|
168
|
+
);
|
|
138
169
|
|
|
139
170
|
// Return minimal context with delegation info
|
|
140
171
|
// Tools that need IDP token for external API calls should check for idpToken
|
|
@@ -147,13 +178,48 @@ export class ToolContextBuilder {
|
|
|
147
178
|
};
|
|
148
179
|
}
|
|
149
180
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
181
|
+
if (delegationToken) {
|
|
182
|
+
// A delegation WAS presented but its authorization type does not satisfy
|
|
183
|
+
// the tool's required type — log and re-challenge instead of authorizing.
|
|
184
|
+
this.config.logger(
|
|
185
|
+
"[ToolContextBuilder] Delegation present but auth-type does not satisfy tool requirement - re-challenging",
|
|
186
|
+
{
|
|
187
|
+
toolName,
|
|
188
|
+
userDid: userDid.substring(0, 20) + "...",
|
|
189
|
+
provider,
|
|
190
|
+
requiredAuthType,
|
|
191
|
+
delegationAuthorizationType: delegationAuthorizationType ?? null,
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Distinguish "re-authenticate and this resolves" from "no mint can
|
|
196
|
+
// ever satisfy this, so the user is in an infinite loop". Without
|
|
197
|
+
// this, an unsatisfiable requirement is indistinguishable from a
|
|
198
|
+
// normal re-challenge in the logs and presents as an inexplicable
|
|
199
|
+
// auth loop. Diagnostic only — the decision above is unchanged.
|
|
200
|
+
if (isUnsatisfiableAuthType(requiredAuthType)) {
|
|
201
|
+
this.config.logger(
|
|
202
|
+
"[ToolContextBuilder] MISCONFIGURATION: tool requires an authorization type no delegation mint can produce - this tool will re-challenge forever",
|
|
203
|
+
{
|
|
204
|
+
toolName,
|
|
205
|
+
requiredAuthType,
|
|
206
|
+
mintableAuthTypes: [...MINTABLE_AUTH_TYPES],
|
|
207
|
+
hint: "Set the tool's authorization.type to one of the mintable types, or add mint support for the required type.",
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// No (satisfying) delegation token - throw OAuthRequiredError to trigger auth flow
|
|
214
|
+
this.config.logger(
|
|
215
|
+
"[ToolContextBuilder] Token not available, throwing OAuthRequiredError",
|
|
216
|
+
{
|
|
217
|
+
toolName,
|
|
218
|
+
userDid: userDid.substring(0, 20) + "...",
|
|
219
|
+
provider,
|
|
220
|
+
scopes: toolProtection.requiredScopes,
|
|
221
|
+
}
|
|
222
|
+
);
|
|
157
223
|
|
|
158
224
|
// Throw error with provider and scopes info
|
|
159
225
|
// Auth URL will be built by the Cloudflare layer (agent.ts)
|
|
@@ -210,7 +276,9 @@ export class ToolContextBuilder {
|
|
|
210
276
|
* @param tokenData - Token data with usage metadata
|
|
211
277
|
* @returns Headers object for API calls
|
|
212
278
|
*/
|
|
213
|
-
private buildAuthHeaders(
|
|
279
|
+
private buildAuthHeaders(
|
|
280
|
+
tokenData: IdpTokensWithMetadata
|
|
281
|
+
): Record<string, string> {
|
|
214
282
|
const authHeaders: Record<string, string> = {};
|
|
215
283
|
|
|
216
284
|
const tokenUsage = tokenData.tokenUsage || "bearer"; // Default to bearer for OAuth
|
|
@@ -282,4 +350,3 @@ export class ToolContextBuilder {
|
|
|
282
350
|
}
|
|
283
351
|
}
|
|
284
352
|
}
|
|
285
|
-
|