antigravity-plugin-context-mode 1.0.2 → 1.2.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/CHANGELOG.md +46 -0
- package/README.md +54 -33
- package/hooks/pre-tool-use.js +393 -41
- package/hooks.json +1 -1
- package/lib/store.js +594 -54
- package/lib/tools.js +6 -2
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/server.js +28 -3
package/hooks/pre-tool-use.js
CHANGED
|
@@ -1,26 +1,57 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { ContextModeStore } from "../lib/store.js";
|
|
4
5
|
|
|
5
6
|
export const DEFAULT_MAX_READ_BYTES = 8192; // 8 KB default cutoff
|
|
6
7
|
export const DEFAULT_MAX_LINE_RANGE = 200; // 200 lines max un-indexed slice
|
|
7
8
|
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
9
|
+
function isProtectionPaused(sessionId = null) {
|
|
10
|
+
try {
|
|
11
|
+
const store = new ContextModeStore();
|
|
12
|
+
const paused = store.isPaused(sessionId);
|
|
13
|
+
store.close();
|
|
14
|
+
return paused;
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getMaxReadBytes(sessionId = null) {
|
|
21
|
+
try {
|
|
22
|
+
const store = new ContextModeStore();
|
|
23
|
+
const cutoff = store.getCutoff(sessionId);
|
|
24
|
+
store.close();
|
|
25
|
+
return cutoff;
|
|
26
|
+
} catch {
|
|
27
|
+
const envVal = process.env.CONTEXT_MODE_MAX_READ_BYTES;
|
|
28
|
+
if (envVal) {
|
|
29
|
+
const parsed = parseInt(envVal, 10);
|
|
30
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
31
|
+
}
|
|
32
|
+
return DEFAULT_MAX_READ_BYTES;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getExemptExtensions(sessionId = null) {
|
|
37
|
+
try {
|
|
38
|
+
const store = new ContextModeStore();
|
|
39
|
+
const exts = store.getExemptExtensions(sessionId);
|
|
40
|
+
store.close();
|
|
41
|
+
return exts;
|
|
42
|
+
} catch {
|
|
43
|
+
return Array.from(BINARY_EXTENSIONS);
|
|
13
44
|
}
|
|
14
|
-
return DEFAULT_MAX_READ_BYTES;
|
|
15
45
|
}
|
|
16
46
|
|
|
17
|
-
function getMaxLineRange() {
|
|
47
|
+
function getMaxLineRange(maxReadBytes = DEFAULT_MAX_READ_BYTES) {
|
|
18
48
|
const envVal = process.env.CONTEXT_MODE_MAX_LINE_RANGE;
|
|
19
49
|
if (envVal) {
|
|
20
50
|
const parsed = parseInt(envVal, 10);
|
|
21
51
|
if (!Number.isNaN(parsed) && parsed > 0) return parsed;
|
|
22
52
|
}
|
|
23
|
-
return
|
|
53
|
+
if (maxReadBytes === 0) return 0;
|
|
54
|
+
return Math.max(50, Math.round(maxReadBytes / 40));
|
|
24
55
|
}
|
|
25
56
|
|
|
26
57
|
function formatBytes(bytes) {
|
|
@@ -33,6 +64,151 @@ function formatBytes(bytes) {
|
|
|
33
64
|
return `${Number.isInteger(mb) ? mb : mb.toFixed(2)} MB`;
|
|
34
65
|
}
|
|
35
66
|
|
|
67
|
+
export function cleanStringArg(val) {
|
|
68
|
+
if (val === undefined || val === null) return "";
|
|
69
|
+
let str = String(val).trim();
|
|
70
|
+
while (
|
|
71
|
+
(str.startsWith('"') && str.endsWith('"') && str.length >= 2) ||
|
|
72
|
+
(str.startsWith("'") && str.endsWith("'") && str.length >= 2)
|
|
73
|
+
) {
|
|
74
|
+
str = str.slice(1, -1).trim();
|
|
75
|
+
}
|
|
76
|
+
return str;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function cleanFilePath(val) {
|
|
80
|
+
let p = cleanStringArg(val);
|
|
81
|
+
if (!p) return "";
|
|
82
|
+
// Strip file URI prefix
|
|
83
|
+
if (p.startsWith("file:///")) {
|
|
84
|
+
p = p.slice(8);
|
|
85
|
+
} else if (p.startsWith("file://")) {
|
|
86
|
+
p = p.slice(7);
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
p = decodeURIComponent(p);
|
|
90
|
+
} catch {}
|
|
91
|
+
// Strip trailing line anchor (e.g. #L1-L20)
|
|
92
|
+
p = p.replace(/#L\d+(-L\d+)?$/i, "");
|
|
93
|
+
// Normalize Windows slashes
|
|
94
|
+
return path.normalize(p.trim());
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function parseLineArg(val) {
|
|
98
|
+
if (val === undefined || val === null) return null;
|
|
99
|
+
const str = cleanStringArg(val);
|
|
100
|
+
if (!str || str.toLowerCase() === "null" || str.toLowerCase() === "undefined") {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const parsed = parseInt(str, 10);
|
|
104
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const KNOWN_TEXT_EXTENSIONS = new Set([
|
|
108
|
+
// Markdown & Documentation
|
|
109
|
+
".md", ".markdown", ".mdown", ".mkd", ".rst", ".adoc",
|
|
110
|
+
// Code
|
|
111
|
+
".cs", ".csx",
|
|
112
|
+
".js", ".jsx", ".mjs", ".cjs",
|
|
113
|
+
".ts", ".tsx", ".mts", ".cts",
|
|
114
|
+
".py", ".pyw",
|
|
115
|
+
".c", ".h", ".cpp", ".hpp", ".cc", ".cxx", ".hh",
|
|
116
|
+
".rs", ".go", ".java", ".kt", ".kts", ".scala",
|
|
117
|
+
".rb", ".php", ".sh", ".bash", ".ps1", ".psm1", ".psd1",
|
|
118
|
+
".bat", ".cmd",
|
|
119
|
+
// Web & Config
|
|
120
|
+
".html", ".htm", ".xhtml", ".css", ".scss", ".sass", ".less",
|
|
121
|
+
".json", ".jsonc", ".json5",
|
|
122
|
+
".xml", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".conf",
|
|
123
|
+
".sql", ".graphql", ".gql",
|
|
124
|
+
".diff", ".patch", ".txt", ".text", ".log", ".csv", ".tsv"
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
const BINARY_EXTENSIONS = new Set([
|
|
128
|
+
// Documents & Office
|
|
129
|
+
".pdf", ".docx", ".doc", ".xlsx", ".xls", ".pptx", ".ppt",
|
|
130
|
+
// Images
|
|
131
|
+
".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".ico", ".tiff", ".tif",
|
|
132
|
+
// Audio & Video
|
|
133
|
+
".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac", ".mp4", ".webm", ".mkv", ".avi", ".mov",
|
|
134
|
+
// Archives & Executables
|
|
135
|
+
".zip", ".tar", ".gz", ".7z", ".rar", ".exe", ".dll", ".so", ".dylib", ".bin",
|
|
136
|
+
// 3D & Models
|
|
137
|
+
".blend", ".fbx", ".obj", ".gltf", ".glb", ".wasm"
|
|
138
|
+
]);
|
|
139
|
+
|
|
140
|
+
function isUserAttachedFile(filePath, absPath) {
|
|
141
|
+
const normalized = (absPath || filePath || "").replace(/\\/g, "/");
|
|
142
|
+
return (
|
|
143
|
+
normalized.includes("/.user_uploaded/") ||
|
|
144
|
+
normalized.includes("/user_uploaded/") ||
|
|
145
|
+
normalized.includes("/.system_generated/uploads/")
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function isBinaryFile(absPath, ext) {
|
|
150
|
+
const lowerExt = (ext || "").toLowerCase();
|
|
151
|
+
if (KNOWN_TEXT_EXTENSIONS.has(lowerExt)) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (BINARY_EXTENSIONS.has(lowerExt)) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const fd = fs.openSync(absPath, "r");
|
|
159
|
+
const buf = Buffer.alloc(512);
|
|
160
|
+
const bytesRead = fs.readSync(fd, buf, 0, 512, 0);
|
|
161
|
+
fs.closeSync(fd);
|
|
162
|
+
if (bytesRead === 0) return false;
|
|
163
|
+
|
|
164
|
+
// Check for UTF-16 LE BOM (FF FE) or BE BOM (FE FF)
|
|
165
|
+
if (bytesRead >= 2 && ((buf[0] === 0xff && buf[1] === 0xfe) || (buf[0] === 0xfe && buf[1] === 0xff))) {
|
|
166
|
+
return false; // Text file with UTF-16 BOM
|
|
167
|
+
}
|
|
168
|
+
// Check for UTF-8 BOM (EF BB BF)
|
|
169
|
+
if (bytesRead >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Check for PDF magic header (%PDF)
|
|
174
|
+
if (bytesRead >= 4 && buf[0] === 0x25 && buf[1] === 0x50 && buf[2] === 0x44 && buf[3] === 0x46) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Check for null bytes indicating binary content
|
|
179
|
+
for (let i = 0; i < bytesRead; i++) {
|
|
180
|
+
if (buf[i] === 0) return true;
|
|
181
|
+
}
|
|
182
|
+
} catch {
|
|
183
|
+
// ignore inspection error
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function recordInterception({ sessionId, cwd, toolName, path: targetPath, size, reason }) {
|
|
189
|
+
try {
|
|
190
|
+
const store = new ContextModeStore();
|
|
191
|
+
store.recordEvent({
|
|
192
|
+
sessionId: sessionId || "default",
|
|
193
|
+
worktree: cwd || process.cwd(),
|
|
194
|
+
agent: "antigravity",
|
|
195
|
+
kind: "interception",
|
|
196
|
+
tool: toolName,
|
|
197
|
+
key: "blocked",
|
|
198
|
+
path: targetPath || null,
|
|
199
|
+
ok: 0,
|
|
200
|
+
exitCode: 1,
|
|
201
|
+
bytes: size || 0,
|
|
202
|
+
body: reason,
|
|
203
|
+
sandboxBytes: size || 0,
|
|
204
|
+
returnedBytes: 0
|
|
205
|
+
});
|
|
206
|
+
store.close();
|
|
207
|
+
} catch {
|
|
208
|
+
// Non-blocking: database recording failure must never crash the hook
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
36
212
|
function readStdin() {
|
|
37
213
|
return new Promise((resolve) => {
|
|
38
214
|
let data = "";
|
|
@@ -46,13 +222,144 @@ function readStdin() {
|
|
|
46
222
|
});
|
|
47
223
|
}
|
|
48
224
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/^\s*
|
|
52
|
-
/^\s*git\s+log\
|
|
53
|
-
/^\s*curl
|
|
225
|
+
// Kept for backward compatibility
|
|
226
|
+
export const UNBOUNDED_COMMAND_PATTERNS = [
|
|
227
|
+
/^\s*(cat|type|Get-Content|gc)\b/i,
|
|
228
|
+
/^\s*git\s+log\b/i,
|
|
229
|
+
/^\s*(curl|wget)\b/i
|
|
54
230
|
];
|
|
55
231
|
|
|
232
|
+
/**
|
|
233
|
+
* Dynamically inspects shell commands against active SQLite cutoff and user exemptions
|
|
234
|
+
* without relying on a static hardcoded file extension whitelist.
|
|
235
|
+
*/
|
|
236
|
+
export function inspectCommand({ commandLine, cwd, sessionId }) {
|
|
237
|
+
if (!commandLine || typeof commandLine !== "string") {
|
|
238
|
+
return { shouldDeny: false };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const cleanCmd = cleanStringArg(commandLine);
|
|
242
|
+
const maxReadBytes = getMaxReadBytes(sessionId);
|
|
243
|
+
const exemptExtensions = getExemptExtensions(sessionId);
|
|
244
|
+
|
|
245
|
+
// 1. Git log without count limiter
|
|
246
|
+
if (/^\s*git\s+log\b/i.test(cleanCmd)) {
|
|
247
|
+
const isLimited = /(-n\s+\d+|-\d+|--max-count|\s*\|\s*(head|Select-Object|select|more))/i.test(cleanCmd);
|
|
248
|
+
if (!isLimited) {
|
|
249
|
+
return {
|
|
250
|
+
shouldDeny: true,
|
|
251
|
+
reason:
|
|
252
|
+
`[context-mode protection] 'git log' without a count limiter risks dumping thousands of commits into context. ` +
|
|
253
|
+
`Please specify a count (e.g. 'git log -n 10') or run inside 'mcp_context-mode_ctx_execute'.`,
|
|
254
|
+
size: 32768,
|
|
255
|
+
targetPath: "git log"
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// 2. Direct curl / wget to stdout without file destination or limiter
|
|
261
|
+
if (/^\s*(curl(\.exe)?|wget(\.exe)?)\b/i.test(cleanCmd)) {
|
|
262
|
+
const writesToFile = /(-o\s+|-O\s+|--output\s+|>)/i.test(cleanCmd);
|
|
263
|
+
const hasLimiter = /\|\s*(head|Select-Object|select|more)/i.test(cleanCmd);
|
|
264
|
+
if (!writesToFile && !hasLimiter) {
|
|
265
|
+
// Allow if downloading a file with an exempt extension (e.g. images, media, office docs)
|
|
266
|
+
const urlMatch = cleanCmd.match(/https?:\/\/[^\s"']+/i);
|
|
267
|
+
if (urlMatch) {
|
|
268
|
+
try {
|
|
269
|
+
const parsedUrl = new URL(urlMatch[0]);
|
|
270
|
+
const ext = path.extname(parsedUrl.pathname).toLowerCase();
|
|
271
|
+
if (ext && exemptExtensions.includes(ext)) {
|
|
272
|
+
return { shouldDeny: false };
|
|
273
|
+
}
|
|
274
|
+
} catch {}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
shouldDeny: true,
|
|
279
|
+
reason:
|
|
280
|
+
`[context-mode protection] Direct curl/wget download to stdout risks flooding the context window. ` +
|
|
281
|
+
`Please use 'mcp_context-mode_ctx_execute', save to disk ('-o <file>'), or append '| head -n 50'.`,
|
|
282
|
+
size: 32768,
|
|
283
|
+
targetPath: urlMatch ? urlMatch[0] : cleanCmd
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 3. Raw file dump utilities (cat, type, Get-Content, gc)
|
|
289
|
+
const printMatch = cleanCmd.match(/^\s*(cat|type|Get-Content|gc)(\.exe)?\b/i);
|
|
290
|
+
if (printMatch) {
|
|
291
|
+
// Allow if command includes pagination flags or piping to limiters / file sinks
|
|
292
|
+
const hasLimiter =
|
|
293
|
+
/(-Head|-TotalCount|-First|-Tail)\s+\d+/i.test(cleanCmd) ||
|
|
294
|
+
/\|\s*(head|Select-Object|select\s+(-[a-zA-Z]+\s+)*-First|more|Out-Null|Set-Content|Out-File)/i.test(cleanCmd) ||
|
|
295
|
+
/>\s*[^>]/i.test(cleanCmd);
|
|
296
|
+
|
|
297
|
+
if (hasLimiter) {
|
|
298
|
+
return { shouldDeny: false };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Extract target argument
|
|
302
|
+
const argMatch = cleanCmd.match(/(?:cat|type|Get-Content|gc)(?:\.exe)?(?:\s+(?:-Path|-LiteralPath))?\s+["']?([^"'\r\n;|]+)["']?/i);
|
|
303
|
+
const rawTarget = argMatch && argMatch[1] ? argMatch[1].trim() : "";
|
|
304
|
+
|
|
305
|
+
// Wildcard dump (e.g. cat *.log, Get-Content *)
|
|
306
|
+
if (rawTarget.includes("*") || rawTarget.includes("?")) {
|
|
307
|
+
return {
|
|
308
|
+
shouldDeny: true,
|
|
309
|
+
reason:
|
|
310
|
+
`[context-mode protection] Wildcard file dump ('${rawTarget}') risks flooding the context window. ` +
|
|
311
|
+
`Please use 'mcp_context-mode_ctx_execute' or append a limiter such as '| Select-Object -First 50'.`,
|
|
312
|
+
size: 32768,
|
|
313
|
+
targetPath: rawTarget
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (rawTarget) {
|
|
318
|
+
const cleanTarget = cleanFilePath(rawTarget);
|
|
319
|
+
const absPath = path.isAbsolute(cleanTarget) ? cleanTarget : path.resolve(cwd, cleanTarget);
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
if (fs.existsSync(absPath)) {
|
|
323
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
324
|
+
// Dynamic exemption check from SQLite/Dashboard configuration
|
|
325
|
+
if (ext && exemptExtensions.includes(ext)) {
|
|
326
|
+
return { shouldDeny: false };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const stat = fs.statSync(absPath);
|
|
330
|
+
// Dynamic cutoff check against active setting
|
|
331
|
+
if (maxReadBytes > 0 && stat.size > maxReadBytes) {
|
|
332
|
+
return {
|
|
333
|
+
shouldDeny: true,
|
|
334
|
+
reason:
|
|
335
|
+
`[context-mode protection] Command attempts an un-paged dump of '${cleanTarget}' (${formatBytes(stat.size)}), ` +
|
|
336
|
+
`exceeding the ${formatBytes(maxReadBytes)} context limit. ` +
|
|
337
|
+
`Please use 'mcp_context-mode_ctx_execute' or append '| Select-Object -First 50' or '-Head 50'.`,
|
|
338
|
+
size: stat.size,
|
|
339
|
+
targetPath: cleanTarget
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// File is small and within cutoff
|
|
344
|
+
return { shouldDeny: false };
|
|
345
|
+
}
|
|
346
|
+
} catch {}
|
|
347
|
+
|
|
348
|
+
// If file doesn't exist on disk or path cannot be resolved, block unpaged dump defensively
|
|
349
|
+
return {
|
|
350
|
+
shouldDeny: true,
|
|
351
|
+
reason:
|
|
352
|
+
`[context-mode protection] Command matches an un-paged file dump ('${rawTarget}') that could flood the context window. ` +
|
|
353
|
+
`Please use 'mcp_context-mode_ctx_execute' or append a limiter such as '| Select-Object -First 50' or '| head -n 50'.`,
|
|
354
|
+
size: 32768,
|
|
355
|
+
targetPath: cleanTarget || rawTarget
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return { shouldDeny: false };
|
|
361
|
+
}
|
|
362
|
+
|
|
56
363
|
async function main() {
|
|
57
364
|
const rawInput = await readStdin();
|
|
58
365
|
if (!rawInput.trim()) {
|
|
@@ -72,29 +379,64 @@ async function main() {
|
|
|
72
379
|
const toolName = toolCall.name || "";
|
|
73
380
|
const args = toolCall.args || {};
|
|
74
381
|
const cwd = payload.cwd || process.cwd();
|
|
382
|
+
const sessionId =
|
|
383
|
+
payload.sessionId ||
|
|
384
|
+
payload.session_id ||
|
|
385
|
+
payload.conversationId ||
|
|
386
|
+
process.env.ANTIGRAVITY_CONVERSATION_ID ||
|
|
387
|
+
null;
|
|
388
|
+
|
|
389
|
+
// If Context Mode protection is paused, allow all tool calls through without interception
|
|
390
|
+
if (isProtectionPaused(sessionId)) {
|
|
391
|
+
process.stdout.write(JSON.stringify({ decision: "allow" }) + "\n");
|
|
392
|
+
process.exit(0);
|
|
393
|
+
}
|
|
75
394
|
|
|
76
|
-
// 1. Guard read_file
|
|
77
|
-
if (toolName === "read_file") {
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
395
|
+
// 1. Guard read_file / view_file
|
|
396
|
+
if (toolName === "read_file" || toolName === "view_file") {
|
|
397
|
+
const rawFilePath = args.Path || args.path || args.filePath || "";
|
|
398
|
+
const filePath = cleanFilePath(rawFilePath);
|
|
399
|
+
const startLine = parseLineArg(args.StartLine ?? args.startLine) ?? 1;
|
|
400
|
+
const endLine = parseLineArg(args.EndLine ?? args.endLine);
|
|
401
|
+
const maxReadBytes = getMaxReadBytes(sessionId);
|
|
402
|
+
const maxLineRange = getMaxLineRange(maxReadBytes);
|
|
83
403
|
|
|
84
404
|
if (filePath) {
|
|
85
405
|
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
|
86
406
|
try {
|
|
87
407
|
if (fs.existsSync(absPath)) {
|
|
408
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
409
|
+
const isUserAttachment = isUserAttachedFile(rawFilePath, absPath);
|
|
410
|
+
const exemptExtensions = getExemptExtensions(sessionId);
|
|
411
|
+
const isExemptExt = exemptExtensions.includes(ext);
|
|
412
|
+
const isBinary = isBinaryFile(absPath, ext);
|
|
413
|
+
|
|
414
|
+
// Prompt attachments (drag-and-drop / user-uploaded), exempt extensions, or binary files bypass cutoff
|
|
415
|
+
if (isUserAttachment || isExemptExt || isBinary) {
|
|
416
|
+
process.stdout.write(JSON.stringify({ decision: "allow" }) + "\n");
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
88
420
|
const stat = fs.statSync(absPath);
|
|
89
|
-
const isLargeFile = stat.size > maxReadBytes;
|
|
90
|
-
const isWideRange = endLine === null || endLine - startLine > maxLineRange;
|
|
421
|
+
const isLargeFile = maxReadBytes > 0 && stat.size > maxReadBytes;
|
|
422
|
+
const isWideRange = maxLineRange > 0 && (endLine === null || (endLine - startLine) > maxLineRange);
|
|
91
423
|
|
|
92
424
|
if (isLargeFile && isWideRange) {
|
|
425
|
+
const lineInstruction = maxLineRange > 0 ? ` or provide a tight StartLine/EndLine range (<= ${maxLineRange} lines).` : "";
|
|
93
426
|
const reason =
|
|
94
427
|
`[context-mode protection] File is ${formatBytes(stat.size)} (~${Math.round(stat.size / 4).toLocaleString()} tokens), exceeding the ${formatBytes(maxReadBytes)} context limit. ` +
|
|
95
428
|
`Reading the entire file risks severe context bloat. ` +
|
|
96
|
-
`Please use 'mcp_context-mode_ctx_index(path: "${filePath}")' followed by 'mcp_context-mode_ctx_search'
|
|
97
|
-
|
|
429
|
+
`Please use 'mcp_context-mode_ctx_index(path: "${filePath}")' followed by 'mcp_context-mode_ctx_search'${lineInstruction}`;
|
|
430
|
+
|
|
431
|
+
// Record interception event in SQLite store so dashboard telemetry updates immediately
|
|
432
|
+
recordInterception({
|
|
433
|
+
sessionId,
|
|
434
|
+
cwd,
|
|
435
|
+
toolName,
|
|
436
|
+
path: filePath,
|
|
437
|
+
size: stat.size,
|
|
438
|
+
reason
|
|
439
|
+
});
|
|
98
440
|
|
|
99
441
|
process.stdout.write(
|
|
100
442
|
JSON.stringify({
|
|
@@ -102,7 +444,7 @@ async function main() {
|
|
|
102
444
|
reason
|
|
103
445
|
}) + "\n"
|
|
104
446
|
);
|
|
105
|
-
|
|
447
|
+
process.exit(0);
|
|
106
448
|
}
|
|
107
449
|
}
|
|
108
450
|
} catch {
|
|
@@ -113,29 +455,39 @@ async function main() {
|
|
|
113
455
|
|
|
114
456
|
// 2. Guard run_command
|
|
115
457
|
if (toolName === "run_command") {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
458
|
+
const rawCommandLine = args.CommandLine || args.commandLine || args.command || "";
|
|
459
|
+
const inspection = inspectCommand({
|
|
460
|
+
commandLine: rawCommandLine,
|
|
461
|
+
cwd,
|
|
462
|
+
sessionId
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
if (inspection.shouldDeny) {
|
|
466
|
+
recordInterception({
|
|
467
|
+
sessionId,
|
|
468
|
+
cwd,
|
|
469
|
+
toolName,
|
|
470
|
+
path: inspection.targetPath || cleanStringArg(rawCommandLine),
|
|
471
|
+
size: inspection.size || 32768,
|
|
472
|
+
reason: inspection.reason
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
process.stdout.write(
|
|
476
|
+
JSON.stringify({
|
|
477
|
+
decision: "deny",
|
|
478
|
+
reason: inspection.reason
|
|
479
|
+
}) + "\n"
|
|
480
|
+
);
|
|
481
|
+
process.exit(0);
|
|
132
482
|
}
|
|
133
483
|
}
|
|
134
484
|
|
|
135
485
|
// Default allow
|
|
136
486
|
process.stdout.write(JSON.stringify({ decision: "allow" }) + "\n");
|
|
487
|
+
process.exit(0);
|
|
137
488
|
}
|
|
138
489
|
|
|
139
490
|
main().catch(() => {
|
|
140
491
|
process.stdout.write(JSON.stringify({ decision: "allow" }) + "\n");
|
|
492
|
+
process.exit(0);
|
|
141
493
|
});
|