File size: 5,318 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { type SandboxPermissions } from '../../services/sandboxManager.js';
import { normalizeCommand } from '../../utils/shell-utils.js';

const NETWORK_RELIANT_TOOLS = new Set([
  'npm',
  'npx',
  'yarn',
  'pnpm',
  'bun',
  'git',
  'ssh',
  'scp',
  'sftp',
  'curl',
  'wget',
]);

const NODE_ECOSYSTEM_TOOLS = new Set(['npm', 'npx', 'yarn', 'pnpm', 'bun']);

const NETWORK_HEAVY_SUBCOMMANDS = new Set([
  'install',
  'i',
  'ci',
  'update',
  'up',
  'publish',
  'add',
  'remove',
  'outdated',
  'audit',
]);

/**
 * Returns true if the command or subcommand is known to be network-reliant.
 */
export function isNetworkReliantCommand(
  commandName: string,
  subCommand?: string,
): boolean {
  const normalizedCommand = normalizeCommand(commandName);
  if (!NETWORK_RELIANT_TOOLS.has(normalizedCommand)) {
    return false;
  }

  // Node ecosystem tools only need network for specific subcommands
  if (NODE_ECOSYSTEM_TOOLS.has(normalizedCommand)) {
    // Bare yarn/bun/pnpm is an alias for install
    if (
      !subCommand &&
      (normalizedCommand === 'yarn' ||
        normalizedCommand === 'bun' ||
        normalizedCommand === 'pnpm')
    ) {
      return true;
    }

    return (
      !!subCommand && NETWORK_HEAVY_SUBCOMMANDS.has(subCommand.toLowerCase())
    );
  }

  // Other tools (ssh, git, curl, etc.) are always network-reliant
  return true;
}

/**
 * Returns suggested additional permissions for network-reliant tools
 * based on common configuration and cache directories.
 */
/**
 * Returns suggested additional permissions for network-reliant tools
 * based on common configuration and cache directories.
 */
export async function getProactiveToolSuggestions(
  commandName: string,
): Promise<SandboxPermissions | undefined> {
  const normalizedCommand = normalizeCommand(commandName);
  if (!NETWORK_RELIANT_TOOLS.has(normalizedCommand)) {
    return undefined;
  }

  const home = os.homedir();
  const readOnlyPaths: string[] = [];
  const primaryCachePaths: string[] = [];
  const optionalCachePaths: string[] = [];

  if (normalizedCommand === 'npm' || normalizedCommand === 'npx') {
    readOnlyPaths.push(path.join(home, '.npmrc'));
    primaryCachePaths.push(path.join(home, '.npm'));
    optionalCachePaths.push(path.join(home, '.node-gyp'));
    optionalCachePaths.push(path.join(home, '.cache'));
  } else if (normalizedCommand === 'yarn') {
    readOnlyPaths.push(path.join(home, '.yarnrc'));
    readOnlyPaths.push(path.join(home, '.yarnrc.yml'));
    primaryCachePaths.push(path.join(home, '.yarn'));
    primaryCachePaths.push(path.join(home, '.config', 'yarn'));
    optionalCachePaths.push(path.join(home, '.cache'));
  } else if (normalizedCommand === 'pnpm') {
    readOnlyPaths.push(path.join(home, '.npmrc'));
    primaryCachePaths.push(path.join(home, '.pnpm-store'));
    primaryCachePaths.push(path.join(home, '.config', 'pnpm'));
    optionalCachePaths.push(path.join(home, '.cache'));
  } else if (normalizedCommand === 'bun') {
    readOnlyPaths.push(path.join(home, '.bunfig.toml'));
    primaryCachePaths.push(path.join(home, '.bun'));
    optionalCachePaths.push(path.join(home, '.cache'));
  } else if (normalizedCommand === 'git') {
    readOnlyPaths.push(path.join(home, '.ssh'));
    readOnlyPaths.push(path.join(home, '.gitconfig'));
    optionalCachePaths.push(path.join(home, '.cache'));
  } else if (
    normalizedCommand === 'ssh' ||
    normalizedCommand === 'scp' ||
    normalizedCommand === 'sftp'
  ) {
    readOnlyPaths.push(path.join(home, '.ssh'));
  }

  // Windows specific paths
  if (os.platform() === 'win32') {
    const appData = process.env['AppData'];
    const localAppData = process.env['LocalAppData'];
    if (normalizedCommand === 'npm' || normalizedCommand === 'npx') {
      if (appData) {
        primaryCachePaths.push(path.join(appData, 'npm'));
        optionalCachePaths.push(path.join(appData, 'npm-cache'));
      }
      if (localAppData) {
        optionalCachePaths.push(path.join(localAppData, 'npm-cache'));
      }
    }
  }

  const finalReadOnly: string[] = [];
  const finalReadWrite: string[] = [];

  const checkExists = async (p: string): Promise<boolean> => {
    try {
      await fs.promises.access(p, fs.constants.F_OK);
      return true;
    } catch {
      return false;
    }
  };

  const readOnlyChecks = await Promise.all(
    readOnlyPaths.map(async (p) => ({ path: p, exists: await checkExists(p) })),
  );
  for (const { path: p, exists } of readOnlyChecks) {
    if (exists) {
      finalReadOnly.push(p);
    }
  }

  for (const p of primaryCachePaths) {
    finalReadWrite.push(p);
  }

  const optionalChecks = await Promise.all(
    optionalCachePaths.map(async (p) => ({
      path: p,
      exists: await checkExists(p),
    })),
  );
  for (const { path: p, exists } of optionalChecks) {
    if (exists) {
      finalReadWrite.push(p);
    }
  }

  return {
    fileSystem:
      finalReadOnly.length > 0 || finalReadWrite.length > 0
        ? {
            read: [...finalReadOnly, ...finalReadWrite],
            write: finalReadWrite,
          }
        : undefined,
    network: true,
  };
}