Skip to content

Commit

Permalink
fix(crypto): relax network interface check for seednode ips (#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and air1one committed Jan 20, 2020
1 parent 3766146 commit 2824ac6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
22 changes: 22 additions & 0 deletions __tests__/unit/crypto/utils/is-valid-peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ describe("isValidPeer", () => {
expect(isValidPeer({ ip: "5.196.105.32" })).toBeTrue();
expect(isValidPeer({ ip: "5.196.105.32" })).toBeTrue();
});

it("should be ok if IP is from network interface", () => {
const ips = [
"167.114.29.51",
"167.114.29.52",
"167.114.29.53",
"167.114.29.54",
"167.114.29.55"
];

const spy = jest.spyOn(os, "networkInterfaces").mockReturnValue({
"eth0": ips.map(ip => ({ address: ip }) as any)
});

for (const ip of ips) {
expect(isValidPeer({ ip }, false)).toBeTrue();
expect(isValidPeer({ ip })).toBeFalse();
}

spy.mockRestore();
});

});
19 changes: 11 additions & 8 deletions packages/crypto/src/utils/is-valid-peer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { parse, process } from "ipaddr.js";
import os from "os";

export const isLocalHost = (ip: string): boolean => {
export const isLocalHost = (ip: string, includeNetworkInterfaces: boolean = true): boolean => {
try {
const parsed = parse(ip);
if (parsed.range() === "loopback" || ip.startsWith("0") || ["127.0.0.1", "::ffff:127.0.0.1"].includes(ip)) {
return true;
}

const interfaces: {
[index: string]: os.NetworkInterfaceInfo[];
} = os.networkInterfaces();
if (includeNetworkInterfaces) {
const interfaces: {
[index: string]: os.NetworkInterfaceInfo[];
} = os.networkInterfaces();

return Object.keys(interfaces).some(ifname => interfaces[ifname].some(iface => iface.address === ip));
return Object.keys(interfaces).some(ifname => interfaces[ifname].some(iface => iface.address === ip));
}

return false;
} catch (error) {
return false;
}
Expand All @@ -26,14 +30,13 @@ const sanitizeRemoteAddress = (ip: string): string | undefined => {
}
};

export const isValidPeer = (peer: { ip: string; status?: string | number }): boolean => {
export const isValidPeer = (peer: { ip: string }, includeNetworkInterfaces: boolean = true): boolean => {
peer.ip = sanitizeRemoteAddress(peer.ip);

if (!peer.ip) {
return false;
}

if (isLocalHost(peer.ip)) {
if (isLocalHost(peer.ip, includeNetworkInterfaces)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/validation/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const vendorField = (ajv: Ajv) => {
const validPeer = (ajv: Ajv) => {
ajv.addFormat("peer", (ip: string) => {
try {
return isValidPeer({ ip });
return isValidPeer({ ip }, false);
} catch {
return false;
}
Expand Down

0 comments on commit 2824ac6

Please sign in to comment.