19 lines
1.1 KiB
TypeScript
19 lines
1.1 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("server-only", () => ({}));
|
|
|
|
describe("fetch-safe", () => {
|
|
it("knows private and special addresses", async () => {
|
|
const { isPrivateAddress } = await import("@/lib/fetch-safe");
|
|
for (const ip of ["10.1.2.3", "127.0.0.1", "192.168.1.1", "172.20.0.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fd00::1", "fe80::1", "::ffff:10.0.0.1"])
|
|
expect([ip, isPrivateAddress(ip)]).toEqual([ip, true]);
|
|
for (const ip of ["8.8.8.8", "93.184.216.34", "2606:4700::1111"]) expect([ip, isPrivateAddress(ip)]).toEqual([ip, false]);
|
|
});
|
|
|
|
it("refuses non-http schemes, odd ports, local names and private hosts before any request", async () => {
|
|
const { fetchPublic, FetchRefused } = await import("@/lib/fetch-safe");
|
|
for (const url of ["file:///etc/passwd", "ftp://example.org/", "http://example.org:8080/", "http://user:pw@example.org/", "http://localhost/", "http://lunch.svc.cluster.local/", "http://127.0.0.1/", "http://[::1]/", "http://192.168.1.10/rezept"])
|
|
await expect(fetchPublic(url)).rejects.toBeInstanceOf(FetchRefused);
|
|
});
|
|
});
|