Files
outline/server/utils/fetch.test.ts
T
Tom Moor 6c5d65b961 fix: Block all special-use addresses when fetching client metadata documents
ALLOWED_PRIVATE_IP_ADDRESSES no longer applies to client ID metadata
document requests (draft -02 section 8.6). The fetch wrapper takes an
allowIPAddressList option to override the environment list.
2026-10-11 22:14:52 -04:00

52 lines
1.6 KiB
TypeScript

import http from "node:http";
import env from "@server/env";
import { server as msw } from "@server/test/msw";
import fetch from "./fetch";
describe("fetch", () => {
const allowedPrivateIPAddresses = env.ALLOWED_PRIVATE_IP_ADDRESSES;
let server: http.Server;
let url: string;
beforeAll(async () => {
// The request interceptor swallows socket errors from the filtering
// agent, so these requests use the real network stack.
msw.close();
server = http.createServer((_req, res) => res.end("ok"));
await new Promise<void>((resolve) =>
server.listen(0, "127.0.0.1", resolve)
);
const address = server.address();
const port = address && typeof address === "object" ? address.port : 0;
url = `http://127.0.0.1:${port}/`;
});
afterAll(async () => {
await new Promise((resolve) => server.close(resolve));
msw.listen({ onUnhandledRequest: "error" });
});
afterEach(() => {
env.ALLOWED_PRIVATE_IP_ADDRESSES = allowedPrivateIPAddresses;
});
it("should block a private address", async () => {
env.ALLOWED_PRIVATE_IP_ADDRESSES = [];
await expect(fetch(url)).rejects.toThrow();
});
it("should allow a private address in ALLOWED_PRIVATE_IP_ADDRESSES", async () => {
env.ALLOWED_PRIVATE_IP_ADDRESSES = ["127.0.0.1"];
const res = await fetch(url);
expect(res.status).toEqual(200);
});
it("should block an allowed private address when given an empty allow list", async () => {
env.ALLOWED_PRIVATE_IP_ADDRESSES = ["127.0.0.1"];
await expect(fetch(url, { allowIPAddressList: [] })).rejects.toThrow();
});
});