Skip to content

Commit 9a50907

Browse files
authored
fix(validateHostname): add 169.254.0.0/16 range to not allowed hostnames (#101)
1 parent 0ce7d3a commit 9a50907

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/_internal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export function _getLocalHosts(additional: HostAddress[]): HostAddress[] {
5656
if (
5757
config.address &&
5858
!config.internal &&
59-
!config.address.startsWith("fe80::") // Link-Local
59+
!config.address.startsWith("fe80::") && // Link-Local
60+
!config.address.startsWith("169.254") // reserved for Automatic Private IP Addressing
6061
) {
6162
hosts.add(config.address);
6263
}

test/index.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Server } from "node:net";
2+
import { networkInterfaces } from "node:os";
23
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
34
import { getPort, getRandomPort } from "../src";
4-
import { _generateRange } from "../src/_internal";
5+
import { _generateRange, _getLocalHosts } from "../src/_internal";
56
import { blockPort } from "./utils";
67

78
const isWindows = process.platform === "win32";
@@ -165,3 +166,61 @@ describe("internal tools", () => {
165166
});
166167
});
167168
});
169+
170+
vi.mock("node:os", () => {
171+
return {
172+
networkInterfaces: vi.fn(),
173+
};
174+
});
175+
176+
describe("_getLocalHosts", () => {
177+
test("should return the allowed host addresses", () => {
178+
vi.mocked(networkInterfaces).mockImplementation(() => ({
179+
eth0: [
180+
{
181+
address: "192.168.1.100",
182+
family: "IPv4",
183+
internal: false,
184+
netmask: "0",
185+
mac: "0",
186+
cidr: "",
187+
},
188+
{
189+
address: "fe80::1",
190+
family: "IPv6",
191+
internal: false,
192+
scopeid: 1,
193+
netmask: "0",
194+
mac: "0",
195+
cidr: "",
196+
},
197+
],
198+
lo: [
199+
{
200+
address: "127.0.0.1",
201+
family: "IPv4",
202+
internal: true,
203+
netmask: "0",
204+
mac: "0",
205+
cidr: "",
206+
},
207+
{
208+
address: "169.254.0.1",
209+
family: "IPv4",
210+
internal: false,
211+
netmask: "0",
212+
mac: "0",
213+
cidr: "",
214+
},
215+
],
216+
}));
217+
218+
// call the function with additional hosts
219+
const additionalHosts = ["192.168.1.200"];
220+
const result = _getLocalHosts(additionalHosts);
221+
222+
expect(result).toEqual(["192.168.1.200", "192.168.1.100"]);
223+
224+
vi.clearAllMocks();
225+
});
226+
});

0 commit comments

Comments
 (0)