fix: reject malformed inspected tcp ports · openclaw/openclaw@c00ac95
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -76,19 +76,27 @@ function normalizeTcpHost(host: string): string {
|
76 | 76 | return normalized.startsWith("::ffff:") ? normalized.slice("::ffff:".length) : normalized; |
77 | 77 | } |
78 | 78 | |
| 79 | +function parseTcpPort(raw: string | undefined): number | null { |
| 80 | +if (!raw || !/^\d+$/.test(raw)) { |
| 81 | +return null; |
| 82 | +} |
| 83 | +const port = Number(raw); |
| 84 | +return Number.isSafeInteger(port) && port >= 0 && port <= 65_535 ? port : null; |
| 85 | +} |
| 86 | + |
79 | 87 | function parseTcpEndpoint(raw: string): { host: string; port: number } | null { |
80 | 88 | const endpoint = raw.trim(); |
81 | 89 | const bracketMatch = endpoint.match(/^\[([^\]]+)\]:(\d+)$/); |
82 | 90 | if (bracketMatch) { |
83 | | -const port = Number.parseInt(bracketMatch[2], 10); |
84 | | -return Number.isFinite(port) ? { host: normalizeTcpHost(bracketMatch[1]), port } : null; |
| 91 | +const port = parseTcpPort(bracketMatch[2]); |
| 92 | +return port === null ? null : { host: normalizeTcpHost(bracketMatch[1]), port }; |
85 | 93 | } |
86 | 94 | const lastColon = endpoint.lastIndexOf(":"); |
87 | 95 | if (lastColon <= 0 || lastColon >= endpoint.length - 1) { |
88 | 96 | return null; |
89 | 97 | } |
90 | | -const port = Number.parseInt(endpoint.slice(lastColon + 1), 10); |
91 | | -if (!Number.isFinite(port)) { |
| 98 | +const port = parseTcpPort(endpoint.slice(lastColon + 1)); |
| 99 | +if (port === null) { |
92 | 100 | return null; |
93 | 101 | } |
94 | 102 | return { host: normalizeTcpHost(endpoint.slice(0, lastColon)), port }; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -206,6 +206,8 @@ describeUnix("inspectPortUsage", () => {
|
206 | 206 | "p111\ncnode\nnTCP 127.0.0.1:50123->127.0.0.1:18789 (ESTABLISHED)\n" + |
207 | 207 | "p222\ncnode\nnTCP 127.0.0.1:18789->127.0.0.1:50123 (ESTABLISHED)\n" + |
208 | 208 | "p444\ncnode\nnTCP 127.0.0.1:50125->[::ffff:127.0.0.1]:18789 (ESTABLISHED)\n" + |
| 209 | +"p555\ncnode\nnTCP 127.0.0.1:50126->127.0.0.1:18789abc (ESTABLISHED)\n" + |
| 210 | +"p666\ncnode\nnTCP 127.0.0.1:50127->127.0.0.1:99999 (ESTABLISHED)\n" + |
209 | 211 | "p333\ncBrowser\nnTCP 127.0.0.1:50124->198.51.100.7:18789 (ESTABLISHED)\n", |
210 | 212 | stderr: "", |
211 | 213 | code: 0, |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。