{"record":{"id":"66857231638beafe","repo":"docmirror/dev-sidecar","slug":"port","errorCode":null,"errorMessage":"无效的代理端口号: ${port}","messagePattern":"无效的代理端口号: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/shell/scripts/set-system-proxy/index.js","lineNumber":311,"sourceCode":"\n/**\n * Strict-validate a proxy host (IPv4 / IPv6 / hostname) and throw if the\n * value looks suspicious.  This is a defence-in-depth guard for the sudo\n * execution path; the primary protection is `shellEscapeArg`.\n */\nfunction validateProxyIp (ip) {\n  if (typeof ip !== 'string' || !/^[\\w.\\-:[\\]]+$/.test(ip)) {\n    throw new Error(`无效的代理 IP 地址: ${ip}`)\n  }\n}\n\n/**\n * Strict-validate a TCP port number.\n */\nfunction validateProxyPort (port) {\n  const n = Number(port)\n  if (!Number.isInteger(n) || n < 1 || n > 65535) {\n    throw new Error(`无效的代理端口号: ${port}`)\n  }\n}\n\nfunction sudoExecMac (cmd) {\n  return new Promise((resolve, reject) => {\n    log.info('以管理员权限执行命令:', cmd)\n    sudoPrompt.exec(cmd, { name: 'dev-sidecar' }, (error, stdout, stderr) => {\n      if (stderr) {\n        log.warn('以管理员权限执行命令，stderr:', stderr)\n      }\n      if (error) {\n        log.error('以管理员权限执行命令失败:', error)\n        reject(error)\n      } else {\n        resolve(stdout)\n      }\n    })\n  })","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/docmirror/dev-sidecar/blob/7710cd56cce760c708f30b01d2d4056eb8c402d5/packages/core/src/shell/scripts/set-system-proxy/index.js#L293-L329","documentation":"validateProxyPort strict-checks the TCP port used in macOS networksetup/sudo commands: Number(port) must be an integer in 1–65535. Otherwise this error is thrown before any privileged command runs, guarding the sudo path from malformed or injected port values.","triggerScenarios":"Calling the macOS set-system-proxy script with a port that is: undefined/null, 0, negative, > 65535, a non-numeric string ('abc', '31,181'), or a non-integer number (31181.5) — including NaN from failed config parsing.","commonSituations":"Corrupted ~/.dev-sidecar/config.json where the proxy port field is empty or textual; remote/personal config layers supplying a bad port; the mitmproxy HTTPS port variable not initialized before the shell script runs.","solutions":["Pass an integer between 1 and 65535 (dev-sidecar's default HTTPS port is 31181).","Inspect ~/.dev-sidecar/config.json and running.json for the port value; fix or delete the bad entry to fall back to defaults.","Coerce and check before calling: `const p = Number(port); if (!Number.isInteger(p) || p < 1 || p > 65535) throw ...`.","Ensure the config merge layer (remote shared/personal) is not overriding the port with an invalid value."],"exampleFix":"// before\nawait api.shell.setSystemProxy({ ip: '127.0.0.1', port: config.proxyPort }) // config.proxyPort may be '' / undefined\n// after\nconst port = Number(config.proxyPort) || 31181\nif (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('bad proxy port')\nawait api.shell.setSystemProxy({ ip: '127.0.0.1', port })","handlingStrategy":"validation","validationCode":"function isValidProxyPort(port) {\n  const n = Number(port)\n  return Number.isInteger(n) && n >= 1 && n <= 65535\n}\nif (!isValidProxyPort(port)) throw new Error(`fix config: bad proxy port ${JSON.stringify(port)}`)","typeGuard":"function isProxyPort(v) {\n  const n = Number(v)\n  return Number.isInteger(n) && n >= 1 && n <= 65535\n}","tryCatchPattern":"try {\n  await setSystemProxy({ ip, port })\n} catch (e) {\n  if (e.message.includes('无效的代理端口号')) {\n    const fallbackPort = 31181 // dev-sidecar default HTTPS port\n    if (isProxyPort(fallbackPort)) return setSystemProxy({ ip, port: fallbackPort })\n  }\n  throw e\n}","preventionTips":["Pass an integer 1–65535; default dev-sidecar HTTPS port is 31181.","Validate the port field when saving settings to ~/.dev-sidecar/config.json.","Check config merge layers (remote shared/personal) are not writing empty or textual port values.","Coerce with Number() and range-check before invoking the proxy script."],"tags":["mac","validation","system-proxy","port","input-validation"],"backgroundTag":"invalid-proxy-port","analyzedSha":"7710cd56cce760c708f30b01d2d4056eb8c402d5","analyzedAt":"2026-08-31T22:07:07.234Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}