Adding unit test and fixing an issue with DNS solving

This commit is contained in:
David Négrier 2021-07-21 18:42:20 +02:00
parent 1bb6d893e0
commit 1435ec89c9
2 changed files with 30 additions and 2 deletions

View File

@ -36,8 +36,10 @@ class MapFetcher {
/** /**
* Returns true if the domain name is localhost of *.localhost * Returns true if the domain name is localhost of *.localhost
* Returns true if the domain name resolves to an IP address that is "private" (like 10.x.x.x or 192.168.x.x) * Returns true if the domain name resolves to an IP address that is "private" (like 10.x.x.x or 192.168.x.x)
*
* @private
*/ */
private async isLocalUrl(url: string): Promise<boolean> { async isLocalUrl(url: string): Promise<boolean> {
const urlObj = new URL(url); const urlObj = new URL(url);
if (urlObj.hostname === "localhost" || urlObj.hostname.endsWith(".localhost")) { if (urlObj.hostname === "localhost" || urlObj.hostname.endsWith(".localhost")) {
return true; return true;
@ -46,7 +48,7 @@ class MapFetcher {
let addresses = []; let addresses = [];
if (!ipaddr.isValid(urlObj.hostname)) { if (!ipaddr.isValid(urlObj.hostname)) {
const resolver = new Resolver(); const resolver = new Resolver();
addresses = await promisify(resolver.resolve)(urlObj.hostname); addresses = await promisify(resolver.resolve).bind(resolver)(urlObj.hostname);
} else { } else {
addresses = [urlObj.hostname]; addresses = [urlObj.hostname];
} }

View File

@ -0,0 +1,26 @@
import { arrayIntersect } from "../src/Services/ArrayHelper";
import { mapFetcher } from "../src/Services/MapFetcher";
describe("MapFetcher", () => {
it("should return true on localhost ending URLs", async () => {
expect(await mapFetcher.isLocalUrl("https://localhost")).toBeTrue();
expect(await mapFetcher.isLocalUrl("https://foo.localhost")).toBeTrue();
});
it("should return true on DNS resolving to a local domain", async () => {
expect(await mapFetcher.isLocalUrl("https://127.0.0.1.nip.io")).toBeTrue();
});
it("should return true on an IP resolving to a local domain", async () => {
expect(await mapFetcher.isLocalUrl("https://127.0.0.1")).toBeTrue();
expect(await mapFetcher.isLocalUrl("https://192.168.0.1")).toBeTrue();
});
it("should return false on an IP resolving to a global domain", async () => {
expect(await mapFetcher.isLocalUrl("https://51.12.42.42")).toBeFalse();
});
it("should return false on an DNS resolving to a global domain", async () => {
expect(await mapFetcher.isLocalUrl("https://maps.workadventu.re")).toBeFalse();
});
});