@withgateway/sdk 0.1.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/README.md +229 -0
- package/dist/context/index.d.mts +225 -0
- package/dist/context/index.d.ts +225 -0
- package/dist/context/index.js +440 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +411 -0
- package/dist/context/index.mjs.map +1 -0
- package/dist/experiments/index.d.mts +260 -0
- package/dist/experiments/index.d.ts +260 -0
- package/dist/experiments/index.js +407 -0
- package/dist/experiments/index.js.map +1 -0
- package/dist/experiments/index.mjs +374 -0
- package/dist/experiments/index.mjs.map +1 -0
- package/dist/index-Caw3XVub.d.mts +193 -0
- package/dist/index-Caw3XVub.d.ts +193 -0
- package/dist/index.d.mts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +1476 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1434 -0
- package/dist/index.mjs.map +1 -0
- package/dist/memory/index.d.mts +124 -0
- package/dist/memory/index.d.ts +124 -0
- package/dist/memory/index.js +192 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/index.mjs +164 -0
- package/dist/memory/index.mjs.map +1 -0
- package/dist/security/index.d.mts +114 -0
- package/dist/security/index.d.ts +114 -0
- package/dist/security/index.js +297 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/index.mjs +275 -0
- package/dist/security/index.mjs.map +1 -0
- package/dist/tracing/index.d.mts +1 -0
- package/dist/tracing/index.d.ts +1 -0
- package/dist/tracing/index.js +226 -0
- package/dist/tracing/index.js.map +1 -0
- package/dist/tracing/index.mjs +202 -0
- package/dist/tracing/index.mjs.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/security/index.ts
|
|
21
|
+
var security_exports = {};
|
|
22
|
+
__export(security_exports, {
|
|
23
|
+
GatewaySecurity: () => GatewaySecurity,
|
|
24
|
+
GatewaySecurityError: () => GatewaySecurityError,
|
|
25
|
+
GatewayToolApprovalRequiredError: () => GatewayToolApprovalRequiredError,
|
|
26
|
+
GatewayToolBlockedError: () => GatewayToolBlockedError,
|
|
27
|
+
gateway: () => gateway
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(security_exports);
|
|
30
|
+
var SECURITY_ATTRIBUTES = {
|
|
31
|
+
AGENT_ID: "gateway.agent.id",
|
|
32
|
+
AGENT_NAME: "gateway.agent.name",
|
|
33
|
+
AGENT_VERSION: "gateway.agent.version",
|
|
34
|
+
TOOLSET_ID: "gateway.toolset.id",
|
|
35
|
+
TOOL_ID: "gateway.tool.id",
|
|
36
|
+
SECURITY_MODE: "gateway.security.mode",
|
|
37
|
+
POLICY_VERSION: "gateway.security.policy_version",
|
|
38
|
+
DECISION_OUTCOME: "gateway.decision.outcome",
|
|
39
|
+
DECISION_REASON_CODE: "gateway.decision.reason_code"
|
|
40
|
+
};
|
|
41
|
+
var GatewaySecurityError = class extends Error {
|
|
42
|
+
constructor(message, options) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "GatewaySecurityError";
|
|
45
|
+
this.cause = options?.cause;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var GatewayToolBlockedError = class extends GatewaySecurityError {
|
|
49
|
+
decision;
|
|
50
|
+
toolId;
|
|
51
|
+
constructor(toolId, decision) {
|
|
52
|
+
super(decision.message || `Gateway blocked tool ${toolId}`);
|
|
53
|
+
this.name = "GatewayToolBlockedError";
|
|
54
|
+
this.toolId = toolId;
|
|
55
|
+
this.decision = decision;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var GatewayToolApprovalRequiredError = class extends GatewaySecurityError {
|
|
59
|
+
decision;
|
|
60
|
+
toolId;
|
|
61
|
+
constructor(toolId, decision) {
|
|
62
|
+
super(decision.message || `Gateway requires approval for tool ${toolId}`);
|
|
63
|
+
this.name = "GatewayToolApprovalRequiredError";
|
|
64
|
+
this.toolId = toolId;
|
|
65
|
+
this.decision = decision;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var GatewaySecurity = class _GatewaySecurity {
|
|
69
|
+
host;
|
|
70
|
+
publicKey;
|
|
71
|
+
secretKey;
|
|
72
|
+
agent;
|
|
73
|
+
debug;
|
|
74
|
+
failOpen;
|
|
75
|
+
constructor(config) {
|
|
76
|
+
this.host = config.host.replace(/\/$/, "");
|
|
77
|
+
this.publicKey = config.publicKey;
|
|
78
|
+
this.secretKey = config.secretKey;
|
|
79
|
+
this.agent = config.agent ?? {};
|
|
80
|
+
this.debug = config.debug ?? false;
|
|
81
|
+
this.failOpen = config.failOpen ?? false;
|
|
82
|
+
}
|
|
83
|
+
static fromEnv(config = {}) {
|
|
84
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
85
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
86
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
87
|
+
const missing = [
|
|
88
|
+
["host", host],
|
|
89
|
+
["publicKey", publicKey],
|
|
90
|
+
["secretKey", secretKey]
|
|
91
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
92
|
+
if (missing.length > 0) {
|
|
93
|
+
throw new GatewaySecurityError(
|
|
94
|
+
`Missing Gateway security configuration: ${missing.join(", ")}`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return new _GatewaySecurity({
|
|
98
|
+
...config,
|
|
99
|
+
host,
|
|
100
|
+
publicKey,
|
|
101
|
+
secretKey
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async secureTools(options) {
|
|
105
|
+
const entries = Object.entries(options.tools);
|
|
106
|
+
const descriptors = entries.map(
|
|
107
|
+
([key, tool]) => this.describeTool(key, tool, options)
|
|
108
|
+
);
|
|
109
|
+
const resolve = await this.resolveToolset({
|
|
110
|
+
agent: this.agent,
|
|
111
|
+
toolsetId: options.toolsetId,
|
|
112
|
+
toolsetName: options.toolsetName,
|
|
113
|
+
provider: options.provider,
|
|
114
|
+
tools: descriptors,
|
|
115
|
+
context: this.normalizeContext(options.context)
|
|
116
|
+
});
|
|
117
|
+
const securedEntries = entries.flatMap(([key, tool]) => {
|
|
118
|
+
const descriptor = descriptors.find((item) => item.name === key);
|
|
119
|
+
if (!this.shouldExposeTool(resolve, descriptor.id)) {
|
|
120
|
+
this.log("Hiding tool %s in %s mode", descriptor.id, resolve.mode);
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
return [
|
|
124
|
+
[
|
|
125
|
+
key,
|
|
126
|
+
this.wrapTool(tool, {
|
|
127
|
+
toolId: descriptor.id,
|
|
128
|
+
toolsetId: options.toolsetId,
|
|
129
|
+
context: this.normalizeContext(options.context),
|
|
130
|
+
resolve
|
|
131
|
+
})
|
|
132
|
+
]
|
|
133
|
+
];
|
|
134
|
+
});
|
|
135
|
+
return Object.fromEntries(securedEntries);
|
|
136
|
+
}
|
|
137
|
+
async resolveToolset(input) {
|
|
138
|
+
return this.postJson(
|
|
139
|
+
"/api/public/security/toolsets/resolve",
|
|
140
|
+
input
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
async authorizeToolCall(input) {
|
|
144
|
+
return this.postJson(
|
|
145
|
+
"/api/public/security/tool-calls/authorize",
|
|
146
|
+
{
|
|
147
|
+
agent: this.agent,
|
|
148
|
+
toolsetId: input.toolsetId,
|
|
149
|
+
toolId: input.toolId,
|
|
150
|
+
context: input.context,
|
|
151
|
+
inputMetadata: input.inputMetadata
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
wrapTool(tool, input) {
|
|
156
|
+
if (typeof tool === "function") {
|
|
157
|
+
return this.wrapExecute(tool, input);
|
|
158
|
+
}
|
|
159
|
+
if (typeof tool.execute !== "function") {
|
|
160
|
+
return tool;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
...tool,
|
|
164
|
+
execute: this.wrapExecute(tool.execute.bind(tool), input)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
wrapExecute(execute, input) {
|
|
168
|
+
return async (...args) => {
|
|
169
|
+
this.setSpanAttributes({
|
|
170
|
+
[SECURITY_ATTRIBUTES.AGENT_ID]: this.agent.id,
|
|
171
|
+
[SECURITY_ATTRIBUTES.AGENT_NAME]: this.agent.name,
|
|
172
|
+
[SECURITY_ATTRIBUTES.AGENT_VERSION]: this.agent.version,
|
|
173
|
+
[SECURITY_ATTRIBUTES.TOOLSET_ID]: input.toolsetId,
|
|
174
|
+
[SECURITY_ATTRIBUTES.TOOL_ID]: input.toolId,
|
|
175
|
+
[SECURITY_ATTRIBUTES.SECURITY_MODE]: input.resolve.mode,
|
|
176
|
+
[SECURITY_ATTRIBUTES.POLICY_VERSION]: input.resolve.policyVersion
|
|
177
|
+
});
|
|
178
|
+
const decision = await this.authorizeOrFallback({
|
|
179
|
+
toolsetId: input.toolsetId,
|
|
180
|
+
toolId: input.toolId,
|
|
181
|
+
context: input.context,
|
|
182
|
+
inputMetadata: { args }
|
|
183
|
+
});
|
|
184
|
+
this.setSpanAttributes({
|
|
185
|
+
[SECURITY_ATTRIBUTES.DECISION_OUTCOME]: decision.outcome,
|
|
186
|
+
[SECURITY_ATTRIBUTES.DECISION_REASON_CODE]: decision.reasonCode,
|
|
187
|
+
[SECURITY_ATTRIBUTES.POLICY_VERSION]: decision.policyVersion
|
|
188
|
+
});
|
|
189
|
+
if (decision.outcome === "block") {
|
|
190
|
+
throw new GatewayToolBlockedError(input.toolId, decision);
|
|
191
|
+
}
|
|
192
|
+
if (decision.outcome === "approval_required") {
|
|
193
|
+
throw new GatewayToolApprovalRequiredError(input.toolId, decision);
|
|
194
|
+
}
|
|
195
|
+
return execute(...args);
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async authorizeOrFallback(input) {
|
|
199
|
+
try {
|
|
200
|
+
return await this.authorizeToolCall(input);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (this.failOpen) {
|
|
203
|
+
this.log("Authorization failed open for tool %s", input.toolId, error);
|
|
204
|
+
return {
|
|
205
|
+
decisionId: "local-fail-open",
|
|
206
|
+
outcome: "flag",
|
|
207
|
+
reasonCode: "authorization_unavailable_fail_open",
|
|
208
|
+
message: "Gateway authorization was unavailable; failOpen allowed execution.",
|
|
209
|
+
policyVersion: 0
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
throw new GatewaySecurityError(
|
|
213
|
+
`Gateway authorization failed for tool ${input.toolId}`,
|
|
214
|
+
{ cause: error }
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async postJson(path, payload) {
|
|
219
|
+
const response = await fetch(`${this.host}${path}`, {
|
|
220
|
+
method: "POST",
|
|
221
|
+
headers: {
|
|
222
|
+
Authorization: this.authHeader(),
|
|
223
|
+
"Content-Type": "application/json"
|
|
224
|
+
},
|
|
225
|
+
body: JSON.stringify(payload)
|
|
226
|
+
});
|
|
227
|
+
if (!response.ok) {
|
|
228
|
+
const body = await response.text();
|
|
229
|
+
throw new GatewaySecurityError(
|
|
230
|
+
`Gateway API ${response.status} on ${path}: ${body || response.statusText}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return await response.json();
|
|
234
|
+
}
|
|
235
|
+
describeTool(key, tool, options) {
|
|
236
|
+
const toolObject = typeof tool === "function" ? void 0 : tool;
|
|
237
|
+
const id = options.toolIds?.[key] ?? `${options.toolsetId}.${toSnakeCase(key)}`;
|
|
238
|
+
return {
|
|
239
|
+
id,
|
|
240
|
+
name: key,
|
|
241
|
+
description: typeof toolObject?.description === "string" ? toolObject.description : void 0,
|
|
242
|
+
metadata: options.toolMetadata?.[key] ?? {}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
shouldExposeTool(resolve, toolId) {
|
|
246
|
+
if (resolve.mode === "observe") return true;
|
|
247
|
+
if (resolve.blockedToolIds.includes(toolId)) return false;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
normalizeContext(context) {
|
|
251
|
+
return {
|
|
252
|
+
...context ?? {},
|
|
253
|
+
metadata: context?.metadata ?? {}
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
authHeader() {
|
|
257
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
258
|
+
"base64"
|
|
259
|
+
)}`;
|
|
260
|
+
}
|
|
261
|
+
setSpanAttributes(attributes) {
|
|
262
|
+
const span = getActiveSpan();
|
|
263
|
+
if (!span) return;
|
|
264
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
265
|
+
if (value === void 0 || value === null) continue;
|
|
266
|
+
span.setAttribute(key, value);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
log(message, ...args) {
|
|
270
|
+
if (this.debug) {
|
|
271
|
+
console.log(`[gateway.security] ${message}`, ...args);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
function getActiveSpan() {
|
|
276
|
+
try {
|
|
277
|
+
const otel = require("@opentelemetry/api");
|
|
278
|
+
return otel.trace?.getActiveSpan?.() ?? null;
|
|
279
|
+
} catch {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function toSnakeCase(value) {
|
|
284
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toLowerCase();
|
|
285
|
+
}
|
|
286
|
+
var gateway = {
|
|
287
|
+
fromEnv: GatewaySecurity.fromEnv
|
|
288
|
+
};
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
GatewaySecurity,
|
|
292
|
+
GatewaySecurityError,
|
|
293
|
+
GatewayToolApprovalRequiredError,
|
|
294
|
+
GatewayToolBlockedError,
|
|
295
|
+
gateway
|
|
296
|
+
});
|
|
297
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/security/index.ts"],"sourcesContent":["type AgentDescriptor = {\n id?: string;\n name?: string;\n version?: string;\n};\n\ntype SecurityContext = {\n executionId?: string;\n userId?: string;\n teamId?: string;\n environment?: string;\n contextLabel?: string;\n contextBundleHash?: string;\n traceId?: string;\n sessionId?: string;\n spanId?: string;\n metadata?: Record<string, unknown>;\n};\n\ntype ToolDescriptor = {\n id: string;\n name?: string;\n description?: string;\n metadata?: Record<string, unknown>;\n};\n\ntype ToolsetResolveResponse = {\n toolsetId: string;\n mode: \"observe\" | \"filter\" | \"enforce\";\n policyVersion: number;\n ttlSeconds: number;\n allowedToolIds: string[];\n blockedToolIds: string[];\n approvalToolIds: string[];\n secretBindings: Record<string, string[]>;\n failMode: \"open\" | \"closed\";\n};\n\ntype ToolCallAuthorizeResponse = {\n decisionId: string;\n outcome: \"allow\" | \"flag\" | \"block\" | \"approval_required\";\n reasonCode: string;\n message: string;\n policyVersion: number;\n};\n\ntype ToolObject = {\n description?: string;\n execute?: (...args: unknown[]) => unknown;\n [key: string]: unknown;\n};\n\ntype ToolCollection = Record<\n string,\n ToolObject | ((...args: unknown[]) => unknown)\n>;\n\ntype SecureToolsOptions<TTools extends ToolCollection> = {\n toolsetId: string;\n toolsetName?: string;\n provider?: string;\n tools: TTools;\n context?: SecurityContext;\n toolIds?: Partial<Record<keyof TTools & string, string>>;\n toolMetadata?: Partial<\n Record<keyof TTools & string, Record<string, unknown>>\n >;\n};\n\ntype GatewaySecurityConfig = {\n host: string;\n publicKey: string;\n secretKey: string;\n agent?: AgentDescriptor;\n debug?: boolean;\n failOpen?: boolean;\n};\n\ntype GatewaySecurityFromEnvConfig = Partial<\n Pick<GatewaySecurityConfig, \"host\" | \"publicKey\" | \"secretKey\">\n> &\n Pick<GatewaySecurityConfig, \"agent\" | \"debug\" | \"failOpen\">;\n\nconst SECURITY_ATTRIBUTES = {\n AGENT_ID: \"gateway.agent.id\",\n AGENT_NAME: \"gateway.agent.name\",\n AGENT_VERSION: \"gateway.agent.version\",\n TOOLSET_ID: \"gateway.toolset.id\",\n TOOL_ID: \"gateway.tool.id\",\n SECURITY_MODE: \"gateway.security.mode\",\n POLICY_VERSION: \"gateway.security.policy_version\",\n DECISION_OUTCOME: \"gateway.decision.outcome\",\n DECISION_REASON_CODE: \"gateway.decision.reason_code\",\n} as const;\n\nexport class GatewaySecurityError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message);\n this.name = \"GatewaySecurityError\";\n this.cause = options?.cause;\n }\n}\n\nexport class GatewayToolBlockedError extends GatewaySecurityError {\n readonly decision: ToolCallAuthorizeResponse;\n readonly toolId: string;\n\n constructor(toolId: string, decision: ToolCallAuthorizeResponse) {\n super(decision.message || `Gateway blocked tool ${toolId}`);\n this.name = \"GatewayToolBlockedError\";\n this.toolId = toolId;\n this.decision = decision;\n }\n}\n\n/**\n * Thrown when Gateway's policy returns `approval_required` for a tool call.\n * The SDK does NOT execute the tool — a human/system approval is needed first\n * (the server engine and the runtime MCP PEP both honor this; this closes the\n * SDK leg of the step-up gate so an approval decision can't be silently run).\n */\nexport class GatewayToolApprovalRequiredError extends GatewaySecurityError {\n readonly decision: ToolCallAuthorizeResponse;\n readonly toolId: string;\n\n constructor(toolId: string, decision: ToolCallAuthorizeResponse) {\n super(decision.message || `Gateway requires approval for tool ${toolId}`);\n this.name = \"GatewayToolApprovalRequiredError\";\n this.toolId = toolId;\n this.decision = decision;\n }\n}\n\nexport class GatewaySecurity {\n private readonly host: string;\n private readonly publicKey: string;\n private readonly secretKey: string;\n private readonly agent: AgentDescriptor;\n private readonly debug: boolean;\n private readonly failOpen: boolean;\n\n constructor(config: GatewaySecurityConfig) {\n this.host = config.host.replace(/\\/$/, \"\");\n this.publicKey = config.publicKey;\n this.secretKey = config.secretKey;\n this.agent = config.agent ?? {};\n this.debug = config.debug ?? false;\n this.failOpen = config.failOpen ?? false;\n }\n\n static fromEnv(config: GatewaySecurityFromEnvConfig = {}) {\n const host = config.host ?? process.env.GATEWAY_HOST;\n const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;\n const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;\n\n const missing = [\n [\"host\", host],\n [\"publicKey\", publicKey],\n [\"secretKey\", secretKey],\n ]\n .filter(([, value]) => !value)\n .map(([name]) => name);\n\n if (missing.length > 0) {\n throw new GatewaySecurityError(\n `Missing Gateway security configuration: ${missing.join(\", \")}`,\n );\n }\n\n return new GatewaySecurity({\n ...config,\n host: host!,\n publicKey: publicKey!,\n secretKey: secretKey!,\n });\n }\n\n async secureTools<TTools extends ToolCollection>(\n options: SecureToolsOptions<TTools>,\n ): Promise<TTools> {\n const entries = Object.entries(options.tools);\n const descriptors = entries.map(([key, tool]) =>\n this.describeTool(key, tool, options),\n );\n const resolve = await this.resolveToolset({\n agent: this.agent,\n toolsetId: options.toolsetId,\n toolsetName: options.toolsetName,\n provider: options.provider,\n tools: descriptors,\n context: this.normalizeContext(options.context),\n });\n\n const securedEntries = entries.flatMap(([key, tool]) => {\n const descriptor = descriptors.find((item) => item.name === key)!;\n if (!this.shouldExposeTool(resolve, descriptor.id)) {\n this.log(\"Hiding tool %s in %s mode\", descriptor.id, resolve.mode);\n return [];\n }\n\n return [\n [\n key,\n this.wrapTool(tool, {\n toolId: descriptor.id,\n toolsetId: options.toolsetId,\n context: this.normalizeContext(options.context),\n resolve,\n }),\n ],\n ];\n });\n\n return Object.fromEntries(securedEntries) as TTools;\n }\n\n private async resolveToolset(input: {\n agent: AgentDescriptor;\n toolsetId: string;\n toolsetName?: string;\n provider?: string;\n tools: ToolDescriptor[];\n context: SecurityContext;\n }) {\n return this.postJson<ToolsetResolveResponse>(\n \"/api/public/security/toolsets/resolve\",\n input,\n );\n }\n\n private async authorizeToolCall(input: {\n toolsetId: string;\n toolId: string;\n context: SecurityContext;\n inputMetadata: Record<string, unknown>;\n }) {\n return this.postJson<ToolCallAuthorizeResponse>(\n \"/api/public/security/tool-calls/authorize\",\n {\n agent: this.agent,\n toolsetId: input.toolsetId,\n toolId: input.toolId,\n context: input.context,\n inputMetadata: input.inputMetadata,\n },\n );\n }\n\n private wrapTool(\n tool: ToolObject | ((...args: unknown[]) => unknown),\n input: {\n toolId: string;\n toolsetId: string;\n context: SecurityContext;\n resolve: ToolsetResolveResponse;\n },\n ) {\n if (typeof tool === \"function\") {\n return this.wrapExecute(tool, input);\n }\n\n if (typeof tool.execute !== \"function\") {\n return tool;\n }\n\n return {\n ...tool,\n execute: this.wrapExecute(tool.execute.bind(tool), input),\n };\n }\n\n private wrapExecute(\n execute: (...args: unknown[]) => unknown,\n input: {\n toolId: string;\n toolsetId: string;\n context: SecurityContext;\n resolve: ToolsetResolveResponse;\n },\n ) {\n return async (...args: unknown[]) => {\n this.setSpanAttributes({\n [SECURITY_ATTRIBUTES.AGENT_ID]: this.agent.id,\n [SECURITY_ATTRIBUTES.AGENT_NAME]: this.agent.name,\n [SECURITY_ATTRIBUTES.AGENT_VERSION]: this.agent.version,\n [SECURITY_ATTRIBUTES.TOOLSET_ID]: input.toolsetId,\n [SECURITY_ATTRIBUTES.TOOL_ID]: input.toolId,\n [SECURITY_ATTRIBUTES.SECURITY_MODE]: input.resolve.mode,\n [SECURITY_ATTRIBUTES.POLICY_VERSION]: input.resolve.policyVersion,\n });\n\n const decision = await this.authorizeOrFallback({\n toolsetId: input.toolsetId,\n toolId: input.toolId,\n context: input.context,\n inputMetadata: { args },\n });\n\n this.setSpanAttributes({\n [SECURITY_ATTRIBUTES.DECISION_OUTCOME]: decision.outcome,\n [SECURITY_ATTRIBUTES.DECISION_REASON_CODE]: decision.reasonCode,\n [SECURITY_ATTRIBUTES.POLICY_VERSION]: decision.policyVersion,\n });\n\n if (decision.outcome === \"block\") {\n throw new GatewayToolBlockedError(input.toolId, decision);\n }\n if (decision.outcome === \"approval_required\") {\n // Enforce the step-up gate: do NOT execute until approved. The tool\n // stays callable (so the approval flow can be triggered), but a call\n // is refused with a typed error rather than running unapproved.\n throw new GatewayToolApprovalRequiredError(input.toolId, decision);\n }\n\n return execute(...args);\n };\n }\n\n private async authorizeOrFallback(input: {\n toolsetId: string;\n toolId: string;\n context: SecurityContext;\n inputMetadata: Record<string, unknown>;\n }) {\n try {\n return await this.authorizeToolCall(input);\n } catch (error) {\n if (this.failOpen) {\n this.log(\"Authorization failed open for tool %s\", input.toolId, error);\n return {\n decisionId: \"local-fail-open\",\n outcome: \"flag\" as const,\n reasonCode: \"authorization_unavailable_fail_open\",\n message:\n \"Gateway authorization was unavailable; failOpen allowed execution.\",\n policyVersion: 0,\n };\n }\n throw new GatewaySecurityError(\n `Gateway authorization failed for tool ${input.toolId}`,\n { cause: error },\n );\n }\n }\n\n private async postJson<TResponse>(\n path: string,\n payload: Record<string, unknown>,\n ): Promise<TResponse> {\n const response = await fetch(`${this.host}${path}`, {\n method: \"POST\",\n headers: {\n Authorization: this.authHeader(),\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n const body = await response.text();\n throw new GatewaySecurityError(\n `Gateway API ${response.status} on ${path}: ${body || response.statusText}`,\n );\n }\n\n return (await response.json()) as TResponse;\n }\n\n private describeTool<TTools extends ToolCollection>(\n key: string,\n tool: ToolObject | ((...args: unknown[]) => unknown),\n options: SecureToolsOptions<TTools>,\n ): ToolDescriptor & { name: string } {\n const toolObject = typeof tool === \"function\" ? undefined : tool;\n const id =\n options.toolIds?.[key] ?? `${options.toolsetId}.${toSnakeCase(key)}`;\n return {\n id,\n name: key,\n description:\n typeof toolObject?.description === \"string\"\n ? toolObject.description\n : undefined,\n metadata: options.toolMetadata?.[key] ?? {},\n };\n }\n\n private shouldExposeTool(resolve: ToolsetResolveResponse, toolId: string) {\n if (resolve.mode === \"observe\") return true;\n if (resolve.blockedToolIds.includes(toolId)) return false;\n return true;\n }\n\n private normalizeContext(context?: SecurityContext): SecurityContext {\n return {\n ...(context ?? {}),\n metadata: context?.metadata ?? {},\n };\n }\n\n private authHeader() {\n return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(\n \"base64\",\n )}`;\n }\n\n private setSpanAttributes(attributes: Record<string, unknown>) {\n const span = getActiveSpan();\n if (!span) return;\n\n for (const [key, value] of Object.entries(attributes)) {\n if (value === undefined || value === null) continue;\n span.setAttribute(key, value);\n }\n }\n\n private log(message: string, ...args: unknown[]) {\n if (this.debug) {\n console.log(`[gateway.security] ${message}`, ...args);\n }\n }\n}\n\nfunction getActiveSpan(): {\n setAttribute: (key: string, value: unknown) => void;\n} | null {\n try {\n // Optional peer dependency. Avoid making security unusable without tracing.\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const otel = require(\"@opentelemetry/api\") as {\n trace?: {\n getActiveSpan?: () =>\n | { setAttribute: (key: string, value: unknown) => void }\n | undefined;\n };\n };\n return otel.trace?.getActiveSpan?.() ?? null;\n } catch {\n return null;\n }\n}\n\nfunction toSnakeCase(value: string) {\n return value\n .replace(/([a-z0-9])([A-Z])/g, \"$1_$2\")\n .replace(/[\\s-]+/g, \"_\")\n .toLowerCase();\n}\n\nexport const gateway = {\n fromEnv: GatewaySecurity.fromEnv,\n};\n\nexport type {\n AgentDescriptor as GatewayAgentDescriptor,\n GatewaySecurityConfig,\n SecurityContext as GatewaySecurityContext,\n SecureToolsOptions,\n ToolCallAuthorizeResponse as GatewayToolCallAuthorizeResponse,\n ToolDescriptor as GatewayToolDescriptor,\n ToolsetResolveResponse as GatewayToolsetResolveResponse,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmFA,IAAM,sBAAsB;AAAA,EAC1B,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,YAAY;AAAA,EACZ,SAAS;AAAA,EACT,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,kBAAkB;AAAA,EAClB,sBAAsB;AACxB;AAEO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC9C,YAAY,SAAiB,SAA+B;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAEO,IAAM,0BAAN,cAAsC,qBAAqB;AAAA,EACvD;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,UAAqC;AAC/D,UAAM,SAAS,WAAW,wBAAwB,MAAM,EAAE;AAC1D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW;AAAA,EAClB;AACF;AAQO,IAAM,mCAAN,cAA+C,qBAAqB;AAAA,EAChE;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,UAAqC;AAC/D,UAAM,SAAS,WAAW,sCAAsC,MAAM,EAAE;AACxE,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW;AAAA,EAClB;AACF;AAEO,IAAM,kBAAN,MAAM,iBAAgB;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY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I,KAAK,SAAS,EAAE,EAAE;AAAA,MACjE;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,kBAAkB,YAAqC;AAC7D,UAAM,OAAO,cAAc;AAC3B,QAAI,CAAC,KAAM;AAEX,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,UAAI,UAAU,UAAa,UAAU,KAAM;AAC3C,WAAK,aAAa,KAAK,KAAK;AAAA,IAC9B;AAAA,EACF;AAAA,EAEQ,IAAI,YAAoB,MAAiB;AAC/C,QAAI,KAAK,OAAO;AACd,cAAQ,IAAI,sBAAsB,OAAO,IAAI,GAAG,IAAI;AAAA,IACtD;AAAA,EACF;AACF;AAEA,SAAS,gBAEA;AACP,MAAI;AAGF,UAAM,OAAO,QAAQ,oBAAoB;AAOzC,WAAO,KAAK,OAAO,gBAAgB,KAAK;AAAA,EAC1C,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,YAAY,OAAe;AAClC,SAAO,MACJ,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,WAAW,GAAG,EACtB,YAAY;AACjB;AAEO,IAAM,UAAU;AAAA,EACrB,SAAS,gBAAgB;AAC3B;","names":[]}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// src/security/index.ts
|
|
9
|
+
var SECURITY_ATTRIBUTES = {
|
|
10
|
+
AGENT_ID: "gateway.agent.id",
|
|
11
|
+
AGENT_NAME: "gateway.agent.name",
|
|
12
|
+
AGENT_VERSION: "gateway.agent.version",
|
|
13
|
+
TOOLSET_ID: "gateway.toolset.id",
|
|
14
|
+
TOOL_ID: "gateway.tool.id",
|
|
15
|
+
SECURITY_MODE: "gateway.security.mode",
|
|
16
|
+
POLICY_VERSION: "gateway.security.policy_version",
|
|
17
|
+
DECISION_OUTCOME: "gateway.decision.outcome",
|
|
18
|
+
DECISION_REASON_CODE: "gateway.decision.reason_code"
|
|
19
|
+
};
|
|
20
|
+
var GatewaySecurityError = class extends Error {
|
|
21
|
+
constructor(message, options) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "GatewaySecurityError";
|
|
24
|
+
this.cause = options?.cause;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var GatewayToolBlockedError = class extends GatewaySecurityError {
|
|
28
|
+
decision;
|
|
29
|
+
toolId;
|
|
30
|
+
constructor(toolId, decision) {
|
|
31
|
+
super(decision.message || `Gateway blocked tool ${toolId}`);
|
|
32
|
+
this.name = "GatewayToolBlockedError";
|
|
33
|
+
this.toolId = toolId;
|
|
34
|
+
this.decision = decision;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var GatewayToolApprovalRequiredError = class extends GatewaySecurityError {
|
|
38
|
+
decision;
|
|
39
|
+
toolId;
|
|
40
|
+
constructor(toolId, decision) {
|
|
41
|
+
super(decision.message || `Gateway requires approval for tool ${toolId}`);
|
|
42
|
+
this.name = "GatewayToolApprovalRequiredError";
|
|
43
|
+
this.toolId = toolId;
|
|
44
|
+
this.decision = decision;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var GatewaySecurity = class _GatewaySecurity {
|
|
48
|
+
host;
|
|
49
|
+
publicKey;
|
|
50
|
+
secretKey;
|
|
51
|
+
agent;
|
|
52
|
+
debug;
|
|
53
|
+
failOpen;
|
|
54
|
+
constructor(config) {
|
|
55
|
+
this.host = config.host.replace(/\/$/, "");
|
|
56
|
+
this.publicKey = config.publicKey;
|
|
57
|
+
this.secretKey = config.secretKey;
|
|
58
|
+
this.agent = config.agent ?? {};
|
|
59
|
+
this.debug = config.debug ?? false;
|
|
60
|
+
this.failOpen = config.failOpen ?? false;
|
|
61
|
+
}
|
|
62
|
+
static fromEnv(config = {}) {
|
|
63
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
64
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
65
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
66
|
+
const missing = [
|
|
67
|
+
["host", host],
|
|
68
|
+
["publicKey", publicKey],
|
|
69
|
+
["secretKey", secretKey]
|
|
70
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
71
|
+
if (missing.length > 0) {
|
|
72
|
+
throw new GatewaySecurityError(
|
|
73
|
+
`Missing Gateway security configuration: ${missing.join(", ")}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return new _GatewaySecurity({
|
|
77
|
+
...config,
|
|
78
|
+
host,
|
|
79
|
+
publicKey,
|
|
80
|
+
secretKey
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async secureTools(options) {
|
|
84
|
+
const entries = Object.entries(options.tools);
|
|
85
|
+
const descriptors = entries.map(
|
|
86
|
+
([key, tool]) => this.describeTool(key, tool, options)
|
|
87
|
+
);
|
|
88
|
+
const resolve = await this.resolveToolset({
|
|
89
|
+
agent: this.agent,
|
|
90
|
+
toolsetId: options.toolsetId,
|
|
91
|
+
toolsetName: options.toolsetName,
|
|
92
|
+
provider: options.provider,
|
|
93
|
+
tools: descriptors,
|
|
94
|
+
context: this.normalizeContext(options.context)
|
|
95
|
+
});
|
|
96
|
+
const securedEntries = entries.flatMap(([key, tool]) => {
|
|
97
|
+
const descriptor = descriptors.find((item) => item.name === key);
|
|
98
|
+
if (!this.shouldExposeTool(resolve, descriptor.id)) {
|
|
99
|
+
this.log("Hiding tool %s in %s mode", descriptor.id, resolve.mode);
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
return [
|
|
103
|
+
[
|
|
104
|
+
key,
|
|
105
|
+
this.wrapTool(tool, {
|
|
106
|
+
toolId: descriptor.id,
|
|
107
|
+
toolsetId: options.toolsetId,
|
|
108
|
+
context: this.normalizeContext(options.context),
|
|
109
|
+
resolve
|
|
110
|
+
})
|
|
111
|
+
]
|
|
112
|
+
];
|
|
113
|
+
});
|
|
114
|
+
return Object.fromEntries(securedEntries);
|
|
115
|
+
}
|
|
116
|
+
async resolveToolset(input) {
|
|
117
|
+
return this.postJson(
|
|
118
|
+
"/api/public/security/toolsets/resolve",
|
|
119
|
+
input
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
async authorizeToolCall(input) {
|
|
123
|
+
return this.postJson(
|
|
124
|
+
"/api/public/security/tool-calls/authorize",
|
|
125
|
+
{
|
|
126
|
+
agent: this.agent,
|
|
127
|
+
toolsetId: input.toolsetId,
|
|
128
|
+
toolId: input.toolId,
|
|
129
|
+
context: input.context,
|
|
130
|
+
inputMetadata: input.inputMetadata
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
wrapTool(tool, input) {
|
|
135
|
+
if (typeof tool === "function") {
|
|
136
|
+
return this.wrapExecute(tool, input);
|
|
137
|
+
}
|
|
138
|
+
if (typeof tool.execute !== "function") {
|
|
139
|
+
return tool;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
...tool,
|
|
143
|
+
execute: this.wrapExecute(tool.execute.bind(tool), input)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
wrapExecute(execute, input) {
|
|
147
|
+
return async (...args) => {
|
|
148
|
+
this.setSpanAttributes({
|
|
149
|
+
[SECURITY_ATTRIBUTES.AGENT_ID]: this.agent.id,
|
|
150
|
+
[SECURITY_ATTRIBUTES.AGENT_NAME]: this.agent.name,
|
|
151
|
+
[SECURITY_ATTRIBUTES.AGENT_VERSION]: this.agent.version,
|
|
152
|
+
[SECURITY_ATTRIBUTES.TOOLSET_ID]: input.toolsetId,
|
|
153
|
+
[SECURITY_ATTRIBUTES.TOOL_ID]: input.toolId,
|
|
154
|
+
[SECURITY_ATTRIBUTES.SECURITY_MODE]: input.resolve.mode,
|
|
155
|
+
[SECURITY_ATTRIBUTES.POLICY_VERSION]: input.resolve.policyVersion
|
|
156
|
+
});
|
|
157
|
+
const decision = await this.authorizeOrFallback({
|
|
158
|
+
toolsetId: input.toolsetId,
|
|
159
|
+
toolId: input.toolId,
|
|
160
|
+
context: input.context,
|
|
161
|
+
inputMetadata: { args }
|
|
162
|
+
});
|
|
163
|
+
this.setSpanAttributes({
|
|
164
|
+
[SECURITY_ATTRIBUTES.DECISION_OUTCOME]: decision.outcome,
|
|
165
|
+
[SECURITY_ATTRIBUTES.DECISION_REASON_CODE]: decision.reasonCode,
|
|
166
|
+
[SECURITY_ATTRIBUTES.POLICY_VERSION]: decision.policyVersion
|
|
167
|
+
});
|
|
168
|
+
if (decision.outcome === "block") {
|
|
169
|
+
throw new GatewayToolBlockedError(input.toolId, decision);
|
|
170
|
+
}
|
|
171
|
+
if (decision.outcome === "approval_required") {
|
|
172
|
+
throw new GatewayToolApprovalRequiredError(input.toolId, decision);
|
|
173
|
+
}
|
|
174
|
+
return execute(...args);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async authorizeOrFallback(input) {
|
|
178
|
+
try {
|
|
179
|
+
return await this.authorizeToolCall(input);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (this.failOpen) {
|
|
182
|
+
this.log("Authorization failed open for tool %s", input.toolId, error);
|
|
183
|
+
return {
|
|
184
|
+
decisionId: "local-fail-open",
|
|
185
|
+
outcome: "flag",
|
|
186
|
+
reasonCode: "authorization_unavailable_fail_open",
|
|
187
|
+
message: "Gateway authorization was unavailable; failOpen allowed execution.",
|
|
188
|
+
policyVersion: 0
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
throw new GatewaySecurityError(
|
|
192
|
+
`Gateway authorization failed for tool ${input.toolId}`,
|
|
193
|
+
{ cause: error }
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async postJson(path, payload) {
|
|
198
|
+
const response = await fetch(`${this.host}${path}`, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers: {
|
|
201
|
+
Authorization: this.authHeader(),
|
|
202
|
+
"Content-Type": "application/json"
|
|
203
|
+
},
|
|
204
|
+
body: JSON.stringify(payload)
|
|
205
|
+
});
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
const body = await response.text();
|
|
208
|
+
throw new GatewaySecurityError(
|
|
209
|
+
`Gateway API ${response.status} on ${path}: ${body || response.statusText}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return await response.json();
|
|
213
|
+
}
|
|
214
|
+
describeTool(key, tool, options) {
|
|
215
|
+
const toolObject = typeof tool === "function" ? void 0 : tool;
|
|
216
|
+
const id = options.toolIds?.[key] ?? `${options.toolsetId}.${toSnakeCase(key)}`;
|
|
217
|
+
return {
|
|
218
|
+
id,
|
|
219
|
+
name: key,
|
|
220
|
+
description: typeof toolObject?.description === "string" ? toolObject.description : void 0,
|
|
221
|
+
metadata: options.toolMetadata?.[key] ?? {}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
shouldExposeTool(resolve, toolId) {
|
|
225
|
+
if (resolve.mode === "observe") return true;
|
|
226
|
+
if (resolve.blockedToolIds.includes(toolId)) return false;
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
normalizeContext(context) {
|
|
230
|
+
return {
|
|
231
|
+
...context ?? {},
|
|
232
|
+
metadata: context?.metadata ?? {}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
authHeader() {
|
|
236
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
237
|
+
"base64"
|
|
238
|
+
)}`;
|
|
239
|
+
}
|
|
240
|
+
setSpanAttributes(attributes) {
|
|
241
|
+
const span = getActiveSpan();
|
|
242
|
+
if (!span) return;
|
|
243
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
244
|
+
if (value === void 0 || value === null) continue;
|
|
245
|
+
span.setAttribute(key, value);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
log(message, ...args) {
|
|
249
|
+
if (this.debug) {
|
|
250
|
+
console.log(`[gateway.security] ${message}`, ...args);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
function getActiveSpan() {
|
|
255
|
+
try {
|
|
256
|
+
const otel = __require("@opentelemetry/api");
|
|
257
|
+
return otel.trace?.getActiveSpan?.() ?? null;
|
|
258
|
+
} catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function toSnakeCase(value) {
|
|
263
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toLowerCase();
|
|
264
|
+
}
|
|
265
|
+
var gateway = {
|
|
266
|
+
fromEnv: GatewaySecurity.fromEnv
|
|
267
|
+
};
|
|
268
|
+
export {
|
|
269
|
+
GatewaySecurity,
|
|
270
|
+
GatewaySecurityError,
|
|
271
|
+
GatewayToolApprovalRequiredError,
|
|
272
|
+
GatewayToolBlockedError,
|
|
273
|
+
gateway
|
|
274
|
+
};
|
|
275
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/security/index.ts"],"sourcesContent":["type AgentDescriptor = {\n id?: string;\n name?: string;\n version?: string;\n};\n\ntype SecurityContext = {\n executionId?: string;\n userId?: string;\n teamId?: string;\n environment?: string;\n contextLabel?: string;\n contextBundleHash?: string;\n traceId?: string;\n sessionId?: string;\n spanId?: string;\n metadata?: Record<string, unknown>;\n};\n\ntype ToolDescriptor = {\n id: string;\n name?: string;\n description?: string;\n metadata?: Record<string, unknown>;\n};\n\ntype ToolsetResolveResponse = {\n toolsetId: string;\n mode: \"observe\" | \"filter\" | \"enforce\";\n policyVersion: number;\n ttlSeconds: number;\n allowedToolIds: string[];\n blockedToolIds: string[];\n approvalToolIds: string[];\n secretBindings: Record<string, string[]>;\n failMode: \"open\" | \"closed\";\n};\n\ntype ToolCallAuthorizeResponse = {\n decisionId: string;\n outcome: \"allow\" | \"flag\" | \"block\" | \"approval_required\";\n reasonCode: string;\n message: string;\n policyVersion: number;\n};\n\ntype ToolObject = {\n description?: string;\n execute?: (...args: unknown[]) => unknown;\n [key: string]: unknown;\n};\n\ntype ToolCollection = Record<\n string,\n ToolObject | ((...args: unknown[]) => unknown)\n>;\n\ntype SecureToolsOptions<TTools extends ToolCollection> = {\n toolsetId: string;\n toolsetName?: string;\n provider?: string;\n tools: TTools;\n context?: SecurityContext;\n toolIds?: Partial<Record<keyof TTools & string, string>>;\n toolMetadata?: Partial<\n Record<keyof TTools & string, Record<string, unknown>>\n >;\n};\n\ntype GatewaySecurityConfig = {\n host: string;\n publicKey: string;\n secretKey: string;\n agent?: AgentDescriptor;\n debug?: boolean;\n failOpen?: boolean;\n};\n\ntype GatewaySecurityFromEnvConfig = Partial<\n Pick<GatewaySecurityConfig, \"host\" | \"publicKey\" | \"secretKey\">\n> &\n Pick<GatewaySecurityConfig, \"agent\" | \"debug\" | \"failOpen\">;\n\nconst SECURITY_ATTRIBUTES = {\n AGENT_ID: \"gateway.agent.id\",\n AGENT_NAME: \"gateway.agent.name\",\n AGENT_VERSION: \"gateway.agent.version\",\n TOOLSET_ID: \"gateway.toolset.id\",\n TOOL_ID: \"gateway.tool.id\",\n SECURITY_MODE: \"gateway.security.mode\",\n POLICY_VERSION: \"gateway.security.policy_version\",\n DECISION_OUTCOME: \"gateway.decision.outcome\",\n DECISION_REASON_CODE: \"gateway.decision.reason_code\",\n} as const;\n\nexport class GatewaySecurityError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message);\n this.name = \"GatewaySecurityError\";\n this.cause = options?.cause;\n }\n}\n\nexport class GatewayToolBlockedError extends GatewaySecurityError {\n readonly decision: ToolCallAuthorizeResponse;\n readonly toolId: string;\n\n constructor(toolId: string, decision: ToolCallAuthorizeResponse) {\n super(decision.message || `Gateway blocked tool ${toolId}`);\n this.name = \"GatewayToolBlockedError\";\n this.toolId = toolId;\n this.decision = decision;\n }\n}\n\n/**\n * Thrown when Gateway's policy returns `approval_required` for a tool call.\n * The SDK does NOT execute the tool — a human/system approval is needed first\n * (the server engine and the runtime MCP PEP both honor this; this closes the\n * SDK leg of the step-up gate so an approval decision can't be silently run).\n */\nexport class GatewayToolApprovalRequiredError extends GatewaySecurityError {\n readonly decision: ToolCallAuthorizeResponse;\n readonly toolId: string;\n\n constructor(toolId: string, decision: ToolCallAuthorizeResponse) {\n super(decision.message || `Gateway requires approval for tool ${toolId}`);\n this.name = \"GatewayToolApprovalRequiredError\";\n this.toolId = toolId;\n this.decision = decision;\n }\n}\n\nexport class GatewaySecurity {\n private readonly host: string;\n private readonly publicKey: string;\n private readonly secretKey: string;\n private readonly agent: AgentDescriptor;\n private readonly debug: boolean;\n private readonly failOpen: boolean;\n\n constructor(config: GatewaySecurityConfig) {\n this.host = config.host.replace(/\\/$/, \"\");\n this.publicKey = config.publicKey;\n this.secretKey = config.secretKey;\n this.agent = config.agent ?? {};\n this.debug = config.debug ?? false;\n this.failOpen = config.failOpen ?? false;\n }\n\n static fromEnv(config: GatewaySecurityFromEnvConfig = {}) {\n const host = config.host ?? process.env.GATEWAY_HOST;\n const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;\n const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;\n\n const missing = [\n [\"host\", host],\n [\"publicKey\", publicKey],\n [\"secretKey\", secretKey],\n ]\n .filter(([, value]) => !value)\n .map(([name]) => name);\n\n if (missing.length > 0) {\n throw new GatewaySecurityError(\n `Missing Gateway security configuration: ${missing.join(\", \")}`,\n );\n }\n\n return new GatewaySecurity({\n ...config,\n host: host!,\n publicKey: publicKey!,\n secretKey: secretKey!,\n });\n }\n\n async secureTools<TTools extends ToolCollection>(\n options: SecureToolsOptions<TTools>,\n ): Promise<TTools> {\n const entries = Object.entries(options.tools);\n const descriptors = entries.map(([key, tool]) =>\n this.describeTool(key, tool, options),\n );\n const resolve = await this.resolveToolset({\n agent: this.agent,\n toolsetId: options.toolsetId,\n toolsetName: options.toolsetName,\n provider: options.provider,\n tools: descriptors,\n context: this.normalizeContext(options.context),\n });\n\n const securedEntries = entries.flatMap(([key, tool]) => {\n const descriptor = descriptors.find((item) => item.name === key)!;\n if (!this.shouldExposeTool(resolve, descriptor.id)) {\n this.log(\"Hiding tool %s in %s mode\", descriptor.id, resolve.mode);\n return [];\n }\n\n return [\n [\n key,\n this.wrapTool(tool, {\n toolId: descriptor.id,\n toolsetId: options.toolsetId,\n context: this.normalizeContext(options.context),\n resolve,\n }),\n ],\n ];\n });\n\n return Object.fromEntries(securedEntries) as TTools;\n }\n\n private async resolveToolset(input: {\n agent: AgentDescriptor;\n toolsetId: string;\n toolsetName?: string;\n provider?: string;\n tools: ToolDescriptor[];\n context: SecurityContext;\n }) {\n return this.postJson<ToolsetResolveResponse>(\n \"/api/public/security/toolsets/resolve\",\n input,\n );\n }\n\n private async authorizeToolCall(input: {\n toolsetId: string;\n toolId: string;\n context: SecurityContext;\n inputMetadata: Record<string, unknown>;\n }) {\n return this.postJson<ToolCallAuthorizeResponse>(\n \"/api/public/security/tool-calls/authorize\",\n {\n agent: this.agent,\n toolsetId: input.toolsetId,\n toolId: input.toolId,\n context: input.context,\n inputMetadata: input.inputMetadata,\n },\n );\n }\n\n private wrapTool(\n tool: ToolObject | ((...args: unknown[]) => unknown),\n input: {\n toolId: string;\n toolsetId: string;\n context: SecurityContext;\n resolve: ToolsetResolveResponse;\n },\n ) {\n if (typeof tool === \"function\") {\n return this.wrapExecute(tool, input);\n }\n\n if (typeof tool.execute !== \"function\") {\n return tool;\n }\n\n return {\n ...tool,\n execute: this.wrapExecute(tool.execute.bind(tool), input),\n };\n }\n\n private wrapExecute(\n execute: (...args: unknown[]) => unknown,\n input: {\n toolId: string;\n toolsetId: string;\n context: SecurityContext;\n resolve: ToolsetResolveResponse;\n },\n ) {\n return async (...args: unknown[]) => {\n this.setSpanAttributes({\n [SECURITY_ATTRIBUTES.AGENT_ID]: this.agent.id,\n [SECURITY_ATTRIBUTES.AGENT_NAME]: this.agent.name,\n [SECURITY_ATTRIBUTES.AGENT_VERSION]: this.agent.version,\n [SECURITY_ATTRIBUTES.TOOLSET_ID]: input.toolsetId,\n [SECURITY_ATTRIBUTES.TOOL_ID]: input.toolId,\n [SECURITY_ATTRIBUTES.SECURITY_MODE]: input.resolve.mode,\n [SECURITY_ATTRIBUTES.POLICY_VERSION]: input.resolve.policyVersion,\n });\n\n const decision = await this.authorizeOrFallback({\n toolsetId: input.toolsetId,\n toolId: input.toolId,\n context: input.context,\n inputMetadata: { args },\n });\n\n this.setSpanAttributes({\n [SECURITY_ATTRIBUTES.DECISION_OUTCOME]: decision.outcome,\n [SECURITY_ATTRIBUTES.DECISION_REASON_CODE]: decision.reasonCode,\n [SECURITY_ATTRIBUTES.POLICY_VERSION]: decision.policyVersion,\n });\n\n if (decision.outcome === \"block\") {\n throw new GatewayToolBlockedError(input.toolId, decision);\n }\n if (decision.outcome === \"approval_required\") {\n // Enforce the step-up gate: do NOT execute until approved. The tool\n // stays callable (so the approval flow can be triggered), but a call\n // is refused with a typed error rather than running unapproved.\n throw new GatewayToolApprovalRequiredError(input.toolId, decision);\n }\n\n return execute(...args);\n };\n }\n\n private async authorizeOrFallback(input: {\n toolsetId: string;\n toolId: string;\n context: SecurityContext;\n inputMetadata: Record<string, unknown>;\n }) {\n try {\n return await this.authorizeToolCall(input);\n } catch (error) {\n if (this.failOpen) {\n this.log(\"Authorization failed open for tool %s\", input.toolId, error);\n return {\n decisionId: \"local-fail-open\",\n outcome: \"flag\" as const,\n reasonCode: \"authorization_unavailable_fail_open\",\n message:\n \"Gateway authorization was unavailable; failOpen allowed execution.\",\n policyVersion: 0,\n };\n }\n throw new GatewaySecurityError(\n `Gateway authorization failed for tool ${input.toolId}`,\n { cause: error },\n );\n }\n }\n\n private async postJson<TResponse>(\n path: string,\n payload: Record<string, unknown>,\n ): Promise<TResponse> {\n const response = await fetch(`${this.host}${path}`, {\n method: \"POST\",\n headers: {\n Authorization: this.authHeader(),\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n const body = await response.text();\n throw new GatewaySecurityError(\n `Gateway API ${response.status} on ${path}: ${body || response.statusText}`,\n );\n }\n\n return (await response.json()) as TResponse;\n }\n\n private describeTool<TTools extends ToolCollection>(\n key: string,\n tool: ToolObject | ((...args: unknown[]) => unknown),\n options: SecureToolsOptions<TTools>,\n ): ToolDescriptor & { name: string } {\n const toolObject = typeof tool === \"function\" ? undefined : tool;\n const id =\n options.toolIds?.[key] ?? `${options.toolsetId}.${toSnakeCase(key)}`;\n return {\n id,\n name: key,\n description:\n typeof toolObject?.description === \"string\"\n ? toolObject.description\n : undefined,\n metadata: options.toolMetadata?.[key] ?? {},\n };\n }\n\n private shouldExposeTool(resolve: ToolsetResolveResponse, toolId: string) {\n if (resolve.mode === \"observe\") return true;\n if (resolve.blockedToolIds.includes(toolId)) return false;\n return true;\n }\n\n private normalizeContext(context?: SecurityContext): SecurityContext {\n return {\n ...(context ?? {}),\n metadata: context?.metadata ?? {},\n };\n }\n\n private authHeader() {\n return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(\n \"base64\",\n )}`;\n }\n\n private setSpanAttributes(attributes: Record<string, unknown>) {\n const span = getActiveSpan();\n if (!span) return;\n\n for (const [key, value] of Object.entries(attributes)) {\n if (value === undefined || value === null) continue;\n span.setAttribute(key, value);\n }\n }\n\n private log(message: string, ...args: unknown[]) {\n if (this.debug) {\n console.log(`[gateway.security] ${message}`, ...args);\n }\n }\n}\n\nfunction getActiveSpan(): {\n setAttribute: (key: string, value: unknown) => void;\n} | null {\n try {\n // Optional peer dependency. Avoid making security unusable without tracing.\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const otel = require(\"@opentelemetry/api\") as {\n trace?: {\n getActiveSpan?: () =>\n | { setAttribute: (key: string, value: unknown) => void }\n | undefined;\n };\n };\n return otel.trace?.getActiveSpan?.() ?? null;\n } catch {\n return null;\n }\n}\n\nfunction toSnakeCase(value: string) {\n return value\n .replace(/([a-z0-9])([A-Z])/g, \"$1_$2\")\n .replace(/[\\s-]+/g, \"_\")\n .toLowerCase();\n}\n\nexport const gateway = {\n fromEnv: GatewaySecurity.fromEnv,\n};\n\nexport type {\n AgentDescriptor as GatewayAgentDescriptor,\n GatewaySecurityConfig,\n SecurityContext as GatewaySecurityContext,\n SecureToolsOptions,\n ToolCallAuthorizeResponse as GatewayToolCallAuthorizeResponse,\n ToolDescriptor as GatewayToolDescriptor,\n ToolsetResolveResponse as GatewayToolsetResolveResponse,\n};\n"],"mappings":";;;;;;;;AAmFA,IAAM,sBAAsB;AAAA,EAC1B,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,YAAY;AAAA,EACZ,SAAS;AAAA,EACT,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,kBAAkB;AAAA,EAClB,sBAAsB;AACxB;AAEO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC9C,YAAY,SAAiB,SAA+B;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAEO,IAAM,0BAAN,cAAsC,qBAAqB;AAAA,EACvD;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,UAAqC;AAC/D,UAAM,SAAS,WAAW,wBAAwB,MAAM,EAAE;AAC1D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW;AAAA,EAClB;AACF;AAQO,IAAM,mCAAN,cAA+C,qBAAqB;AAAA,EAChE;AAAA,EACA;AAAA,EAET,YAAY,QAAgB,UAAqC;AAC/D,UAAM,SAAS,WAAW,sCAAsC,MAAM,EAAE;AACxE,SAAK,OAAO;AACZ,SAAK,S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A,MACL,GAAI,WAAW,CAAC;AAAA,MAChB,UAAU,SAAS,YAAY,CAAC;AAAA,IAClC;AAAA,EACF;AAAA,EAEQ,aAAa;AACnB,WAAO,SAAS,OAAO,KAAK,GAAG,KAAK,SAAS,IAAI,KAAK,SAAS,EAAE,EAAE;AAAA,MACjE;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,kBAAkB,YAAqC;AAC7D,UAAM,OAAO,cAAc;AAC3B,QAAI,CAAC,KAAM;AAEX,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,UAAI,UAAU,UAAa,UAAU,KAAM;AAC3C,WAAK,aAAa,KAAK,KAAK;AAAA,IAC9B;AAAA,EACF;AAAA,EAEQ,IAAI,YAAoB,MAAiB;AAC/C,QAAI,KAAK,OAAO;AACd,cAAQ,IAAI,sBAAsB,OAAO,IAAI,GAAG,IAAI;AAAA,IACtD;AAAA,EACF;AACF;AAEA,SAAS,gBAEA;AACP,MAAI;AAGF,UAAM,OAAO,UAAQ,oBAAoB;AAOzC,WAAO,KAAK,OAAO,gBAAgB,KAAK;AAAA,EAC1C,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,YAAY,OAAe;AAClC,SAAO,MACJ,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,WAAW,GAAG,EACtB,YAAY;AACjB;AAEO,IAAM,UAAU;AAAA,EACrB,SAAS,gBAAgB;AAC3B;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { T as TracingConfig, a as TracingState, f as flush, i as init, b as isGatewayConfigured, c as isInitialized, s as shutdown } from '../index-Caw3XVub.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { T as TracingConfig, a as TracingState, f as flush, i as init, b as isGatewayConfigured, c as isInitialized, s as shutdown } from '../index-Caw3XVub.js';
|