Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net): check scopeid === 0 in getNetworkAddress() #4879

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion net/get_network_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getNetworkAddress(
// Cannot lie within 127.0.0.0/8
? !i.address.startsWith("127")
// Cannot be loopback or link-local addresses
: !(i.address === "::1" || i.address === "fe80::1"))
: !(i.address === "::1" || i.address === "fe80::1") && i.scopeid === 0)
)
?.address;
}
29 changes: 27 additions & 2 deletions net/get_network_address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const INTERFACES: Deno.NetworkInterfaceInfo[] = [
cidr: "fe80::1/64",
mac: "00:00:00:00:00:00",
},
{
family: "IPv6",
name: "utun2",
address: "fe80::7c00:d02e:f1ae:3980",
netmask: "ffff:ffff:ffff:ffff::",
scopeid: 17,
cidr: "fe80::7c00:d02e:f1ae:3980/64",
mac: "00:00:00:00:00:00"
},
// Network accessible
{
family: "IPv4",
Expand Down Expand Up @@ -60,7 +69,15 @@ Deno.test("getNetworkAddress() works with IPv4", () => {
() => INTERFACES,
);
const hostname = getNetworkAddress();
assertEquals(hostname, INTERFACES[3]!.address);
assertEquals(hostname, INTERFACES[4]!.address);
});

Deno.test("getNetworkAddress() returns listenable IPv4 address", () => {
const hostname = getNetworkAddress();
// Only do this test if listenable network interfaces exist
if (hostname !== undefined) {
using _listener = Deno.listen({ hostname, port: 0 });
}
});

Deno.test("getNetworkAddress() works with IPv6", () => {
Expand All @@ -70,5 +87,13 @@ Deno.test("getNetworkAddress() works with IPv6", () => {
() => INTERFACES,
);
const hostname = getNetworkAddress("IPv6");
assertEquals(hostname, INTERFACES[4]!.address);
assertEquals(hostname, INTERFACES[5]!.address);
});

Deno.test("getNetworkAddress() returns listenable IPv6 address", () => {
const hostname = getNetworkAddress("IPv6");
// Only do this test if listenable network interfaces exist
if (hostname !== undefined) {
using _listener = Deno.listen({ hostname, port: 0 });
}
});