fix(cli): improve network handling and shell escaping

- Add network connectivity check before CLIProxy operations

- Enhance PowerShell argument escaping for special characters
This commit is contained in:
kaitranntt
2026-02-03 22:34:15 -05:00
parent 4d87a649de
commit 3c1cf91da4
2 changed files with 48 additions and 1 deletions
+23
View File
@@ -286,6 +286,29 @@ export async function execClaudeWithCLIProxy(
spinner.succeed('CLIProxy binary ready');
} catch (error) {
spinner.fail('Failed to prepare CLIProxy');
const err = error as Error;
// Check if network offline (DNS, connection, or timeout failure)
const networkErrors = [
'getaddrinfo',
'ENOTFOUND',
'ETIMEDOUT',
'ECONNREFUSED',
'ENETUNREACH',
'EAI_AGAIN',
];
const isNetworkError = networkErrors.some((errCode) => err.message.includes(errCode));
if (isNetworkError) {
console.error('');
console.error(fail('No network connection detected'));
console.error('');
console.error('CLIProxy binary download requires internet access.');
console.error('Please check your network connection and try again.');
console.error('');
process.exit(1);
}
throw error;
}
}
+25 -1
View File
@@ -11,9 +11,33 @@ import { getImageReadBlockHookEnv } from './hooks/image-read-block-hook-env';
/**
* Escape arguments for shell execution (Windows compatibility)
* Handles PowerShell special characters: backticks, $variables, double quotes
*/
export function escapeShellArg(arg: string): string {
return '"' + String(arg).replace(/"/g, '""') + '"';
const isWindows = process.platform === 'win32';
if (isWindows) {
// PowerShell: Use single quotes for literal strings to prevent variable expansion
// Escape single quotes by doubling them (PowerShell syntax)
// Fallback to double quotes with escapes if single quotes present
if (arg.includes("'")) {
// Contains single quote - use double quotes with escape sequences
return (
'"' +
String(arg)
.replace(/\$/g, '`$') // Escape $ to prevent variable expansion
.replace(/`/g, '``') // Escape backticks
.replace(/"/g, '`"') + // Escape double quotes
'"'
);
} else {
// No single quotes - use single quotes for literal string (safest)
return "'" + String(arg) + "'";
}
} else {
// Unix/macOS: Double quotes with escaped inner quotes
return '"' + String(arg).replace(/"/g, '""') + '"';
}
}
/**