@dexto/tools-filesystem 1.8.12 → 1.9.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,7 +4,6 @@ import * as fs from "node:fs/promises";
4
4
  import * as os from "node:os";
5
5
  import {
6
6
  ApprovalManager,
7
- ApprovalStatus,
8
7
  DextoRuntimeError
9
8
  } from "@dexto/core";
10
9
  import { InMemoryDextoStores } from "@dexto/core/storage";
@@ -14,6 +13,7 @@ import { createReadFileTool } from "./read-file-tool.js";
14
13
  import { createWriteFileTool } from "./write-file-tool.js";
15
14
  import { createEditFileTool } from "./edit-file-tool.js";
16
15
  import { fileSystemToolsFactory } from "./tool-factory.js";
16
+ import { createDirectoryApprovalKey } from "./directory-approval.js";
17
17
  const createMockLogger = () => {
18
18
  const noopAsync = async () => void 0;
19
19
  const logger = {
@@ -49,6 +49,12 @@ function createToolContext(logger, approval, sessionId, workspacePath) {
49
49
  }
50
50
  };
51
51
  }
52
+ async function callNeedsApproval(needsApproval, input, context) {
53
+ if (typeof needsApproval !== "function") {
54
+ throw new Error("Expected callable needsApproval");
55
+ }
56
+ return needsApproval(input, context);
57
+ }
52
58
  let tempWorkspaceRoot = process.cwd();
