Files
Netcatty/electron/bridges/mcpServerBridge.cjs
T
陈大猫 dcaf25ae57 feat: inline approval gate for tool execution (#423)
* feat: inline approval gate for tool execution

Replace SDK-level needsApproval with Promise-based approval gate inside
tool execute functions. The SDK stream stays alive while the UI shows
inline approve/reject buttons on ToolCall blocks.

Changes:
- Add approvalGate.ts: Promise-based approval system with event listeners
- tools.ts: requestApproval() inside execute for confirm mode
- tool-call.tsx: inline approval buttons and keyboard shortcuts
- ChatMessageList.tsx: subscribe to approval events, render approval UI
- useAIChatStreaming.ts: remove old useToolApproval hook integration
- AIChatSidePanel.tsx: remove old approval hook, clean up unused destructuring
- systemPrompt.ts: update confirm mode to not ask for text confirmation
- preload.cjs: filter pager env var prefixes from terminal display
- mcpServerBridge.cjs: add approval gate for ACP/MCP write operations
- aiBridge.cjs: wire IPC for MCP approval response and main window getter
- preload.cjs: add onMcpApprovalRequest/respondMcpApproval APIs

* fix: scope approval gate by chatSessionId and replay for late subscribers

Address Codex PR review comments:
- Add chatSessionId to ApprovalRequest for session isolation
- Scope clearAllPendingApprovals(chatSessionId?) to only clear
  approvals belonging to the target session
- Add replayPendingApprovals() so late-mounting ChatMessageList
  picks up approvals that fired while unmounted
- Scope MCP clearPendingApprovals in aiBridge cancel handler to
  effectiveChatSessionId instead of clearing all
- Pass chatSessionId through MCP approval IPC flow

* chore: remove old approval flow code

- Delete useToolApproval.ts (unused hook)
- Delete InlineApprovalCard.tsx (replaced by ToolCall inline buttons)
- Remove stale comments referencing old hook in AIChatSidePanel
- Remove unused ai.chat.toolApprovalTitle i18n key from en/zh-CN

* fix: session-scoped approval gate and MCP replay survival

- handleStop passes activeSessionId to clearAllPendingApprovals
- setupMcpApprovalBridge stores MCP approvals in pendingApprovals map
  so they survive ChatMessageList unmount/remount cycles
- ChatMessageList accepts activeSessionId prop and filters standalone
  MCP approval blocks to the current session only
- AIChatSidePanel passes activeSessionId to ChatMessageList

* fix: filter PTY exec marker echoes and exit code lines from terminal

Extend filterMcpMarkers in preload.cjs to strip all shell-visible
artifacts from AI command execution:

- Echoed printf start marker: printf '%s\n' '__NCMCP_..._S'
- Echoed exit code restoration: (exit $__nc)
- PowerShell: Write-Output, $global:LASTEXITCODE, $__nc assignment
- Fish: set __nc $status
- Cmd: echo __NCMCP_...
- Widen guard to also trigger on __nc and PAGER=cat strings

* fix: scope SDK approvals, deny MCP on no renderer, fix memo comparator

- createCattyTools accepts chatSessionId and passes it to
  requestApproval so SDK approvals can be matched by
  clearAllPendingApprovals(activeSessionId) on stop
- useAIChatStreaming passes sessionId to createCattyTools
- mcpServerBridge: deny (resolve false) when no renderer window is
  available instead of auto-approving, preserving confirm mode safety
- ChatMessageList: add activeSessionId to React.memo comparator so
  switching sessions triggers re-render for correct MCP approval filter

* fix: MCP listener lifecycle, approval timeout, and UI sync on stop

- Move setupMcpApprovalBridge from ChatMessageList to AIChatSidePanel
  so the IPC listener survives tab/panel switches
- Add 5-minute auto-deny timeout to requestApproval to prevent
  indefinite isStreaming hangs when user walks away
- Add onApprovalCleared listener system: clearAllPendingApprovals now
  notifies UI subscribers so ChatMessageList removes stale cards
- ChatMessageList subscribes to onApprovalCleared to sync local state

* fix: main-process approval timeout and full tool args in payload

- Add 5-minute auto-deny timeout to requestApprovalFromRenderer
  matching the renderer-side requestApproval behavior
- Forward all tool params (excluding chatSessionId) to approval UI
  instead of cherry-picking command/input/path, so sftpRename
  oldPath/newPath and other tool-specific args are visible

* fix: move MCP bridge to TerminalLayer, narrow terminal filter guard

- Move setupMcpApprovalBridge from AIChatSidePanel to TerminalLayer
  so the IPC listener stays alive regardless of side panel tab.
  AIChatSidePanel only mounts when activeSidePanelTab==='ai'.
- Narrow preload.cjs filter guard back to __NCMCP_ only, preventing
  false-positive stripping of user scripts containing __nc or PAGER=cat

* fix: eliminate PTY wrapper echo leakage and duplicate prompts

- Posix wrapper now emits 2 lines instead of 4: start marker + command
  on line 1 (joined with ;), end marker + exit on line 2. This
  eliminates the duplicate prompt echo from the separate start marker.
- Rename __nc to __NCMCP_rc in all shell variants (posix/fish/powershell)
  so every wrapper variable contains the __NCMCP_ prefix. The preload
  guard `data.includes("__NCMCP_")` now reliably catches ALL wrapper
  artifacts regardless of chunk boundaries.
- Update all filterMcpMarkers regex patterns to match the restructured
  wrapper format and renamed variable.

* fix: sync main-process approval timeout with renderer UI cleanup

- When requestApprovalFromRenderer times out, send IPC event
  netcatty:ai:mcp:approval-cleared to renderer so stale approval
  cards are removed
- Add onMcpApprovalCleared preload bridge for the new IPC channel
- setupMcpApprovalBridge now subscribes to cleared events, removes
  timed-out entries from pendingApprovals and notifies clearedListeners
  so ChatMessageList drops the stale card

* fix: surface denied inline approvals as errors in UI

- Detect error or denial payloads ("error" string or "ok: false")
  returned by tools when the user denies an execution
- Set isError: true on the tool-result message so the ToolCall UI
  renders it as a failure (red/rejected) instead of a success (green)
2026-03-20 22:02:21 +08:00

976 lines
31 KiB
JavaScript

/**
* MCP Server Bridge — TCP host in Electron main process
*
* Starts a local TCP server that the netcatty-mcp-server.cjs child process
* connects to. Handles JSON-RPC calls by dispatching to real SSH sessions
* and SFTP clients.
*/
"use strict";
const net = require("node:net");
const crypto = require("node:crypto");
const path = require("node:path");
const { existsSync } = require("node:fs");
const { toUnpackedAsarPath } = require("./ai/shellUtils.cjs");
const { execViaPty, execViaChannel } = require("./ai/ptyExec.cjs");
let sessions = null; // Map<sessionId, { sshClient, stream, pty, proc, conn, ... }>
let sftpClients = null; // Map<sftpId, SFTPWrapper>
let tcpServer = null;
let tcpPort = null;
let authToken = null; // Random token generated when TCP server starts
// Track which sockets have completed authentication
const authenticatedSockets = new WeakSet();
/**
* Safely quote a string for use in a POSIX shell command.
* Wraps the value in single quotes and escapes any embedded single quotes.
*/
function shellQuote(s) {
return "'" + s.replace(/'/g, "'\\''") + "'";
}
// Per-scope metadata: chatSessionId → { sessionIds: string[], metadata: Map<sessionId, meta> }
// Each chat session only sees the hosts registered for its scope.
const scopedMetadata = new Map();
// Fallback: last-registered scope (used when no chatSessionId is provided)
let fallbackScopedSessionIds = [];
// Command safety checking (reuse from aiBridge)
let commandBlocklist = [];
// Cached compiled RegExp objects for commandBlocklist (rebuilt when blocklist changes)
let compiledBlocklist = [];
// Command timeout in milliseconds (default 60s, synced from user settings)
let commandTimeoutMs = 60000;
// Max iterations for AI agent loops (default 20, synced from user settings)
let maxIterations = 20;
// Permission mode: 'observer' | 'confirm' | 'autonomous' (synced from user settings)
let permissionMode = "confirm";
// Track active PTY executions for cancellation
const activePtyExecs = new Map(); // marker → { ptyStream, cleanup }
const cancelledChatSessions = new Set();
// ── Approval gate (for confirm mode with ACP/MCP agents) ──
let getMainWindowFn = null; // () => BrowserWindow | null
const pendingApprovals = new Map(); // approvalId → { resolve, chatSessionId }
let approvalIdCounter = 0;
function setMainWindowGetter(fn) {
getMainWindowFn = fn;
}
/**
* Request approval from the renderer process.
* Sends an IPC event and returns a Promise<boolean> that resolves
* when the user approves/rejects in the UI, or auto-denies after timeout.
*/
const APPROVAL_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
function requestApprovalFromRenderer(toolName, args, chatSessionId) {
return new Promise((resolve) => {
const mainWin = typeof getMainWindowFn === 'function' ? getMainWindowFn() : null;
if (!mainWin || mainWin.isDestroyed()) {
// No renderer available — deny to preserve confirm mode safety guarantee
resolve(false);
return;
}
const approvalId = `mcp_approval_${++approvalIdCounter}_${Date.now()}`;
// Auto-deny after timeout so ACP/MCP tool calls don't hang indefinitely
const timerId = setTimeout(() => {
if (pendingApprovals.has(approvalId)) {
pendingApprovals.delete(approvalId);
resolve(false);
// Notify renderer to remove the stale approval card
try {
const win = typeof getMainWindowFn === 'function' ? getMainWindowFn() : null;
if (win && !win.isDestroyed()) {
win.webContents.send('netcatty:ai:mcp:approval-cleared', { approvalIds: [approvalId] });
}
} catch { /* ignore */ }
}
}, APPROVAL_TIMEOUT_MS);
pendingApprovals.set(approvalId, {
resolve: (approved) => {
clearTimeout(timerId);
resolve(approved);
},
chatSessionId: chatSessionId || null,
});
mainWin.webContents.send('netcatty:ai:mcp:approval-request', {
approvalId,
toolName,
args,
chatSessionId: chatSessionId || undefined,
});
});
}
function resolveApprovalFromRenderer(approvalId, approved) {
const entry = pendingApprovals.get(approvalId);
if (entry) {
pendingApprovals.delete(approvalId);
entry.resolve(approved);
}
}
/**
* Clear pending MCP approvals, optionally scoped to a specific chatSessionId.
* Resolves matched entries with false (denied) to unblock hanging promises.
*/
function clearPendingApprovals(chatSessionId) {
if (!chatSessionId) {
for (const [, entry] of pendingApprovals) {
entry.resolve(false);
}
pendingApprovals.clear();
return;
}
for (const [id, entry] of pendingApprovals) {
if (entry.chatSessionId === chatSessionId) {
pendingApprovals.delete(id);
entry.resolve(false);
}
}
}
function cancelAllPtyExecs() {
for (const [marker, entry] of activePtyExecs) {
try {
entry.cleanup();
// Send Ctrl+C to kill the running command
if (entry.ptyStream && typeof entry.ptyStream.write === "function") {
entry.ptyStream.write("\x03");
}
} catch { /* ignore */ }
}
activePtyExecs.clear();
}
function init(deps) {
sessions = deps.sessions;
sftpClients = deps.sftpClients;
if (deps.commandBlocklist) {
commandBlocklist = deps.commandBlocklist;
}
}
function setCommandBlocklist(list) {
commandBlocklist = list || [];
// Recompile cached regexes when blocklist changes
compiledBlocklist = [];
for (const pattern of commandBlocklist) {
try {
compiledBlocklist.push(new RegExp(pattern, "i"));
} catch {
compiledBlocklist.push(null); // placeholder for invalid patterns
}
}
}
function setCommandTimeout(seconds) {
commandTimeoutMs = Math.max(1, Math.min(3600, seconds || 60)) * 1000;
}
function getCommandTimeoutMs() {
return commandTimeoutMs;
}
function setMaxIterations(value) {
maxIterations = Math.max(1, Math.min(100, value || 20));
}
function getMaxIterations() {
return maxIterations;
}
function setPermissionMode(mode) {
if (mode === "observer" || mode === "confirm" || mode === "autonomous") {
permissionMode = mode;
}
}
function getPermissionMode() {
return permissionMode;
}
function setChatSessionCancelled(chatSessionId, cancelled) {
if (!chatSessionId) return;
if (cancelled) {
cancelledChatSessions.add(chatSessionId);
} else {
cancelledChatSessions.delete(chatSessionId);
}
}
function isChatSessionCancelled(chatSessionId) {
return Boolean(chatSessionId && cancelledChatSessions.has(chatSessionId));
}
/**
* Register metadata for terminal sessions (called from renderer via IPC).
* Metadata is stored per-scope (chatSessionId) so different AI chat sessions
* only see their own hosts.
* @param {Array<{sessionId, hostname, label, os, username, connected, protocol?, shellType?}>} sessionList
* @param {string} [chatSessionId] - AI chat session ID for per-scope isolation
*/
function updateSessionMetadata(sessionList, chatSessionId) {
const ids = sessionList.map(s => s.sessionId);
const metaMap = new Map();
for (const s of sessionList) {
metaMap.set(s.sessionId, {
hostname: s.hostname || "",
label: s.label || "",
os: s.os || "",
username: s.username || "",
protocol: s.protocol || "",
shellType: s.shellType || "",
connected: s.connected !== false,
});
}
// Store per-scope metadata when chatSessionId is provided
if (chatSessionId) {
scopedMetadata.set(chatSessionId, { sessionIds: ids, metadata: metaMap });
} else {
// Only update fallback when no chatSessionId — prevents scoped updates from
// leaking all sessions to unscoped agents
fallbackScopedSessionIds = ids.slice();
}
}
/**
* Get scoped session IDs. If chatSessionId is provided, returns IDs for that
* specific scope; otherwise returns the last-registered fallback.
*/
function getScopedSessionIds(chatSessionId) {
if (chatSessionId) {
const scoped = scopedMetadata.get(chatSessionId);
if (scoped) return scoped.sessionIds;
}
return fallbackScopedSessionIds;
}
/**
* Look up metadata for a sessionId, scoped to a specific chat session.
* Falls back to session object properties if no scoped metadata is found.
*/
function getSessionMeta(sessionId, chatSessionId) {
// Try scoped metadata first
if (chatSessionId) {
const scoped = scopedMetadata.get(chatSessionId);
if (scoped?.metadata?.has(sessionId)) return scoped.metadata.get(sessionId);
}
// Fallback: check all scopes for this sessionId (backwards compat)
for (const [, scope] of scopedMetadata) {
if (scope.metadata?.has(sessionId)) return scope.metadata.get(sessionId);
}
return null;
}
function sessionSupportsSftp(session) {
const sshClient = session?.conn || session?.sshClient;
return !!(sshClient && typeof sshClient.exec === "function");
}
function scopeHasSftpSessions(sessionIds) {
if (!Array.isArray(sessionIds) || sessionIds.length === 0) return false;
for (const sessionId of sessionIds) {
const session = sessions?.get(sessionId);
if (sessionSupportsSftp(session)) return true;
}
return false;
}
/**
* Run an array of async task factories with a concurrency limit.
*/
async function limitConcurrency(tasks, limit) {
const results = [];
const executing = new Set();
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i];
const p = task().then(r => { results[i] = r; }).finally(() => executing.delete(p));
executing.add(p);
if (executing.size >= limit) {
await Promise.race(executing);
}
}
await Promise.all(executing);
return results;
}
function checkCommandSafety(command) {
for (let i = 0; i < compiledBlocklist.length; i++) {
const re = compiledBlocklist[i];
if (re && re.test(command)) {
return { blocked: true, matchedPattern: commandBlocklist[i] };
}
}
return { blocked: false };
}
// ── TCP Server ──
function getOrCreateHost() {
if (tcpServer && tcpPort) return Promise.resolve(tcpPort);
// Generate a random auth token for this server instance
authToken = crypto.randomBytes(32).toString("hex");
return new Promise((resolve, reject) => {
const server = net.createServer((socket) => {
handleConnection(socket);
});
server.listen(0, "127.0.0.1", () => {
tcpPort = server.address().port;
tcpServer = server;
resolve(tcpPort);
});
server.on("error", (err) => {
console.error("[MCP Bridge] TCP server error:", err.message);
reject(err);
});
});
}
const MAX_TCP_BUFFER = 10 * 1024 * 1024; // 10MB
function handleConnection(socket) {
let buffer = "";
socket.setEncoding("utf-8");
socket.on("data", (chunk) => {
if (buffer.length + chunk.length > MAX_TCP_BUFFER) {
console.error("[MCP Bridge] TCP buffer exceeded max size, dropping connection");
socket.destroy();
return;
}
buffer += chunk;
let newlineIdx;
while ((newlineIdx = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, newlineIdx);
buffer = buffer.slice(newlineIdx + 1);
if (!line.trim()) continue;
handleMessage(socket, line);
}
});
socket.on("error", () => {
// Client disconnected — nothing to do
});
}
async function handleMessage(socket, line) {
let msg;
try {
msg = JSON.parse(line);
} catch {
return;
}
const { id, method, params } = msg;
if (id == null || !method) return;
// ── Authentication gate ──
// The first message from any connection MUST be auth/verify with the correct token.
// All other methods are rejected until the socket is authenticated.
if (!authenticatedSockets.has(socket)) {
if (method === "auth/verify" && params?.token === authToken) {
authenticatedSockets.add(socket);
const response = JSON.stringify({ jsonrpc: "2.0", id, result: { ok: true } }) + "\n";
if (!socket.destroyed) socket.write(response);
return;
}
// Wrong token or wrong method — reject and close
const response = JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32001, message: "Authentication required. Send auth/verify with valid token first." },
}) + "\n";
if (!socket.destroyed) {
socket.write(response);
socket.destroy();
}
return;
}
try {
const result = await dispatch(method, params || {});
const response = JSON.stringify({ jsonrpc: "2.0", id, result }) + "\n";
if (!socket.destroyed) socket.write(response);
} catch (err) {
const response = JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32000, message: err?.message || String(err) },
}) + "\n";
if (!socket.destroyed) socket.write(response);
}
}
// ── RPC Dispatch ──
// Methods that modify remote state — blocked in observer mode
const WRITE_METHODS = new Set([
"netcatty/exec",
"netcatty/terminalWrite",
"netcatty/sftpWrite",
"netcatty/sftpMkdir",
"netcatty/sftpRemove",
"netcatty/sftpRename",
"netcatty/multiExec",
]);
/**
* Validate that a sessionId is allowed in the current scope.
* Checks both process-level SCOPED_SESSION_IDS and per-chatSession scoped metadata.
*/
function validateSessionScope(sessionId, chatSessionId) {
if (!sessionId) return null; // will fail at handler level
const scopedIds = getScopedSessionIds(chatSessionId);
if (scopedIds && scopedIds.length > 0 && !scopedIds.includes(sessionId)) {
return `Session "${sessionId}" is not in the current scope.`;
}
return null;
}
async function dispatch(method, params) {
// Observer mode: block all write operations
if (permissionMode === "observer" && WRITE_METHODS.has(method)) {
return { ok: false, error: `Operation denied: permission mode is "observer" (read-only). Change to "confirm" or "autonomous" in Settings → AI → Safety to allow this action.` };
}
if (WRITE_METHODS.has(method) && isChatSessionCancelled(params?.chatSessionId)) {
return { ok: false, error: "Operation cancelled: the ACP session was stopped." };
}
// Confirm mode: request user approval for write operations
if (permissionMode === "confirm" && WRITE_METHODS.has(method)) {
const { chatSessionId, ...toolArgs } = params || {};
const approved = await requestApprovalFromRenderer(method, toolArgs, chatSessionId);
if (!approved) {
return { ok: false, error: "Operation denied by user." };
}
}
// Scope validation for session-targeted operations
if (method !== "netcatty/getContext" && params?.sessionId) {
const scopeErr = validateSessionScope(params.sessionId, params?.chatSessionId);
if (scopeErr) return { ok: false, error: scopeErr };
}
// For multi-exec, validate all session IDs
if (method === "netcatty/multiExec" && Array.isArray(params?.sessionIds)) {
for (const sid of params.sessionIds) {
const scopeErr = validateSessionScope(sid, params?.chatSessionId);
if (scopeErr) return { ok: false, error: scopeErr };
}
}
switch (method) {
case "netcatty/getContext":
return handleGetContext(params);
case "netcatty/exec":
return handleExec(params);
case "netcatty/terminalWrite":
return handleTerminalWrite(params);
case "netcatty/sftpList":
return handleSftpList(params);
case "netcatty/sftpRead":
return handleSftpRead(params);
case "netcatty/sftpWrite":
return handleSftpWrite(params);
case "netcatty/sftpMkdir":
return handleSftpMkdir(params);
case "netcatty/sftpRemove":
return handleSftpRemove(params);
case "netcatty/sftpRename":
return handleSftpRename(params);
case "netcatty/sftpStat":
return handleSftpStat(params);
case "netcatty/multiExec":
return handleMultiExec(params);
default:
throw new Error(`Unknown method: ${method}`);
}
}
// ── Handler: getContext ──
function handleGetContext(params) {
if (!sessions) return { hosts: [], instructions: "No sessions available." };
// chatSessionId may be passed via env for per-scope metadata lookup
const chatSessionId = params?.chatSessionId || null;
const explicitScopedIds = Array.isArray(params?.scopedSessionIds)
? params.scopedSessionIds
: null;
const resolvedScopedIds = explicitScopedIds ?? (chatSessionId ? getScopedSessionIds(chatSessionId) : null);
const hasScopedContext = explicitScopedIds !== null || chatSessionId !== null;
const scopedIds = resolvedScopedIds ? new Set(resolvedScopedIds) : null;
const hosts = [];
// When a scoped context exists but currently resolves to zero sessions, treat
// it as "no access" rather than falling back to all sessions.
if (hasScopedContext && (!resolvedScopedIds || resolvedScopedIds.length === 0)) {
return {
environment: "netcatty-terminal",
description: "No hosts are available in the current scope.",
hosts: [],
hostCount: 0,
};
}
for (const [sessionId, session] of sessions.entries()) {
if (scopedIds && !scopedIds.has(sessionId)) continue;
const ptyStream = session.stream || session.pty || session.proc;
const sshClient = session.conn || session.sshClient;
const hasCommandablePty = ptyStream && typeof ptyStream.write === "function";
const hasSshExec = sshClient && typeof sshClient.exec === "function";
if (!hasCommandablePty && !hasSshExec) continue;
// Look up metadata scoped to this chat session
const meta = getSessionMeta(sessionId, chatSessionId) || {};
hosts.push({
sessionId,
hostname: meta.hostname || session.hostname || "",
label: meta.label || session.label || "",
os: meta.os || "",
username: meta.username || session.username || "",
protocol: meta.protocol || session.protocol || session.type || "",
shellType: meta.shellType || session.shellKind || "",
supportsSftp: sessionSupportsSftp(session),
connected: meta.connected !== undefined ? meta.connected : !!(session.sshClient || session.conn || ptyStream),
});
}
return {
environment: "netcatty-terminal",
description: "You are operating inside Netcatty, a multi-session terminal manager. " +
"The available sessions may be remote hosts, local terminals, or Mosh-backed shells. " +
"Use the provided tools to execute commands through the sessions exposed by Netcatty. " +
"SFTP tools only work for remote SSH sessions. " +
"Always prefer these tools over suggesting the user to do things manually.",
hosts,
hostCount: hosts.length,
};
}
// ── Handler: exec ──
function handleExec(params) {
const { sessionId, command } = params;
if (!sessionId || !command) throw new Error("sessionId and command are required");
if (typeof command !== 'string' || !command.trim()) {
return { ok: false, error: 'Invalid command', exitCode: 1 };
}
const safety = checkCommandSafety(command);
if (safety.blocked) {
return { ok: false, error: `Command blocked by safety policy. Pattern: ${safety.matchedPattern}` };
}
const session = sessions?.get(sessionId);
if (!session) return { ok: false, error: "Session not found" };
if ((session.protocol === "local" || session.type === "local") && session.shellKind === "unknown") {
return {
ok: false,
error: "AI execution is not supported for this local shell executable. Configure the local terminal to use bash/zsh/sh, fish, PowerShell/pwsh, or cmd.exe.",
};
}
const sshClient = session.conn || session.sshClient;
const ptyStream = session.stream || session.pty || session.proc;
// Prefer the interactive PTY so the user sees command/output in-session.
if (ptyStream && typeof ptyStream.write === "function") {
return execViaPty(ptyStream, command, {
trackForCancellation: activePtyExecs,
timeoutMs: commandTimeoutMs,
shellKind: session.shellKind,
});
}
// If no PTY stream, fall back to exec channel for SSH sessions only.
if (!sshClient || typeof sshClient.exec !== "function") {
return { ok: false, error: "Session does not support command execution" };
}
if (!ptyStream || typeof ptyStream.write !== "function") {
return execViaChannel(sshClient, command, {
timeoutMs: commandTimeoutMs,
trackForCancellation: activePtyExecs,
});
}
}
// ── Handler: terminalWrite ──
function handleTerminalWrite(params) {
const { sessionId, input } = params;
if (!sessionId || input == null) throw new Error("sessionId and input are required");
// Validate input against command blocklist
const safety = checkCommandSafety(input);
if (safety.blocked) {
return { ok: false, error: `Input blocked by safety policy. Pattern: ${safety.matchedPattern}` };
}
const session = sessions?.get(sessionId);
if (!session) return { ok: false, error: "Session not found" };
if (session.stream) {
session.stream.write(input);
return { ok: true };
}
if (session.pty) {
session.pty.write(input);
return { ok: true };
}
if (session.proc) {
session.proc.write(input);
return { ok: true };
}
return { ok: false, error: "No writable stream" };
}
// ── SFTP Helpers ──
function findSftpForSession(sessionId) {
// Try to find an SFTP client keyed by the same sessionId
if (sftpClients?.has(sessionId)) {
return sftpClients.get(sessionId);
}
// Look through all SFTP clients for one sharing the same SSH connection
const session = sessions?.get(sessionId);
if (!session?.sshClient) return null;
for (const [, client] of sftpClients || []) {
if (client.client === session.sshClient || client._sshClient === session.sshClient) {
return client;
}
}
return null;
}
// ── Handler: sftpList ──
async function handleSftpList(params) {
const { sessionId, path: dirPath } = params;
if (!sessionId || !dirPath) throw new Error("sessionId and path are required");
const sftpClient = findSftpForSession(sessionId);
if (sftpClient) {
try {
const list = await sftpClient.list(dirPath);
return {
files: list.map(f => ({
name: f.name,
type: f.type === "d" ? "directory" : f.type === "l" ? "symlink" : "file",
size: f.size,
lastModified: f.modifyTime,
permissions: f.rights ? `${f.rights.user}${f.rights.group}${f.rights.other}` : undefined,
})),
};
} catch (err) {
return { ok: false, error: err.message };
}
}
// Fallback: use SSH exec
const result = await handleExec({ sessionId, command: `ls -la ${shellQuote(dirPath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { output: result.stdout || "(empty directory)" };
}
// ── Handler: sftpRead ──
async function handleSftpRead(params) {
const { sessionId, path: filePath } = params;
if (params.maxBytes != null && (typeof params.maxBytes !== 'number' || params.maxBytes < 1 || params.maxBytes > 10 * 1024 * 1024)) {
return { ok: false, error: 'maxBytes must be a positive number between 1 and 10485760' };
}
// Clamp maxBytes to a safe upper bound (10MB)
const maxBytes = Math.max(1, Math.min(Number(params.maxBytes) || 10000, 10 * 1024 * 1024));
if (!sessionId || !filePath) throw new Error("sessionId and path are required");
// Fallback to SSH exec (more reliable across SFTP client states)
const result = await handleExec({ sessionId, command: `head -c ${maxBytes} ${shellQuote(filePath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { content: result.stdout || "(empty file)" };
}
// ── Handler: sftpWrite ──
async function handleSftpWrite(params) {
const { sessionId, path: filePath, content } = params;
if (!sessionId || !filePath || content == null) throw new Error("sessionId, path and content are required");
const sftpClient = findSftpForSession(sessionId);
if (sftpClient) {
try {
await sftpClient.put(Buffer.from(content, "utf-8"), filePath);
return { written: filePath };
} catch {
// Fallback to SSH
}
}
// Use base64 encoding to avoid heredoc delimiter collision issues
const b64 = Buffer.from(content, "utf-8").toString("base64");
const result = await handleExec({ sessionId, command: `echo ${shellQuote(b64)} | base64 -d > ${shellQuote(filePath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { written: filePath };
}
// ── Handler: sftpMkdir ──
async function handleSftpMkdir(params) {
const { sessionId, path: dirPath } = params;
if (!sessionId || !dirPath) throw new Error("sessionId and path are required");
const sftpClient = findSftpForSession(sessionId);
if (sftpClient) {
try {
await sftpClient.mkdir(dirPath, true); // recursive
return { created: dirPath };
} catch {
// Fallback
}
}
const result = await handleExec({ sessionId, command: `mkdir -p ${shellQuote(dirPath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { created: dirPath };
}
// ── Handler: sftpRemove ──
// Critical paths that must never be removed (module-level constant)
const CRITICAL_PATHS = new Set([
"/", "/root", "/home", "/etc", "/var", "/usr", "/boot",
"/bin", "/sbin", "/lib", "/lib64", "/dev", "/proc", "/sys", "/tmp", "/opt",
]);
async function handleSftpRemove(params) {
const { sessionId, path: targetPath } = params;
if (!sessionId || !targetPath) throw new Error("sessionId and path are required");
// Guard against deleting root or critical system directories
// Normalize to resolve "..", "//", and trailing slashes before checking
const normalizedPath = path.posix.normalize(targetPath).replace(/\/+$/, "") || "/";
if (CRITICAL_PATHS.has(normalizedPath) || /^\/[^/]+$/.test(normalizedPath)) {
return { ok: false, error: `Refusing to remove critical or root-level path: ${targetPath}` };
}
// Use rm -r (without -f) so permission errors surface instead of being silently ignored
const result = await handleExec({ sessionId, command: `rm -r ${shellQuote(targetPath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { removed: targetPath };
}
// ── Handler: sftpRename ──
async function handleSftpRename(params) {
const { sessionId, oldPath, newPath } = params;
if (!sessionId || !oldPath || !newPath) throw new Error("sessionId, oldPath and newPath are required");
const sftpClient = findSftpForSession(sessionId);
if (sftpClient) {
try {
await sftpClient.rename(oldPath, newPath);
return { renamed: `${oldPath} → ${newPath}` };
} catch {
// Fallback
}
}
const result = await handleExec({ sessionId, command: `mv ${shellQuote(oldPath)} ${shellQuote(newPath)}` });
if (!result.ok) return { ok: false, error: result.error };
return { renamed: `${oldPath} → ${newPath}` };
}
// ── Handler: sftpStat ──
async function handleSftpStat(params) {
const { sessionId, path: targetPath } = params;
if (!sessionId || !targetPath) throw new Error("sessionId and path are required");
const sftpClient = findSftpForSession(sessionId);
if (sftpClient) {
try {
const stat = await sftpClient.stat(targetPath);
return {
name: path.basename(targetPath),
type: stat.isDirectory ? "directory" : stat.isSymbolicLink ? "symlink" : "file",
size: stat.size,
lastModified: stat.modifyTime,
permissions: stat.mode ? (stat.mode & 0o777).toString(8) : undefined,
};
} catch {
// Fallback
}
}
// Fallback: use stat command
const result = await handleExec({ sessionId, command: `stat -c '{"size":%s,"mode":"%a","mtime":%Y,"type":"%F"}' ${shellQuote(targetPath)}` });
if (!result.ok) return { ok: false, error: result.error };
try {
const parsed = JSON.parse(result.stdout.trim());
return {
name: path.basename(targetPath),
type: parsed.type?.includes("directory") ? "directory" : "file",
size: parsed.size,
lastModified: parsed.mtime * 1000,
permissions: parsed.mode,
};
} catch {
return { ok: false, error: "Failed to parse stat output" };
}
}
// ── Handler: multiExec ──
async function handleMultiExec(params) {
const { sessionIds, command, mode = "parallel", stopOnError = false } = params;
if (!Array.isArray(sessionIds) || !command) throw new Error("sessionIds and command are required");
if (sessionIds.length > 50) {
return { ok: false, error: 'Too many session IDs: maximum is 50' };
}
if (typeof command !== 'string' || !command.trim()) {
return { ok: false, error: 'Invalid command' };
}
const safety = checkCommandSafety(command);
if (safety.blocked) {
return { ok: false, error: `Command blocked by safety policy. Pattern: ${safety.matchedPattern}` };
}
const results = {};
if (mode === "sequential") {
for (const sid of sessionIds) {
const result = await handleExec({ sessionId: sid, command });
results[sid] = {
ok: result.ok,
output: result.ok ? (result.stdout || "(no output)") : `Error: ${result.error || result.stderr || "Failed"}`,
};
if (!result.ok && stopOnError) break;
}
} else {
// Parallel execution with concurrency limit
const tasks = sessionIds.map((sid) => () => {
return Promise.resolve(handleExec({ sessionId: sid, command })).then(result => ({
sid,
ok: result.ok,
output: result.ok ? (result.stdout || "(no output)") : `Error: ${result.error || result.stderr || "Failed"}`,
}));
});
const resolved = await limitConcurrency(tasks, 10);
for (const r of resolved) {
results[r.sid] = { ok: r.ok, output: r.output };
}
}
return { results };
}
// ── MCP Server Config Builder ──
function buildMcpServerConfig(port, scopedSessionIds, chatSessionId) {
// Use provided scoped IDs, or resolve from chatSessionId, or fall back
const effectiveIds = (scopedSessionIds && scopedSessionIds.length > 0)
? scopedSessionIds
: getScopedSessionIds(chatSessionId);
const runtimePath = toUnpackedAsarPath(
path.join(__dirname, "..", "mcp", "netcatty-mcp-server.cjs"),
);
const env = [
{ name: "NETCATTY_MCP_PORT", value: String(port) },
];
if (authToken) {
env.push({ name: "NETCATTY_MCP_TOKEN", value: authToken });
}
// When chatSessionId is present, the MCP subprocess resolves scope dynamically
// through main-process metadata, so avoid freezing session IDs at spawn time.
if (!chatSessionId && effectiveIds && effectiveIds.length > 0) {
env.push({ name: "NETCATTY_MCP_SESSION_IDS", value: effectiveIds.join(",") });
}
// Pass chatSessionId so MCP server can scope getContext responses
if (chatSessionId) {
env.push({ name: "NETCATTY_MCP_CHAT_SESSION_ID", value: chatSessionId });
}
env.push({
name: "NETCATTY_MCP_ENABLE_SFTP",
value: scopeHasSftpSessions(effectiveIds) ? "1" : "0",
});
// Pass permission mode so MCP server can enforce it locally (defense-in-depth)
env.push({ name: "NETCATTY_MCP_PERMISSION_MODE", value: permissionMode });
return {
name: "netcatty-remote-hosts",
type: "stdio",
command: "node",
args: [runtimePath],
env,
};
}
// ── Cleanup ──
function cleanupScopedMetadata(chatSessionId) {
if (chatSessionId) {
scopedMetadata.delete(chatSessionId);
cancelledChatSessions.delete(chatSessionId);
}
}
function cleanup() {
if (tcpServer) {
tcpServer.close();
tcpServer = null;
tcpPort = null;
}
scopedMetadata.clear();
}
module.exports = {
init,
setCommandBlocklist,
setCommandTimeout,
getCommandTimeoutMs,
setMaxIterations,
getMaxIterations,
setPermissionMode,
getPermissionMode,
setChatSessionCancelled,
checkCommandSafety,
updateSessionMetadata,
getScopedSessionIds,
getOrCreateHost,
buildMcpServerConfig,
cancelAllPtyExecs,
cleanupScopedMetadata,
cleanup,
setMainWindowGetter,
resolveApprovalFromRenderer,
clearPendingApprovals,
};