File tree Expand file tree Collapse file tree
packages/core/src/shell/scripts/set-system-proxy Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -280,6 +280,38 @@ async function getMacNetworkService (exec) {
280280// macOS exit code 14 = "You don't have permission to change the system preferences."
281281const MACOS_NETWORKSETUP_PERMISSION_ERROR_CODE = 14
282282
283+ /**
284+ * POSIX single-quote escaping: wraps `arg` in single quotes, escaping any
285+ * embedded single quotes with the '\''-idiom. This prevents shell
286+ * metacharacter expansion regardless of the character set of the value.
287+ * @param {string|number } arg
288+ * @returns {string }
289+ */
290+ function shellEscapeArg ( arg ) {
291+ return "'" + String ( arg ) . replace ( / ' / g, "'\\''" ) + "'"
292+ }
293+
294+ /**
295+ * Strict-validate a proxy host (IPv4 / IPv6 / hostname) and throw if the
296+ * value looks suspicious. This is a defence-in-depth guard for the sudo
297+ * execution path; the primary protection is `shellEscapeArg`.
298+ */
299+ function validateProxyIp ( ip ) {
300+ if ( typeof ip !== 'string' || ! / ^ [ \w . \- : [ \] ] + $ / . test ( ip ) ) {
301+ throw new Error ( `无效的代理 IP 地址: ${ ip } ` )
302+ }
303+ }
304+
305+ /**
306+ * Strict-validate a TCP port number.
307+ */
308+ function validateProxyPort ( port ) {
309+ const n = Number ( port )
310+ if ( ! Number . isInteger ( n ) || n < 1 || n > 65535 ) {
311+ throw new Error ( `无效的代理端口号: ${ port } ` )
312+ }
313+ }
314+
283315function sudoExecMac ( cmd ) {
284316 return new Promise ( ( resolve , reject ) => {
285317 log . info ( '以管理员权限执行命令:' , cmd )
You can’t perform that action at this time.
0 commit comments