|
| 1 | +const { validURL, validateURL } = require("../../../utils/url"); |
| 2 | + |
| 3 | +// Mock the RuntimeSettings module |
| 4 | +jest.mock("../../../utils/runtimeSettings", () => { |
| 5 | + const mockInstance = { |
| 6 | + get: jest.fn(), |
| 7 | + set: jest.fn(), |
| 8 | + }; |
| 9 | + return jest.fn().mockImplementation(() => mockInstance); |
| 10 | +}); |
| 11 | + |
| 12 | +describe("validURL", () => { |
| 13 | + let mockRuntimeSettings; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + const RuntimeSettings = require("../../../utils/runtimeSettings"); |
| 17 | + mockRuntimeSettings = new RuntimeSettings(); |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should validate a valid URL", () => { |
| 22 | + mockRuntimeSettings.get.mockImplementation((key) => { |
| 23 | + if (key === "allowAnyIp") return false; |
| 24 | + if (key === "seenAnyIpWarning") return true; // silence the warning for tests |
| 25 | + return false; |
| 26 | + }); |
| 27 | + |
| 28 | + expect(validURL("https://www.google.com")).toBe(true); |
| 29 | + expect(validURL("http://www.google.com")).toBe(true); |
| 30 | + |
| 31 | + // JS URL does not require extensions, so in theory |
| 32 | + // these should be valid |
| 33 | + expect(validURL("https://random")).toBe(true); |
| 34 | + expect(validURL("http://123")).toBe(true); |
| 35 | + |
| 36 | + // missing protocols |
| 37 | + expect(validURL("www.google.com")).toBe(false); |
| 38 | + expect(validURL("google.com")).toBe(false); |
| 39 | + |
| 40 | + // invalid protocols |
| 41 | + expect(validURL("ftp://www.google.com")).toBe(false); |
| 42 | + expect(validURL("mailto://www.google.com")).toBe(false); |
| 43 | + expect(validURL("tel://www.google.com")).toBe(false); |
| 44 | + expect(validURL("data://www.google.com")).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should block private/local IPs when allowAnyIp is false (default behavior)", () => { |
| 48 | + mockRuntimeSettings.get.mockImplementation((key) => { |
| 49 | + if (key === "allowAnyIp") return false; |
| 50 | + if (key === "seenAnyIpWarning") return true; // silence the warning for tests |
| 51 | + return false; |
| 52 | + }); |
| 53 | + |
| 54 | + expect(validURL("http://192.168.1.1")).toBe(false); |
| 55 | + expect(validURL("http://10.0.0.1")).toBe(false); |
| 56 | + expect(validURL("http://172.16.0.1")).toBe(false); |
| 57 | + |
| 58 | + // But localhost should still be allowed |
| 59 | + expect(validURL("http://127.0.0.1")).toBe(true); |
| 60 | + expect(validURL("http://0.0.0.0")).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should allow any IP when allowAnyIp is true", () => { |
| 64 | + mockRuntimeSettings.get.mockImplementation((key) => { |
| 65 | + if (key === "allowAnyIp") return true; |
| 66 | + if (key === "seenAnyIpWarning") return true; // silence the warning for tests |
| 67 | + return false; |
| 68 | + }); |
| 69 | + |
| 70 | + expect(validURL("http://192.168.1.1")).toBe(true); |
| 71 | + expect(validURL("http://10.0.0.1")).toBe(true); |
| 72 | + expect(validURL("http://172.16.0.1")).toBe(true); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("validateURL", () => { |
| 77 | + it("should return the exact same URL if it's already valid", () => { |
| 78 | + expect(validateURL("https://www.google.com")).toBe("https://www.google.com"); |
| 79 | + expect(validateURL("http://www.google.com")).toBe("http://www.google.com"); |
| 80 | + expect(validateURL("https://random")).toBe("https://random"); |
| 81 | + |
| 82 | + // With numbers as a url this will turn into an ip |
| 83 | + expect(validateURL("123")).toBe("https://0.0.0.123"); |
| 84 | + expect(validateURL("123.123.123.123")).toBe("https://123.123.123.123"); |
| 85 | + expect(validateURL("http://127.0.123.45")).toBe("http://127.0.123.45"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should assume https:// if the URL doesn't have a protocol", () => { |
| 89 | + expect(validateURL("www.google.com")).toBe("https://www.google.com"); |
| 90 | + expect(validateURL("google.com")).toBe("https://google.com"); |
| 91 | + expect(validateURL("ftp://www.google.com")).toBe("ftp://www.google.com"); |
| 92 | + expect(validateURL("mailto://www.google.com")).toBe("mailto://www.google.com"); |
| 93 | + expect(validateURL("tel://www.google.com")).toBe("tel://www.google.com"); |
| 94 | + expect(validateURL("data://www.google.com")).toBe("data://www.google.com"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should remove trailing slashes post-validation", () => { |
| 98 | + expect(validateURL("https://www.google.com/")).toBe("https://www.google.com"); |
| 99 | + expect(validateURL("http://www.google.com/")).toBe("http://www.google.com"); |
| 100 | + expect(validateURL("https://random/")).toBe("https://random"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should handle edge cases and bad data inputs", () => { |
| 104 | + expect(validateURL({})).toBe(""); |
| 105 | + expect(validateURL(null)).toBe(""); |
| 106 | + expect(validateURL(undefined)).toBe(""); |
| 107 | + expect(validateURL(124512)).toBe(""); |
| 108 | + expect(validateURL("")).toBe(""); |
| 109 | + expect(validateURL(" ")).toBe(""); |
| 110 | + expect(validateURL(" look here! ")).toBe("look here!"); |
| 111 | + }); |
| 112 | +}); |
0 commit comments