scalix 1.5.1 → 1.6.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.
@@ -4,9 +4,11 @@ import fs from 'node:fs/promises';
4
4
  import path from 'node:path';
5
5
  import { sandboxPath, getSandboxRoot } from './sandbox.js';
6
6
  import { isPrivateHost } from './constants.js';
7
+ import { checkLocalAccess, isLocalOriginApproved } from './browser-access.js';
7
8
  import { encodeImageToDataUrl } from './tools-vision.js';
8
- // Validate a navigation target before handing it to Playwright: http/https only
9
- // and never a localhost/private-network host (defeats browser-based SSRF).
9
+ // Validate a navigation target's shape before handing it to Playwright:
10
+ // http/https only. Whether a *local* target is reachable is a separate,
11
+ // user-answerable question — see checkLocalAccess in browser-access.ts.
10
12
  // Returns an error string when the URL must be rejected, otherwise null.
11
13
  function validateNavigationUrl(rawUrl) {
12
14
  let parsed;
@@ -19,18 +21,17 @@ function validateNavigationUrl(rawUrl) {
19
21
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
20
22
  return `Blocked: only http/https URLs are allowed (got ${parsed.protocol})`;
21
23
  }
22
- if (isPrivateHost(parsed.hostname)) {
23
- return 'Blocked: navigation to localhost and private networks is not allowed';
24
- }
25
24
  return null;
26
25
  }
27
26
  // Re-check the URL a page actually landed on (post-redirect) and reject when it
28
- // resolved to a private host.
27
+ // resolved to a private host the user has not cleared. A redirect is not a
28
+ // moment to interrupt with a prompt, so an unapproved local origin is simply
29
+ // refused here — the model can navigate to it directly and be asked properly.
29
30
  function validateResolvedUrl(rawUrl) {
30
31
  try {
31
32
  const parsed = new URL(rawUrl);
32
33
  if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
33
- if (isPrivateHost(parsed.hostname)) {
34
+ if (isPrivateHost(parsed.hostname) && !isLocalOriginApproved(rawUrl)) {
34
35
  return 'Blocked: navigation redirected to a private network address';
35
36
  }
36
37
  }
@@ -40,6 +41,27 @@ function validateResolvedUrl(rawUrl) {
40
41
  }
41
42
  return null;
42
43
  }
44
+ let frameSink = null;
45
+ /** Front ends that can display the agent's browser register here. */
46
+ export function setBrowserFrameSink(fn) {
47
+ frameSink = fn;
48
+ }
49
+ // Capture is best-effort and must never turn a working action into a failed
50
+ // one: a page that navigated away mid-shot is a missing frame, not an error.
51
+ async function emitFrame(page, action) {
52
+ if (!frameSink)
53
+ return;
54
+ try {
55
+ const buf = await page.screenshot({ type: 'jpeg', quality: 55 });
56
+ frameSink({
57
+ action,
58
+ url: page.url(),
59
+ title: await page.title().catch(() => ''),
60
+ dataUrl: `data:image/jpeg;base64,${buf.toString('base64')}`,
61
+ });
62
+ }
63
+ catch { }
64
+ }
43
65
  let browserInstance = null;
44
66
  let browserContext = null;
45
67
  let browserPage = null;
@@ -76,12 +98,30 @@ async function ensureBrowser() {
76
98
  browserPage = null;
77
99
  }
78
100
  }
79
- browserInstance = await pw.chromium.launch({ headless: true });
80
- browserContext = await browserInstance.newContext({
81
- viewport: { width: 1280, height: 720 },
82
- });
83
- browserPage = await browserContext.newPage();
84
- return { browser: browserInstance, page: browserPage };
101
+ try {
102
+ browserInstance = await pw.chromium.launch({ headless: true });
103
+ browserContext = await browserInstance.newContext({
104
+ viewport: { width: 1280, height: 720 },
105
+ });
106
+ browserPage = await browserContext.newPage();
107
+ return { browser: browserInstance, page: browserPage };
108
+ }
109
+ catch (err) {
110
+ if (browserInstance) {
111
+ try {
112
+ await browserInstance.close();
113
+ }
114
+ catch { }
115
+ browserInstance = null;
116
+ }
117
+ browserContext = null;
118
+ browserPage = null;
119
+ const msg = err?.message || String(err);
120
+ if (msg.includes('Executable doesn\'t exist') || msg.includes('playwright install') || msg.includes('chromium')) {
121
+ throw new Error(`Playwright browser binaries not found. Run 'npx playwright install chromium' to enable browser automation.`);
122
+ }
123
+ throw err;
124
+ }
85
125
  }
