mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Update tools to allow reading any VFS path, writing to /tmp
This commit is contained in:
17 files changed
+356
-60
No files matched your search
@@ -134,7 +134,21 @@ describe('TextEditorStrReplaceTool', () => {
|
||||
path: '/absolute/path/test.ts',
|
||||
old_str: 'old',
|
||||
new_str: 'new'
|
||||
})).rejects.toThrow('❌ Absolute paths are not supported');
|
||||
})).rejects.toThrow('Write access denied');
|
||||
});
|
||||
|
||||
it('should allow writes to /tmp/ directory', async () => {
|
||||
// First create the file in /tmp/
|
||||
await mockFS.writeFile('/tmp/temp-file.txt', 'original content');
|
||||
|
||||
const result = await tool.execute({
|
||||
path: '/tmp/temp-file.txt',
|
||||
old_str: 'original',
|
||||
new_str: 'modified'
|
||||
});
|
||||
|
||||
expect(result).toContain('String successfully replaced');
|
||||
expect(mockFS.getFileContent('/tmp/temp-file.txt')).toBe('modified content');
|
||||
});
|
||||
|
||||
it('should handle whitespace normalization', async () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
|
||||
import type { Tool } from "./Tool";
|
||||
import type { JSRuntimeFS } from "../JSRuntime";
|
||||
import { isAbsolutePath, createWriteAccessDeniedError } from "../security";
|
||||
|
||||
interface TextEditorStrReplaceParams {
|
||||
path: string;
|
||||
@@ -31,12 +32,27 @@ export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams
|
||||
this.cwd = cwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if write operations are allowed for the given absolute path
|
||||
*/
|
||||
private isWriteAllowed(absolutePath: string): boolean {
|
||||
// Allow writes to /tmp/ and its subdirectories
|
||||
if (absolutePath.startsWith('/tmp/') || absolutePath === '/tmp') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// For other absolute paths, deny write access
|
||||
return false;
|
||||
}
|
||||
|
||||
async execute(args: TextEditorStrReplaceParams): Promise<string> {
|
||||
const { path, old_str, new_str, normalize_whitespace = true } = args;
|
||||
|
||||
// Check for absolute paths and provide helpful error
|
||||
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
|
||||
throw new Error(`❌ Absolute paths are not supported.\n\nThe path "${path}" appears to be an absolute path.\n\n💡 Please use a relative path instead. Examples:\n- "src/index.ts" (relative to current directory)\n- "./src/index.ts" (explicit relative path)\n- "../other-project/src/index.ts" (relative to parent directory)\n\nCurrent working directory: ${this.cwd}`);
|
||||
// Check for absolute paths and validate write permissions
|
||||
if (isAbsolutePath(path)) {
|
||||
if (!this.isWriteAllowed(path)) {
|
||||
throw new Error(createWriteAccessDeniedError(path, undefined, this.cwd));
|
||||
}
|
||||
}
|
||||
|
||||
// Ban editing dependencies in package.json
|
||||
@@ -60,7 +76,14 @@ export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams
|
||||
}
|
||||
|
||||
try {
|
||||
const absolutePath = join(this.cwd, path);
|
||||
// Handle both absolute and relative paths
|
||||
let absolutePath: string;
|
||||
if (isAbsolutePath(path)) {
|
||||
absolutePath = path;
|
||||
} else {
|
||||
absolutePath = join(this.cwd, path);
|
||||
}
|
||||
|
||||
const content = await this.fs.readFile(absolutePath, "utf8");
|
||||
|
||||
// Helper function to normalize whitespace
|
||||
|
||||
@@ -124,11 +124,22 @@ describe('TextEditorWriteTool', () => {
|
||||
expect(mockFS.getFileContent('/project/src/test.ts')).toBe('console.log("test");');
|
||||
});
|
||||
|
||||
it('should reject absolute paths with helpful error message', async () => {
|
||||
it('should reject absolute paths outside allowed directories', async () => {
|
||||
await expect(tool.execute({
|
||||
path: '/absolute/path/test.ts',
|
||||
file_text: 'test content'
|
||||
})).rejects.toThrow('❌ Absolute paths are not supported');
|
||||
})).rejects.toThrow('Write access denied');
|
||||
});
|
||||
|
||||
it('should allow writes to /tmp/ directory', async () => {
|
||||
const result = await tool.execute({
|
||||
path: '/tmp/temp-file.txt',
|
||||
file_text: 'temporary content'
|
||||
});
|
||||
|
||||
expect(result).toContain('File successfully written to /tmp/temp-file.txt');
|
||||
expect(mockFS.hasFile('/tmp/temp-file.txt')).toBe(true);
|
||||
expect(mockFS.getFileContent('/tmp/temp-file.txt')).toBe('temporary content');
|
||||
});
|
||||
|
||||
it('should reject package.json writes', async () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
|
||||
import type { Tool } from "./Tool";
|
||||
import type { JSRuntimeFS } from "../JSRuntime";
|
||||
import { isAbsolutePath, createWriteAccessDeniedError } from "../security";
|
||||
|
||||
interface TextEditorWriteParams {
|
||||
path: string;
|
||||
@@ -27,12 +28,27 @@ export class TextEditorWriteTool implements Tool<TextEditorWriteParams> {
|
||||
this.cwd = cwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if write operations are allowed for the given absolute path
|
||||
*/
|
||||
private isWriteAllowed(absolutePath: string): boolean {
|
||||
// Allow writes to /tmp/ and its subdirectories
|
||||
if (absolutePath.startsWith('/tmp/') || absolutePath === '/tmp') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// For other absolute paths, deny write access
|
||||
return false;
|
||||
}
|
||||
|
||||
async execute(args: TextEditorWriteParams): Promise<string> {
|
||||
const { path, file_text } = args;
|
||||
|
||||
// Check for absolute paths and provide helpful error
|
||||
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
|
||||
throw new Error(`❌ Absolute paths are not supported.\n\nThe path "${path}" appears to be an absolute path.\n\n💡 Please use a relative path instead. Examples:\n- "src/index.ts" (relative to current directory)\n- "./src/index.ts" (explicit relative path)\n- "../other-project/src/index.ts" (relative to parent directory)\n\nCurrent working directory: ${this.cwd}`);
|
||||
// Check for absolute paths and validate write permissions
|
||||
if (isAbsolutePath(path)) {
|
||||
if (!this.isWriteAllowed(path)) {
|
||||
throw new Error(createWriteAccessDeniedError(path, undefined, this.cwd));
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -43,7 +59,14 @@ export class TextEditorWriteTool implements Tool<TextEditorWriteParams> {
|
||||
}
|
||||
|
||||
try {
|
||||
const absolutePath = join(this.cwd, path);
|
||||
// Handle both absolute and relative paths
|
||||
let absolutePath: string;
|
||||
if (isAbsolutePath(path)) {
|
||||
absolutePath = path;
|
||||
} else {
|
||||
absolutePath = join(this.cwd, path);
|
||||
}
|
||||
|
||||
await this.fs.mkdir(dirname(absolutePath), { recursive: true });
|
||||
await this.fs.writeFile(absolutePath, file_text, "utf8");
|
||||
return `File successfully written to ${path}`;
|
||||
|
||||
Reference in new issue
Block a user