protect-mcp 0.3.3 → 0.4.1
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/LICENSE +21 -68
- package/README.md +1 -1
- package/dist/{chunk-WDCPUM2O.mjs → chunk-7HBHIKLN.mjs} +104 -135
- package/dist/chunk-VWUN6AI6.mjs +143 -0
- package/dist/cli.js +4019 -3685
- package/dist/cli.mjs +16 -4
- package/dist/http-transport-RIVV2RVQ.mjs +157 -0
- package/dist/index.d.mts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +103 -0
- package/dist/index.mjs +10 -8
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,68 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
### License Grant
|
|
24
|
-
|
|
25
|
-
Subject to your compliance with this License Grant and the Patents, Redistribution and Trademark clauses below, we hereby grant you the right to use, copy, modify, create derivative works, publicly perform, publicly display and redistribute the Software for any Permitted Purpose identified below.
|
|
26
|
-
|
|
27
|
-
### Permitted Purpose
|
|
28
|
-
|
|
29
|
-
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use means making the Software available to others in a commercial product or service that:
|
|
30
|
-
|
|
31
|
-
1. substitutes for the Software;
|
|
32
|
-
2. substitutes for any other product or service we offer using the Software that exists as of the date we make the Software available; or
|
|
33
|
-
3. offers the same or substantially similar functionality as the Software.
|
|
34
|
-
|
|
35
|
-
Permitted Purposes specifically include using the Software:
|
|
36
|
-
|
|
37
|
-
1. for your internal use and access;
|
|
38
|
-
2. for non-commercial education;
|
|
39
|
-
3. for non-commercial research; and
|
|
40
|
-
4. in connection with professional services that you provide to a licensee using the Software in accordance with these Terms and Conditions.
|
|
41
|
-
|
|
42
|
-
### Patents
|
|
43
|
-
|
|
44
|
-
To the extent your use for a Permitted Purpose would necessarily infringe our patents, the license grant above includes a license under our patents. If you make a claim against any party that the Software infringes or contributes to the infringement of any patent, then your patent license to the Software ends immediately.
|
|
45
|
-
|
|
46
|
-
### Redistribution
|
|
47
|
-
|
|
48
|
-
The Terms and Conditions apply to all copies, modifications and derivatives of the Software. If you redistribute any copies, modifications or derivatives of the Software, you must include a copy of or a link to these Terms and Conditions and not remove any copyright notices provided in or with the Software.
|
|
49
|
-
|
|
50
|
-
### Disclaimer
|
|
51
|
-
|
|
52
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT. IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
53
|
-
|
|
54
|
-
### Trademarks
|
|
55
|
-
|
|
56
|
-
Except for displaying the License Details and identifying us as the origin of the Software, you have no right under these Terms and Conditions to use our trademarks, trade names, service marks or product names.
|
|
57
|
-
|
|
58
|
-
## Grant of Future License
|
|
59
|
-
|
|
60
|
-
We hereby irrevocably grant you an additional license to use the Software under the MIT license that is effective on the second anniversary of the date we make the Software available.
|
|
61
|
-
|
|
62
|
-
On or after that date, you may use the Software under the MIT license, in which case the following will apply:
|
|
63
|
-
|
|
64
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
65
|
-
|
|
66
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
67
|
-
|
|
68
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tom Farley / ScopeBlind
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -252,6 +252,6 @@ Supports OPA, Cerbos, Cedar (AWS AgentCore), and generic HTTP endpoints:
|
|
|
252
252
|
|
|
253
253
|
## License
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
MIT — free to use, modify, distribute, and build upon without restriction.
|
|
256
256
|
|
|
257
257
|
[scopeblind.com](https://scopeblind.com) · [npm](https://www.npmjs.com/package/protect-mcp) · [GitHub](https://github.com/tomjwxf/scopeblind-gateway) · [IETF Draft](https://datatracker.ietf.org/doc/draft-farley-acta-signed-receipts/)
|
|
@@ -814,6 +814,9 @@ var ProtectGateway = class {
|
|
|
814
814
|
approvalNonce = randomBytes(16).toString("hex");
|
|
815
815
|
currentTier = "unknown";
|
|
816
816
|
admissionResult = null;
|
|
817
|
+
/** HTTP transport mode: pending response resolvers keyed by JSON-RPC id */
|
|
818
|
+
pendingResponses = /* @__PURE__ */ new Map();
|
|
819
|
+
httpMode = false;
|
|
817
820
|
constructor(config) {
|
|
818
821
|
this.config = config;
|
|
819
822
|
this.logFilePath = join3(process.cwd(), LOG_FILE2);
|
|
@@ -1133,8 +1136,108 @@ var ProtectGateway = class {
|
|
|
1133
1136
|
if (this.child?.stdin?.writable) this.child.stdin.write(message + "\n");
|
|
1134
1137
|
}
|
|
1135
1138
|
sendToClient(message) {
|
|
1139
|
+
if (this.httpMode) {
|
|
1140
|
+
try {
|
|
1141
|
+
const parsed = JSON.parse(message);
|
|
1142
|
+
if (parsed.id !== void 0 && parsed.id !== null) {
|
|
1143
|
+
const pending = this.pendingResponses.get(parsed.id);
|
|
1144
|
+
if (pending) {
|
|
1145
|
+
clearTimeout(pending.timeout);
|
|
1146
|
+
this.pendingResponses.delete(parsed.id);
|
|
1147
|
+
pending.resolve(message);
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
} catch {
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1136
1154
|
process.stdout.write(message + "\n");
|
|
1137
1155
|
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Enable HTTP transport mode.
|
|
1158
|
+
* In this mode, sendToClient resolves pending promises instead of
|
|
1159
|
+
* writing to stdout, and start() skips stdin reading.
|
|
1160
|
+
*/
|
|
1161
|
+
enableHttpMode() {
|
|
1162
|
+
this.httpMode = true;
|
|
1163
|
+
}
|
|
1164
|
+
/**
|
|
1165
|
+
* Start in HTTP mode — spawns child process but does NOT read from
|
|
1166
|
+
* process.stdin. Requests come in via processRequest() instead.
|
|
1167
|
+
*/
|
|
1168
|
+
async startForHttp() {
|
|
1169
|
+
this.httpMode = true;
|
|
1170
|
+
const { command, args, verbose } = this.config;
|
|
1171
|
+
const mode = this.config.enforce ? "enforce" : "shadow";
|
|
1172
|
+
if (verbose) {
|
|
1173
|
+
this.log(`Starting gateway in ${mode} mode (HTTP transport)`);
|
|
1174
|
+
this.log(`Wrapping: ${command} ${args.join(" ")}`);
|
|
1175
|
+
}
|
|
1176
|
+
this.log(`Approval nonce: ${this.approvalNonce}`);
|
|
1177
|
+
const childEnv = { ...process.env };
|
|
1178
|
+
if (this.config.credentials) {
|
|
1179
|
+
for (const [label, credConfig] of Object.entries(this.config.credentials)) {
|
|
1180
|
+
if (credConfig.inject === "env" && credConfig.name && credConfig.value_env) {
|
|
1181
|
+
const envValue = process.env[credConfig.value_env];
|
|
1182
|
+
if (envValue) {
|
|
1183
|
+
childEnv[credConfig.name] = envValue;
|
|
1184
|
+
if (verbose) this.log(`Credential "${label}": injected as env var "${credConfig.name}"`);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
this.child = spawn(command, args, { stdio: ["pipe", "pipe", "pipe"], env: childEnv });
|
|
1190
|
+
if (!this.child.stdin || !this.child.stdout || !this.child.stderr) {
|
|
1191
|
+
throw new Error("Failed to create pipes to child process");
|
|
1192
|
+
}
|
|
1193
|
+
this.child.stderr.on("data", (data) => {
|
|
1194
|
+
process.stderr.write(data);
|
|
1195
|
+
});
|
|
1196
|
+
const childReader = createInterface({ input: this.child.stdout, crlfDelay: Infinity });
|
|
1197
|
+
childReader.on("line", (line) => {
|
|
1198
|
+
this.handleServerMessage(line);
|
|
1199
|
+
});
|
|
1200
|
+
this.child.on("exit", (code, signal) => {
|
|
1201
|
+
if (verbose) this.log(`Child process exited (code=${code}, signal=${signal})`);
|
|
1202
|
+
this.evidenceStore.save();
|
|
1203
|
+
});
|
|
1204
|
+
this.child.on("error", (err) => {
|
|
1205
|
+
this.log(`Child process error: ${err.message}`);
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
/**
|
|
1209
|
+
* Process a JSON-RPC request programmatically (for HTTP transport).
|
|
1210
|
+
* Returns a promise that resolves with the JSON-RPC response string.
|
|
1211
|
+
*/
|
|
1212
|
+
async processRequest(jsonRpc) {
|
|
1213
|
+
const REQUEST_TIMEOUT_MS = 3e4;
|
|
1214
|
+
if (jsonRpc.method === "tools/call" && jsonRpc.id !== void 0) {
|
|
1215
|
+
const blocked = await this.interceptToolCall(jsonRpc);
|
|
1216
|
+
if (blocked) {
|
|
1217
|
+
return JSON.stringify(blocked);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
return new Promise((resolve, reject) => {
|
|
1221
|
+
const id = jsonRpc.id;
|
|
1222
|
+
if (id === void 0 || id === null) {
|
|
1223
|
+
const modified2 = this.injectParamsCredentials(jsonRpc);
|
|
1224
|
+
this.sendToChild(JSON.stringify(modified2));
|
|
1225
|
+
resolve(JSON.stringify({ jsonrpc: "2.0", result: {}, id: null }));
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
1228
|
+
const timeout = setTimeout(() => {
|
|
1229
|
+
this.pendingResponses.delete(id);
|
|
1230
|
+
resolve(JSON.stringify({
|
|
1231
|
+
jsonrpc: "2.0",
|
|
1232
|
+
error: { code: -32e3, message: "Request timeout (30s)" },
|
|
1233
|
+
id
|
|
1234
|
+
}));
|
|
1235
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1236
|
+
this.pendingResponses.set(id, { resolve, timeout });
|
|
1237
|
+
const modified = this.injectParamsCredentials(jsonRpc);
|
|
1238
|
+
this.sendToChild(JSON.stringify(modified));
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1138
1241
|
log(message) {
|
|
1139
1242
|
process.stderr.write(`[PROTECT_MCP] ${message}
|
|
1140
1243
|
`);
|
|
@@ -1150,137 +1253,6 @@ var ProtectGateway = class {
|
|
|
1150
1253
|
}
|
|
1151
1254
|
};
|
|
1152
1255
|
|
|
1153
|
-
// src/simulate.ts
|
|
1154
|
-
import { readFileSync as readFileSync5 } from "fs";
|
|
1155
|
-
function parseLogFile(path) {
|
|
1156
|
-
const raw = readFileSync5(path, "utf-8");
|
|
1157
|
-
const entries = [];
|
|
1158
|
-
for (const line of raw.split("\n")) {
|
|
1159
|
-
const trimmed = line.trim();
|
|
1160
|
-
if (!trimmed) continue;
|
|
1161
|
-
const jsonStr = trimmed.replace(/^\[PROTECT_MCP\]\s*/, "");
|
|
1162
|
-
try {
|
|
1163
|
-
const parsed = JSON.parse(jsonStr);
|
|
1164
|
-
if (parsed.tool && parsed.decision) {
|
|
1165
|
-
entries.push(parsed);
|
|
1166
|
-
}
|
|
1167
|
-
} catch {
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
return entries;
|
|
1171
|
-
}
|
|
1172
|
-
function simulate(entries, policy, tier = "unknown") {
|
|
1173
|
-
const rateLimitStore = /* @__PURE__ */ new Map();
|
|
1174
|
-
const toolResults = /* @__PURE__ */ new Map();
|
|
1175
|
-
const totals = {
|
|
1176
|
-
allow: 0,
|
|
1177
|
-
block: 0,
|
|
1178
|
-
rate_limited: 0,
|
|
1179
|
-
require_approval: 0,
|
|
1180
|
-
tier_insufficient: 0
|
|
1181
|
-
};
|
|
1182
|
-
const originalTotals = { allow: 0, deny: 0 };
|
|
1183
|
-
const changes = [];
|
|
1184
|
-
for (const entry of entries) {
|
|
1185
|
-
const toolName = entry.tool;
|
|
1186
|
-
const toolPolicy = getToolPolicy(toolName, policy);
|
|
1187
|
-
if (entry.decision === "allow") {
|
|
1188
|
-
originalTotals.allow++;
|
|
1189
|
-
} else {
|
|
1190
|
-
originalTotals.deny++;
|
|
1191
|
-
}
|
|
1192
|
-
let newDecision;
|
|
1193
|
-
if (toolPolicy.block) {
|
|
1194
|
-
newDecision = "block";
|
|
1195
|
-
} else if (toolPolicy.min_tier && !meetsMinTier(tier, toolPolicy.min_tier)) {
|
|
1196
|
-
newDecision = "tier_insufficient";
|
|
1197
|
-
} else if (toolPolicy.require_approval) {
|
|
1198
|
-
newDecision = "require_approval";
|
|
1199
|
-
} else if (toolPolicy.rate_limit) {
|
|
1200
|
-
const limit = parseRateLimit(toolPolicy.rate_limit);
|
|
1201
|
-
const result = checkRateLimit(toolName, limit, rateLimitStore);
|
|
1202
|
-
newDecision = result.allowed ? "allow" : "rate_limited";
|
|
1203
|
-
} else {
|
|
1204
|
-
newDecision = "allow";
|
|
1205
|
-
}
|
|
1206
|
-
totals[newDecision]++;
|
|
1207
|
-
if (!toolResults.has(toolName)) {
|
|
1208
|
-
toolResults.set(toolName, {
|
|
1209
|
-
tool: toolName,
|
|
1210
|
-
calls: 0,
|
|
1211
|
-
results: { allow: 0, block: 0, rate_limited: 0, require_approval: 0, tier_insufficient: 0 },
|
|
1212
|
-
original: { allow: 0, deny: 0 }
|
|
1213
|
-
});
|
|
1214
|
-
}
|
|
1215
|
-
const tr = toolResults.get(toolName);
|
|
1216
|
-
tr.calls++;
|
|
1217
|
-
tr.results[newDecision]++;
|
|
1218
|
-
if (entry.decision === "allow") {
|
|
1219
|
-
tr.original.allow++;
|
|
1220
|
-
} else {
|
|
1221
|
-
tr.original.deny++;
|
|
1222
|
-
}
|
|
1223
|
-
}
|
|
1224
|
-
for (const [tool, result] of toolResults) {
|
|
1225
|
-
const wasAllBlocked = result.original.allow === 0;
|
|
1226
|
-
const nowAllBlocked = result.results.allow === 0;
|
|
1227
|
-
const wasAllAllowed = result.original.deny === 0;
|
|
1228
|
-
if (wasAllAllowed && result.results.block > 0) {
|
|
1229
|
-
changes.push(`${tool}: ${result.results.block} calls would be blocked (was: all allowed)`);
|
|
1230
|
-
}
|
|
1231
|
-
if (wasAllAllowed && result.results.rate_limited > 0) {
|
|
1232
|
-
changes.push(`${tool}: ${result.results.rate_limited} calls would be rate-limited (was: all allowed)`);
|
|
1233
|
-
}
|
|
1234
|
-
if (wasAllAllowed && result.results.require_approval > 0) {
|
|
1235
|
-
changes.push(`${tool}: ${result.results.require_approval} calls would require approval (was: all allowed)`);
|
|
1236
|
-
}
|
|
1237
|
-
if (wasAllAllowed && result.results.tier_insufficient > 0) {
|
|
1238
|
-
changes.push(`${tool}: ${result.results.tier_insufficient} calls would fail tier check (was: all allowed)`);
|
|
1239
|
-
}
|
|
1240
|
-
if (wasAllBlocked && result.results.allow > 0 && !nowAllBlocked) {
|
|
1241
|
-
changes.push(`${tool}: ${result.results.allow} calls would now be allowed (was: all blocked)`);
|
|
1242
|
-
}
|
|
1243
|
-
}
|
|
1244
|
-
return {
|
|
1245
|
-
policy_file: "",
|
|
1246
|
-
log_file: "",
|
|
1247
|
-
total_calls: entries.length,
|
|
1248
|
-
results: totals,
|
|
1249
|
-
original: originalTotals,
|
|
1250
|
-
tool_breakdown: Array.from(toolResults.values()).sort((a, b) => b.calls - a.calls),
|
|
1251
|
-
changes
|
|
1252
|
-
};
|
|
1253
|
-
}
|
|
1254
|
-
function formatSimulation(summary) {
|
|
1255
|
-
const lines = [];
|
|
1256
|
-
lines.push(`Simulating ${summary.policy_file} against ${summary.total_calls} recorded tool calls:
|
|
1257
|
-
`);
|
|
1258
|
-
const maxToolLen = Math.max(...summary.tool_breakdown.map((t) => t.tool.length), 4);
|
|
1259
|
-
for (const tr of summary.tool_breakdown) {
|
|
1260
|
-
const parts = [];
|
|
1261
|
-
if (tr.results.allow > 0) parts.push(`${tr.results.allow} allow`);
|
|
1262
|
-
if (tr.results.block > 0) parts.push(`\x1B[31m${tr.results.block} blocked\x1B[0m`);
|
|
1263
|
-
if (tr.results.rate_limited > 0) parts.push(`\x1B[33m${tr.results.rate_limited} rate_limited\x1B[0m`);
|
|
1264
|
-
if (tr.results.require_approval > 0) parts.push(`\x1B[36m${tr.results.require_approval} require_approval\x1B[0m`);
|
|
1265
|
-
if (tr.results.tier_insufficient > 0) parts.push(`\x1B[35m${tr.results.tier_insufficient} tier_insufficient\x1B[0m`);
|
|
1266
|
-
const originalParts = [];
|
|
1267
|
-
if (tr.original.allow > 0) originalParts.push(`${tr.original.allow} allow`);
|
|
1268
|
-
if (tr.original.deny > 0) originalParts.push(`${tr.original.deny} deny`);
|
|
1269
|
-
lines.push(` ${tr.tool.padEnd(maxToolLen)} \xD7 ${String(tr.calls).padStart(3)} \u2192 ${parts.join(", ")} (was: ${originalParts.join(", ")})`);
|
|
1270
|
-
}
|
|
1271
|
-
lines.push("");
|
|
1272
|
-
lines.push(`Summary: ${summary.results.allow} allow, ${summary.results.block} blocked, ${summary.results.rate_limited} rate_limited, ${summary.results.require_approval} require_approval, ${summary.results.tier_insufficient} tier_insufficient`);
|
|
1273
|
-
lines.push(` vs original: ${summary.original.allow} allow, ${summary.original.deny} deny`);
|
|
1274
|
-
if (summary.changes.length > 0) {
|
|
1275
|
-
lines.push("");
|
|
1276
|
-
lines.push("Changes:");
|
|
1277
|
-
for (const change of summary.changes) {
|
|
1278
|
-
lines.push(` \u2022 ${change}`);
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
return lines.join("\n");
|
|
1282
|
-
}
|
|
1283
|
-
|
|
1284
1256
|
export {
|
|
1285
1257
|
loadPolicy,
|
|
1286
1258
|
getToolPolicy,
|
|
@@ -1297,8 +1269,5 @@ export {
|
|
|
1297
1269
|
isSigningEnabled,
|
|
1298
1270
|
queryExternalPDP,
|
|
1299
1271
|
buildDecisionContext,
|
|
1300
|
-
ProtectGateway
|
|
1301
|
-
parseLogFile,
|
|
1302
|
-
simulate,
|
|
1303
|
-
formatSimulation
|
|
1272
|
+
ProtectGateway
|
|
1304
1273
|
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkRateLimit,
|
|
3
|
+
getToolPolicy,
|
|
4
|
+
meetsMinTier,
|
|
5
|
+
parseRateLimit
|
|
6
|
+
} from "./chunk-7HBHIKLN.mjs";
|
|
7
|
+
|
|
8
|
+
// src/simulate.ts
|
|
9
|
+
import { readFileSync } from "fs";
|
|
10
|
+
function parseLogFile(path) {
|
|
11
|
+
const raw = readFileSync(path, "utf-8");
|
|
12
|
+
const entries = [];
|
|
13
|
+
for (const line of raw.split("\n")) {
|
|
14
|
+
const trimmed = line.trim();
|
|
15
|
+
if (!trimmed) continue;
|
|
16
|
+
const jsonStr = trimmed.replace(/^\[PROTECT_MCP\]\s*/, "");
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(jsonStr);
|
|
19
|
+
if (parsed.tool && parsed.decision) {
|
|
20
|
+
entries.push(parsed);
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return entries;
|
|
26
|
+
}
|
|
27
|
+
function simulate(entries, policy, tier = "unknown") {
|
|
28
|
+
const rateLimitStore = /* @__PURE__ */ new Map();
|
|
29
|
+
const toolResults = /* @__PURE__ */ new Map();
|
|
30
|
+
const totals = {
|
|
31
|
+
allow: 0,
|
|
32
|
+
block: 0,
|
|
33
|
+
rate_limited: 0,
|
|
34
|
+
require_approval: 0,
|
|
35
|
+
tier_insufficient: 0
|
|
36
|
+
};
|
|
37
|
+
const originalTotals = { allow: 0, deny: 0 };
|
|
38
|
+
const changes = [];
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
const toolName = entry.tool;
|
|
41
|
+
const toolPolicy = getToolPolicy(toolName, policy);
|
|
42
|
+
if (entry.decision === "allow") {
|
|
43
|
+
originalTotals.allow++;
|
|
44
|
+
} else {
|
|
45
|
+
originalTotals.deny++;
|
|
46
|
+
}
|
|
47
|
+
let newDecision;
|
|
48
|
+
if (toolPolicy.block) {
|
|
49
|
+
newDecision = "block";
|
|
50
|
+
} else if (toolPolicy.min_tier && !meetsMinTier(tier, toolPolicy.min_tier)) {
|
|
51
|
+
newDecision = "tier_insufficient";
|
|
52
|
+
} else if (toolPolicy.require_approval) {
|
|
53
|
+
newDecision = "require_approval";
|
|
54
|
+
} else if (toolPolicy.rate_limit) {
|
|
55
|
+
const limit = parseRateLimit(toolPolicy.rate_limit);
|
|
56
|
+
const result = checkRateLimit(toolName, limit, rateLimitStore);
|
|
57
|
+
newDecision = result.allowed ? "allow" : "rate_limited";
|
|
58
|
+
} else {
|
|
59
|
+
newDecision = "allow";
|
|
60
|
+
}
|
|
61
|
+
totals[newDecision]++;
|
|
62
|
+
if (!toolResults.has(toolName)) {
|
|
63
|
+
toolResults.set(toolName, {
|
|
64
|
+
tool: toolName,
|
|
65
|
+
calls: 0,
|
|
66
|
+
results: { allow: 0, block: 0, rate_limited: 0, require_approval: 0, tier_insufficient: 0 },
|
|
67
|
+
original: { allow: 0, deny: 0 }
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const tr = toolResults.get(toolName);
|
|
71
|
+
tr.calls++;
|
|
72
|
+
tr.results[newDecision]++;
|
|
73
|
+
if (entry.decision === "allow") {
|
|
74
|
+
tr.original.allow++;
|
|
75
|
+
} else {
|
|
76
|
+
tr.original.deny++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const [tool, result] of toolResults) {
|
|
80
|
+
const wasAllBlocked = result.original.allow === 0;
|
|
81
|
+
const nowAllBlocked = result.results.allow === 0;
|
|
82
|
+
const wasAllAllowed = result.original.deny === 0;
|
|
83
|
+
if (wasAllAllowed && result.results.block > 0) {
|
|
84
|
+
changes.push(`${tool}: ${result.results.block} calls would be blocked (was: all allowed)`);
|
|
85
|
+
}
|
|
86
|
+
if (wasAllAllowed && result.results.rate_limited > 0) {
|
|
87
|
+
changes.push(`${tool}: ${result.results.rate_limited} calls would be rate-limited (was: all allowed)`);
|
|
88
|
+
}
|
|
89
|
+
if (wasAllAllowed && result.results.require_approval > 0) {
|
|
90
|
+
changes.push(`${tool}: ${result.results.require_approval} calls would require approval (was: all allowed)`);
|
|
91
|
+
}
|
|
92
|
+
if (wasAllAllowed && result.results.tier_insufficient > 0) {
|
|
93
|
+
changes.push(`${tool}: ${result.results.tier_insufficient} calls would fail tier check (was: all allowed)`);
|
|
94
|
+
}
|
|
95
|
+
if (wasAllBlocked && result.results.allow > 0 && !nowAllBlocked) {
|
|
96
|
+
changes.push(`${tool}: ${result.results.allow} calls would now be allowed (was: all blocked)`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
policy_file: "",
|
|
101
|
+
log_file: "",
|
|
102
|
+
total_calls: entries.length,
|
|
103
|
+
results: totals,
|
|
104
|
+
original: originalTotals,
|
|
105
|
+
tool_breakdown: Array.from(toolResults.values()).sort((a, b) => b.calls - a.calls),
|
|
106
|
+
changes
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function formatSimulation(summary) {
|
|
110
|
+
const lines = [];
|
|
111
|
+
lines.push(`Simulating ${summary.policy_file} against ${summary.total_calls} recorded tool calls:
|
|
112
|
+
`);
|
|
113
|
+
const maxToolLen = Math.max(...summary.tool_breakdown.map((t) => t.tool.length), 4);
|
|
114
|
+
for (const tr of summary.tool_breakdown) {
|
|
115
|
+
const parts = [];
|
|
116
|
+
if (tr.results.allow > 0) parts.push(`${tr.results.allow} allow`);
|
|
117
|
+
if (tr.results.block > 0) parts.push(`\x1B[31m${tr.results.block} blocked\x1B[0m`);
|
|
118
|
+
if (tr.results.rate_limited > 0) parts.push(`\x1B[33m${tr.results.rate_limited} rate_limited\x1B[0m`);
|
|
119
|
+
if (tr.results.require_approval > 0) parts.push(`\x1B[36m${tr.results.require_approval} require_approval\x1B[0m`);
|
|
120
|
+
if (tr.results.tier_insufficient > 0) parts.push(`\x1B[35m${tr.results.tier_insufficient} tier_insufficient\x1B[0m`);
|
|
121
|
+
const originalParts = [];
|
|
122
|
+
if (tr.original.allow > 0) originalParts.push(`${tr.original.allow} allow`);
|
|
123
|
+
if (tr.original.deny > 0) originalParts.push(`${tr.original.deny} deny`);
|
|
124
|
+
lines.push(` ${tr.tool.padEnd(maxToolLen)} \xD7 ${String(tr.calls).padStart(3)} \u2192 ${parts.join(", ")} (was: ${originalParts.join(", ")})`);
|
|
125
|
+
}
|
|
126
|
+
lines.push("");
|
|
127
|
+
lines.push(`Summary: ${summary.results.allow} allow, ${summary.results.block} blocked, ${summary.results.rate_limited} rate_limited, ${summary.results.require_approval} require_approval, ${summary.results.tier_insufficient} tier_insufficient`);
|
|
128
|
+
lines.push(` vs original: ${summary.original.allow} allow, ${summary.original.deny} deny`);
|
|
129
|
+
if (summary.changes.length > 0) {
|
|
130
|
+
lines.push("");
|
|
131
|
+
lines.push("Changes:");
|
|
132
|
+
for (const change of summary.changes) {
|
|
133
|
+
lines.push(` \u2022 ${change}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return lines.join("\n");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
parseLogFile,
|
|
141
|
+
simulate,
|
|
142
|
+
formatSimulation
|
|
143
|
+
};
|