53
59
  function createWorkspaceManager(workspaceRoot) {
54
60
  return {
@@ -135,127 +141,118 @@ describe("Directory Approval Integration Tests", () => {
135
141
  } catch {
136
142
  }
137
143
  });
138
- describe("getApprovalOverride", () => {
139
- it("should return null for paths within config-allowed roots", async () => {
144
+ describe("needsApproval", () => {
145
+ it("should return false for paths within config-allowed roots", async () => {
140
146
  const tool = createReadFileTool(getFileSystemService);
141
- const overrideFn = tool.approval?.override;
142
- expect(overrideFn).toBeDefined();
147
+ const needsApproval = tool.needsApproval;
148
+ expect(needsApproval).toBeDefined();
143
149
  const testFile = path.join(tempDir, "test.txt");
144
150
  await fs.writeFile(testFile, "test content");
145
- const metadata = await overrideFn(
151
+ const decision = await callNeedsApproval(
152
+ needsApproval,
146
153
  tool.inputSchema.parse({ file_path: testFile }),
147
154
  toolContext
148
155
  );
149
- expect(metadata).toBeNull();
156
+ expect(decision).toBe(false);
150
157
  });
151
- it("should return directory access metadata for external paths", async () => {
158
+ it("should return a directory approval key for external paths", async () => {
152
159
  const tool = createReadFileTool(getFileSystemService);
153
- const overrideFn = tool.approval?.override;
154
- expect(overrideFn).toBeDefined();
160
+ const needsApproval = tool.needsApproval;
161
+ expect(needsApproval).toBeDefined();
155
162
  const externalPath = "/external/project/file.ts";
156
- const metadata = await overrideFn(
163
+ const decision = await callNeedsApproval(
164
+ needsApproval,
157
165
  tool.inputSchema.parse({ file_path: externalPath }),
158
166
  toolContext
159
167
  );
160
- expect(metadata).not.toBeNull();
161
- expect(metadata).toMatchObject({
162
- type: "directory_access",
163
- metadata: {
164
- path: path.resolve(externalPath),
165
- parentDir: path.dirname(path.resolve(externalPath)),
166
- operation: "read",
167
- toolName: "read_file"
168
- }
169
- });
168
+ expect(decision).toBe(
169
+ createDirectoryApprovalKey(path.dirname(path.resolve(externalPath)), "read")
170
+ );
170
171
  });
171
- it("should return null when external path is session-approved", async () => {
172
- await approvalManager.addApprovedDirectory("/external/project", "session");
172
+ it("should return false when external path is session-approved", async () => {
173
+ await approvalManager.addApprovedKey(
174
+ createDirectoryApprovalKey("/external/project"),
175
+ "session"
176
+ );
173
177
  const tool = createReadFileTool(getFileSystemService);
174
- const overrideFn = tool.approval?.override;
175
- expect(overrideFn).toBeDefined();
178
+ const needsApproval = tool.needsApproval;
179
+ expect(needsApproval).toBeDefined();
176
180
  const externalPath = "/external/project/file.ts";
177
- const metadata = await overrideFn(
181
+ const decision = await callNeedsApproval(
182
+ needsApproval,
178
183
  tool.inputSchema.parse({ file_path: externalPath }),
179
184
  toolContext
180
185
  );
181
- expect(metadata).toBeNull();
186
+ expect(decision).toBe(false);
182
187
  });
183
- it("should still return metadata when external path is once-approved (prompt again)", async () => {
184
- await approvalManager.addApprovedDirectory("/external/project", "once");
188
+ it("should still return a key when external path is once-approved (prompt again)", async () => {
189
+ await approvalManager.addApprovedKey(
190
+ createDirectoryApprovalKey("/external/project"),
191
+ "once"
192
+ );
185
193
  const tool = createReadFileTool(getFileSystemService);
186
- const overrideFn = tool.approval?.override;
187
- expect(overrideFn).toBeDefined();
194
+ const needsApproval = tool.needsApproval;
195
+ expect(needsApproval).toBeDefined();
188
196
  const externalPath = "/external/project/file.ts";
189
- const metadata = await overrideFn(
197
+ const decision = await callNeedsApproval(
198
+ needsApproval,
190
199
  tool.inputSchema.parse({ file_path: externalPath }),
191
200
  toolContext
192
201
  );
193
- expect(metadata).not.toBeNull();
202
+ expect(decision).toBe(createDirectoryApprovalKey("/external/project", "read"));
194
203
  });
195
204
  it("should remember directory approvals only for the granting session", async () => {
196
205
  const tool = createReadFileTool(getFileSystemService);
197
- const overrideFn = tool.approval?.override;
198
- const onGrantedFn = tool.approval?.onGranted;
199
- expect(overrideFn).toBeDefined();
200
- expect(onGrantedFn).toBeDefined();
206
+ const needsApproval = tool.needsApproval;
207
+ expect(needsApproval).toBeDefined();
201
208
  const externalPath = "/external/project/file.ts";
202
209
  const sessionAContext = createToolContext(mockLogger, approvalManager, "session-a");
203
210
  const sessionBContext = createToolContext(mockLogger, approvalManager, "session-b");
204
- const approvalRequest = await overrideFn(
211
+ const approvalKey = await callNeedsApproval(
212
+ needsApproval,
205
213
  tool.inputSchema.parse({ file_path: externalPath }),
206
214
  sessionAContext
207
215
  );
208
- expect(approvalRequest).not.toBeNull();
209
- await onGrantedFn(
210
- {
211
- approvalId: "approval-1",
212
- status: ApprovalStatus.APPROVED,
213
- data: { rememberDirectory: true }
214
- },
215
- sessionAContext,
216
- approvalRequest
217
- );
216
+ expect(approvalKey).toBe(createDirectoryApprovalKey("/external/project", "read"));
217
+ await approvalManager.addApprovedKey(approvalKey, "session", "session-a");
218
218
  expect(
219
- await overrideFn(
219
+ await callNeedsApproval(
220
+ needsApproval,
220
221
  tool.inputSchema.parse({ file_path: externalPath }),
221
222
  sessionAContext
222
223
  )
223
- ).toBeNull();
224
+ ).toBe(false);
224
225
  expect(
225
- await overrideFn(
226
+ await callNeedsApproval(
227
+ needsApproval,
226
228
  tool.inputSchema.parse({ file_path: externalPath }),
227
229
  sessionBContext
228
230
  )
229
- ).not.toBeNull();
231
+ ).toBe(createDirectoryApprovalKey("/external/project", "read"));
230
232
  });
231
233
  });
232
234
  describe("Different tool operations", () => {
233
- it("should label write operations correctly", async () => {
235
+ it("should return a directory key for write operations", async () => {
234
236
  const tool = createWriteFileTool(getFileSystemService);
235
- const overrideFn = tool.approval?.override;
236
- expect(overrideFn).toBeDefined();
237
+ const needsApproval = tool.needsApproval;
238
+ expect(needsApproval).toBeDefined();
237
239
  const externalPath = "/external/project/new.ts";
238
- const metadata = await overrideFn(
240
+ const decision = await callNeedsApproval(
241
+ needsApproval,
239
242
  tool.inputSchema.parse({ file_path: externalPath, content: "test" }),
240
243
  toolContext
241
244
  );
242
- expect(metadata).not.toBeNull();
243
- expect(metadata).toMatchObject({
244
- type: "directory_access",
245
- metadata: {
246
- path: path.resolve(externalPath),
247
- parentDir: path.dirname(path.resolve(externalPath)),
248
- operation: "write",
249
- toolName: "write_file"
250
- }
251
- });
245
+ expect(decision).toBe(
246
+ createDirectoryApprovalKey(path.dirname(path.resolve(externalPath)), "write")
247
+ );
252
248
  });
253
- it("should label edit operations correctly", async () => {
249
+ it("should return a directory key for edit operations", async () => {
254
250
  const tool = createEditFileTool(getFileSystemService);
255
- const overrideFn = tool.approval?.override;
256
- expect(overrideFn).toBeDefined();
251
+ const needsApproval = tool.needsApproval;
252
+ expect(needsApproval).toBeDefined();
257
253
  const externalPath = "/external/project/existing.ts";
258
- const metadata = await overrideFn(
254
+ const decision = await callNeedsApproval(
255
+ needsApproval,
259
256
  tool.inputSchema.parse({
260
257
  file_path: externalPath,
261
258
  old_string: "old",
@@ -263,50 +260,72 @@ describe("Directory Approval Integration Tests", () => {
263
260
  }),
264
261
  toolContext
265
262
  );
266
- expect(metadata).not.toBeNull();
267
- expect(metadata).toMatchObject({
268
- type: "directory_access",
269
- metadata: {
270
- path: path.resolve(externalPath),
271
- parentDir: path.dirname(path.resolve(externalPath)),
272
- operation: "edit",
273
- toolName: "edit_file"
274
- }
275
- });
263
+ expect(decision).toBe(
264
+ createDirectoryApprovalKey(path.dirname(path.resolve(externalPath)), "edit")
265
+ );
276
266
  });
277
267
  });
278
268
  describe("Path containment scenarios", () => {
279
269
  it("should cover child paths when parent directory is session-approved", async () => {
280
270
  const tool = createReadFileTool(getFileSystemService);
281
- const overrideFn = tool.approval?.override;
282
- expect(overrideFn).toBeDefined();
283
- await approvalManager.addApprovedDirectory("/external/project", "session");
284
- const metadata1 = await overrideFn(
271
+ const needsApproval = tool.needsApproval;
272
+ expect(needsApproval).toBeDefined();
273
+ await approvalManager.addApprovedKey(
274
+ createDirectoryApprovalKey("/external/project"),
275
+ "session"
276
+ );
277
+ const decision1 = await callNeedsApproval(
278
+ needsApproval,
285
279
  tool.inputSchema.parse({ file_path: "/external/project/file.ts" }),
286
280
  toolContext
287
281
  );
288
- expect(metadata1).toBeNull();
289
- const metadata2 = await overrideFn(
282
+ expect(decision1).toBe(false);
283
+ const decision2 = await callNeedsApproval(
284
+ needsApproval,
290
285
  tool.inputSchema.parse({ file_path: "/external/project/deep/nested/file.ts" }),
291
286
  toolContext
292
287
  );
293
- expect(metadata2).toBeNull();
288
+ expect(decision2).toBe(false);
294
289
  });
295
290
  it("should NOT cover sibling directories", async () => {
296
291
  const tool = createReadFileTool(getFileSystemService);
297
- const overrideFn = tool.approval?.override;
298
- expect(overrideFn).toBeDefined();
299
- await approvalManager.addApprovedDirectory("/external/sub", "session");
300
- const metadata1 = await overrideFn(
292
+ const needsApproval = tool.needsApproval;
293
+ expect(needsApproval).toBeDefined();
294
+ await approvalManager.addApprovedKey(
295
+ createDirectoryApprovalKey("/external/sub"),
296
+ "session"
297
+ );
298
+ const decision1 = await callNeedsApproval(
299
+ needsApproval,
301
300
  tool.inputSchema.parse({ file_path: "/external/sub/file.ts" }),
302
301
  toolContext
303
302
  );
304
- expect(metadata1).toBeNull();
305
- const metadata2 = await overrideFn(
303
+ expect(decision1).toBe(false);
304
+ const decision2 = await callNeedsApproval(
305
+ needsApproval,
306
306
  tool.inputSchema.parse({ file_path: "/external/other/file.ts" }),
307
307
  toolContext
308
308
  );
309
- expect(metadata2).not.toBeNull();
309
+ expect(decision2).toBe(createDirectoryApprovalKey("/external/other", "read"));
310
+ });
311
+ it("should not use a read directory approval for write operations", async () => {
312
+ await approvalManager.addApprovedKey(
313
+ createDirectoryApprovalKey("/external/project", "read"),
314
+ "session"
315
+ );
316
+ const writeTool = createWriteFileTool(getFileSystemService);
317
+ const needsApproval = writeTool.needsApproval;
318
+ expect(needsApproval).toBeDefined();
319
+ await expect(
320
+ callNeedsApproval(
321
+ needsApproval,
322
+ writeTool.inputSchema.parse({
323
+ file_path: "/external/project/new.ts",
324
+ content: "test"
325
+ }),
326
+ toolContext
327
+ )
328
+ ).resolves.toBe(createDirectoryApprovalKey("/external/project", "write"));
310
329
  });
311
330
  });
312
331
  describe("Execution approval scoping", () => {
@@ -326,7 +345,11 @@ describe("Directory Approval Integration Tests", () => {
326
345
  const externalDir = await fs.mkdtemp(path.join(os.tmpdir(), "dexto-fs-external-"));
327
346
  try {
328
347
  const externalFile = path.join(externalDir, "approved.txt");
329
- await approvalManager.addApprovedDirectory(externalDir, "once", "session-a");
348
+ await approvalManager.addApprovedKey(
349
+ createDirectoryApprovalKey(externalDir, "write"),
350
+ "once",
351
+ "session-a"
352
+ );
330
353
  await expect(
331
354
  writeTool.execute(
332
355
  writeTool.inputSchema.parse({
@@ -349,15 +372,12 @@ describe("Directory Approval Integration Tests", () => {
349
372
  content: "should fail"
350
373
  });
351
374
  await expect(
352
- writeTool.approval.override(
375
+ callNeedsApproval(
376
+ writeTool.needsApproval,
353
377
  blockedInput,
354
378
  createToolContext(mockLogger, approvalManager, "session-b")
355
379
  )
356
- ).resolves.toEqual(
357
- expect.objectContaining({
358
- type: "directory_access"
359
- })
360
- );
380
+ ).resolves.toBe(createDirectoryApprovalKey(externalDir, "write"));
361
381
  } finally {
362
382
  await fs.rm(externalDir, { recursive: true, force: true });
363
383
  }
@@ -492,11 +512,12 @@ describe("Directory Approval Integration Tests", () => {
492
512
  describe("Without ApprovalManager in context", () => {
493
513
  it("should throw for external paths", async () => {
494
514
  const tool = createReadFileTool(getFileSystemService);
495
- const overrideFn = tool.approval?.override;
496
- expect(overrideFn).toBeDefined();
515
+ const needsApproval = tool.needsApproval;
516
+ expect(needsApproval).toBeDefined();
497
517
  const contextWithoutApprovalManager = { logger: mockLogger };
498
518
  await expect(
499
- overrideFn(
519
+ callNeedsApproval(
520
+ needsApproval,
500
521
  tool.inputSchema.parse({ file_path: "/external/project/file.ts" }),
501
522
  contextWithoutApprovalManager
502
523
  )
@@ -1,66 +1,69 @@
1
1
  import * as path from "node:path";
2
- import { ApprovalStatus, ApprovalType } from "@dexto/core/approval";
3
2
  import { ToolError } from "@dexto/core/tools";
4
3
  import { resolveUserPath } from "./path-utils.js";
4
+ const DIRECTORY_APPROVAL_KEY_PREFIX = "directory:";
5
+ const DIRECTORY_APPROVAL_OPERATION_SEPARATOR = ":operation:";
6
+ function createDirectoryApprovalKey(directoryPath, operation) {
7
+ const directoryKey = `${DIRECTORY_APPROVAL_KEY_PREFIX}${path.resolve(directoryPath)}`;
8
+ return operation === void 0 ? directoryKey : `${directoryKey}${DIRECTORY_APPROVAL_OPERATION_SEPARATOR}${operation}`;
9
+ }
10
+ function isPathApprovedByDirectoryKey(filePath, approvedKeys, approvedTypes = /* @__PURE__ */ new Set(["session", "once"]), operation) {
11
+ const resolvedFilePath = path.resolve(filePath);
12
+ for (const [key, type] of approvedKeys) {
13
+ if (!key.startsWith(DIRECTORY_APPROVAL_KEY_PREFIX)) {
14
+ continue;
15
+ }
16
+ if (!approvedTypes.has(type)) {
17
+ continue;
18
+ }
19
+ const keyParts = key.slice(DIRECTORY_APPROVAL_KEY_PREFIX.length).split(DIRECTORY_APPROVAL_OPERATION_SEPARATOR);
20
+ const approvedDir = keyParts[0];
21
+ const approvedOperation = keyParts[1];
22
+ if (approvedDir === void 0) {
23
+ continue;
24
+ }
25
+ if (approvedOperation !== void 0 && approvedOperation !== operation) {
26
+ continue;
27
+ }
28
+ const relative = path.relative(approvedDir, resolvedFilePath);
29
+ if (!relative.startsWith("..") && !path.isAbsolute(relative)) {
30
+ return true;
31
+ }
32
+ }
33
+ return false;
34
+ }
5
35
  function resolveFilePath(workingDirectory, filePath) {
6
36
  const resolvedPath = resolveUserPath(workingDirectory, filePath);
7
37
  return { path: resolvedPath, parentDir: path.dirname(resolvedPath) };
8
38
  }
9
39
  function createDirectoryAccessApprovalHandlers(options) {
10
40
  return {
11
- approval: {
12
- async override(input, context) {
13
- const resolvedFileSystemService = await options.getFileSystemService(context);
14
- const paths = options.resolvePaths(input, resolvedFileSystemService);
15
- const isAllowed = await resolvedFileSystemService.isPathWithinConfigAllowed(
16
- paths.path
17
- );
18
- if (isAllowed) {
19
- return null;
20
- }
21
- const approvalManager = context.services?.approval;
22
- if (!approvalManager) {
23
- throw ToolError.configInvalid(
24
- `${options.toolName} requires ToolExecutionContext.services.approval`
25
- );
26
- }
27
- if (approvalManager.isDirectorySessionApproved(paths.path, context.sessionId)) {
28
- return null;
29
- }
30
- return {
31
- type: ApprovalType.DIRECTORY_ACCESS,
32
- metadata: {
33
- path: paths.path,
34
- parentDir: paths.parentDir,
35
- operation: options.operation,
36
- toolName: options.toolName
37
- }
38
- };
39
- },
40
- async onGranted(response, context, approvalRequest) {
41
- const approvalManager = context.services?.approval;
42
- if (!approvalManager) {
43
- return;
44
- }
45
- if (response.status !== ApprovalStatus.APPROVED) {
46
- return;
47
- }
48
- const data = response.data;
49
- const rememberDirectory = data?.rememberDirectory ?? false;
50
- const metadata = approvalRequest.metadata;
51
- if (!metadata?.parentDir) {
52
- return;
53
- }
54
- await approvalManager.addApprovedDirectory(
55
- metadata.parentDir,
56
- rememberDirectory ? "session" : "once",
57
- context.sessionId
41
+ async needsApproval(input, context) {
42
+ const resolvedFileSystemService = await options.getFileSystemService(context);
43
+ const paths = options.resolvePaths(input, resolvedFileSystemService);
44
+ const isAllowed = await resolvedFileSystemService.isPathWithinConfigAllowed(paths.path);
45
+ if (isAllowed) {
46
+ return false;
47
+ }
48
+ const approvalManager = context.services?.approval;
49
+ if (!approvalManager) {
50
+ throw ToolError.configInvalid(
51
+ `${options.toolName} requires ToolExecutionContext.services.approval`
58
52
  );
59
53
  }
54
+ const approvalKey = createDirectoryApprovalKey(paths.parentDir, options.operation);
55
+ return isPathApprovedByDirectoryKey(
56
+ paths.path,
57
+ approvalManager.getApprovedKeys(context.sessionId),
58
+ /* @__PURE__ */ new Set(["session"]),
59
+ options.operation
60
+ ) ? false : approvalKey;
60
61
  }
61
62
  };
62
63
  }
63
64
  export {
64
65
  createDirectoryAccessApprovalHandlers,
66
+ createDirectoryApprovalKey,
67
+ isPathApprovedByDirectoryKey,
65
68
  resolveFilePath
66
69
  };
@@ -160,7 +160,7 @@ class FileSystemService {
160
160
  return this.pathValidator.isPathWithinAllowed(filePath);
161
161
  }
162
162
  async validateReadPath(filePath, mode) {
163
- const validation = mode === "toolPreview" ? await this.pathValidator.validatePathForPreview(filePath) : await this.pathValidator.validatePath(filePath);
163
+ const validation = mode === "toolPreview" ? await this.pathValidator.validatePathForPreview(filePath) : await this.pathValidator.validatePath(filePath, "read");
164
164
  if (!validation.isValid || !validation.normalizedPath) {
165
165
  throw import_errors2.FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
166
166
  }
@@ -332,7 +332,7 @@ class FileSystemService {
332
332
  });
333
333
  const validFiles = [];
334
334
  for (const file of files) {
335
- const validation = await this.pathValidator.validatePath(file);
335
+ const validation = await this.pathValidator.validatePath(file, "read");
336
336
  if (!validation.isValid || !validation.normalizedPath) {
337
337
  this.logger.debug(`Skipping invalid path: ${file}`);
338
338
  continue;
@@ -381,7 +381,7 @@ class FileSystemService {
381
381
  */
382
382
  async listDirectory(dirPath, options = {}) {
383
383
  await this.ensureInitialized();
384
- const validation = await this.pathValidator.validatePath(dirPath);
384
+ const validation = await this.pathValidator.validatePath(dirPath, "read");
385
385
  if (!validation.isValid || !validation.normalizedPath) {
386
386
  throw import_errors2.FileSystemError.invalidPath(dirPath, validation.error || "Unknown error");
387
387
  }
@@ -420,7 +420,10 @@ class FileSystemService {
420
420
  concurrency,
421
421
  async (entry) => {
422
422
  const entryPath = path.join(normalizedPath, entry.name);
423
- const entryValidation = await this.pathValidator.validatePath(entryPath);
423
+ const entryValidation = await this.pathValidator.validatePath(
424
+ entryPath,
425
+ "read"
426
+ );
424
427
  if (!entryValidation.isValid || !entryValidation.normalizedPath) {
425
428
  return null;
426
429
  }
@@ -531,7 +534,7 @@ class FileSystemService {
531
534
  */
532
535
  async createDirectory(dirPath, options = {}) {
533
536
  await this.ensureInitialized();
534
- const validation = await this.pathValidator.validatePath(dirPath);
537
+ const validation = await this.pathValidator.validatePath(dirPath, "write");
535
538
  if (!validation.isValid || !validation.normalizedPath) {
536
539
  throw import_errors2.FileSystemError.invalidPath(dirPath, validation.error || "Unknown error");
537
540
  }
@@ -566,7 +569,7 @@ class FileSystemService {
566
569
  */
567
570
  async deletePath(targetPath, options = {}) {
568
571
  await this.ensureInitialized();
569
- const validation = await this.pathValidator.validatePath(targetPath);
572
+ const validation = await this.pathValidator.validatePath(targetPath, "write");
570
573
  if (!validation.isValid || !validation.normalizedPath) {
571
574
  throw import_errors2.FileSystemError.invalidPath(targetPath, validation.error || "Unknown error");
572
575
  }
@@ -593,11 +596,11 @@ class FileSystemService {
593
596
  */
594
597
  async renamePath(fromPath, toPath) {
595
598
  await this.ensureInitialized();
596
- const fromValidation = await this.pathValidator.validatePath(fromPath);
599
+ const fromValidation = await this.pathValidator.validatePath(fromPath, "write");
597
600
  if (!fromValidation.isValid || !fromValidation.normalizedPath) {
598
601
  throw import_errors2.FileSystemError.invalidPath(fromPath, fromValidation.error || "Unknown error");
599
602
  }
600
- const toValidation = await this.pathValidator.validatePath(toPath);
603
+ const toValidation = await this.pathValidator.validatePath(toPath, "write");
601
604
  if (!toValidation.isValid || !toValidation.normalizedPath) {
602
605
  throw import_errors2.FileSystemError.invalidPath(toPath, toValidation.error || "Unknown error");
603
606
  }
@@ -652,7 +655,7 @@ class FileSystemService {
652
655
  const basePath = options.path || this.config.workingDirectory || process.cwd();
653
656
  let searchPath;
654
657
  let globPattern;
655
- const baseValidation = await this.pathValidator.validatePath(basePath);
658
+ const baseValidation = await this.pathValidator.validatePath(basePath, "read");
656
659
  if (!baseValidation.isValid || !baseValidation.normalizedPath) {
657
660
  throw import_errors2.FileSystemError.invalidPath(basePath, baseValidation.error || "Unknown error");
658
661
  }
@@ -754,7 +757,7 @@ class FileSystemService {
754
757
  */
755
758
  async writeFile(filePath, content, options = {}) {
756
759
  await this.ensureInitialized();
757
- const validation = await this.pathValidator.validatePath(filePath);
760
+ const validation = await this.pathValidator.validatePath(filePath, "write");
758
761
  if (!validation.isValid || !validation.normalizedPath) {
759
762
  throw import_errors2.FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
760
763
  }
@@ -794,12 +797,12 @@ class FileSystemService {
794
797
  */
795
798
  async editFile(filePath, operation, options = {}) {
796
799
  await this.ensureInitialized();
797
- const validation = await this.pathValidator.validatePath(filePath);
800
+ const validation = await this.pathValidator.validatePath(filePath, "edit");
798
801
  if (!validation.isValid || !validation.normalizedPath) {
799
802
  throw import_errors2.FileSystemError.invalidPath(filePath, validation.error || "Unknown error");
800
803
  }
801
804
  const normalizedPath = validation.normalizedPath;
802
- const fileContent = await this.readFile(normalizedPath);
805
+ const fileContent = await this.readNormalizedFile(normalizedPath);
803
806
  const originalContent = fileContent.content;
804
807
  const occurrences = (originalContent.match(
805
808
  new RegExp(operation.oldString.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g")
@@ -927,7 +930,7 @@ class FileSystemService {
927
930
  * Check if a path is allowed (async for non-blocking symlink resolution)
928
931
  */
929
932
  async isPathAllowed(filePath) {
930
- const validation = await this.pathValidator.validatePath(filePath);
933
+ const validation = await this.pathValidator.validatePath(filePath, "read");
931
934
  return validation.isValid;
932
935
  }
933
936
  }
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import { type Logger } from '@dexto/core/logger';
7
7
  import { FileSystemConfig, FileContent, MediaFileContent, ReadFileOptions, GlobOptions, GlobResult, GrepOptions, SearchResult, WriteFileOptions, WriteResult, EditFileOptions, EditResult, EditOperation, ListDirectoryOptions, ListDirectoryResult, CreateDirectoryOptions, CreateDirectoryResult, DeletePathOptions, DeletePathResult, RenamePathResult } from './types.js';
8
+ import { type DirectoryApprovalOperation } from './path-validator.js';
8
9
  /**
9
10
  * FileSystemService - Handles all file system operations with security checks
10
11
  *
@@ -60,7 +61,7 @@ export declare class FileSystemService {
60
61
  *
61
62
  * @param checker Function that returns true if path is in an approved directory
62
63
  */
63
- setDirectoryApprovalChecker(checker: (filePath: string) => boolean): void;
64
+ setDirectoryApprovalChecker(checker: (filePath: string, operation: DirectoryApprovalOperation) => boolean): void;
64
65
  /**
65
66
  * Update the working directory at runtime (e.g., when workspace changes).
66
67
  * Rebuilds the PathValidator so allowed/blocked path roots are recalculated.
@@ -1 +1 @@
1
- {"version":3,"file":"filesystem-service.d.ts","sourceRoot":"","sources":["../src/filesystem-service.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,EAAqB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEpE,OAAO,EACH,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,UAAU,EACV,WAAW,EACX,YAAY,EAEZ,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,UAAU,EACV,aAAa,EAGb,oBAAoB,EACpB,mBAAmB,EACnB,sBAAsB,EACtB,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAEnB,MAAM,YAAY,CAAC;AAYpB;;;;;;;;GAQG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