Files
lunch/tests/fetch-safe.test.ts
kai 9dcd02dffe
release / which version (push) Successful in 2s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 57s
release / tag and release (push) Successful in 3s
Dishes: pool, classics, portions, own dishes, photos, recipe link (26.11.6) (#7)
2026-11-06 21:42:10 +00:00

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);
});
});