import { z } from 'zod'; import { existsSync, lstatSync, realpathSync, statSync, unlinkSync } from 'fs'; import { mkdir, stat } from 'fs/promises'; import { randomBytes } from 'crypto'; import { basename, dirname, join, resolve as resolvePath } from 'path'; import type { Client, SFTPWrapper } from 'ssh2'; import type { ToolContext, ToolDefinition, ToolResult } from '../../agent/types.js'; import { getSshPool, confirmFnFrom } from './runtime.js'; import { HostKeyError } from './known-hosts.js'; import { getLogger } from '../../logging/index.js'; import { sensitivePathNeedsConfirm, sensitivePathPreflight } from '../fs/sensitive.js'; import { currentSignature, renameWithRetry } from '../fs/file-io.js'; const log = getLogger('ssh'); const uploadSchema = z.object({ host: z.string().describe('Alias del host'), localPath: z.string().describe('Ruta local del archivo a subir'), remotePath: z.string().describe('Ruta de destino en el host remoto'), }); const downloadSchema = z.object({ host: z.string().describe('Alias del host'), remotePath: z.string().describe('Ruta remota del archivo a descargar'), localPath: z.string().describe('Ruta local de destino'), }); /** `client.sftp()` promisificado. */ function openSftp(client: Client, alias: string): Promise { return new Promise((resolve, reject) => { client.sftp((err, sftp) => { if (err) { reject(new Error(`No se pudo abrir la sesión SFTP con "${alias}": ${err.message}`)); return; } resolve(sftp); }); }); } function transfer( sftp: SFTPWrapper, direction: 'put' | 'get', from: string, to: string, signal: AbortSignal, ): Promise { return new Promise((resolve, reject) => { const onAbort = (): void => reject(new Error('Transferencia SFTP cancelada')); signal.addEventListener('abort', onAbort, { once: true }); const done = (err: Error | null | undefined): void => { signal.removeEventListener('abort', onAbort); if (err) reject(err); else resolve(); }; if (direction === 'put') sftp.fastPut(from, to, done); else sftp.fastGet(from, to, done); }); } function humanBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } /** * Rutas de la llamada con la local ya resuelta contra el cwd: la capa 3 (rutas * sensibles, Hito 11) tiene que ver `~/.ssh/id_ed25519` aunque el modelo pase * `id_ed25519` desde `~/.ssh`. */ function pathsOf(params: unknown, ctx: ToolContext): { localPath?: string; remotePath?: string } { if (typeof params !== 'object' || params === null) return {}; const p = params as Record; return { ...(typeof p.localPath === 'string' ? { localPath: resolvePath(ctx.cwd, p.localPath) } : {}), ...(typeof p.remotePath === 'string' ? { remotePath: p.remotePath } : {}), }; } /** * Capa 3 para transferencias: subir `~/.ssh/id_rsa` es exfiltrar la clave, y * descargar sobre un fichero de credenciales es sobrescribirlo. Mismos niveles * que `read_file`/`write_file`: `blocked` veta, `confirm` pregunta. */ const sensitiveTransferHooks = { preflight(params: unknown, ctx: ToolContext): ToolResult | null { return sensitivePathPreflight(pathsOf(params, ctx), ctx); }, isDestructive(params: unknown, ctx: ToolContext): boolean { return sensitivePathNeedsConfirm(pathsOf(params, ctx), ctx); }, }; /** Cierra el canal SFTP: sin esto cada transferencia dejaba uno abierto en la conexión del pool. */ function closeSftp(sftp: SFTPWrapper | undefined): void { try { sftp?.end(); } catch { /* el canal ya estaba cerrado */ } } /** Traduce fallos de conexión/verificación al `ToolResult` correspondiente. */ function toError(err: unknown): ToolResult { const recoverable = err instanceof HostKeyError ? err.recoverable : true; return { ok: false, error: (err as Error).message, recoverable }; } export const sshUploadTool: ToolDefinition = { name: 'ssh_upload', description: 'Sube un archivo local a un host remoto vía SFTP. ' + 'El directorio remoto de destino debe existir: créalo antes con exec (target "ssh:") si hace falta.', schema: uploadSchema, destructive: false, serialized: false, timeout: 600000, ...sensitiveTransferHooks, async execute(params: unknown, ctx: ToolContext): Promise { const input = uploadSchema.parse(params); const localPath = resolvePath(ctx.cwd, input.localPath); // Comprobar el fichero antes de abrir SFTP: el error es mucho más claro. if (!existsSync(localPath)) { return { ok: false, error: `El fichero local ${localPath} no existe.`, recoverable: true, }; } if (statSync(localPath).isDirectory()) { return { ok: false, error: `${localPath} es un directorio; ssh_upload transfiere ficheros sueltos.`, recoverable: true, }; } const started = Date.now(); let sftp: SFTPWrapper | undefined; try { const pool = getSshPool(ctx.config); const client = await pool.getConnection(input.host, confirmFnFrom(ctx)); sftp = await openSftp(client, input.host); await transfer(sftp, 'put', localPath, input.remotePath, ctx.signal); const size = (await stat(localPath)).size; const durationMs = Date.now() - started; log.info('sftp upload', { alias: input.host, size, durationMs }); return { ok: true, output: `Subido ${localPath} → ${input.host}:${input.remotePath} ` + `(${humanBytes(size)} en ${durationMs}ms)`, }; } catch (err) { return toError(err); } finally { closeSftp(sftp); } }, }; export const sshDownloadTool: ToolDefinition = { name: 'ssh_download', description: 'Descarga un archivo de un host remoto vía SFTP.', schema: downloadSchema, destructive: false, serialized: false, timeout: 600000, ...sensitiveTransferHooks, async execute(params: unknown, ctx: ToolContext): Promise { const input = downloadSchema.parse(params); const requested = resolvePath(ctx.cwd, input.localPath); // Un symlink se conserva: se escribe en su destino, como `write_file`. let localPath = requested; try { if (lstatSync(requested).isSymbolicLink()) localPath = realpathSync(requested); } catch { /* no existe todavía */ } if (existsSync(localPath) && statSync(localPath).isDirectory()) { return { ok: false, error: `${localPath} es un directorio; indica la ruta completa del fichero de destino.`, recoverable: true, }; } // Misma regla que `write_file`: si el agente leyó este fichero y cambió // desde entonces, sobrescribirlo borraría cambios que nadie ha visto. const before = currentSignature(localPath); if (ctx.fileState?.check(localPath, before) === 'stale') { return { ok: false, error: `${localPath} cambió desde que lo leíste; vuelve a leerlo antes de sobrescribirlo ` + 'con la descarga.', recoverable: true, }; } const started = Date.now(); // Descarga a un temporal en el mismo directorio y rename: un corte o una // cancelación a mitad ya no deja el fichero de destino truncado. const tmp = join( dirname(localPath), `.${basename(localPath)}.stratum-dl-${process.pid}-${randomBytes(4).toString('hex')}`, ); let sftp: SFTPWrapper | undefined; try { await mkdir(dirname(localPath), { recursive: true }); const pool = getSshPool(ctx.config); const client = await pool.getConnection(input.host, confirmFnFrom(ctx)); sftp = await openSftp(client, input.host); await transfer(sftp, 'get', input.remotePath, tmp, ctx.signal); const now = currentSignature(localPath); const unchanged = before === null ? now === null : now !== null && now.hash === before.hash; if (!unchanged) { return { ok: false, error: `${localPath} cambió durante la descarga; no se ha sobrescrito.`, recoverable: true, }; } renameWithRetry(tmp, localPath); // El contenido nuevo no lo ha visto el agente. ctx.fileState?.forget(localPath); const size = (await stat(localPath)).size; const durationMs = Date.now() - started; log.info('sftp download', { alias: input.host, size, durationMs }); return { ok: true, output: `Descargado ${input.host}:${input.remotePath} → ${localPath} ` + `(${humanBytes(size)} en ${durationMs}ms)`, }; } catch (err) { return toError(err); } finally { // Cerrar el canal antes de borrar: un `fastGet` cancelado puede seguir // escribiendo en el temporal hasta que el canal se cierra. closeSftp(sftp); try { unlinkSync(tmp); } catch { /* ya renombrado o nunca creado */ } } }, };