@dexto/tools-filesystem 1.8.12 → 1.9.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/dist/directory-approval.cjs +53 -48
- package/dist/directory-approval.d.ts +3 -5
- package/dist/directory-approval.d.ts.map +1 -1
- package/dist/directory-approval.integration.test.cjs +125 -103
- package/dist/directory-approval.integration.test.js +125 -104
- package/dist/directory-approval.js +51 -48
- package/dist/filesystem-service.cjs +16 -13
- package/dist/filesystem-service.d.ts +2 -1
- package/dist/filesystem-service.d.ts.map +1 -1
- package/dist/filesystem-service.js +16 -13
- package/dist/index.d.cts +109 -108
- package/dist/path-validator.cjs +10 -10
- package/dist/path-validator.d.ts +4 -3
- package/dist/path-validator.d.ts.map +1 -1
- package/dist/path-validator.js +10 -10
- package/dist/path-validator.test.cjs +30 -16
- package/dist/path-validator.test.js +30 -16
- package/dist/tool-factory.cjs +7 -1
- package/dist/tool-factory.d.ts.map +1 -1
- package/dist/tool-factory.js +7 -1
- package/package.json +3 -3
|
@@ -29,73 +29,78 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
var directory_approval_exports = {};
|
|
30
30
|
__export(directory_approval_exports, {
|
|
31
31
|
createDirectoryAccessApprovalHandlers: () => createDirectoryAccessApprovalHandlers,
|
|
32
|
+
createDirectoryApprovalKey: () => createDirectoryApprovalKey,
|
|
33
|
+
isPathApprovedByDirectoryKey: () => isPathApprovedByDirectoryKey,
|
|
32
34
|
resolveFilePath: () => resolveFilePath
|
|
33
35
|
});
|
|
34
36
|
module.exports = __toCommonJS(directory_approval_exports);
|
|
35
37
|
var path = __toESM(require("node:path"), 1);
|
|
36
|
-
var import_approval = require("@dexto/core/approval");
|
|
37
38
|
var import_tools = require("@dexto/core/tools");
|
|
38
39
|
var import_path_utils = require("./path-utils.js");
|
|
40
|
+
const DIRECTORY_APPROVAL_KEY_PREFIX = "directory:";
|
|
41
|
+
const DIRECTORY_APPROVAL_OPERATION_SEPARATOR = ":operation:";
|
|
42
|
+
function createDirectoryApprovalKey(directoryPath, operation) {
|
|
43
|
+
const directoryKey = `${DIRECTORY_APPROVAL_KEY_PREFIX}${path.resolve(directoryPath)}`;
|
|
44
|
+
return operation === void 0 ? directoryKey : `${directoryKey}${DIRECTORY_APPROVAL_OPERATION_SEPARATOR}${operation}`;
|
|
45
|
+
}
|
|
46
|
+
function isPathApprovedByDirectoryKey(filePath, approvedKeys, approvedTypes = /* @__PURE__ */ new Set(["session", "once"]), operation) {
|
|
47
|
+
const resolvedFilePath = path.resolve(filePath);
|
|
48
|
+
for (const [key, type] of approvedKeys) {
|
|
49
|
+
if (!key.startsWith(DIRECTORY_APPROVAL_KEY_PREFIX)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!approvedTypes.has(type)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const keyParts = key.slice(DIRECTORY_APPROVAL_KEY_PREFIX.length).split(DIRECTORY_APPROVAL_OPERATION_SEPARATOR);
|
|
56
|
+
const approvedDir = keyParts[0];
|
|
57
|
+
const approvedOperation = keyParts[1];
|
|
58
|
+
if (approvedDir === void 0) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (approvedOperation !== void 0 && approvedOperation !== operation) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const relative = path.relative(approvedDir, resolvedFilePath);
|
|
65
|
+
if (!relative.startsWith("..") && !path.isAbsolute(relative)) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
39
71
|
function resolveFilePath(workingDirectory, filePath) {
|
|
40
72
|
const resolvedPath = (0, import_path_utils.resolveUserPath)(workingDirectory, filePath);
|
|
41
73
|
return { path: resolvedPath, parentDir: path.dirname(resolvedPath) };
|
|
42
74
|
}
|
|
43
75
|
function createDirectoryAccessApprovalHandlers(options) {
|
|
44
76
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (!approvalManager) {
|
|
57
|
-
throw import_tools.ToolError.configInvalid(
|
|
58
|
-
`${options.toolName} requires ToolExecutionContext.services.approval`
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
if (approvalManager.isDirectorySessionApproved(paths.path, context.sessionId)) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
type: import_approval.ApprovalType.DIRECTORY_ACCESS,
|
|
66
|
-
metadata: {
|
|
67
|
-
path: paths.path,
|
|
68
|
-
parentDir: paths.parentDir,
|
|
69
|
-
operation: options.operation,
|
|
70
|
-
toolName: options.toolName
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
async onGranted(response, context, approvalRequest) {
|
|
75
|
-
const approvalManager = context.services?.approval;
|
|
76
|
-
if (!approvalManager) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (response.status !== import_approval.ApprovalStatus.APPROVED) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const data = response.data;
|
|
83
|
-
const rememberDirectory = data?.rememberDirectory ?? false;
|
|
84
|
-
const metadata = approvalRequest.metadata;
|
|
85
|
-
if (!metadata?.parentDir) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
await approvalManager.addApprovedDirectory(
|
|
89
|
-
metadata.parentDir,
|
|
90
|
-
rememberDirectory ? "session" : "once",
|
|
91
|
-
context.sessionId
|
|
77
|
+
async needsApproval(input, context) {
|
|
78
|
+
const resolvedFileSystemService = await options.getFileSystemService(context);
|
|
79
|
+
const paths = options.resolvePaths(input, resolvedFileSystemService);
|
|
80
|
+
const isAllowed = await resolvedFileSystemService.isPathWithinConfigAllowed(paths.path);
|
|
81
|
+
if (isAllowed) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const approvalManager = context.services?.approval;
|
|
85
|
+
if (!approvalManager) {
|
|
86
|
+
throw import_tools.ToolError.configInvalid(
|
|
87
|
+
`${options.toolName} requires ToolExecutionContext.services.approval`
|
|
92
88
|
);
|
|
93
89
|
}
|
|
90
|
+
const approvalKey = createDirectoryApprovalKey(paths.parentDir, options.operation);
|
|
91
|
+
return isPathApprovedByDirectoryKey(
|
|
92
|
+
paths.path,
|
|
93
|
+
approvalManager.getApprovedKeys(context.sessionId),
|
|
94
|
+
/* @__PURE__ */ new Set(["session"]),
|
|
95
|
+
options.operation
|
|
96
|
+
) ? false : approvalKey;
|
|
94
97
|
}
|
|
95
98
|
};
|
|
96
99
|
}
|
|
97
100
|
// Annotate the CommonJS export names for ESM import in node:
|
|
98
101
|
0 && (module.exports = {
|
|
99
102
|
createDirectoryAccessApprovalHandlers,
|
|
103
|
+
createDirectoryApprovalKey,
|
|
104
|
+
isPathApprovedByDirectoryKey,
|
|
100
105
|
resolveFilePath
|
|
101
106
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { z, ZodTypeAny } from 'zod';
|
|
2
|
-
import type { ApprovalRequestDetails, ApprovalResponse } from '@dexto/core/approval';
|
|
3
2
|
import type { ToolExecutionContext } from '@dexto/core/tools';
|
|
4
3
|
import type { FileSystemService } from './filesystem-service.js';
|
|
5
4
|
import type { FileSystemServiceGetter } from './file-tool-types.js';
|
|
@@ -8,6 +7,8 @@ type DirectoryApprovalPaths = {
|
|
|
8
7
|
path: string;
|
|
9
8
|
parentDir: string;
|
|
10
9
|
};
|
|
10
|
+
export declare function createDirectoryApprovalKey(directoryPath: string, operation?: DirectoryApprovalOperation): string;
|
|
11
|
+
export declare function isPathApprovedByDirectoryKey(filePath: string, approvedKeys: ReadonlyMap<string, 'session' | 'once'>, approvedTypes?: ReadonlySet<'session' | 'once'>, operation?: DirectoryApprovalOperation): boolean;
|
|
11
12
|
export declare function resolveFilePath(workingDirectory: string, filePath: string): DirectoryApprovalPaths;
|
|
12
13
|
export declare function createDirectoryAccessApprovalHandlers<const TSchema extends ZodTypeAny>(options: {
|
|
13
14
|
toolName: string;
|
|
@@ -16,10 +17,7 @@ export declare function createDirectoryAccessApprovalHandlers<const TSchema exte
|
|
|
16
17
|
getFileSystemService: FileSystemServiceGetter;
|
|
17
18
|
resolvePaths: (input: z.output<TSchema>, fileSystemService: FileSystemService) => DirectoryApprovalPaths;
|
|
18
19
|
}): {
|
|
19
|
-
|
|
20
|
-
override: (input: z.output<TSchema>, context: ToolExecutionContext) => Promise<ApprovalRequestDetails | null>;
|
|
21
|
-
onGranted: (response: ApprovalResponse, context: ToolExecutionContext, approvalRequest: ApprovalRequestDetails) => Promise<void>;
|
|
22
|
-
};
|
|
20
|
+
needsApproval: (input: z.output<TSchema>, context: ToolExecutionContext) => Promise<string | false>;
|
|
23
21
|
};
|
|
24
22
|
export {};
|
|
25
23
|
//# sourceMappingURL=directory-approval.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"directory-approval.d.ts","sourceRoot":"","sources":["../src/directory-approval.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEzC,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"directory-approval.d.ts","sourceRoot":"","sources":["../src/directory-approval.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEzC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAGpE,KAAK,0BAA0B,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAE5D,KAAK,sBAAsB,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACrB,CAAC;AAKF,wBAAgB,0BAA0B,CACtC,aAAa,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,0BAA0B,GACvC,MAAM,CAKR;AAED,wBAAgB,4BAA4B,CACxC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC,EACrD,aAAa,GAAE,WAAW,CAAC,SAAS,GAAG,MAAM,CAAgC,EAC7E,SAAS,CAAC,EAAE,0BAA0B,GACvC,OAAO,CA4BT;AAED,wBAAgB,eAAe,CAC3B,gBAAgB,EAAE,MAAM,EACxB,QAAQ,EAAE,MAAM,GACjB,sBAAsB,CAGxB;AAED,wBAAgB,qCAAqC,CAAC,KAAK,CAAC,OAAO,SAAS,UAAU,EAAE,OAAO,EAAE;IAC7F,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,0BAA0B,CAAC;IACtC,WAAW,EAAE,OAAO,CAAC;IACrB,oBAAoB,EAAE,uBAAuB,CAAC;IAC9C,YAAY,EAAE,CACV,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EACxB,iBAAiB,EAAE,iBAAiB,KACnC,sBAAsB,CAAC;CAC/B,GAAG;IACA,aAAa,EAAE,CACX,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EACxB,OAAO,EAAE,oBAAoB,KAC5B,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC;CAChC,CAgCA"}
|
|
@@ -33,6 +33,7 @@ var import_read_file_tool = require("./read-file-tool.js");
|
|
|
33
33
|
var import_write_file_tool = require("./write-file-tool.js");
|
|
34
34
|
var import_edit_file_tool = require("./edit-file-tool.js");
|
|
35
35
|
var import_tool_factory = require("./tool-factory.js");
|
|
36
|
+
var import_directory_approval = require("./directory-approval.js");
|
|
36
37
|
const createMockLogger = () => {
|
|
37
38
|
const noopAsync = async () => void 0;
|
|
38
39
|
const logger = {
|
|
@@ -68,6 +69,12 @@ function createToolContext(logger, approval, sessionId, workspacePath) {
|
|
|
68
69
|
}
|
|
69
70
|
};
|
|
70
71
|
}
|
|
72
|
+
async function callNeedsApproval(needsApproval, input, context) {
|
|
73
|
+
if (typeof needsApproval !== "function") {
|
|
74
|
+
throw new Error("Expected callable needsApproval");
|
|
75
|
+
}
|
|
76
|
+
return needsApproval(input, context);
|
|
77
|
+
}
|
|
71
78
|
let tempWorkspaceRoot = process.cwd();
|
|
72
79
|
function createWorkspaceManager(workspaceRoot) {
|
|
73
80
|
return {
|
|
@@ -154,127 +161,118 @@ async function readWorkspaceFile(resolvedPath, filePath) {
|
|
|
154
161
|
} catch {
|
|
155
162
|
}
|
|
156
163
|
});
|
|
157
|
-
(0, import_vitest.describe)("
|
|
158
|
-
(0, import_vitest.it)("should return
|
|
164
|
+
(0, import_vitest.describe)("needsApproval", () => {
|
|
165
|
+
(0, import_vitest.it)("should return false for paths within config-allowed roots", async () => {
|
|
159
166
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
160
|
-
const
|
|
161
|
-
(0, import_vitest.expect)(
|
|
167
|
+
const needsApproval = tool.needsApproval;
|
|
168
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
162
169
|
const testFile = path.join(tempDir, "test.txt");
|
|
163
170
|
await fs.writeFile(testFile, "test content");
|
|
164
|
-
const
|
|
171
|
+
const decision = await callNeedsApproval(
|
|
172
|
+
needsApproval,
|
|
165
173
|
tool.inputSchema.parse({ file_path: testFile }),
|
|
166
174
|
toolContext
|
|
167
175
|
);
|
|
168
|
-
(0, import_vitest.expect)(
|
|
176
|
+
(0, import_vitest.expect)(decision).toBe(false);
|
|
169
177
|
});
|
|
170
|
-
(0, import_vitest.it)("should return directory
|
|
178
|
+
(0, import_vitest.it)("should return a directory approval key for external paths", async () => {
|
|
171
179
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
172
|
-
const
|
|
173
|
-
(0, import_vitest.expect)(
|
|
180
|
+
const needsApproval = tool.needsApproval;
|
|
181
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
174
182
|
const externalPath = "/external/project/file.ts";
|
|
175
|
-
const
|
|
183
|
+
const decision = await callNeedsApproval(
|
|
184
|
+
needsApproval,
|
|
176
185
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
177
186
|
toolContext
|
|
178
187
|
);
|
|
179
|
-
(0, import_vitest.expect)(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
metadata: {
|
|
183
|
-
path: path.resolve(externalPath),
|
|
184
|
-
parentDir: path.dirname(path.resolve(externalPath)),
|
|
185
|
-
operation: "read",
|
|
186
|
-
toolName: "read_file"
|
|
187
|
-
}
|
|
188
|
-
});
|
|
188
|
+
(0, import_vitest.expect)(decision).toBe(
|
|
189
|
+
(0, import_directory_approval.createDirectoryApprovalKey)(path.dirname(path.resolve(externalPath)), "read")
|
|
190
|
+
);
|
|
189
191
|
});
|
|
190
|
-
(0, import_vitest.it)("should return
|
|
191
|
-
await approvalManager.
|
|
192
|
+
(0, import_vitest.it)("should return false when external path is session-approved", async () => {
|
|
193
|
+
await approvalManager.addApprovedKey(
|
|
194
|
+
(0, import_directory_approval.createDirectoryApprovalKey)("/external/project"),
|
|
195
|
+
"session"
|
|
196
|
+
);
|
|
192
197
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
193
|
-
const
|
|
194
|
-
(0, import_vitest.expect)(
|
|
198
|
+
const needsApproval = tool.needsApproval;
|
|
199
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
195
200
|
const externalPath = "/external/project/file.ts";
|
|
196
|
-
const
|
|
201
|
+
const decision = await callNeedsApproval(
|
|
202
|
+
needsApproval,
|
|
197
203
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
198
204
|
toolContext
|
|
199
205
|
);
|
|
200
|
-
(0, import_vitest.expect)(
|
|
206
|
+
(0, import_vitest.expect)(decision).toBe(false);
|
|
201
207
|
});
|
|
202
|
-
(0, import_vitest.it)("should still return
|
|
203
|
-
await approvalManager.
|
|
208
|
+
(0, import_vitest.it)("should still return a key when external path is once-approved (prompt again)", async () => {
|
|
209
|
+
await approvalManager.addApprovedKey(
|
|
210
|
+
(0, import_directory_approval.createDirectoryApprovalKey)("/external/project"),
|
|
211
|
+
"once"
|
|
212
|
+
);
|
|
204
213
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
205
|
-
const
|
|
206
|
-
(0, import_vitest.expect)(
|
|
214
|
+
const needsApproval = tool.needsApproval;
|
|
215
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
207
216
|
const externalPath = "/external/project/file.ts";
|
|
208
|
-
const
|
|
217
|
+
const decision = await callNeedsApproval(
|
|
218
|
+
needsApproval,
|
|
209
219
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
210
220
|
toolContext
|
|
211
221
|
);
|
|
212
|
-
(0, import_vitest.expect)(
|
|
222
|
+
(0, import_vitest.expect)(decision).toBe((0, import_directory_approval.createDirectoryApprovalKey)("/external/project", "read"));
|
|
213
223
|
});
|
|
214
224
|
(0, import_vitest.it)("should remember directory approvals only for the granting session", async () => {
|
|
215
225
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
(0, import_vitest.expect)(overrideFn).toBeDefined();
|
|
219
|
-
(0, import_vitest.expect)(onGrantedFn).toBeDefined();
|
|
226
|
+
const needsApproval = tool.needsApproval;
|
|
227
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
220
228
|
const externalPath = "/external/project/file.ts";
|
|
221
229
|
const sessionAContext = createToolContext(mockLogger, approvalManager, "session-a");
|
|
222
230
|
const sessionBContext = createToolContext(mockLogger, approvalManager, "session-b");
|
|
223
|
-
const
|
|
231
|
+
const approvalKey = await callNeedsApproval(
|
|
232
|
+
needsApproval,
|
|
224
233
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
225
234
|
sessionAContext
|
|
226
235
|
);
|
|
227
|
-
(0, import_vitest.expect)(
|
|
228
|
-
await
|
|
229
|
-
{
|
|
230
|
-
approvalId: "approval-1",
|
|
231
|
-
status: import_core.ApprovalStatus.APPROVED,
|
|
232
|
-
data: { rememberDirectory: true }
|
|
233
|
-
},
|
|
234
|
-
sessionAContext,
|
|
235
|
-
approvalRequest
|
|
236
|
-
);
|
|
236
|
+
(0, import_vitest.expect)(approvalKey).toBe((0, import_directory_approval.createDirectoryApprovalKey)("/external/project", "read"));
|
|
237
|
+
await approvalManager.addApprovedKey(approvalKey, "session", "session-a");
|
|
237
238
|
(0, import_vitest.expect)(
|
|
238
|
-
await
|
|
239
|
+
await callNeedsApproval(
|
|
240
|
+
needsApproval,
|
|
239
241
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
240
242
|
sessionAContext
|
|
241
243
|
)
|
|
242
|
-
).
|
|
244
|
+
).toBe(false);
|
|
243
245
|
(0, import_vitest.expect)(
|
|
244
|
-
await
|
|
246
|
+
await callNeedsApproval(
|
|
247
|
+
needsApproval,
|
|
245
248
|
tool.inputSchema.parse({ file_path: externalPath }),
|
|
246
249
|
sessionBContext
|
|
247
250
|
)
|
|
248
|
-
).
|
|
251
|
+
).toBe((0, import_directory_approval.createDirectoryApprovalKey)("/external/project", "read"));
|
|
249
252
|
});
|
|
250
253
|
});
|
|
251
254
|
(0, import_vitest.describe)("Different tool operations", () => {
|
|
252
|
-
(0, import_vitest.it)("should
|
|
255
|
+
(0, import_vitest.it)("should return a directory key for write operations", async () => {
|
|
253
256
|
const tool = (0, import_write_file_tool.createWriteFileTool)(getFileSystemService);
|
|
254
|
-
const
|
|
255
|
-
(0, import_vitest.expect)(
|
|
257
|
+
const needsApproval = tool.needsApproval;
|
|
258
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
256
259
|
const externalPath = "/external/project/new.ts";
|
|
257
|
-
const
|
|
260
|
+
const decision = await callNeedsApproval(
|
|
261
|
+
needsApproval,
|
|
258
262
|
tool.inputSchema.parse({ file_path: externalPath, content: "test" }),
|
|
259
263
|
toolContext
|
|
260
264
|
);
|
|
261
|
-
(0, import_vitest.expect)(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
metadata: {
|
|
265
|
-
path: path.resolve(externalPath),
|
|
266
|
-
parentDir: path.dirname(path.resolve(externalPath)),
|
|
267
|
-
operation: "write",
|
|
268
|
-
toolName: "write_file"
|
|
269
|
-
}
|
|
270
|
-
});
|
|
265
|
+
(0, import_vitest.expect)(decision).toBe(
|
|
266
|
+
(0, import_directory_approval.createDirectoryApprovalKey)(path.dirname(path.resolve(externalPath)), "write")
|
|
267
|
+
);
|
|
271
268
|
});
|
|
272
|
-
(0, import_vitest.it)("should
|
|
269
|
+
(0, import_vitest.it)("should return a directory key for edit operations", async () => {
|
|
273
270
|
const tool = (0, import_edit_file_tool.createEditFileTool)(getFileSystemService);
|
|
274
|
-
const
|
|
275
|
-
(0, import_vitest.expect)(
|
|
271
|
+
const needsApproval = tool.needsApproval;
|
|
272
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
276
273
|
const externalPath = "/external/project/existing.ts";
|
|
277
|
-
const
|
|
274
|
+
const decision = await callNeedsApproval(
|
|
275
|
+
needsApproval,
|
|
278
276
|
tool.inputSchema.parse({
|
|
279
277
|
file_path: externalPath,
|
|
280
278
|
old_string: "old",
|
|
@@ -282,50 +280,72 @@ async function readWorkspaceFile(resolvedPath, filePath) {
|
|
|
282
280
|
}),
|
|
283
281
|
toolContext
|
|
284
282
|
);
|
|
285
|
-
(0, import_vitest.expect)(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
metadata: {
|
|
289
|
-
path: path.resolve(externalPath),
|
|
290
|
-
parentDir: path.dirname(path.resolve(externalPath)),
|
|
291
|
-
operation: "edit",
|
|
292
|
-
toolName: "edit_file"
|
|
293
|
-
}
|
|
294
|
-
});
|
|
283
|
+
(0, import_vitest.expect)(decision).toBe(
|
|
284
|
+
(0, import_directory_approval.createDirectoryApprovalKey)(path.dirname(path.resolve(externalPath)), "edit")
|
|
285
|
+
);
|
|
295
286
|
});
|
|
296
287
|
});
|
|
297
288
|
(0, import_vitest.describe)("Path containment scenarios", () => {
|
|
298
289
|
(0, import_vitest.it)("should cover child paths when parent directory is session-approved", async () => {
|
|
299
290
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
300
|
-
const
|
|
301
|
-
(0, import_vitest.expect)(
|
|
302
|
-
await approvalManager.
|
|
303
|
-
|
|
291
|
+
const needsApproval = tool.needsApproval;
|
|
292
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
293
|
+
await approvalManager.addApprovedKey(
|
|
294
|
+
(0, import_directory_approval.createDirectoryApprovalKey)("/external/project"),
|
|
295
|
+
"session"
|
|
296
|
+
);
|
|
297
|
+
const decision1 = await callNeedsApproval(
|
|
298
|
+
needsApproval,
|
|
304
299
|
tool.inputSchema.parse({ file_path: "/external/project/file.ts" }),
|
|
305
300
|
toolContext
|
|
306
301
|
);
|
|
307
|
-
(0, import_vitest.expect)(
|
|
308
|
-
const
|
|
302
|
+
(0, import_vitest.expect)(decision1).toBe(false);
|
|
303
|
+
const decision2 = await callNeedsApproval(
|
|
304
|
+
needsApproval,
|
|
309
305
|
tool.inputSchema.parse({ file_path: "/external/project/deep/nested/file.ts" }),
|
|
310
306
|
toolContext
|
|
311
307
|
);
|
|
312
|
-
(0, import_vitest.expect)(
|
|
308
|
+
(0, import_vitest.expect)(decision2).toBe(false);
|
|
313
309
|
});
|
|
314
310
|
(0, import_vitest.it)("should NOT cover sibling directories", async () => {
|
|
315
311
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
316
|
-
const
|
|
317
|
-
(0, import_vitest.expect)(
|
|
318
|
-
await approvalManager.
|
|
319
|
-
|
|
312
|
+
const needsApproval = tool.needsApproval;
|
|
313
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
314
|
+
await approvalManager.addApprovedKey(
|
|
315
|
+
(0, import_directory_approval.createDirectoryApprovalKey)("/external/sub"),
|
|
316
|
+
"session"
|
|
317
|
+
);
|
|
318
|
+
const decision1 = await callNeedsApproval(
|
|
319
|
+
needsApproval,
|
|
320
320
|
tool.inputSchema.parse({ file_path: "/external/sub/file.ts" }),
|
|
321
321
|
toolContext
|
|
322
322
|
);
|
|
323
|
-
(0, import_vitest.expect)(
|
|
324
|
-
const
|
|
323
|
+
(0, import_vitest.expect)(decision1).toBe(false);
|
|
324
|
+
const decision2 = await callNeedsApproval(
|
|
325
|
+
needsApproval,
|
|
325
326
|
tool.inputSchema.parse({ file_path: "/external/other/file.ts" }),
|
|
326
327
|
toolContext
|
|
327
328
|
);
|
|
328
|
-
(0, import_vitest.expect)(
|
|
329
|
+
(0, import_vitest.expect)(decision2).toBe((0, import_directory_approval.createDirectoryApprovalKey)("/external/other", "read"));
|
|
330
|
+
});
|
|
331
|
+
(0, import_vitest.it)("should not use a read directory approval for write operations", async () => {
|
|
332
|
+
await approvalManager.addApprovedKey(
|
|
333
|
+
(0, import_directory_approval.createDirectoryApprovalKey)("/external/project", "read"),
|
|
334
|
+
"session"
|
|
335
|
+
);
|
|
336
|
+
const writeTool = (0, import_write_file_tool.createWriteFileTool)(getFileSystemService);
|
|
337
|
+
const needsApproval = writeTool.needsApproval;
|
|
338
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
339
|
+
await (0, import_vitest.expect)(
|
|
340
|
+
callNeedsApproval(
|
|
341
|
+
needsApproval,
|
|
342
|
+
writeTool.inputSchema.parse({
|
|
343
|
+
file_path: "/external/project/new.ts",
|
|
344
|
+
content: "test"
|
|
345
|
+
}),
|
|
346
|
+
toolContext
|
|
347
|
+
)
|
|
348
|
+
).resolves.toBe((0, import_directory_approval.createDirectoryApprovalKey)("/external/project", "write"));
|
|
329
349
|
});
|
|
330
350
|
});
|
|
331
351
|
(0, import_vitest.describe)("Execution approval scoping", () => {
|
|
@@ -345,7 +365,11 @@ async function readWorkspaceFile(resolvedPath, filePath) {
|
|
|
345
365
|
const externalDir = await fs.mkdtemp(path.join(os.tmpdir(), "dexto-fs-external-"));
|
|
346
366
|
try {
|
|
347
367
|
const externalFile = path.join(externalDir, "approved.txt");
|
|
348
|
-
await approvalManager.
|
|
368
|
+
await approvalManager.addApprovedKey(
|
|
369
|
+
(0, import_directory_approval.createDirectoryApprovalKey)(externalDir, "write"),
|
|
370
|
+
"once",
|
|
371
|
+
"session-a"
|
|
372
|
+
);
|
|
349
373
|
await (0, import_vitest.expect)(
|
|
350
374
|
writeTool.execute(
|
|
351
375
|
writeTool.inputSchema.parse({
|
|
@@ -368,15 +392,12 @@ async function readWorkspaceFile(resolvedPath, filePath) {
|
|
|
368
392
|
content: "should fail"
|
|
369
393
|
});
|
|
370
394
|
await (0, import_vitest.expect)(
|
|
371
|
-
|
|
395
|
+
callNeedsApproval(
|
|
396
|
+
writeTool.needsApproval,
|
|
372
397
|
blockedInput,
|
|
373
398
|
createToolContext(mockLogger, approvalManager, "session-b")
|
|
374
399
|
)
|
|
375
|
-
).resolves.
|
|
376
|
-
import_vitest.expect.objectContaining({
|
|
377
|
-
type: "directory_access"
|
|
378
|
-
})
|
|
379
|
-
);
|
|
400
|
+
).resolves.toBe((0, import_directory_approval.createDirectoryApprovalKey)(externalDir, "write"));
|
|
380
401
|
} finally {
|
|
381
402
|
await fs.rm(externalDir, { recursive: true, force: true });
|
|
382
403
|
}
|
|
@@ -511,11 +532,12 @@ async function readWorkspaceFile(resolvedPath, filePath) {
|
|
|
511
532
|
(0, import_vitest.describe)("Without ApprovalManager in context", () => {
|
|
512
533
|
(0, import_vitest.it)("should throw for external paths", async () => {
|
|
513
534
|
const tool = (0, import_read_file_tool.createReadFileTool)(getFileSystemService);
|
|
514
|
-
const
|
|
515
|
-
(0, import_vitest.expect)(
|
|
535
|
+
const needsApproval = tool.needsApproval;
|
|
536
|
+
(0, import_vitest.expect)(needsApproval).toBeDefined();
|
|
516
537
|
const contextWithoutApprovalManager = { logger: mockLogger };
|
|
517
538
|
await (0, import_vitest.expect)(
|
|
518
|
-
|
|
539
|
+
callNeedsApproval(
|
|
540
|
+
needsApproval,
|
|
519
541
|
tool.inputSchema.parse({ file_path: "/external/project/file.ts" }),
|
|
520
542
|
contextWithoutApprovalManager
|
|
521
543
|
)
|