/** * @license * Copyright 2026 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { sanitizeEnvironment, type EnvironmentSanitizationConfig, } from './environmentSanitization.js'; /** * Request for preparing a command to run in a sandbox. */ export interface SandboxRequest { /** The program to execute. */ command: string; /** Arguments for the program. */ args: string[]; /** The working directory. */ cwd: string; /** Environment variables to be passed to the program. */ env: NodeJS.ProcessEnv; /** Optional sandbox-specific configuration. */ config?: { sanitizationConfig?: Partial; }; } /** * A command that has been prepared for sandboxed execution. */ export interface SandboxedCommand { /** The program or wrapper to execute. */ program: string; /** Final arguments for the program. */ args: string[]; /** Sanitized environment variables. */ env: NodeJS.ProcessEnv; /** The working directory. */ cwd?: string; } /** * Interface for a service that prepares commands for sandboxed execution. */ export interface SandboxManager { /** * Prepares a command to run in a sandbox, including environment sanitization. */ prepareCommand(req: SandboxRequest): Promise; } /** * A no-op implementation of SandboxManager that silently passes commands * through while applying environment sanitization. */ export class NoopSandboxManager implements SandboxManager { /** * Prepares a command by sanitizing the environment and passing through * the original program and arguments. */ async prepareCommand(req: SandboxRequest): Promise { const sanitizationConfig: EnvironmentSanitizationConfig = { allowedEnvironmentVariables: req.config?.sanitizationConfig?.allowedEnvironmentVariables ?? [], blockedEnvironmentVariables: req.config?.sanitizationConfig?.blockedEnvironmentVariables ?? [], enableEnvironmentVariableRedaction: req.config?.sanitizationConfig?.enableEnvironmentVariableRedaction ?? true, }; const sanitizedEnv = sanitizeEnvironment(req.env, sanitizationConfig); return { program: req.command, args: req.args, env: sanitizedEnv, }; } } /** * SandboxManager that implements actual sandboxing. */ export class LocalSandboxManager implements SandboxManager { async prepareCommand(_req: SandboxRequest): Promise { throw new Error('Tool sandboxing is not yet implemented.'); } } /** * Creates a sandbox manager based on the provided settings. */ export function createSandboxManager( sandboxingEnabled: boolean, ): SandboxManager { if (sandboxingEnabled) { return new LocalSandboxManager(); } return new NoopSandboxManager(); }