File size: 5,412 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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { spawn } from 'node:child_process';
import { type FileSystemService } from './fileSystemService.js';
import { type SandboxManager } from './sandboxManager.js';
import { debugLogger } from '../utils/debugLogger.js';
import { isNodeError } from '../utils/errors.js';
import { resolveToRealPath, isSubpath } from '../utils/paths.js';

/**
 * A FileSystemService implementation that performs operations through a sandbox.
 */
export class SandboxedFileSystemService implements FileSystemService {
  constructor(
    private sandboxManager: SandboxManager,
    private cwd: string,
  ) {}

  private sanitizeAndValidatePath(filePath: string): string {
    const resolvedPath = resolveToRealPath(filePath);
    const workspace = resolveToRealPath(this.sandboxManager.getWorkspace());

    if (isSubpath(workspace, resolvedPath) || workspace === resolvedPath) {
      return resolvedPath;
    }

    // Check if the path is explicitly allowed by the sandbox manager
    const options = this.sandboxManager.getOptions();
    const allowedPaths = options?.includeDirectories ?? [];

    for (const allowed of allowedPaths) {
      const resolvedAllowed = resolveToRealPath(allowed);
      if (
        isSubpath(resolvedAllowed, resolvedPath) ||
        resolvedAllowed === resolvedPath
      ) {
        return resolvedPath;
      }
    }

    throw new Error(
      `Access denied: Path '${filePath}' is outside the workspace and not in allowed paths.`,
    );
  }

  async readTextFile(filePath: string): Promise<string> {
    const safePath = this.sanitizeAndValidatePath(filePath);
    const prepared = await this.sandboxManager.prepareCommand({
      command: '__read',
      args: [safePath],
      cwd: this.cwd,
      env: process.env,
      policy: {
        allowedPaths: [safePath],
      },
    });

    try {
      return await new Promise((resolve, reject) => {
        // Direct spawn is necessary here for streaming large file contents.

        const child = spawn(prepared.program, prepared.args, {
          cwd: this.cwd,
          env: prepared.env,
        });

        let output = '';
        let error = '';

        child.stdout?.on('data', (data) => {
          output += data.toString();
        });

        child.stderr?.on('data', (data) => {
          error += data.toString();
        });

        child.on('close', (code) => {
          if (code === 0) {
            resolve(output);
          } else {
            const isEnoent =
              error.toLowerCase().includes('no such file or directory') ||
              error.toLowerCase().includes('enoent') ||
              error.toLowerCase().includes('could not find file') ||
              error.toLowerCase().includes('could not find a part of the path');
            const err = new Error(
              `Sandbox Error: read_file failed for '${filePath}'. Exit code ${code}. ${error ? 'Details: ' + error : ''}`,
            );
            if (isEnoent) {
              Object.assign(err, { code: 'ENOENT' });
            }
            reject(err);
          }
        });

        child.on('error', (err) => {
          reject(
            new Error(
              `Sandbox Error: Failed to spawn read_file for '${filePath}': ${err.message}`,
            ),
          );
        });
      });
    } finally {
      prepared.cleanup?.();
    }
  }

  async writeTextFile(filePath: string, content: string): Promise<void> {
    const safePath = this.sanitizeAndValidatePath(filePath);
    const prepared = await this.sandboxManager.prepareCommand({
      command: '__write',
      args: [safePath],
      cwd: this.cwd,
      env: process.env,
      policy: {
        allowedPaths: [safePath],
        additionalPermissions: {
          fileSystem: {
            write: [safePath],
          },
        },
      },
    });

    try {
      return await new Promise((resolve, reject) => {
        // Direct spawn is necessary here for streaming large file contents.

        const child = spawn(prepared.program, prepared.args, {
          cwd: this.cwd,
          env: prepared.env,
        });

        child.stdin?.on('error', (err) => {
          // Silently ignore EPIPE errors on stdin, they will be caught by the process error/close listeners
          if (isNodeError(err) && err.code === 'EPIPE') {
            return;
          }
          debugLogger.error(
            `Sandbox Error: stdin error for '${filePath}': ${
              err instanceof Error ? err.message : String(err)
            }`,
          );
        });

        child.stdin?.write(content);
        child.stdin?.end();

        let error = '';
        child.stderr?.on('data', (data) => {
          error += data.toString();
        });

        child.on('close', (code) => {
          if (code === 0) {
            resolve();
          } else {
            reject(
              new Error(
                `Sandbox Error: write_file failed for '${filePath}'. Exit code ${code}. ${error ? 'Details: ' + error : ''}`,
              ),
            );
          }
        });

        child.on('error', (err) => {
          reject(
            new Error(
              `Sandbox Error: Failed to spawn write_file for '${filePath}': ${err.message}`,
            ),
          );
        });
      });
    } finally {
      prepared.cleanup?.();
    }
  }
}