Update tools to allow reading any VFS path, writing to /tmp

This commit is contained in:
Alex Gleason committed 2025-09-14 12:31:23 -05:00
1 parent f23b92c79f
commit be8309b503
17 files changed
+356 -60

No files matched your search

+3 -3
View File
@@ -118,11 +118,11 @@ describe('CpCommand', () => {
expect(result.stderr).toContain('No such file or directory');
});
it('should reject absolute paths', async () => {
const result = await cpCommand.execute(['/absolute/source', 'dest'], testCwd);
it('should reject absolute destination paths outside allowed directories', async () => {
const result = await cpCommand.execute(['src/test.txt', '/absolute/dest'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.stderr).toContain('write access denied');
});
it('should return error when missing operands', async () => {
+20 -7
View File
@@ -2,6 +2,7 @@ import { join, dirname, basename } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Implementation of the 'cp' command
@@ -33,14 +34,20 @@ export class CpCommand implements ShellCommand {
const sources = paths.slice(0, -1);
const destination = paths[paths.length - 1];
// Check for absolute paths
for (const path of [...sources, destination]) {
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${path}`);
}
// Check write permissions for destination (sources can be read from anywhere)
try {
validateWritePath(destination, this.name);
} catch (error) {
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
}
const destAbsolutePath = join(cwd, destination);
// Handle both absolute and relative paths for destination
let destAbsolutePath: string;
if (isAbsolutePath(destination)) {
destAbsolutePath = destination;
} else {
destAbsolutePath = join(cwd, destination);
}
// Check if destination exists and is a directory
let destIsDir = false;
@@ -58,7 +65,13 @@ export class CpCommand implements ShellCommand {
for (const source of sources) {
try {
const sourceAbsolutePath = join(cwd, source);
// Handle both absolute and relative paths for source
let sourceAbsolutePath: string;
if (isAbsolutePath(source)) {
sourceAbsolutePath = source;
} else {
sourceAbsolutePath = join(cwd, source);
}
const sourceStats = await this.fs.stat(sourceAbsolutePath);
let targetPath: string;
+10 -5
View File
@@ -2,6 +2,7 @@ import { join } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Parsed curl options
@@ -364,12 +365,16 @@ export class CurlCommand implements ShellCommand {
*/
private async writeToFile(filename: string, content: string, cwd: string): Promise<void> {
try {
// Handle absolute paths
if (filename.startsWith('/') || filename.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(filename)) {
throw new Error('absolute paths are not supported');
}
// Validate write permissions for absolute paths
validateWritePath(filename, 'curl');
const absolutePath = join(cwd, filename);
// Handle both absolute and relative paths
let absolutePath: string;
if (isAbsolutePath(filename)) {
absolutePath = filename;
} else {
absolutePath = join(cwd, filename);
}
await this.fs.writeFile(absolutePath, content, 'utf8');
} catch (error) {
+10 -10
View File
@@ -39,56 +39,56 @@ describe('MkdirCommand', () => {
it('should create new directory', async () => {
const result = await command.execute(['newdir'], '/project');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('');
});
it('should handle existing directory error', async () => {
const result = await command.execute(['existing'], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('File exists');
});
it('should handle existing file error', async () => {
const result = await command.execute(['file.txt'], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('File exists');
});
it('should create parent directories with -p', async () => {
const result = await command.execute(['-p', 'deep/nested'], '/project');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('');
});
it('should handle missing parent directory without -p', async () => {
const result = await command.execute(['nonexistent/newdir'], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('No such file or directory');
});
it('should require directory operand', async () => {
const result = await command.execute([], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('missing operand');
});
it('should reject absolute paths', async () => {
it('should reject absolute paths outside allowed directories', async () => {
const result = await command.execute(['/absolute/path'], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.stderr).toContain('write access denied');
});
it('should handle multiple directories', async () => {
const result = await command.execute(['newdir', 'deep'], '/project');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('');
});
+13 -4
View File
@@ -2,6 +2,7 @@ import { join, dirname } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Implementation of the 'mkdir' command
@@ -32,12 +33,20 @@ export class MkdirCommand implements ShellCommand {
try {
for (const dirPath of directories) {
try {
// Handle absolute paths
if (dirPath.startsWith('/') || dirPath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(dirPath)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${dirPath}`);
// Validate write permissions for absolute paths
try {
validateWritePath(dirPath, this.name);
} catch (error) {
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
}
const absolutePath = join(cwd, dirPath);
// Handle both absolute and relative paths
let absolutePath: string;
if (isAbsolutePath(dirPath)) {
absolutePath = dirPath;
} else {
absolutePath = join(cwd, dirPath);
}
if (options.parents) {
// Create parent directories as needed (-p option)
+2 -2
View File
@@ -104,11 +104,11 @@ describe('MvCommand', () => {
expect(result.stderr).toContain('File exists');
});
it('should reject absolute paths', async () => {
it('should reject absolute paths outside allowed directories', async () => {
const result = await mvCommand.execute(['/absolute/source', 'dest'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.stderr).toContain('write access denied');
});
it('should return error when missing operands', async () => {
+22 -6
View File
@@ -2,6 +2,7 @@ import { join, dirname, basename } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Implementation of the 'mv' command
@@ -27,14 +28,23 @@ export class MvCommand implements ShellCommand {
const sources = args.slice(0, -1);
const destination = args[args.length - 1];
// Check for absolute paths
for (const path of [...sources, destination]) {
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${path}`);
// Check write permissions for destination and sources (since mv deletes the source)
const pathsToValidate = [destination, ...sources];
for (const path of pathsToValidate) {
try {
validateWritePath(path, this.name);
} catch (error) {
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
}
}
const destAbsolutePath = join(cwd, destination);
// Handle both absolute and relative paths for destination
let destAbsolutePath: string;
if (isAbsolutePath(destination)) {
destAbsolutePath = destination;
} else {
destAbsolutePath = join(cwd, destination);
}
// Check if destination exists and is a directory
let destIsDir = false;
@@ -52,7 +62,13 @@ export class MvCommand implements ShellCommand {
for (const source of sources) {
try {
const sourceAbsolutePath = join(cwd, source);
// Handle both absolute and relative paths for source
let sourceAbsolutePath: string;
if (isAbsolutePath(source)) {
sourceAbsolutePath = source;
} else {
sourceAbsolutePath = join(cwd, source);
}
// Check if source exists
await this.fs.stat(sourceAbsolutePath);
+2 -2
View File
@@ -89,11 +89,11 @@ describe('RmCommand', () => {
expect(result.exitCode).toBe(0);
});
it('should reject absolute paths', async () => {
it('should reject absolute paths outside allowed directories', async () => {
const result = await rmCommand.execute(['/absolute/path'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.stderr).toContain('write access denied');
});
it('should reject removing current or parent directory', async () => {
+15 -4
View File
@@ -2,6 +2,7 @@ import { join } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Implementation of the 'rm' command
@@ -32,9 +33,19 @@ export class RmCommand implements ShellCommand {
for (const path of paths) {
try {
// Handle absolute paths
if (path.startsWith('/') || path.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(path)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${path}`);
// Validate write permissions for absolute paths
try {
validateWritePath(path, this.name);
} catch (error) {
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
}
// Handle both absolute and relative paths
let absolutePath: string;
if (isAbsolutePath(path)) {
absolutePath = path;
} else {
absolutePath = join(cwd, path);
}
// Prevent removing current directory or parent directory
@@ -42,7 +53,7 @@ export class RmCommand implements ShellCommand {
return createErrorResult(`${this.name}: cannot remove '${path}': Invalid argument`);
}
const absolutePath = join(cwd, path);
try {
const stats = await this.fs.stat(absolutePath);
+2 -2
View File
@@ -65,11 +65,11 @@ describe('TouchCommand', () => {
expect(result.stderr).toContain('missing file operand');
});
it('should reject absolute paths', async () => {
it('should reject absolute paths outside allowed directories', async () => {
const result = await command.execute(['/absolute/path'], '/project');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('absolute paths are not supported');
expect(result.stderr).toContain('write access denied');
});
it('should handle missing parent directory', async () => {
+13 -4
View File
@@ -2,6 +2,7 @@ import { join, dirname } from "@std/path";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
import { createSuccessResult, createErrorResult } from "./ShellCommand";
import { isAbsolutePath, validateWritePath } from "../security";
/**
* Implementation of the 'touch' command
@@ -26,12 +27,20 @@ export class TouchCommand implements ShellCommand {
try {
for (const filePath of args) {
try {
// Handle absolute paths
if (filePath.startsWith('/') || filePath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(filePath)) {
return createErrorResult(`${this.name}: absolute paths are not supported: ${filePath}`);
// Validate write permissions for absolute paths
try {
validateWritePath(filePath, this.name);
} catch (error) {
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
}
const absolutePath = join(cwd, filePath);
// Handle both absolute and relative paths
let absolutePath: string;
if (isAbsolutePath(filePath)) {
absolutePath = filePath;
} else {
absolutePath = join(cwd, filePath);
}
try {
// Check if file exists