mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-14 06:17:27 +00:00
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:
1 parent
4d87a649de
commit
3c1cf91da4
2 files changed
+48
-1
No files matched your search
@@ -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, '""') + '"';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user