fix(desktop): parse server ports strictly - #5509
Conversation
|
📌 Maintainer note — consolidation & helper reuse (applies across your recent First, genuine thanks — the volume includes real, valuable fixes that are already merged (e.g. #5489 where Two process asks going forward, so review can keep up and we stop accumulating duplicate code: 1. Consolidate same-theme changes into one PR instead of one-PR-per-file. Each of these is really a single idea fanned across many files:
One PR per theme (touching all relevant files) reviews as a unit and lands the same value. 2. Reuse the existing shared helpers instead of re-implementing them per file — this is already duplicating code in
3. No need for a separate issue + PR per micro-fix — grouping related findings keeps the queue reviewable. None of this knocks the work; it's about review throughput and a DRY codebase. Consolidated PRs will get fast-tracked. Thanks! 🙏 中文说明📌 维护者说明 —— 合并同类项与复用 helper(适用于你最近这一系列 先真诚感谢:你提交的量里有不少是真有价值、已经合并的(比如 #5489 —— 后续两点流程上的请求,目的是让 review 跟得上、并停止累积重复代码: 1. 同主题改动合并成一个 PR,而不是一个文件一个 PR。下面每一类其实都是同一个点子散在多个文件:
每个主题一个 PR(覆盖相关文件)更容易作为整体审查,价值一样。 2. 复用已有的共享 helper,不要每个文件各写一份 —— 这已经在
3. 不必为每个微小修复单独开 issue + PR —— 把相关发现归并,队列才审得过来。 这些都不是否定你的工作,只是为了 review 吞吐合理、代码保持 DRY。整合后的 PR 会优先快速过。谢谢!🙏 |
|
@qwen-code /triage |
|
Thanks for the PR! Template looks good ✓ On direction: strict port parsing is a legitimate robustness fix. On approach: the implementation itself is clean and focused — good test coverage, correct validation, fail-fast behavior. However, this PR introduces the third near-identical port parser in the codebase:
I see the existing functions are private to their modules with different error semantics (return Not blocking on this — the code is correct and the tests are solid. But worth thinking about whether a quick extraction PR (or follow-up commit here) makes sense before more callers appear. Moving on to code review. 🔍 中文说明感谢贡献! 模板完整 ✓ 方向:严格端口解析是正确的健壮性修复。 方案:实现本身干净、聚焦——测试覆盖好、校验正确、快速失败。但本 PR 引入了代码库中第三个几乎一样的端口解析器:
现有函数是各自模块私有的、错误语义不同(返回 不作为阻塞项——代码正确、测试扎实。但值得考虑是否在这里加一个后续 commit(或单独的抽取 PR),在更多调用方出现前解决。 进入代码审查 🔍 — Qwen Code · qwen3.7-max |
Code ReviewIndependent proposal: I'd add a strict port parser using The PR matches this approach in logic but places The implementation itself is clean:
One minor note: error message format differs between string path ( No correctness bugs, security holes, or regressions found. Test ResultsUnit tests (5/5 pass): Smoke tests (6/6 pass): Typecheck: server-core ✓, server ✓ Real-Scenario Testing (tmux)Invalid CRAFT_RPC_PORT=9100abcInvalid CRAFT_HEALTH_PORT=3000abcValid ports (RPC=9100, HEALTH=0)All three scenarios behave exactly as expected — invalid ports fail fast with clear errors, valid ports start normally. 中文说明代码审查独立方案:用 PR 的逻辑与方案一致,但 实现本身干净:
一个小问题:错误信息格式在 string 路径( 未发现正确性 bug、安全漏洞或回归。 测试结果单测 5/5 通过、冒烟测试 6/6 通过、类型检查通过。 真实场景测试
三种场景完全符合预期。 — Qwen Code · qwen3.7-max |
|
Verdict: Approve ✅ The fix is correct, well-tested, and does exactly what it says. Invalid port env vars now fail fast with clear errors instead of silently binding a partially-parsed port. Unit tests, smoke tests, typecheck, and real-scenario testing all pass. The one reservation is the third-copy problem — Recommending merge. 中文说明结论:通过 ✅ 修复正确、测试充分、行为完全符合描述。非法端口环境变量现在快速失败并给出清晰错误,而不是静默绑定部分解析出的端口。单测、冒烟测试、类型检查和真实场景测试全部通过。 唯一的顾虑是第三份拷贝问题—— 建议合并。 — Qwen Code · qwen3.7-max |
qwen-code-ci-bot
left a comment
There was a problem hiding this comment.
LGTM, looks ready to ship. The fix is correct and well-tested. The port parser consolidation is a valid follow-up but doesn't block this merge. ✅
|
@tt-a1i — quick note on the triage bot: 中文说明@tt-a1i —— 关于 triage bot 说明一下: |
What this PR does
parseServerPorthelper for desktop server bootstrap ports.CRAFT_RPC_PORTin standalone server bootstrap, standalone WebUI setup, and Electron embedded server env overrides.CRAFT_HEALTH_PORT, parsing the health port before starting the standalone server.Why it's needed
parseIntaccepts partial numbers. That means values likeCRAFT_RPC_PORT=9100abcorCRAFT_HEALTH_PORT=3000abccould be treated as valid ports instead of failing fast. The server should only accept whole decimal TCP port numbers in the0..65535range.Reviewer Test Plan
How to verify
parseServerPort: it should accept whole decimal ports and reject trailing junk, fractions, exponent notation, negatives, and out-of-range values.CRAFT_HEALTH_PORTshould be rejected before starting the WebSocket server.CRAFT_RPC_PORTenv overrides should use the shared strict parser instead ofparseInt.bun test packages/desktop/packages/server-core/src/bootstrap/__tests__/server-port.test.tsfrom the repo root.bun test packages/desktop/packages/server/src/__tests__/smoke.test.tsfrom the repo root.bun run typecheckfrompackages/desktop/packages/server-core.bun run typecheckfrompackages/desktop/packages/server.bun run build:mainfrompackages/desktop/apps/electron.Evidence (Before & After)
Before:
parseInt('9100abc', 10)returned9100, so malformed port env vars could silently bind a partially parsed port. After:CRAFT_RPC_PORT=9100abcandCRAFT_HEALTH_PORT=3000abcboth exit non-zero with a clear invalid port error.Tested on
Environment (optional)
Local desktop workspace after
bun install --cwd packages/desktop --frozen-lockfileand rootnpm ci --ignore-scriptsfor tooling.Risk & Scope
typecheck:allstill fails in existing Electron files unrelated to this PR (src/main/auto-update.tsandsrc/main/handlers/__tests__/settings-default-thinking.test.ts).9100abc,3000.5, or1e3must be changed to plain decimal port numbers.Linked Issues
Fixes #5508
中文说明
What this PR does
parseServerPorthelper。CRAFT_RPC_PORT改成复用严格解析。CRAFT_HEALTH_PORT改成严格解析,并提前到 standalone server 启动前解析。Why it's needed
parseInt会接受部分数字。比如CRAFT_RPC_PORT=9100abc或CRAFT_HEALTH_PORT=3000abc可能被当成合法端口,而不是尽早失败。server 端口环境变量应该只接受0..65535范围内的完整十进制整数。Reviewer Test Plan
How to verify
parseServerPort:应接受完整十进制端口,拒绝尾随字符、小数、指数写法、负数和越界值。CRAFT_HEALTH_PORT应该在 WebSocket server 启动前被拒绝。CRAFT_RPC_PORTenv override 应该使用共享严格 parser,而不是parseInt。Evidence (Before & After)
修复前:
parseInt('9100abc', 10)返回9100,格式错误的端口环境变量可能被静默截断并绑定到部分解析出的端口。修复后:CRAFT_RPC_PORT=9100abc和CRAFT_HEALTH_PORT=3000abc都会非零退出,并打印清晰的 invalid port 错误。Tested on
Environment (optional)
本地 desktop workspace,已运行
bun install --cwd packages/desktop --frozen-lockfile;为了 tooling 也运行了 rootnpm ci --ignore-scripts。Risk & Scope
typecheck:all仍在本 PR 无关的既有 Electron 文件失败(src/main/auto-update.ts和src/main/handlers/__tests__/settings-default-thinking.test.ts)。9100abc、3000.5、1e3这类配置需要改成普通十进制端口数字。Linked Issues
Fixes #5508
AI Assistance Disclosure
I used Codex to review the changes, sanity-check the implementation against existing patterns, and help spot potential edge cases.