Repository navigation
fix(cli): correctly identify localhost loopback URLs - #67420
Conversation
There was a problem hiding this comment.
The fix works. I called machineReach/formatMachineAdd directly on the base and with this PR. On the base, --url http://localhost.:4096 was reported as "Works from computers that can reach one of these addresses", and 127.example.com was reported as "Only works on this computer". With the PR both are classified correctly, and 127.0.0.1 is still loopback. test/machine.test.ts passes (22 tests) and tsc -b is clean.
Public API: unchanged (isLoopback is internal to the CLI).
One small note: every caller passes URL.hostname, which keeps the brackets on IPv6 ([::1]). So the bare ::1 check never receives real input and could be dropped. It does no harm either way.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
ambujone
left a comment
There was a problem hiding this comment.
Ran bun test test/machine.test.ts in packages/cli on the PR head: 22 pass. With src/ reverted to the parent commit and the new tests kept, 4 fail (localhost., api.localhost., 127.example.com, ::1), so the tests do exercise the change. bun typecheck in packages/cli is clean. I also fed new URL(...).hostname for several inputs through isLoopback: http://localhost.:4096, http://LOCALHOST.:1, http://127.1:4096 (URL normalizes to 127.0.0.1) and http://127.0.0.1.:1 classify as loopback, and 127.example.com and localhost.. do not. One small thing: URL.hostname always brackets IPv6, so the new bare "::1" branch in service-urls.ts is only reachable by calling isLoopback directly; harmless, but you could drop it and the ::1 test case if you want the helper to match its callers (machine/common.ts:41,61 and pair.ts:34). The check at pair.ts:59 uses its own exact-match list and still shows no SSH-forward hint for localhost., which is cosmetic and arguably out of scope.
Issue for this PR
Closes #67419
Type of change
What does this PR do?
isLoopback()did not recognize hostnames ending with a trailing DNS dot, such aslocalhost..This PR fixes the classification by removing one trailing dot before checking localhost names. It also uses Node's
isIP()to ensure that addresses beginning with127.are actually valid IPv4 addresses, preventing hostnames such as127.example.comfrom being incorrectly classified as loopback.Added regression tests covering trailing-dot localhost, IPv4, IPv6, and ordinary hostnames.
How did you verify your code works?
git diff --checkpassed.@babel/typeswas missing).Screenshots / recordings
Not applicable.
Checklist