86
126
  export async function closeBrowser() {
87
127
  if (browserContext) {
@@ -111,6 +151,11 @@ export const browserNavigateTool = tool({
111
151
  const preCheck = validateNavigationUrl(url);
112
152
  if (preCheck)
113
153
  return { error: preCheck };
154
+ // localhost and the LAN need the user's say-so, once per origin per
155
+ // project. This is what lets the agent open the dev server it just edited.
156
+ const localGate = await checkLocalAccess(url);
157
+ if (localGate)
158
+ return { error: localGate };
114
159
  const session = await ensureBrowser();
115
160
  if (!session) {
116
161
  return { error: 'Playwright not available. Install it: npm install playwright && npx playwright install chromium' };
@@ -130,6 +175,7 @@ export const browserNavigateTool = tool({
130
175
  return { error: postCheck, url: currentUrl };
131
176
  }
132
177
  const title = await session.page.title();
178
+ await emitFrame(session.page, `navigate ${currentUrl}`);
133
179
  return {
134
180
  url: currentUrl,
135
181
  title,
@@ -228,7 +274,7 @@ export const browserScreenshotTool = tool({
228
274
  overlay.remove();
229
275
  });
230
276
  }
231
- return {
277
+ const shot = {
232
278
  url: session.page.url(),
233
279
  title: await session.page.title(),
234
280
  path: outputPath || undefined,
@@ -236,6 +282,11 @@ export const browserScreenshotTool = tool({
236
282
  dataUrl,
237
283
  annotations,
238
284
  };
285
+ // Reuse the shot the model is about to look at rather than taking a
286
+ // second one — the annotated overlay is exactly what is worth showing.
287
+ if (frameSink)
288
+ frameSink({ action: 'screenshot', url: shot.url, title: shot.title, dataUrl });
289
+ return shot;
239
290
  }
240
291
  catch (error) {
241
292
  return { error: `Screenshot failed: ${error.message}` };
@@ -273,6 +324,7 @@ export const browserClickTool = tool({
273
324
  }
274
325
  await session.page.waitForTimeout(500);
275
326
  const target = typeof x === 'number' ? `coords:(${x},${y})` : selector || `text:"${text}"`;
327
+ await emitFrame(session.page, `click ${target}`);
276
328
  return { clicked: target, url: session.page.url() };
277
329
  }
278
330
  catch (error) {
@@ -323,6 +375,7 @@ export const browserMouseTool = tool({
323
375
  await session.page.waitForTimeout(300);
324
376
  break;
325
377
  }
378
+ await emitFrame(session.page, `${action} (${x},${y})`);
326
379
  return { action, x, y, url: session.page.url() };
327
380
  }
328
381
  catch (error) {
@@ -355,6 +408,7 @@ export const browserTypeTool = tool({
355
408
  await session.page.press(selector, 'Enter');
356
409
  await session.page.waitForTimeout(500);
357
410
  }
411
+ await emitFrame(session.page, `type into ${selector}`);
358
412
  return { typed: text.length > 50 ? text.slice(0, 50) + '...' : text, selector, url: session.page.url() };
359
413
  }
360
414
  catch (error) {
@@ -413,10 +467,16 @@ export const browserGetTextTool = tool({
413
467
  },
414
468
  });
415
469
  const SESSION_DIR = '.scalix/browser-sessions';
470
+ // Session names become file names under .scalix/browser-sessions/ — restrict
471
+ // them to a safe charset so `../` cannot escape the sandbox root.
472
+ const SESSION_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
416
473
  async function saveSessionState(name) {
417
474
  if (!browserContext || !browserPage) {
418
475
  throw new Error('No active browser session to save');
419
476
  }
477
+ if (!SESSION_NAME_RE.test(name)) {
478
+ throw new Error(`Invalid session name "${name}" — use letters, digits, dot, dash, underscore`);
479
+ }
420
480
  const storageState = await browserContext.storageState();
421
481
  const url = browserPage.url();
422
482
  const sessionDir = path.join(getSandboxRoot(), SESSION_DIR);
@@ -442,7 +502,7 @@ export const browserSessionSaveTool = tool({
442
502
  name: 'browser_session_save',
443
503
  description: 'Save the current browser session (cookies, localStorage, URL) to disk for later restoration.',
444
504
  inputSchema: z.object({
445
- name: z.string().max(100).describe('Session name (used to restore later)'),
505
+ name: z.string().max(100).regex(SESSION_NAME_RE, 'Session name may only contain letters, digits, dot, dash, underscore (no slashes)').describe('Session name (used to restore later)'),
446
506
  }),
447
507
  execute: async ({ name }) => {
448
508
  try {
@@ -458,7 +518,7 @@ export const browserSessionRestoreTool = tool({
458
518
  name: 'browser_session_restore',
459
519
  description: 'Restore a previously saved browser session. Recreates the browser context with saved cookies/localStorage and navigates to the saved URL.',
460
520
  inputSchema: z.object({
461
- name: z.string().max(100).describe('Session name to restore'),
521
+ name: z.string().max(100).regex(SESSION_NAME_RE, 'Session name may only contain letters, digits, dot, dash, underscore (no slashes)').describe('Session name to restore'),
462
522
  }),
463
523
  execute: async ({ name }) => {
464
524
  const pw = await getPlaywright();
@@ -489,7 +549,16 @@ export const browserSessionRestoreTool = tool({
489
549
  browserPage = null;
490
550
  }
491
551
  if (!browserInstance) {
492
- browserInstance = await pw.chromium.launch({ headless: true });
552
+ try {
553
+ browserInstance = await pw.chromium.launch({ headless: true });
554
+ }
555
+ catch (err) {
556
+ const msg = err?.message || String(err);
557
+ if (msg.includes('Executable doesn\'t exist') || msg.includes('playwright install') || msg.includes('chromium')) {
558
+ return { error: `Session restore failed: Playwright browser binaries not found. Run 'npx playwright install chromium' to enable browser automation.` };
559
+ }
560
+ return { error: `Session restore failed: ${msg}` };
561
+ }
493
562
  }
494
563
  browserContext = await browserInstance.newContext({
495
564
  viewport: session.viewport,
@@ -497,7 +566,11 @@ export const browserSessionRestoreTool = tool({
497
566
  });
498
567
  browserPage = await browserContext.newPage();
499
568
  if (session.url && session.url !== 'about:blank') {
500
- const preCheck = validateNavigationUrl(session.url);
569
+ const preCheck = validateNavigationUrl(session.url)
570
+ // A saved session is a stored URL replayed without the model naming
571
+ // it, so a local target still has to clear the same consent gate as
572
+ // a fresh navigation.
573
+ || await checkLocalAccess(session.url);
501
574
  if (preCheck) {
502
575
  return { error: `Session restore blocked: saved URL rejected — ${preCheck}` };
503
576
  }
@@ -575,6 +648,10 @@ export const browserCloseTool = tool({
575
648
  }
576
649
  catch { }
577
650
  await closeBrowser();
651
+ // An empty frame means "the agent's browser is gone" — the viewer keeps the
652
+ // last picture but stops implying it is live.
653
+ if (frameSink)
654
+ frameSink({ action: 'closed', url: '', title: '', dataUrl: '' });
578
655
  return { status: 'closed', sessionSaved: saveSession || undefined };
579
656
  },
580
657
  });
@@ -102,7 +102,11 @@ export const searchFilesTool = tool({
102
102
  const beforeContext = args['-B'];
103
103
  try {
104
104
  const cwd = process.cwd();
105
- const target = searchPath || '.';
105
+ // Contain the search target before ANY backend runs — the rg fast path
106
+ // takes arbitrary paths and would otherwise read outside the sandbox
107
+ // (the built-in fallback below already validates).
108
+ const safeTarget = searchPath ? sandboxPath(searchPath) : '.';
109
+ const target = safeTarget;
106
110
  // Try ripgrep first
107
111
  const rgArgs = [];
108
112
  if (outputMode === 'files_with_matches')
@@ -136,18 +140,34 @@ export const searchFilesTool = tool({
136
140
  });
137
141
  const lines = stdout.split('\n').filter(Boolean);
138
142
  const sliced = lines.slice(offset, headLimit > 0 ? offset + headLimit : undefined);
143
+ // The target is resolved to an absolute sandbox path, so rg prints
144
+ // absolute paths — render them relative to cwd like the fallback does.
145
+ const relPath = (p) => {
146
+ const abs = path.isAbsolute(p) ? p : path.resolve(cwd, p);
147
+ return path.relative(cwd, abs) || '.';
148
+ };
149
+ const splitPrefix = (line, parts) => {
150
+ const segs = line.split(':');
151
+ const file = segs.slice(0, segs.length - parts).join(':');
152
+ const rest = segs.slice(segs.length - parts).join(':');
153
+ return [relPath(file), rest];
154
+ };
139
155
  if (outputMode === 'files_with_matches') {
140
- return { pattern, count: sliced.length, files: sliced };
156
+ return { pattern, count: sliced.length, files: sliced.map(relPath) };
141
157
  }
142
158
  else if (outputMode === 'count') {
143
159
  const counts = sliced.map(line => {
144
- const [file, cnt] = line.split(':');
160
+ const [file, cnt] = splitPrefix(line, 1);
145
161
  return { file, count: parseInt(cnt, 10) || 0 };
146
162
  });
147
163
  return { pattern, entries: counts };
148
164
  }
149
165
  else {
150
- return { pattern, matchCount: sliced.length, content: sliced.join('\n') };
166
+ const contentLines = sliced.map(line => {
167
+ const [file, rest] = splitPrefix(line, 2);
168
+ return `${file}:${rest}`;
169
+ });
170
+ return { pattern, matchCount: sliced.length, content: contentLines.join('\n') };
151
171
  }
152
172
  }
153
173
  catch (rgErr) {
@@ -160,9 +180,21 @@ export const searchFilesTool = tool({
160
180
  }
161
181
  }
162
182
  // Fallback: built-in search using fast-glob + regex
163
- const globPattern = fileGlob || '**/*';
164
- const files = await fg(globPattern, {
165
- cwd,
183
+ let searchCwd = cwd;
184
+ let targetPattern = fileGlob || '**/*';
185
+ if (searchPath) {
186
+ const safeTarget = sandboxPath(searchPath);
187
+ const st = await fs.stat(safeTarget).catch(() => null);
188
+ if (st && !st.isDirectory()) {
189
+ searchCwd = path.dirname(safeTarget);
190
+ targetPattern = path.basename(safeTarget);
191
+ }
192
+ else {
193
+ searchCwd = safeTarget;
194
+ }
195
+ }
196
+ const files = await fg(targetPattern, {
197
+ cwd: searchCwd,
166
198
  ignore: ['node_modules/**', '.git/**', 'dist/**', 'coverage/**', '*.lock'],
167
199
  absolute: true,
168
200
  onlyFiles: true,
@@ -327,6 +359,66 @@ export const findFilesTool = tool({
327
359
  }
328
360
  },
329
361
  });
362
+ // Artifacts — the Antigravity-parity surface: reviewable, durable outputs a
363
+ // turn produces (plan, walkthrough, task list, summary) instead of burying
364
+ // them in chat scrollback. Files live under <workspace>/.scalix/artifacts/ so
365
+ // they are plain markdown any editor can open, with frontmatter the UI reads.
366
+ const ARTIFACT_TYPES = ['plan', 'walkthrough', 'tasklist', 'summary'];
367
+ export const writeArtifactTool = tool({
368
+ name: 'write_artifact',
369
+ description: 'Write a reviewable artifact for this task: an implementation plan before multi-step work, a walkthrough of what was done after it, a task list, or a summary. ' +
370
+ 'Markdown with a # title. Returns the artifact path.',
371
+ inputSchema: z.object({
372
+ type: z.enum(ARTIFACT_TYPES).describe('plan | walkthrough | tasklist | summary'),
373
+ title: z.string().min(1).max(120).describe('Short human title, e.g. "Auth refactor plan"'),
374
+ content: z.string().min(1).max(100_000).describe('Markdown body (no frontmatter — added automatically).'),
375
+ }),
376
+ execute: async ({ type, title, content }) => {
377
+ try {
378
+ const dir = sandboxPath('.scalix/artifacts');
379
+ await fs.mkdir(dir, { recursive: true });
380
+ const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'artifact';
381
+ const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
382
+ const rel = `.scalix/artifacts/${stamp}-${type}-${slug}.md`;
383
+ const abs = sandboxPath(rel);
384
+ const frontmatter = `---\ntype: ${type}\ntitle: ${title.replace(/\n/g, ' ')}\ncreatedAt: ${new Date().toISOString()}\n---\n\n`;
385
+ await fs.writeFile(abs, frontmatter + content + (content.endsWith('\n') ? '' : '\n'), 'utf8');
386
+ return { ok: true, path: rel, type, title };
387
+ }
388
+ catch (error) {
389
+ return { error: error.message };
390
+ }
391
+ },
392
+ });
393
+ export const listArtifactsTool = tool({
394
+ name: 'list_artifacts',
395
+ description: 'List artifacts previously written in this workspace (.scalix/artifacts), newest first.',
396
+ inputSchema: z.object({}),
397
+ execute: async () => {
398
+ try {
399
+ const dir = sandboxPath('.scalix/artifacts');
400
+ const names = await fs.readdir(dir);
401
+ const files = names.filter((n) => n.endsWith('.md')).sort().reverse().slice(0, 50);
402
+ const artifacts = await Promise.all(files.map(async (n) => {
403
+ try {
404
+ const head = (await fs.readFile(path.join(dir, n), 'utf8')).slice(0, 512);
405
+ const type = head.match(/^type:\s*(\S+)/m)?.[1] ?? 'unknown';
406
+ const title = head.match(/^title:\s*(.+)$/m)?.[1] ?? n;
407
+ return { path: `.scalix/artifacts/${n}`, type, title };
408
+ }
409
+ catch {
410
+ return { path: `.scalix/artifacts/${n}`, type: 'unknown', title: n };
411
+ }
412
+ }));
413
+ return { artifacts };
414
+ }
415
+ catch (error) {
416
+ if (error.code === 'ENOENT')
417
+ return { artifacts: [] };
418
+ return { error: error.message };
419
+ }
420
+ },
421
+ });
330
422
  export const gitStatusTool = tool({
331
423
  name: 'git_status',
332
424
  description: 'Get git repository status: current branch, staged/unstaged changes, untracked files.',
@@ -414,6 +506,8 @@ export const extraTools = [
414
506
  editFileTool,
415
507
  searchFilesTool,
416
508
  findFilesTool,
509
+ writeArtifactTool,
510
+ listArtifactsTool,
417
511
  gitStatusTool,
418
512
  gitDiffTool,
419
513
  gitCommitTool,
@@ -0,0 +1,74 @@
1
+ import { tool } from './tool-helper.js';
2
+ import { z } from 'zod';
3
+ import { loadMemory, addConvention, addPattern, addNote } from './project-memory.js';
4
+ import { getPersistentCwd } from './tools.js';
5
+ export const saveMemoryTool = tool({
6
+ name: 'save_memory',
7
+ description: 'Save a project convention, architectural pattern, or durable note into the project memory (.scalix/memory.json). Use whenever the user asks you to remember something, corrects a convention, or when establishing project standards.',
8
+ inputSchema: z.object({
9
+ category: z.enum(['convention', 'pattern', 'note']).describe('Type of memory to store'),
10
+ key: z.string().max(100).optional().describe('Convention key (e.g., "package_manager", "database", "api_base", "styling") when category is "convention"'),
11
+ value: z.string().max(1000).optional().describe('Convention value (e.g., "pnpm", "PostgreSQL", "Tailwind CSS") when category is "convention"'),
12
+ note: z.string().max(2000).optional().describe('Text note/guideline/rule when category is "note"'),
13
+ pattern: z.object({
14
+ name: z.string().max(100).describe('Pattern name (e.g., "Repository Pattern", "Zod Validation")'),
15
+ description: z.string().max(2000).describe('Detailed explanation of how and when to use this pattern'),
16
+ example: z.string().max(2000).optional().describe('Code snippet or usage example'),
17
+ }).optional().describe('Pattern definition when category is "pattern"'),
18
+ }),
19
+ execute: async ({ category, key, value, note, pattern }) => {
20
+ const cwd = getPersistentCwd() || process.cwd();
21
+ if (category === 'convention') {
22
+ if (!key || !value) {
23
+ return { success: false, error: 'Both "key" and "value" are required when saving a convention.' };
24
+ }
25
+ await addConvention(cwd, key, value, 'user');
26
+ return { success: true, message: `Saved convention: ${key} = ${value}` };
27
+ }
28
+ if (category === 'pattern') {
29
+ if (!pattern || !pattern.name || !pattern.description) {
30
+ return { success: false, error: '"pattern.name" and "pattern.description" are required when saving a pattern.' };
31
+ }
32
+ await addPattern(cwd, pattern);
33
+ return { success: true, message: `Saved pattern: ${pattern.name}` };
34
+ }
35
+ if (category === 'note') {
36
+ if (!note || !note.trim()) {
37
+ return { success: false, error: '"note" text is required when saving a note.' };
38
+ }
39
+ await addNote(cwd, note.trim());
40
+ return { success: true, message: `Saved note: "${note.trim()}"` };
41
+ }
42
+ return { success: false, error: `Unsupported category "${category}"` };
43
+ },
44
+ });
45
+ export const getMemoryTool = tool({
46
+ name: 'get_memory',
47
+ description: 'Retrieve stored project memory (.scalix/memory.json), including conventions, architectural patterns, and user notes.',
48
+ inputSchema: z.object({
49
+ category: z.enum(['all', 'conventions', 'patterns', 'notes']).optional().describe('Filter by memory category (default: "all")'),
50
+ }),
51
+ execute: async ({ category = 'all' }) => {
52
+ const cwd = getPersistentCwd() || process.cwd();
53
+ const mem = await loadMemory(cwd);
54
+ if (category === 'conventions') {
55
+ return { conventions: mem.conventions, lastUpdated: mem.lastUpdated };
56
+ }
57
+ if (category === 'patterns') {
58
+ return { patterns: mem.patterns, lastUpdated: mem.lastUpdated };
59
+ }
60
+ if (category === 'notes') {
61
+ return { notes: mem.notes, lastUpdated: mem.lastUpdated };
62
+ }
63
+ return {
64
+ conventions: mem.conventions,
65
+ patterns: mem.patterns,
66
+ notes: mem.notes,
67
+ lastUpdated: mem.lastUpdated,
68
+ };
69
+ },
70
+ });
71
+ export const memoryTools = [
72
+ saveMemoryTool,
73
+ getMemoryTool,
74
+ ];
package/dist/tools-web.js CHANGED
@@ -2,34 +2,38 @@ import { tool } from './tool-helper.js';
2
2
  import { z } from 'zod';
3
3
  import fs from 'node:fs/promises';
4
4
  import path from 'node:path';
5
- import dns from 'node:dns';
5
+ import { resolveLookupAddresses, pinnedFetch } from './secure-fetch.js';
6
6
  import { sandboxPath } from './sandbox.js';
7
7
  import { logger } from './logger.js';
8
- import { USER_AGENT, SEARCH_ENDPOINTS, WEB_DEFAULTS, TOOL_DEFAULTS, HTML_STRIP_TAGS, isPrivateHost, isPrivateIp, } from './constants.js';
8
+ import { USER_AGENT, SEARCH_ENDPOINTS, WEB_DEFAULTS, TOOL_DEFAULTS, HTML_STRIP_TAGS, isPrivateHost, isPrivateIp, isHostExplicitlyAllowed, } from './constants.js';
9
9
  import { detectPromptInjection } from './injection-detector.js';
10
10
  import { DEFAULT_LIMITS } from './settings.js';
11
11
  const MAX_FETCH_REDIRECTS = 5;
12
12
  // Verify a URL is safe to fetch: http/https only, non-private hostname, and no
13
13
  // resolved IP that lands in a private/loopback/link-local range (defeats DNS
14
- // rebinding). Returns an error string when the URL must be blocked, else null.
14
+ // rebinding). The addresses that PASSED the check are returned so the actual
15
+ // connection can be pinned to them — otherwise the fetch re-resolves the name
16
+ // and a rebinding answer lands the request inside the private network the
17
+ // check just cleared.
15
18
  async function validateFetchTarget(target) {
16
19
  if (target.protocol !== 'http:' && target.protocol !== 'https:') {
17
- return `Blocked: only http/https URLs are allowed (got ${target.protocol})`;
20
+ return { blocked: `Blocked: only http/https URLs are allowed (got ${target.protocol})`, addresses: [] };
18
21
  }
19
22
  const hostname = target.hostname.toLowerCase();
20
23
  if (isPrivateHost(hostname)) {
21
- return 'Blocked: requests to localhost and private networks are not allowed';
24
+ return { blocked: 'Blocked: requests to localhost and private networks are not allowed', addresses: [] };
22
25
  }
23
- try {
24
- const addresses = await dns.promises.lookup(hostname, { all: true });
25
- if (addresses.some((a) => isPrivateIp(a.address))) {
26
- return 'Blocked: hostname resolves to a private network address';
27
- }
26
+ // An operator who allow-listed this exact name did so knowing it points into
27
+ // a private range; re-checking the resolved address would only undo that.
28
+ if (isHostExplicitlyAllowed(hostname))
29
+ return { blocked: null, addresses: [] };
30
+ const addresses = await resolveLookupAddresses(hostname);
31
+ if (addresses.length > 0 && addresses.some((a) => isPrivateIp(a.address))) {
32
+ return { blocked: 'Blocked: hostname resolves to a private network address', addresses: [] };
28
33
  }
29
- catch {
30
- // DNS failure will surface as a normal fetch error below; don't block here.
31
- }
32
- return null;
34
+ // A DNS failure is left to the request itself to report — and pins to the
35
+ // (empty) address list, so plain fetch surfaces the usual error.
36
+ return { blocked: null, addresses };
33
37
  }
34
38
  let _limits = DEFAULT_LIMITS;
35
39
  export function setWebToolLimits(limits) { _limits = limits; }
@@ -84,20 +88,23 @@ export const webFetchTool = tool({
84
88
  // SSRF checks (a 3xx to a private host would otherwise bypass them).
85
89
  let currentUrl = parsed;
86
90
  let response;
91
+ const requestInit = {
92
+ signal: controller.signal,
93
+ redirect: 'manual',
94
+ headers: {
95
+ 'User-Agent': `Mozilla/5.0 (compatible; ${USER_AGENT})`,
96
+ 'Accept': 'text/html,application/json,text/plain;q=0.9,*/*;q=0.8',
97
+ },
98
+ };
87
99
  try {
88
100
  let hops = 0;
89
101
  while (true) {
90
- const blocked = await validateFetchTarget(currentUrl);
102
+ const { blocked, addresses } = await validateFetchTarget(currentUrl);
91
103
  if (blocked)
92
104
  return { error: blocked, url: currentUrl.href };
93
- response = await fetch(currentUrl.href, {
94
- signal: controller.signal,
95
- redirect: 'manual',
96
- headers: {
97
- 'User-Agent': `Mozilla/5.0 (compatible; ${USER_AGENT})`,
98
- 'Accept': 'text/html,application/json,text/plain;q=0.9,*/*;q=0.8',
99
- },
100
- });
105
+ // Pinned to the addresses the SSRF check just validated, so the
106
+ // connection cannot be re-pointed between the check and the dial.
107
+ response = await pinnedFetch(currentUrl, requestInit, addresses);
101
108
  if (response.status >= 300 && response.status < 400) {
102
109
  const location = response.headers.get('location');
103
110
  if (!location)
@@ -325,30 +332,86 @@ export const treeTool = tool({
325
332
  return { tree: lines.join('\n') };
326
333
  },
327
334
  });
335
+ // The tool advertises itself as the one to reach for on large files, so it is
336
+ // the last place that should read the whole file into a string first. Above
337
+ // `readFileMaxSize` it streams and stops at the requested end line; below it,
338
+ // the whole-file read stays (it is cheaper and gives an exact totalLines).
339
+ // Either way the span itself is capped — `endLine: 10_000_000` used to return
340
+ // every line in the file and blow the context window open.
341
+ const MAX_LINE_SPAN = 2000;
342
+ const MAX_LINE_CHARS = 4000;
343
+ async function streamLineRange(safePath, start, end) {
344
+ const { createReadStream } = await import('node:fs');
345
+ const readline = await import('node:readline');
346
+ const rl = readline.createInterface({
347
+ input: createReadStream(safePath, { encoding: 'utf-8' }),
348
+ crlfDelay: Infinity,
349
+ });
350
+ const lines = [];
351
+ let n = 0;
352
+ let reachedEof = true;
353
+ try {
354
+ for await (const line of rl) {
355
+ n++;
356
+ if (n < start)
357
+ continue;
358
+ lines.push({ lineNumber: n, text: line.slice(0, MAX_LINE_CHARS) });
359
+ if (n >= end) {
360
+ reachedEof = false;
361
+ break;
362
+ }
363
+ }
364
+ }
365
+ finally {
366
+ rl.close();
367
+ }
368
+ return { lines, reachedEof, scanned: n };
369
+ }
328
370
  export const readFileLinesTool = tool({
329
371
  name: 'read_file_lines',
330
372
  description: 'Read specific line range from a file. Useful for large files. Returns lines with line numbers.',
331
373
  inputSchema: z.object({
332
374
  filePath: z.string().max(500).describe('Relative path to the file'),
333
375
  startLine: z.number().min(1).describe('First line to read (1-based)'),
334
- endLine: z.number().min(1).optional().describe('Last line to read (default: startLine + 100)'),
376
+ endLine: z.number().min(1).optional().describe(`Last line to read (default: startLine + 100, at most ${MAX_LINE_SPAN} lines per call)`),
335
377
  }),
336
378
  execute: async ({ filePath, startLine, endLine }) => {
337
379
  try {
338
380
  const safePath = sandboxPath(filePath);
339
- const content = await fs.readFile(safePath, 'utf-8');
340
- const allLines = content.split('\n');
341
381
  const start = Math.max(1, startLine);
342
382
  if (endLine !== undefined && endLine < start) {
343
383
  return { error: `endLine (${endLine}) must be >= startLine (${start})` };
344
384
  }
385
+ const requestedEnd = endLine ?? (start + 100);
386
+ const cappedEnd = Math.min(requestedEnd, start + MAX_LINE_SPAN - 1);
387
+ const spanCapped = cappedEnd < requestedEnd;
388
+ const stat = await fs.stat(safePath);
389
+ if (stat.isDirectory())
390
+ return { error: `${filePath} is a directory` };
391
+ if (stat.size > _limits.readFileMaxSize) {
392
+ const { lines, reachedEof, scanned } = await streamLineRange(safePath, start, cappedEnd);
393
+ if (lines.length === 0) {
394
+ return { error: `startLine (${start}) exceeds file length (${scanned} lines)`, totalLines: scanned };
395
+ }
396
+ const last = lines[lines.length - 1].lineNumber;
397
+ return {
398
+ filePath,
399
+ startLine: start,
400
+ endLine: last,
401
+ ...(reachedEof ? { totalLines: scanned } : { hint: `Showing lines ${start}-${last}; file exceeds ${_limits.readFileMaxSize} bytes so the total line count was not counted` }),
402
+ ...(spanCapped ? { truncated: `Capped at ${MAX_LINE_SPAN} lines per call; continue from line ${last + 1}` } : {}),
403
+ lines,
404
+ };
405
+ }
406
+ const content = await fs.readFile(safePath, 'utf-8');
407
+ const allLines = content.split('\n');
345
408
  if (start > allLines.length) {
346
409
  return { error: `startLine (${start}) exceeds file length (${allLines.length} lines)`, totalLines: allLines.length };
347
410
  }
348
- const end = Math.min(endLine ?? (startLine + 100), allLines.length);
411
+ const end = Math.min(cappedEnd, allLines.length);
349
412
  const selectedLines = allLines.slice(start - 1, end).map((line, i) => ({
350
413
  lineNumber: start + i,
351
- text: line,
414
+ text: line.slice(0, MAX_LINE_CHARS),
352
415
  }));
353
416
  return {
354
417
  filePath,
@@ -356,6 +419,7 @@ export const readFileLinesTool = tool({
356
419
  startLine: start,
357
420
  endLine: end,
358
421
  ...(end < allLines.length ? { hint: `Showing lines ${start}-${end} of ${allLines.length}` } : {}),
422
+ ...(spanCapped ? { truncated: `Capped at ${MAX_LINE_SPAN} lines per call; continue from line ${end + 1}` } : {}),
359
423
  lines: selectedLines,
360
424
  };
361
425
  }