@solongate/core 0.1.0
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/dist/index.d.ts +698 -0
- package/dist/index.js +661 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var SolonGateError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
timestamp;
|
|
7
|
+
details;
|
|
8
|
+
constructor(message, code, details = {}) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "SolonGateError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
13
|
+
this.details = Object.freeze({ ...details });
|
|
14
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Serializable representation for logging and API responses.
|
|
18
|
+
* Never includes stack traces (information leakage prevention).
|
|
19
|
+
*/
|
|
20
|
+
toJSON() {
|
|
21
|
+
return {
|
|
22
|
+
name: this.name,
|
|
23
|
+
code: this.code,
|
|
24
|
+
message: this.message,
|
|
25
|
+
timestamp: this.timestamp,
|
|
26
|
+
details: this.details
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var PolicyDeniedError = class extends SolonGateError {
|
|
31
|
+
constructor(toolName, reason, details = {}) {
|
|
32
|
+
super(
|
|
33
|
+
`Policy denied execution of tool "${toolName}": ${reason}`,
|
|
34
|
+
"POLICY_DENIED",
|
|
35
|
+
{ toolName, reason, ...details }
|
|
36
|
+
);
|
|
37
|
+
this.name = "PolicyDeniedError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var TrustEscalationError = class extends SolonGateError {
|
|
41
|
+
constructor(message) {
|
|
42
|
+
super(message, "TRUST_ESCALATION");
|
|
43
|
+
this.name = "TrustEscalationError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var SchemaValidationError = class extends SolonGateError {
|
|
47
|
+
constructor(toolName, validationErrors) {
|
|
48
|
+
super(
|
|
49
|
+
`Schema validation failed for tool "${toolName}": ${validationErrors.join("; ")}`,
|
|
50
|
+
"SCHEMA_VALIDATION_FAILED",
|
|
51
|
+
{ toolName, validationErrors }
|
|
52
|
+
);
|
|
53
|
+
this.name = "SchemaValidationError";
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var RateLimitError = class extends SolonGateError {
|
|
57
|
+
constructor(toolName, limitPerMinute) {
|
|
58
|
+
super(
|
|
59
|
+
`Rate limit exceeded for tool "${toolName}": max ${limitPerMinute}/min`,
|
|
60
|
+
"RATE_LIMIT_EXCEEDED",
|
|
61
|
+
{ toolName, limitPerMinute }
|
|
62
|
+
);
|
|
63
|
+
this.name = "RateLimitError";
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var ToolNotFoundError = class extends SolonGateError {
|
|
67
|
+
constructor(toolName, serverName) {
|
|
68
|
+
super(
|
|
69
|
+
`Tool "${toolName}" not found on server "${serverName}"`,
|
|
70
|
+
"TOOL_NOT_FOUND",
|
|
71
|
+
{ toolName, serverName }
|
|
72
|
+
);
|
|
73
|
+
this.name = "ToolNotFoundError";
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
var UnsafeConfigurationError = class extends SolonGateError {
|
|
77
|
+
constructor(message, field) {
|
|
78
|
+
super(
|
|
79
|
+
`Unsafe configuration detected: ${message}`,
|
|
80
|
+
"UNSAFE_CONFIGURATION",
|
|
81
|
+
{ field }
|
|
82
|
+
);
|
|
83
|
+
this.name = "UnsafeConfigurationError";
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var InputGuardError = class extends SolonGateError {
|
|
87
|
+
constructor(toolName, threats) {
|
|
88
|
+
super(
|
|
89
|
+
`Input guard blocked tool "${toolName}": ${threats.map((t) => t.description).join("; ")}`,
|
|
90
|
+
"INPUT_GUARD_BLOCKED",
|
|
91
|
+
{ toolName, threatCount: threats.length, threats }
|
|
92
|
+
);
|
|
93
|
+
this.name = "InputGuardError";
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var NetworkError = class extends SolonGateError {
|
|
97
|
+
constructor(operation, statusCode, details = {}) {
|
|
98
|
+
super(
|
|
99
|
+
`Network error during ${operation}${statusCode ? ` (HTTP ${statusCode})` : ""}`,
|
|
100
|
+
"NETWORK_ERROR",
|
|
101
|
+
{ operation, statusCode, ...details }
|
|
102
|
+
);
|
|
103
|
+
this.name = "NetworkError";
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/trust.ts
|
|
108
|
+
var TrustLevel = {
|
|
109
|
+
UNTRUSTED: "UNTRUSTED",
|
|
110
|
+
VERIFIED: "VERIFIED",
|
|
111
|
+
TRUSTED: "TRUSTED"
|
|
112
|
+
};
|
|
113
|
+
function isValidTrustLevel(value) {
|
|
114
|
+
return typeof value === "string" && Object.values(TrustLevel).includes(value);
|
|
115
|
+
}
|
|
116
|
+
function assertValidTransition(from, to) {
|
|
117
|
+
if (to === TrustLevel.TRUSTED) {
|
|
118
|
+
throw new TrustEscalationError(
|
|
119
|
+
"Cannot escalate to TRUSTED level. TRUSTED is reserved for system-internal operations."
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (from === TrustLevel.VERIFIED && to === TrustLevel.UNTRUSTED) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (from === TrustLevel.UNTRUSTED && to === TrustLevel.VERIFIED) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (from === to) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
throw new TrustEscalationError(
|
|
132
|
+
`Invalid trust transition from ${from} to ${to}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
var Permission = {
|
|
136
|
+
READ: "READ",
|
|
137
|
+
WRITE: "WRITE",
|
|
138
|
+
EXECUTE: "EXECUTE"
|
|
139
|
+
};
|
|
140
|
+
var PermissionSchema = z.enum(["READ", "WRITE", "EXECUTE"]);
|
|
141
|
+
function createPermissionSet(permissions) {
|
|
142
|
+
for (const p of permissions) {
|
|
143
|
+
PermissionSchema.parse(p);
|
|
144
|
+
}
|
|
145
|
+
return new Set(permissions);
|
|
146
|
+
}
|
|
147
|
+
var NO_PERMISSIONS = Object.freeze(
|
|
148
|
+
/* @__PURE__ */ new Set()
|
|
149
|
+
);
|
|
150
|
+
var READ_ONLY = Object.freeze(
|
|
151
|
+
/* @__PURE__ */ new Set([Permission.READ])
|
|
152
|
+
);
|
|
153
|
+
function hasPermission(permissions, required) {
|
|
154
|
+
return permissions.has(required);
|
|
155
|
+
}
|
|
156
|
+
function hasAllPermissions(permissions, required) {
|
|
157
|
+
return required.every((p) => permissions.has(p));
|
|
158
|
+
}
|
|
159
|
+
function permissionForMethod(method) {
|
|
160
|
+
if (method.startsWith("resources/") || method.startsWith("prompts/") || method === "tools/list") {
|
|
161
|
+
return Permission.READ;
|
|
162
|
+
}
|
|
163
|
+
if (method === "tools/call") {
|
|
164
|
+
return Permission.EXECUTE;
|
|
165
|
+
}
|
|
166
|
+
return Permission.EXECUTE;
|
|
167
|
+
}
|
|
168
|
+
var PolicyEffect = {
|
|
169
|
+
ALLOW: "ALLOW",
|
|
170
|
+
DENY: "DENY"
|
|
171
|
+
};
|
|
172
|
+
var PolicyRuleSchema = z.object({
|
|
173
|
+
id: z.string().min(1).max(256),
|
|
174
|
+
description: z.string().max(1024),
|
|
175
|
+
effect: z.enum(["ALLOW", "DENY"]),
|
|
176
|
+
priority: z.number().int().min(0).max(1e4).default(1e3),
|
|
177
|
+
toolPattern: z.string().min(1).max(512),
|
|
178
|
+
permission: z.enum(["READ", "WRITE", "EXECUTE"]),
|
|
179
|
+
minimumTrustLevel: z.enum(["UNTRUSTED", "VERIFIED", "TRUSTED"]),
|
|
180
|
+
argumentConstraints: z.record(z.unknown()).optional(),
|
|
181
|
+
pathConstraints: z.object({
|
|
182
|
+
allowed: z.array(z.string()).optional(),
|
|
183
|
+
denied: z.array(z.string()).optional(),
|
|
184
|
+
rootDirectory: z.string().optional(),
|
|
185
|
+
allowSymlinks: z.boolean().optional()
|
|
186
|
+
}).optional(),
|
|
187
|
+
commandConstraints: z.object({
|
|
188
|
+
allowed: z.array(z.string()).optional(),
|
|
189
|
+
denied: z.array(z.string()).optional()
|
|
190
|
+
}).optional(),
|
|
191
|
+
enabled: z.boolean().default(true),
|
|
192
|
+
createdAt: z.string().datetime(),
|
|
193
|
+
updatedAt: z.string().datetime()
|
|
194
|
+
});
|
|
195
|
+
var PolicySetSchema = z.object({
|
|
196
|
+
id: z.string().min(1).max(256),
|
|
197
|
+
name: z.string().min(1).max(256),
|
|
198
|
+
description: z.string().max(2048),
|
|
199
|
+
version: z.number().int().min(0),
|
|
200
|
+
rules: z.array(PolicyRuleSchema),
|
|
201
|
+
createdAt: z.string().datetime(),
|
|
202
|
+
updatedAt: z.string().datetime()
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// src/tool.ts
|
|
206
|
+
function createToolCapability(params) {
|
|
207
|
+
return {
|
|
208
|
+
maxPermissions: [],
|
|
209
|
+
defaultPermissions: [],
|
|
210
|
+
hasSideEffects: true,
|
|
211
|
+
accessesSensitiveData: true,
|
|
212
|
+
rateLimitPerMinute: 60,
|
|
213
|
+
...params
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/context.ts
|
|
218
|
+
function createSecurityContext(params) {
|
|
219
|
+
return {
|
|
220
|
+
trustLevel: "UNTRUSTED",
|
|
221
|
+
grantedPermissions: /* @__PURE__ */ new Set(),
|
|
222
|
+
sessionId: null,
|
|
223
|
+
metadata: {},
|
|
224
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
225
|
+
...params
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/constants.ts
|
|
230
|
+
var DEFAULT_POLICY_EFFECT = "DENY";
|
|
231
|
+
var MAX_RULES_PER_POLICY_SET = 1e3;
|
|
232
|
+
var MAX_ARGUMENT_DEPTH = 10;
|
|
233
|
+
var MAX_ARGUMENTS_SIZE_BYTES = 1048576;
|
|
234
|
+
var MAX_TOOL_NAME_LENGTH = 256;
|
|
235
|
+
var MAX_SERVER_NAME_LENGTH = 256;
|
|
236
|
+
var DEFAULT_RATE_LIMIT_PER_MINUTE = 60;
|
|
237
|
+
var MAX_RATE_LIMIT_PER_MINUTE = 1e4;
|
|
238
|
+
var SECURITY_CONTEXT_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
239
|
+
var POLICY_EVALUATION_TIMEOUT_MS = 100;
|
|
240
|
+
var INPUT_GUARD_MAX_LENGTH = 4096;
|
|
241
|
+
var INPUT_GUARD_ENTROPY_THRESHOLD = 4.5;
|
|
242
|
+
var INPUT_GUARD_MIN_ENTROPY_LENGTH = 32;
|
|
243
|
+
var INPUT_GUARD_MAX_WILDCARDS = 3;
|
|
244
|
+
var TOKEN_DEFAULT_TTL_SECONDS = 30;
|
|
245
|
+
var TOKEN_MIN_SECRET_LENGTH = 32;
|
|
246
|
+
var TOKEN_MAX_AGE_SECONDS = 300;
|
|
247
|
+
var RATE_LIMIT_WINDOW_MS = 6e4;
|
|
248
|
+
var RATE_LIMIT_MAX_ENTRIES = 1e4;
|
|
249
|
+
var UNSAFE_CONFIGURATION_WARNINGS = {
|
|
250
|
+
WILDCARD_ALLOW: "Wildcard ALLOW rules grant permission to ALL tools. This bypasses the default-deny model.",
|
|
251
|
+
TRUSTED_LEVEL_EXTERNAL: "Setting trust level to TRUSTED for external requests bypasses all security checks.",
|
|
252
|
+
WRITE_WITHOUT_READ: "Granting WRITE without READ is unusual and may indicate a misconfiguration.",
|
|
253
|
+
EXECUTE_WITHOUT_REVIEW: "EXECUTE permission allows tools to perform arbitrary actions. Review carefully.",
|
|
254
|
+
RATE_LIMIT_ZERO: "A rate limit of 0 means unlimited calls. This removes protection against runaway loops.",
|
|
255
|
+
DISABLED_VALIDATION: "Disabling schema validation removes input sanitization protections."
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// src/mcp-types.ts
|
|
259
|
+
function createDeniedToolResult(reason) {
|
|
260
|
+
return {
|
|
261
|
+
content: [
|
|
262
|
+
{
|
|
263
|
+
type: "text",
|
|
264
|
+
text: JSON.stringify({
|
|
265
|
+
error: "POLICY_DENIED",
|
|
266
|
+
message: reason,
|
|
267
|
+
hint: "This tool call was blocked by SolonGate security policy. Check your policy configuration."
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
],
|
|
271
|
+
isError: true
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
var DEFAULT_OPTIONS = {
|
|
275
|
+
maxDepth: MAX_ARGUMENT_DEPTH,
|
|
276
|
+
maxSizeBytes: MAX_ARGUMENTS_SIZE_BYTES,
|
|
277
|
+
stripUnknown: false
|
|
278
|
+
};
|
|
279
|
+
function validateToolInput(schema, input, options) {
|
|
280
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
281
|
+
const errors = [];
|
|
282
|
+
const sizeError = checkInputSize(input, opts.maxSizeBytes);
|
|
283
|
+
if (sizeError) {
|
|
284
|
+
return { valid: false, errors: [sizeError], sanitized: null };
|
|
285
|
+
}
|
|
286
|
+
const depthError = checkInputDepth(input, opts.maxDepth);
|
|
287
|
+
if (depthError) {
|
|
288
|
+
return { valid: false, errors: [depthError], sanitized: null };
|
|
289
|
+
}
|
|
290
|
+
const result = schema.safeParse(input);
|
|
291
|
+
if (!result.success) {
|
|
292
|
+
for (const issue of result.error.issues) {
|
|
293
|
+
const path = issue.path.length > 0 ? issue.path.join(".") : "root";
|
|
294
|
+
errors.push(`${path}: ${issue.message}`);
|
|
295
|
+
}
|
|
296
|
+
return { valid: false, errors, sanitized: null };
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
valid: true,
|
|
300
|
+
errors: [],
|
|
301
|
+
sanitized: result.data
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function createStrictSchema(shape) {
|
|
305
|
+
return z.object(shape).strict();
|
|
306
|
+
}
|
|
307
|
+
function checkInputSize(input, maxBytes) {
|
|
308
|
+
let serialized;
|
|
309
|
+
try {
|
|
310
|
+
serialized = JSON.stringify(input);
|
|
311
|
+
} catch {
|
|
312
|
+
return "Input cannot be serialized to JSON";
|
|
313
|
+
}
|
|
314
|
+
const sizeBytes = new TextEncoder().encode(serialized).length;
|
|
315
|
+
if (sizeBytes > maxBytes) {
|
|
316
|
+
return `Input size ${sizeBytes} bytes exceeds maximum ${maxBytes} bytes`;
|
|
317
|
+
}
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
function checkInputDepth(input, maxDepth) {
|
|
321
|
+
const depth = measureDepth(input, 0);
|
|
322
|
+
if (depth > maxDepth) {
|
|
323
|
+
return `Input depth ${depth} exceeds maximum ${maxDepth}`;
|
|
324
|
+
}
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
function measureDepth(value, currentDepth) {
|
|
328
|
+
if (currentDepth > MAX_ARGUMENT_DEPTH + 1) {
|
|
329
|
+
return currentDepth;
|
|
330
|
+
}
|
|
331
|
+
if (value === null || value === void 0 || typeof value !== "object") {
|
|
332
|
+
return currentDepth;
|
|
333
|
+
}
|
|
334
|
+
if (Array.isArray(value)) {
|
|
335
|
+
let maxChildDepth2 = currentDepth + 1;
|
|
336
|
+
for (const item of value) {
|
|
337
|
+
const childDepth = measureDepth(item, currentDepth + 1);
|
|
338
|
+
if (childDepth > maxChildDepth2) maxChildDepth2 = childDepth;
|
|
339
|
+
}
|
|
340
|
+
return maxChildDepth2;
|
|
341
|
+
}
|
|
342
|
+
let maxChildDepth = currentDepth + 1;
|
|
343
|
+
for (const key of Object.keys(value)) {
|
|
344
|
+
const childDepth = measureDepth(
|
|
345
|
+
value[key],
|
|
346
|
+
currentDepth + 1
|
|
347
|
+
);
|
|
348
|
+
if (childDepth > maxChildDepth) maxChildDepth = childDepth;
|
|
349
|
+
}
|
|
350
|
+
return maxChildDepth;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/input-guard.ts
|
|
354
|
+
var DEFAULT_INPUT_GUARD_CONFIG = Object.freeze({
|
|
355
|
+
pathTraversal: true,
|
|
356
|
+
shellInjection: true,
|
|
357
|
+
wildcardAbuse: true,
|
|
358
|
+
lengthLimit: 4096,
|
|
359
|
+
entropyLimit: true,
|
|
360
|
+
ssrf: true,
|
|
361
|
+
sqlInjection: true
|
|
362
|
+
});
|
|
363
|
+
var PATH_TRAVERSAL_PATTERNS = [
|
|
364
|
+
/\.\.\//,
|
|
365
|
+
// ../
|
|
366
|
+
/\.\.\\/,
|
|
367
|
+
// ..\
|
|
368
|
+
/%2e%2e/i,
|
|
369
|
+
// URL-encoded ..
|
|
370
|
+
/%2e\./i,
|
|
371
|
+
// partial URL-encoded
|
|
372
|
+
/\.%2e/i,
|
|
373
|
+
// partial URL-encoded
|
|
374
|
+
/%252e%252e/i,
|
|
375
|
+
// double URL-encoded
|
|
376
|
+
/\.\.\0/
|
|
377
|
+
// null byte variant
|
|
378
|
+
];
|
|
379
|
+
var SENSITIVE_PATHS = [
|
|
380
|
+
/\/etc\/passwd/i,
|
|
381
|
+
/\/etc\/shadow/i,
|
|
382
|
+
/\/proc\//i,
|
|
383
|
+
/\/dev\//i,
|
|
384
|
+
/c:\\windows\\system32/i,
|
|
385
|
+
/c:\\windows\\syswow64/i,
|
|
386
|
+
/\/root\//i,
|
|
387
|
+
/~\//,
|
|
388
|
+
/\.env(\.|$)/i,
|
|
389
|
+
// .env, .env.local, .env.production
|
|
390
|
+
/\.aws\/credentials/i,
|
|
391
|
+
// AWS credentials
|
|
392
|
+
/\.ssh\/id_/i,
|
|
393
|
+
// SSH keys
|
|
394
|
+
/\.kube\/config/i,
|
|
395
|
+
// Kubernetes config
|
|
396
|
+
/wp-config\.php/i,
|
|
397
|
+
// WordPress config
|
|
398
|
+
/\.git\/config/i,
|
|
399
|
+
// Git config
|
|
400
|
+
/\.npmrc/i,
|
|
401
|
+
// npm credentials
|
|
402
|
+
/\.pypirc/i
|
|
403
|
+
// PyPI credentials
|
|
404
|
+
];
|
|
405
|
+
function detectPathTraversal(value) {
|
|
406
|
+
for (const pattern of PATH_TRAVERSAL_PATTERNS) {
|
|
407
|
+
if (pattern.test(value)) return true;
|
|
408
|
+
}
|
|
409
|
+
for (const pattern of SENSITIVE_PATHS) {
|
|
410
|
+
if (pattern.test(value)) return true;
|
|
411
|
+
}
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
var SHELL_INJECTION_PATTERNS = [
|
|
415
|
+
/[;|&`]/,
|
|
416
|
+
// Command separators and backtick execution
|
|
417
|
+
/\$\(/,
|
|
418
|
+
// Command substitution $(...)
|
|
419
|
+
/\$\{/,
|
|
420
|
+
// Variable expansion ${...}
|
|
421
|
+
/>\s*/,
|
|
422
|
+
// Output redirect
|
|
423
|
+
/<\s*/,
|
|
424
|
+
// Input redirect
|
|
425
|
+
/&&/,
|
|
426
|
+
// AND chaining
|
|
427
|
+
/\|\|/,
|
|
428
|
+
// OR chaining
|
|
429
|
+
/\beval\b/i,
|
|
430
|
+
// eval command
|
|
431
|
+
/\bexec\b/i,
|
|
432
|
+
// exec command
|
|
433
|
+
/\bsystem\b/i,
|
|
434
|
+
// system call
|
|
435
|
+
/%0a/i,
|
|
436
|
+
// URL-encoded newline
|
|
437
|
+
/%0d/i,
|
|
438
|
+
// URL-encoded carriage return
|
|
439
|
+
/%09/i,
|
|
440
|
+
// URL-encoded tab
|
|
441
|
+
/\r\n/,
|
|
442
|
+
// CRLF injection
|
|
443
|
+
/\n/
|
|
444
|
+
// Newline (command separator on Unix)
|
|
445
|
+
];
|
|
446
|
+
function detectShellInjection(value) {
|
|
447
|
+
for (const pattern of SHELL_INJECTION_PATTERNS) {
|
|
448
|
+
if (pattern.test(value)) return true;
|
|
449
|
+
}
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
var MAX_WILDCARDS_PER_VALUE = 3;
|
|
453
|
+
function detectWildcardAbuse(value) {
|
|
454
|
+
if (value.includes("**")) return true;
|
|
455
|
+
const wildcardCount = (value.match(/\*/g) || []).length;
|
|
456
|
+
if (wildcardCount > MAX_WILDCARDS_PER_VALUE) return true;
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
var SSRF_PATTERNS = [
|
|
460
|
+
/^https?:\/\/localhost\b/i,
|
|
461
|
+
/^https?:\/\/127\.\d{1,3}\.\d{1,3}\.\d{1,3}/,
|
|
462
|
+
/^https?:\/\/0\.0\.0\.0/,
|
|
463
|
+
/^https?:\/\/\[::1\]/,
|
|
464
|
+
// IPv6 loopback
|
|
465
|
+
/^https?:\/\/10\.\d{1,3}\.\d{1,3}\.\d{1,3}/,
|
|
466
|
+
// 10.x.x.x
|
|
467
|
+
/^https?:\/\/172\.(1[6-9]|2\d|3[01])\./,
|
|
468
|
+
// 172.16-31.x.x
|
|
469
|
+
/^https?:\/\/192\.168\./,
|
|
470
|
+
// 192.168.x.x
|
|
471
|
+
/^https?:\/\/169\.254\./,
|
|
472
|
+
// Link-local / AWS metadata
|
|
473
|
+
/metadata\.google\.internal/i,
|
|
474
|
+
// GCP metadata
|
|
475
|
+
/^https?:\/\/metadata\b/i,
|
|
476
|
+
// Generic metadata endpoint
|
|
477
|
+
// IPv6 bypass patterns
|
|
478
|
+
/^https?:\/\/\[fe80:/i,
|
|
479
|
+
// IPv6 link-local
|
|
480
|
+
/^https?:\/\/\[fc00:/i,
|
|
481
|
+
// IPv6 unique local
|
|
482
|
+
/^https?:\/\/\[fd[0-9a-f]{2}:/i,
|
|
483
|
+
// IPv6 unique local (fd00::/8)
|
|
484
|
+
/^https?:\/\/\[::ffff:127\./i,
|
|
485
|
+
// IPv4-mapped IPv6 loopback
|
|
486
|
+
/^https?:\/\/\[::ffff:10\./i,
|
|
487
|
+
// IPv4-mapped IPv6 private
|
|
488
|
+
/^https?:\/\/\[::ffff:172\.(1[6-9]|2\d|3[01])\./i,
|
|
489
|
+
// IPv4-mapped IPv6 private
|
|
490
|
+
/^https?:\/\/\[::ffff:192\.168\./i,
|
|
491
|
+
// IPv4-mapped IPv6 private
|
|
492
|
+
/^https?:\/\/\[::ffff:169\.254\./i,
|
|
493
|
+
// IPv4-mapped IPv6 link-local
|
|
494
|
+
// Hex IP bypass (e.g., 0x7f000001 = 127.0.0.1)
|
|
495
|
+
/^https?:\/\/0x[0-9a-f]+\b/i,
|
|
496
|
+
// Octal IP bypass (e.g., 0177.0.0.1 = 127.0.0.1)
|
|
497
|
+
/^https?:\/\/0[0-7]{1,3}\./
|
|
498
|
+
];
|
|
499
|
+
function detectDecimalIP(value) {
|
|
500
|
+
const match = value.match(/^https?:\/\/(\d{8,10})(?:[:/]|$)/);
|
|
501
|
+
if (!match || !match[1]) return false;
|
|
502
|
+
const decimal = parseInt(match[1], 10);
|
|
503
|
+
if (isNaN(decimal) || decimal > 4294967295) return false;
|
|
504
|
+
return decimal >= 2130706432 && decimal <= 2147483647 || // 127.0.0.0/8
|
|
505
|
+
decimal >= 167772160 && decimal <= 184549375 || // 10.0.0.0/8
|
|
506
|
+
decimal >= 2886729728 && decimal <= 2887778303 || // 172.16.0.0/12
|
|
507
|
+
decimal >= 3232235520 && decimal <= 3232301055 || // 192.168.0.0/16
|
|
508
|
+
decimal >= 2851995648 && decimal <= 2852061183 || // 169.254.0.0/16
|
|
509
|
+
decimal === 0;
|
|
510
|
+
}
|
|
511
|
+
function detectSSRF(value) {
|
|
512
|
+
for (const pattern of SSRF_PATTERNS) {
|
|
513
|
+
if (pattern.test(value)) return true;
|
|
514
|
+
}
|
|
515
|
+
if (detectDecimalIP(value)) return true;
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
var SQL_INJECTION_PATTERNS = [
|
|
519
|
+
/'\s{0,20}(OR|AND)\s{0,20}'.{0,200}'/i,
|
|
520
|
+
// ' OR '1'='1 — bounded to prevent ReDoS
|
|
521
|
+
/'\s{0,10};\s{0,10}(DROP|DELETE|UPDATE|INSERT|ALTER|CREATE|EXEC)/i,
|
|
522
|
+
// '; DROP TABLE
|
|
523
|
+
/UNION\s+(ALL\s+)?SELECT/i,
|
|
524
|
+
// UNION SELECT
|
|
525
|
+
/--\s*$/m,
|
|
526
|
+
// SQL comment at end of line
|
|
527
|
+
/\/\*.{0,500}?\*\//,
|
|
528
|
+
// SQL block comment — bounded + non-greedy
|
|
529
|
+
/\bSLEEP\s*\(/i,
|
|
530
|
+
// Time-based injection
|
|
531
|
+
/\bBENCHMARK\s*\(/i,
|
|
532
|
+
// MySQL benchmark
|
|
533
|
+
/\bWAITFOR\s+DELAY/i,
|
|
534
|
+
// MSSQL delay
|
|
535
|
+
/\b(LOAD_FILE|INTO\s+OUTFILE|INTO\s+DUMPFILE)\b/i
|
|
536
|
+
// File operations
|
|
537
|
+
];
|
|
538
|
+
function detectSQLInjection(value) {
|
|
539
|
+
for (const pattern of SQL_INJECTION_PATTERNS) {
|
|
540
|
+
if (pattern.test(value)) return true;
|
|
541
|
+
}
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
function checkLengthLimits(value, maxLength = 4096) {
|
|
545
|
+
return value.length <= maxLength;
|
|
546
|
+
}
|
|
547
|
+
var ENTROPY_THRESHOLD = 4.5;
|
|
548
|
+
var MIN_LENGTH_FOR_ENTROPY_CHECK = 32;
|
|
549
|
+
function checkEntropyLimits(value) {
|
|
550
|
+
if (value.length < MIN_LENGTH_FOR_ENTROPY_CHECK) return true;
|
|
551
|
+
const entropy = calculateShannonEntropy(value);
|
|
552
|
+
return entropy <= ENTROPY_THRESHOLD;
|
|
553
|
+
}
|
|
554
|
+
function calculateShannonEntropy(str) {
|
|
555
|
+
const freq = /* @__PURE__ */ new Map();
|
|
556
|
+
for (const char of str) {
|
|
557
|
+
freq.set(char, (freq.get(char) ?? 0) + 1);
|
|
558
|
+
}
|
|
559
|
+
let entropy = 0;
|
|
560
|
+
const len = str.length;
|
|
561
|
+
for (const count of freq.values()) {
|
|
562
|
+
const p = count / len;
|
|
563
|
+
if (p > 0) {
|
|
564
|
+
entropy -= p * Math.log2(p);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return entropy;
|
|
568
|
+
}
|
|
569
|
+
function sanitizeInput(field, value, config = DEFAULT_INPUT_GUARD_CONFIG) {
|
|
570
|
+
const threats = [];
|
|
571
|
+
if (typeof value !== "string") {
|
|
572
|
+
if (typeof value === "object" && value !== null) {
|
|
573
|
+
return sanitizeObject(field, value, config);
|
|
574
|
+
}
|
|
575
|
+
return { safe: true, threats: [] };
|
|
576
|
+
}
|
|
577
|
+
if (config.pathTraversal && detectPathTraversal(value)) {
|
|
578
|
+
threats.push({
|
|
579
|
+
type: "PATH_TRAVERSAL",
|
|
580
|
+
field,
|
|
581
|
+
value: truncate(value, 100),
|
|
582
|
+
description: "Path traversal pattern detected"
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
if (config.shellInjection && detectShellInjection(value)) {
|
|
586
|
+
threats.push({
|
|
587
|
+
type: "SHELL_INJECTION",
|
|
588
|
+
field,
|
|
589
|
+
value: truncate(value, 100),
|
|
590
|
+
description: "Shell injection pattern detected"
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
if (config.wildcardAbuse && detectWildcardAbuse(value)) {
|
|
594
|
+
threats.push({
|
|
595
|
+
type: "WILDCARD_ABUSE",
|
|
596
|
+
field,
|
|
597
|
+
value: truncate(value, 100),
|
|
598
|
+
description: "Wildcard abuse pattern detected"
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
if (!checkLengthLimits(value, config.lengthLimit)) {
|
|
602
|
+
threats.push({
|
|
603
|
+
type: "LENGTH_EXCEEDED",
|
|
604
|
+
field,
|
|
605
|
+
value: `[${value.length} chars]`,
|
|
606
|
+
description: `Value exceeds maximum length of ${config.lengthLimit}`
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
if (config.entropyLimit && !checkEntropyLimits(value)) {
|
|
610
|
+
threats.push({
|
|
611
|
+
type: "HIGH_ENTROPY",
|
|
612
|
+
field,
|
|
613
|
+
value: truncate(value, 100),
|
|
614
|
+
description: "High entropy string detected - possible encoded payload"
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
if (config.ssrf && detectSSRF(value)) {
|
|
618
|
+
threats.push({
|
|
619
|
+
type: "SSRF",
|
|
620
|
+
field,
|
|
621
|
+
value: truncate(value, 100),
|
|
622
|
+
description: "Server-side request forgery pattern detected \u2014 internal/metadata URL blocked"
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
if (config.sqlInjection && detectSQLInjection(value)) {
|
|
626
|
+
threats.push({
|
|
627
|
+
type: "SQL_INJECTION",
|
|
628
|
+
field,
|
|
629
|
+
value: truncate(value, 100),
|
|
630
|
+
description: "SQL injection pattern detected"
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
return { safe: threats.length === 0, threats };
|
|
634
|
+
}
|
|
635
|
+
function sanitizeObject(basePath, obj, config) {
|
|
636
|
+
const threats = [];
|
|
637
|
+
if (Array.isArray(obj)) {
|
|
638
|
+
for (let i = 0; i < obj.length; i++) {
|
|
639
|
+
const result = sanitizeInput(`${basePath}[${i}]`, obj[i], config);
|
|
640
|
+
threats.push(...result.threats);
|
|
641
|
+
}
|
|
642
|
+
} else {
|
|
643
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
644
|
+
const result = sanitizeInput(`${basePath}.${key}`, val, config);
|
|
645
|
+
threats.push(...result.threats);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return { safe: threats.length === 0, threats };
|
|
649
|
+
}
|
|
650
|
+
function truncate(str, maxLen) {
|
|
651
|
+
return str.length > maxLen ? str.slice(0, maxLen) + "..." : str;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// src/capability-token.ts
|
|
655
|
+
var DEFAULT_TOKEN_TTL_SECONDS = 30;
|
|
656
|
+
var TOKEN_ALGORITHM = "HS256";
|
|
657
|
+
var MIN_SECRET_LENGTH = 32;
|
|
658
|
+
|
|
659
|
+
export { DEFAULT_INPUT_GUARD_CONFIG, DEFAULT_POLICY_EFFECT, DEFAULT_RATE_LIMIT_PER_MINUTE, DEFAULT_TOKEN_TTL_SECONDS, INPUT_GUARD_ENTROPY_THRESHOLD, INPUT_GUARD_MAX_LENGTH, INPUT_GUARD_MAX_WILDCARDS, INPUT_GUARD_MIN_ENTROPY_LENGTH, InputGuardError, MAX_ARGUMENTS_SIZE_BYTES, MAX_ARGUMENT_DEPTH, MAX_RATE_LIMIT_PER_MINUTE, MAX_RULES_PER_POLICY_SET, MAX_SERVER_NAME_LENGTH, MAX_TOOL_NAME_LENGTH, MIN_SECRET_LENGTH, NO_PERMISSIONS, NetworkError, POLICY_EVALUATION_TIMEOUT_MS, Permission, PermissionSchema, PolicyDeniedError, PolicyEffect, PolicyRuleSchema, PolicySetSchema, RATE_LIMIT_MAX_ENTRIES, RATE_LIMIT_WINDOW_MS, READ_ONLY, RateLimitError, SECURITY_CONTEXT_TIMEOUT_MS, SchemaValidationError, SolonGateError, TOKEN_ALGORITHM, TOKEN_DEFAULT_TTL_SECONDS, TOKEN_MAX_AGE_SECONDS, TOKEN_MIN_SECRET_LENGTH, ToolNotFoundError, TrustEscalationError, TrustLevel, UNSAFE_CONFIGURATION_WARNINGS, UnsafeConfigurationError, assertValidTransition, checkEntropyLimits, checkLengthLimits, createDeniedToolResult, createPermissionSet, createSecurityContext, createStrictSchema, createToolCapability, detectPathTraversal, detectSQLInjection, detectSSRF, detectShellInjection, detectWildcardAbuse, hasAllPermissions, hasPermission, isValidTrustLevel, permissionForMethod, sanitizeInput, validateToolInput };
|
|
660
|
+
//# sourceMappingURL=index.js.map
|
|
661
|
+
//# sourceMappingURL=index.js.map
|