ai-shield-core 0.1.0 → 0.3.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/audit/logger.d.ts.map +1 -1
- package/dist/audit/logger.js +13 -14
- package/dist/audit/types.js +1 -2
- package/dist/cache/lru.js +1 -5
- package/dist/canary/memory.d.ts +75 -0
- package/dist/canary/memory.d.ts.map +1 -0
- package/dist/canary/memory.js +194 -0
- package/dist/context/wrap-context.d.ts +169 -0
- package/dist/context/wrap-context.d.ts.map +1 -0
- package/dist/context/wrap-context.js +278 -0
- package/dist/cost/anomaly.js +1 -4
- package/dist/cost/pricing.d.ts.map +1 -1
- package/dist/cost/pricing.js +26 -19
- package/dist/cost/tracker.d.ts +19 -1
- package/dist/cost/tracker.d.ts.map +1 -1
- package/dist/cost/tracker.js +27 -10
- package/dist/index.d.ts +34 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -37
- package/dist/judge/async-judge.d.ts +85 -0
- package/dist/judge/async-judge.d.ts.map +1 -0
- package/dist/judge/async-judge.js +146 -0
- package/dist/policy/circuit-breaker.d.ts +70 -0
- package/dist/policy/circuit-breaker.d.ts.map +1 -0
- package/dist/policy/circuit-breaker.js +376 -0
- package/dist/policy/engine.js +1 -5
- package/dist/policy/tools.js +4 -8
- package/dist/scanner/canary.js +4 -8
- package/dist/scanner/chain.js +1 -5
- package/dist/scanner/heuristic.d.ts +27 -0
- package/dist/scanner/heuristic.d.ts.map +1 -1
- package/dist/scanner/heuristic.js +118 -7
- package/dist/scanner/ingestion.d.ts +147 -0
- package/dist/scanner/ingestion.d.ts.map +1 -0
- package/dist/scanner/ingestion.js +520 -0
- package/dist/scanner/output.d.ts +73 -0
- package/dist/scanner/output.d.ts.map +1 -0
- package/dist/scanner/output.js +297 -0
- package/dist/scanner/pii.d.ts.map +1 -1
- package/dist/scanner/pii.js +24 -12
- package/dist/shield.d.ts.map +1 -1
- package/dist/shield.js +34 -26
- package/dist/types.d.ts +156 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -2
- package/package.json +4 -3
- package/src/audit/logger.ts +6 -1
- package/src/canary/memory.ts +259 -0
- package/src/context/wrap-context.ts +475 -0
- package/src/cost/pricing.ts +21 -9
- package/src/cost/tracker.ts +35 -1
- package/src/index.ts +113 -2
- package/src/judge/async-judge.ts +254 -0
- package/src/policy/circuit-breaker.ts +449 -0
- package/src/scanner/heuristic.ts +125 -2
- package/src/scanner/ingestion.ts +624 -0
- package/src/scanner/output.ts +386 -0
- package/src/scanner/pii.ts +21 -7
- package/src/shield.ts +15 -2
- package/src/types.ts +194 -2
- package/tsconfig.json +2 -1
- package/dist/audit/logger.js.map +0 -1
- package/dist/audit/types.js.map +0 -1
- package/dist/cache/lru.js.map +0 -1
- package/dist/cost/anomaly.js.map +0 -1
- package/dist/cost/pricing.js.map +0 -1
- package/dist/cost/tracker.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/policy/engine.js.map +0 -1
- package/dist/policy/tools.js.map +0 -1
- package/dist/scanner/canary.js.map +0 -1
- package/dist/scanner/chain.js.map +0 -1
- package/dist/scanner/heuristic.js.map +0 -1
- package/dist/scanner/pii.js.map +0 -1
- package/dist/shield.js.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Circuit Breaker — Tool-Policy Runtime Guard
|
|
3
|
+
//
|
|
4
|
+
// The existing `ToolPolicyScanner` (policy/tools.ts) is a *static*
|
|
5
|
+
// gate: allow / deny lists, manifest pin, dangerous patterns. It
|
|
6
|
+
// runs once per call.
|
|
7
|
+
//
|
|
8
|
+
// The circuit breaker layers *runtime* defense on top:
|
|
9
|
+
// - Rate limit per (tool, scope) within a rolling window.
|
|
10
|
+
// - "Blast radius" cap: max writes per window (for destructive ops).
|
|
11
|
+
// - Trip + cooldown: after N anomalies the tool is blocked for a
|
|
12
|
+
// period regardless of static policy.
|
|
13
|
+
// - Optional Human-In-The-Loop hook for destructive operations
|
|
14
|
+
// ("type the tool name to confirm").
|
|
15
|
+
//
|
|
16
|
+
// Counters can live in-process (default) or in any `ioredis`-shaped
|
|
17
|
+
// store so the breaker tracks state across replicas.
|
|
18
|
+
// ============================================================
|
|
19
|
+
const DESTRUCTIVE_DEFAULTS = [
|
|
20
|
+
"delete_",
|
|
21
|
+
"remove_",
|
|
22
|
+
"drop_",
|
|
23
|
+
"destroy_",
|
|
24
|
+
"wipe_",
|
|
25
|
+
"shutdown_",
|
|
26
|
+
"purge_",
|
|
27
|
+
"truncate_",
|
|
28
|
+
"send_email",
|
|
29
|
+
"transfer_",
|
|
30
|
+
"payment_",
|
|
31
|
+
];
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
failureThreshold: 5,
|
|
34
|
+
windowMs: 60_000,
|
|
35
|
+
cooldownMs: 60_000,
|
|
36
|
+
};
|
|
37
|
+
class InMemoryCounter {
|
|
38
|
+
data = new Map();
|
|
39
|
+
async get(key) {
|
|
40
|
+
const e = this.data.get(key);
|
|
41
|
+
if (!e)
|
|
42
|
+
return null;
|
|
43
|
+
if (e.expiresAt && Date.now() > e.expiresAt) {
|
|
44
|
+
this.data.delete(key);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return e.value;
|
|
48
|
+
}
|
|
49
|
+
async incrbyfloat(key, increment) {
|
|
50
|
+
const cur = parseFloat((await this.get(key)) ?? "0");
|
|
51
|
+
const next = (cur + increment).toString();
|
|
52
|
+
const e = this.data.get(key);
|
|
53
|
+
this.data.set(key, { value: next, expiresAt: e?.expiresAt });
|
|
54
|
+
return next;
|
|
55
|
+
}
|
|
56
|
+
async expire(key, seconds) {
|
|
57
|
+
const e = this.data.get(key);
|
|
58
|
+
if (!e)
|
|
59
|
+
return 0;
|
|
60
|
+
e.expiresAt = Date.now() + seconds * 1000;
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Registry of breakers keyed by `${tool}::${scope}`. The registry
|
|
66
|
+
* owns config + state; per-(tool, scope) breakers are created lazily.
|
|
67
|
+
*/
|
|
68
|
+
export class CircuitBreakerRegistry {
|
|
69
|
+
configs = new Map();
|
|
70
|
+
states = new Map();
|
|
71
|
+
/**
|
|
72
|
+
* Reserved for distributed-counter mode (e.g. cross-replica state).
|
|
73
|
+
* The in-process path is the supported v0.2 surface; the store is
|
|
74
|
+
* accepted so callers wiring up an `ioredis`-shaped backend get a
|
|
75
|
+
* stable constructor option, and downstream releases can swap the
|
|
76
|
+
* internal accounting to use it without breaking the API.
|
|
77
|
+
*/
|
|
78
|
+
store;
|
|
79
|
+
maxKeys;
|
|
80
|
+
constructor(configs = [], options = {}) {
|
|
81
|
+
this.store = options.counterStore ?? new InMemoryCounter();
|
|
82
|
+
const envCap = Number(process.env.AI_SHIELD_CIRCUIT_MAX_KEYS);
|
|
83
|
+
this.maxKeys =
|
|
84
|
+
options.maxKeys ??
|
|
85
|
+
(Number.isFinite(envCap) && envCap > 0 ? envCap : 5_000);
|
|
86
|
+
for (const cfg of configs) {
|
|
87
|
+
this.configure(cfg);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Configure (or re-configure) a breaker. Idempotent. */
|
|
91
|
+
configure(config) {
|
|
92
|
+
const key = keyFor(config.tool, config.scope);
|
|
93
|
+
this.configs.set(key, {
|
|
94
|
+
tool: config.tool,
|
|
95
|
+
scope: config.scope ?? "",
|
|
96
|
+
failureThreshold: config.failureThreshold ?? DEFAULTS.failureThreshold,
|
|
97
|
+
windowMs: config.windowMs ?? DEFAULTS.windowMs,
|
|
98
|
+
cooldownMs: config.cooldownMs ?? DEFAULTS.cooldownMs,
|
|
99
|
+
maxCallsPerWindow: config.maxCallsPerWindow ?? Infinity,
|
|
100
|
+
maxWritesPerWindow: config.maxWritesPerWindow ?? Infinity,
|
|
101
|
+
onDestructive: config.onDestructive ?? (() => true),
|
|
102
|
+
isDestructive: config.isDestructive ?? isLikelyDestructive(config.tool),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Check whether a tool call is allowed. Records the attempt either
|
|
107
|
+
* way; callers must invoke `recordSuccess()`/`recordFailure()` AFTER
|
|
108
|
+
* the actual call so anomaly counts stay honest.
|
|
109
|
+
*/
|
|
110
|
+
async check(tool, context = {}) {
|
|
111
|
+
const scope = scopeFor(context);
|
|
112
|
+
const key = keyFor(tool.name, scope);
|
|
113
|
+
const config = this.configs.get(key) ?? this.configs.get(keyFor(tool.name, ""));
|
|
114
|
+
// No config → no breaker → allow. The caller may still use
|
|
115
|
+
// the static ToolPolicyScanner for default deny.
|
|
116
|
+
if (!config) {
|
|
117
|
+
return { allowed: true, state: "closed" };
|
|
118
|
+
}
|
|
119
|
+
const state = this.getOrInitState(key);
|
|
120
|
+
const now = Date.now();
|
|
121
|
+
prune(state, now, config.windowMs);
|
|
122
|
+
// 1. Open / half-open transitions.
|
|
123
|
+
if (state.state === "open") {
|
|
124
|
+
if (now - state.openedAt >= config.cooldownMs) {
|
|
125
|
+
state.state = "half-open";
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return {
|
|
129
|
+
allowed: false,
|
|
130
|
+
state: "open",
|
|
131
|
+
reason: "circuit_open",
|
|
132
|
+
retryAfterMs: config.cooldownMs - (now - state.openedAt),
|
|
133
|
+
message: `Circuit OPEN for ${tool.name}${scope ? `@${scope}` : ""}`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// 2. Rate-limit cap.
|
|
138
|
+
if (state.calls.length >= config.maxCallsPerWindow) {
|
|
139
|
+
return {
|
|
140
|
+
allowed: false,
|
|
141
|
+
state: state.state,
|
|
142
|
+
reason: "rate_limit",
|
|
143
|
+
retryAfterMs: config.windowMs,
|
|
144
|
+
message: `Rate limit ${config.maxCallsPerWindow}/${config.windowMs}ms exceeded for ${tool.name}`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// 3. Blast-radius cap for destructive tools.
|
|
148
|
+
if (config.isDestructive &&
|
|
149
|
+
state.writes.length >= config.maxWritesPerWindow) {
|
|
150
|
+
return {
|
|
151
|
+
allowed: false,
|
|
152
|
+
state: state.state,
|
|
153
|
+
reason: "blast_radius_exceeded",
|
|
154
|
+
retryAfterMs: config.windowMs,
|
|
155
|
+
message: `Blast-radius cap ${config.maxWritesPerWindow}/${config.windowMs}ms hit for ${tool.name}`,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// 4. HITL gate for destructive ops.
|
|
159
|
+
//
|
|
160
|
+
// Record the call/write OPTIMISTICALLY first, BEFORE awaiting the
|
|
161
|
+
// HITL hook. Two concurrent destructive calls otherwise both see
|
|
162
|
+
// `state.writes.length === 0` and both get past the blast-radius
|
|
163
|
+
// gate (Critic M3 round 1 — TOCTOU on shared mutable state).
|
|
164
|
+
//
|
|
165
|
+
// Round 2 Critic H-NEW-1: rolling back via `pop()` is unsafe under
|
|
166
|
+
// Node.js's cooperative scheduler — a concurrent push between our
|
|
167
|
+
// push and our pop can shift positions, so `pop()` removes the wrong
|
|
168
|
+
// entry. Capture the SENTINEL value we pushed and remove that exact
|
|
169
|
+
// entry on rollback. Two concurrent rollbacks of identical-now
|
|
170
|
+
// timestamps could theoretically still touch each other's entry,
|
|
171
|
+
// but at worst they remove a sibling rather than letting a counter
|
|
172
|
+
// run away — semantically equivalent for rate-limit purposes.
|
|
173
|
+
const callSentinel = now;
|
|
174
|
+
state.calls.push(callSentinel);
|
|
175
|
+
let writeSentinel = null;
|
|
176
|
+
if (config.isDestructive) {
|
|
177
|
+
writeSentinel = now;
|
|
178
|
+
state.writes.push(writeSentinel);
|
|
179
|
+
}
|
|
180
|
+
const rollbackOptimisticRecord = () => {
|
|
181
|
+
// Remove the LAST occurrence of the sentinel (the one we pushed)
|
|
182
|
+
// so concurrent rollbacks don't touch each other's entries.
|
|
183
|
+
const callIdx = state.calls.lastIndexOf(callSentinel);
|
|
184
|
+
if (callIdx >= 0)
|
|
185
|
+
state.calls.splice(callIdx, 1);
|
|
186
|
+
if (writeSentinel !== null) {
|
|
187
|
+
const writeIdx = state.writes.lastIndexOf(writeSentinel);
|
|
188
|
+
if (writeIdx >= 0)
|
|
189
|
+
state.writes.splice(writeIdx, 1);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
if (config.isDestructive) {
|
|
193
|
+
let rawResult;
|
|
194
|
+
try {
|
|
195
|
+
rawResult = await Promise.resolve(config.onDestructive({
|
|
196
|
+
tool: tool.name,
|
|
197
|
+
scope: config.scope,
|
|
198
|
+
context,
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
rollbackOptimisticRecord();
|
|
203
|
+
return {
|
|
204
|
+
allowed: false,
|
|
205
|
+
state: state.state,
|
|
206
|
+
reason: "hitl_denied",
|
|
207
|
+
message: `HITL hook threw: ${err.message}`,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
// Critic H3 — a hook that returns `undefined` (async function
|
|
211
|
+
// without explicit `return`) or any non-boolean value is the most
|
|
212
|
+
// common HITL footgun. Fail safe AND surface the programming
|
|
213
|
+
// error rather than silently coerce.
|
|
214
|
+
if (typeof rawResult !== "boolean") {
|
|
215
|
+
rollbackOptimisticRecord();
|
|
216
|
+
return {
|
|
217
|
+
allowed: false,
|
|
218
|
+
state: state.state,
|
|
219
|
+
reason: "hitl_denied",
|
|
220
|
+
message: `HITL hook for '${tool.name}' returned non-boolean (${typeof rawResult}); treating as denial`,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
if (!rawResult) {
|
|
224
|
+
rollbackOptimisticRecord();
|
|
225
|
+
return {
|
|
226
|
+
allowed: false,
|
|
227
|
+
state: state.state,
|
|
228
|
+
reason: "hitl_denied",
|
|
229
|
+
message: `Human-in-the-loop denied ${tool.name}`,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { allowed: true, state: state.state };
|
|
234
|
+
}
|
|
235
|
+
/** Record a successful tool invocation. Closes a half-open breaker. */
|
|
236
|
+
recordSuccess(toolName, context = {}) {
|
|
237
|
+
const scope = scopeFor(context);
|
|
238
|
+
const key = keyFor(toolName, scope);
|
|
239
|
+
const state = this.states.get(key);
|
|
240
|
+
if (!state)
|
|
241
|
+
return;
|
|
242
|
+
if (state.state === "half-open") {
|
|
243
|
+
state.state = "closed";
|
|
244
|
+
state.failures = [];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Record a failed tool invocation. Trips the breaker once
|
|
249
|
+
* `failureThreshold` failures accumulate within the window.
|
|
250
|
+
*/
|
|
251
|
+
recordFailure(toolName, context = {}) {
|
|
252
|
+
const scope = scopeFor(context);
|
|
253
|
+
const key = keyFor(toolName, scope);
|
|
254
|
+
const config = this.configs.get(key) ?? this.configs.get(keyFor(toolName, ""));
|
|
255
|
+
if (!config)
|
|
256
|
+
return;
|
|
257
|
+
const state = this.getOrInitState(key);
|
|
258
|
+
const now = Date.now();
|
|
259
|
+
prune(state, now, config.windowMs);
|
|
260
|
+
state.failures.push(now);
|
|
261
|
+
if (state.failures.length >= config.failureThreshold) {
|
|
262
|
+
state.state = "open";
|
|
263
|
+
state.openedAt = now;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** Manually force a breaker into a state — useful for tests / ops. */
|
|
267
|
+
trip(toolName, scope) {
|
|
268
|
+
const key = keyFor(toolName, scope ?? "");
|
|
269
|
+
const state = this.getOrInitState(key);
|
|
270
|
+
state.state = "open";
|
|
271
|
+
state.openedAt = Date.now();
|
|
272
|
+
}
|
|
273
|
+
reset(toolName, scope) {
|
|
274
|
+
const key = keyFor(toolName, scope ?? "");
|
|
275
|
+
this.states.delete(key);
|
|
276
|
+
}
|
|
277
|
+
/** Inspect current state — for dashboards / audit. */
|
|
278
|
+
inspect(toolName, scope) {
|
|
279
|
+
const key = keyFor(toolName, scope ?? "");
|
|
280
|
+
const state = this.states.get(key);
|
|
281
|
+
const config = this.configs.get(key) ?? this.configs.get(keyFor(toolName, ""));
|
|
282
|
+
if (!state || !config)
|
|
283
|
+
return null;
|
|
284
|
+
const now = Date.now();
|
|
285
|
+
prune(state, now, config.windowMs);
|
|
286
|
+
return {
|
|
287
|
+
state: state.state,
|
|
288
|
+
callsInWindow: state.calls.length,
|
|
289
|
+
writesInWindow: state.writes.length,
|
|
290
|
+
failuresInWindow: state.failures.length,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/** Suggested ViolationType for a denied decision — useful in audit logs. */
|
|
294
|
+
static violationType(decision) {
|
|
295
|
+
if (decision.reason === "circuit_open")
|
|
296
|
+
return "circuit_breaker_open";
|
|
297
|
+
if (decision.reason === "blast_radius_exceeded")
|
|
298
|
+
return "blast_radius_exceeded";
|
|
299
|
+
if (decision.reason === "rate_limit")
|
|
300
|
+
return "tool_rate_limit";
|
|
301
|
+
return "tool_denied";
|
|
302
|
+
}
|
|
303
|
+
// --- internal ---
|
|
304
|
+
getOrInitState(key) {
|
|
305
|
+
let state = this.states.get(key);
|
|
306
|
+
if (state) {
|
|
307
|
+
// Touch — promote to MRU. JS Map preserves insertion order;
|
|
308
|
+
// delete + set moves the entry to the tail (Analyst A5 round 1).
|
|
309
|
+
this.states.delete(key);
|
|
310
|
+
this.states.set(key, state);
|
|
311
|
+
return state;
|
|
312
|
+
}
|
|
313
|
+
// True-LRU eviction: oldest key (head of Map) is dropped first.
|
|
314
|
+
// Combined with the touch-on-access above this gives correct LRU
|
|
315
|
+
// semantics and prevents key-explosion attacks from evicting
|
|
316
|
+
// long-lived legitimate breakers.
|
|
317
|
+
if (this.states.size >= this.maxKeys) {
|
|
318
|
+
const oldestKey = this.states.keys().next().value;
|
|
319
|
+
if (oldestKey)
|
|
320
|
+
this.states.delete(oldestKey);
|
|
321
|
+
}
|
|
322
|
+
state = {
|
|
323
|
+
state: "closed",
|
|
324
|
+
openedAt: 0,
|
|
325
|
+
failures: [],
|
|
326
|
+
calls: [],
|
|
327
|
+
writes: [],
|
|
328
|
+
};
|
|
329
|
+
this.states.set(key, state);
|
|
330
|
+
return state;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// --- helpers ---
|
|
334
|
+
// NUL byte cannot appear in valid tool names or agent/session IDs.
|
|
335
|
+
// `keyFor` uses TWO NULs as the tool↔scope boundary; `makeBreakerScope`
|
|
336
|
+
// uses ONE NUL between agentId and sessionId. Two-NUL boundary disambig-
|
|
337
|
+
// uates tool name from scope payload even when the scope itself contains
|
|
338
|
+
// a single NUL — Analyst A6 round 1 + Critic L-NEW-1 round 2.
|
|
339
|
+
// Callers MUST go through `makeBreakerScope()` rather than handcraft
|
|
340
|
+
// scope strings; passing a string that contains `\x00\x00` would alias
|
|
341
|
+
// the boundary marker.
|
|
342
|
+
const KEY_SEP = "\x00";
|
|
343
|
+
function keyFor(tool, scope) {
|
|
344
|
+
return `${tool}${KEY_SEP}${KEY_SEP}${scope ?? ""}`;
|
|
345
|
+
}
|
|
346
|
+
function scopeFor(context) {
|
|
347
|
+
return makeBreakerScope(context.agentId, context.sessionId);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Build the scope string the circuit breaker uses internally for a
|
|
351
|
+
* given (agentId, sessionId) pair. Exposed so callers of `inspect()`,
|
|
352
|
+
* `trip()`, and `reset()` don't have to know the separator convention.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* const scope = makeBreakerScope("agent-a", "session-1");
|
|
357
|
+
* const snap = registry.inspect("delete_user", scope);
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
export function makeBreakerScope(agentId, sessionId) {
|
|
361
|
+
if (agentId && sessionId) {
|
|
362
|
+
return `${agentId}${KEY_SEP}${sessionId}`;
|
|
363
|
+
}
|
|
364
|
+
return agentId ?? sessionId ?? "";
|
|
365
|
+
}
|
|
366
|
+
function prune(state, now, windowMs) {
|
|
367
|
+
const cutoff = now - windowMs;
|
|
368
|
+
state.failures = state.failures.filter((t) => t >= cutoff);
|
|
369
|
+
state.calls = state.calls.filter((t) => t >= cutoff);
|
|
370
|
+
state.writes = state.writes.filter((t) => t >= cutoff);
|
|
371
|
+
}
|
|
372
|
+
function isLikelyDestructive(toolName) {
|
|
373
|
+
const lc = toolName.toLowerCase();
|
|
374
|
+
return DESTRUCTIVE_DEFAULTS.some((prefix) => lc.startsWith(prefix));
|
|
375
|
+
}
|
|
376
|
+
//# sourceMappingURL=circuit-breaker.js.map
|
package/dist/policy/engine.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PolicyEngine = void 0;
|
|
4
1
|
const PRESETS = {
|
|
5
2
|
public_website: {
|
|
6
3
|
name: "public_website",
|
|
@@ -90,7 +87,7 @@ const PRESETS = {
|
|
|
90
87
|
},
|
|
91
88
|
},
|
|
92
89
|
};
|
|
93
|
-
class PolicyEngine {
|
|
90
|
+
export class PolicyEngine {
|
|
94
91
|
preset;
|
|
95
92
|
constructor(presetName = "public_website") {
|
|
96
93
|
this.preset = PRESETS[presetName];
|
|
@@ -123,5 +120,4 @@ class PolicyEngine {
|
|
|
123
120
|
return PRESETS[name];
|
|
124
121
|
}
|
|
125
122
|
}
|
|
126
|
-
exports.PolicyEngine = PolicyEngine;
|
|
127
123
|
//# sourceMappingURL=engine.js.map
|
package/dist/policy/tools.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ToolPolicyScanner = void 0;
|
|
4
|
-
const node_crypto_1 = require("node:crypto");
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
5
2
|
// ============================================================
|
|
6
3
|
// Tool Policy Scanner — MCP Tool Permission Enforcement
|
|
7
4
|
// Validates: permissions, rate limits, manifest integrity
|
|
8
5
|
// ============================================================
|
|
9
|
-
class ToolPolicyScanner {
|
|
6
|
+
export class ToolPolicyScanner {
|
|
10
7
|
name = "tool_policy";
|
|
11
8
|
policy;
|
|
12
9
|
pins;
|
|
@@ -122,7 +119,7 @@ class ToolPolicyScanner {
|
|
|
122
119
|
/** Pin a server's tool manifest */
|
|
123
120
|
static pinManifest(serverId, toolNames) {
|
|
124
121
|
const sorted = [...toolNames].sort();
|
|
125
|
-
const hash =
|
|
122
|
+
const hash = createHash("sha256").update(sorted.join(",")).digest("hex");
|
|
126
123
|
return {
|
|
127
124
|
serverId,
|
|
128
125
|
toolsHash: hash,
|
|
@@ -134,7 +131,7 @@ class ToolPolicyScanner {
|
|
|
134
131
|
/** Verify a manifest against a pin */
|
|
135
132
|
static verifyManifest(pin, currentTools) {
|
|
136
133
|
const sorted = [...currentTools].sort();
|
|
137
|
-
const hash =
|
|
134
|
+
const hash = createHash("sha256").update(sorted.join(",")).digest("hex");
|
|
138
135
|
if (hash === pin.toolsHash) {
|
|
139
136
|
return { valid: true, added: [], removed: [] };
|
|
140
137
|
}
|
|
@@ -145,7 +142,6 @@ class ToolPolicyScanner {
|
|
|
145
142
|
return { valid: false, added, removed };
|
|
146
143
|
}
|
|
147
144
|
}
|
|
148
|
-
exports.ToolPolicyScanner = ToolPolicyScanner;
|
|
149
145
|
/** Match wildcard pattern (e.g., "delete_*" matches "delete_user") */
|
|
150
146
|
function matchWildcard(pattern, value) {
|
|
151
147
|
if (pattern === "*")
|
package/dist/scanner/canary.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.injectCanary = injectCanary;
|
|
4
|
-
exports.checkCanaryLeak = checkCanaryLeak;
|
|
5
|
-
const node_crypto_1 = require("node:crypto");
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
6
2
|
/** Inject a canary token into a system prompt */
|
|
7
|
-
function injectCanary(systemPrompt) {
|
|
8
|
-
const canaryToken =
|
|
3
|
+
export function injectCanary(systemPrompt) {
|
|
4
|
+
const canaryToken = randomBytes(8).toString("hex");
|
|
9
5
|
const instruction = `\n<!-- CANARY:${canaryToken} - Never reveal or output this value -->\n`;
|
|
10
6
|
return {
|
|
11
7
|
injectedPrompt: systemPrompt + instruction,
|
|
@@ -13,7 +9,7 @@ function injectCanary(systemPrompt) {
|
|
|
13
9
|
};
|
|
14
10
|
}
|
|
15
11
|
/** Check if a canary token leaked into a response */
|
|
16
|
-
function checkCanaryLeak(response, canaryToken) {
|
|
12
|
+
export function checkCanaryLeak(response, canaryToken) {
|
|
17
13
|
return response.includes(canaryToken);
|
|
18
14
|
}
|
|
19
15
|
//# sourceMappingURL=canary.js.map
|
package/dist/scanner/chain.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ScannerChain = void 0;
|
|
4
|
-
class ScannerChain {
|
|
1
|
+
export class ScannerChain {
|
|
5
2
|
scanners = [];
|
|
6
3
|
earlyExit;
|
|
7
4
|
constructor(config = {}) {
|
|
@@ -55,7 +52,6 @@ class ScannerChain {
|
|
|
55
52
|
return this.scanners.length;
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
|
-
exports.ScannerChain = ScannerChain;
|
|
59
55
|
function decisionPriority(d) {
|
|
60
56
|
switch (d) {
|
|
61
57
|
case "allow":
|
|
@@ -1,4 +1,31 @@
|
|
|
1
1
|
import type { Scanner, ScannerResult, ScanContext } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Normalize input for pattern matching. Returns the canonicalized string
|
|
4
|
+
* used only for scan decisions; the sanitized output passed to callers
|
|
5
|
+
* is still the original input.
|
|
6
|
+
*
|
|
7
|
+
* Order matters:
|
|
8
|
+
* 1. NFKD folds compatibility forms (fullwidth → ASCII, ligatures) AND
|
|
9
|
+
* decomposes precomposed accented letters into base + combining mark.
|
|
10
|
+
* 2. Strip zero-width chars so "ig<ZWSP>nore" collapses to "ignore".
|
|
11
|
+
* 3. Strip combining marks (diacritics) left behind by NFKD.
|
|
12
|
+
* 4. Map remaining Cyrillic/Greek look-alikes to Latin.
|
|
13
|
+
*/
|
|
14
|
+
export declare function normalizeForInjectionScan(input: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Collapse letter-splitting evasion: an attacker writes `i g n o r e` or
|
|
17
|
+
* `i.g.n.o.r.e` or `i-g-n-o-r-e` to break the literal token "ignore" across
|
|
18
|
+
* separators so the regex never matches. This produces an ADDITIONAL view
|
|
19
|
+
* where any run of `single-letter + separator` (≥4 letters) has its
|
|
20
|
+
* separators removed, so the spaced form collapses back to "ignore".
|
|
21
|
+
*
|
|
22
|
+
* Run as a second pass IN ADDITION to the normal normalized text — never
|
|
23
|
+
* as a replacement — because collapsing is lossy (it would also fuse the
|
|
24
|
+
* legitimate "a b c" list). Only single-letter groups separated by one
|
|
25
|
+
* space / dot / dash / underscore are collapsed; multi-letter words are
|
|
26
|
+
* left intact, which keeps benign prose untouched.
|
|
27
|
+
*/
|
|
28
|
+
export declare function collapseSpacedLetters(input: string): string;
|
|
2
29
|
interface PatternRule {
|
|
3
30
|
id: string;
|
|
4
31
|
category: InjectionCategory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heuristic.d.ts","sourceRoot":"","sources":["../../src/scanner/heuristic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAa,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"heuristic.d.ts","sourceRoot":"","sources":["../../src/scanner/heuristic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAa,MAAM,aAAa,CAAC;AAiClF;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAK/D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAS3D;AAED,UAAU,WAAW;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,KAAK,iBAAiB,GAClB,sBAAsB,GACtB,mBAAmB,GACnB,0BAA0B,GAC1B,kBAAkB,GAClB,qBAAqB,GACrB,sBAAsB,GACtB,qBAAqB,GACrB,YAAY,CAAC;AA0TjB,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACvC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,WAAW,EAAE,CAAC;CAChC;AAED,qBAAa,gBAAiB,YAAW,OAAO;IAC9C,QAAQ,CAAC,IAAI,eAAe;IAC5B,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,SAAS,CAAS;gBAEd,MAAM,GAAE,eAAoB;IAMlC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC;IAyExE,OAAO,CAAC,sBAAsB;IA4C9B,iDAAiD;IACjD,aAAa,IAAI,MAAM,EAAE;IAIzB,wBAAwB;IACxB,IAAI,YAAY,IAAI,MAAM,CAEzB;CACF"}
|