diff --git a/back/src/Services/MapFetcher.ts b/back/src/Services/MapFetcher.ts index 99465ac4..0a8cb4bd 100644 --- a/back/src/Services/MapFetcher.ts +++ b/back/src/Services/MapFetcher.ts @@ -36,8 +36,10 @@ class MapFetcher { /** * 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) + * + * @private */ - private async isLocalUrl(url: string): Promise { + async isLocalUrl(url: string): Promise { const urlObj = new URL(url); if (urlObj.hostname === "localhost" || urlObj.hostname.endsWith(".localhost")) { return true; @@ -46,7 +48,7 @@ class MapFetcher { let addresses = []; if (!ipaddr.isValid(urlObj.hostname)) { const resolver = new Resolver(); - addresses = await promisify(resolver.resolve)(urlObj.hostname); + addresses = await promisify(resolver.resolve).bind(resolver)(urlObj.hostname); } else { addresses = [urlObj.hostname]; } diff --git a/back/tests/MapFetcherTest.ts b/back/tests/MapFetcherTest.ts new file mode 100644 index 00000000..3b47e73b --- /dev/null +++ b/back/tests/MapFetcherTest.ts @@ -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(); + }); +});