Build a secure filesystem operations tool for Docker sandbox environments using the Loopstack framework
You are building @loopstack/sandbox-filesystem, a Loopstack tool that provides secure, controlled filesystem operations within Docker sandbox environments. This tool enables workflows to read, write, list, and manage files and directories in isolated containers.
Create a NestJS injectable tool class that extends ToolBase and provides filesystem operations executed within sandbox containers using @loopstack/sandbox-tool.
Define a Zod schema with the following structure:
const sandboxFilesystemSchema = z.object({
operation: z.enum(['read', 'write', 'list', 'createDir', 'delete', 'exists', 'info']),
path: z.string().describe('Target filesystem path within sandbox'),
content: z.string().optional().describe('Content to write (for write operation)'),
encoding: z.string().default('utf-8').optional().describe('Character encoding'),
recursive: z.boolean().default(false).optional().describe('Enable recursive operations'),
force: z.boolean().default(false).optional().describe('Force overwrite or deletion'),
});
type SandboxFilesystemArgs = z.infer<typeof sandboxFilesystemSchema>;
import { Injectable } from '@nestjs/common';
import { z } from 'zod';
import { ToolBase } from '@loopstack/core';
import { BlockConfig, ToolResult, WithArguments } from '@loopstack/common';
@Injectable()
@BlockConfig({
config: {
description: 'Secure filesystem operations within Docker sandbox environments',
},
})
@WithArguments(sandboxFilesystemSchema)
export class SandboxFilesystemTool extends ToolBase<SandboxFilesystemArgs> {
async execute(args: SandboxFilesystemArgs): Promise<ToolResult<any>> {
// 1. Validate path for security (no ../ traversal)
// 2. Normalize path
// 3. Execute operation based on args.operation
// 4. Return structured result with success, data, metadata
}
}
Return a ToolResult with this structure:
{
data: {
success: boolean,
data: any, // Operation-specific payload
error?: string, // Error message if failed
metadata?: { // Additional context
bytesWritten?: number,
itemsCount?: number,
// etc.
}
}
}
..//etc, /sys, /procmaxFileSize configuration (suggest default: 10MB)Support these configuration properties in the tool:
interface SandboxFilesystemConfig {
defaultEncoding?: string; // Default: 'utf-8'
maxFileSize?: number; // Default: 10485760 (10MB)
allowedPaths?: string[]; // Whitelist of accessible paths
timeoutMs?: number; // Default: 5000
}