test: isolate media network fetches

This commit is contained in:
Peter Steinberger
2026-04-10 23:56:00 +01:00
parent 6bc7822ec7
commit f3abc0c076
3 changed files with 16 additions and 13 deletions

View File

@@ -178,7 +178,7 @@ describe("fetchWithSlackAuth", () => {
describe("resolveSlackMedia", () => {
beforeEach(() => {
mockFetch = vi.fn();
globalThis.fetch = withFetchPreconnect(mockFetch);
globalThis.fetch = mockFetch as unknown as typeof fetch;
mockPinnedHostnameResolution();
});
@@ -655,16 +655,8 @@ describe("Slack media SSRF policy", () => {
describe("resolveSlackAttachmentContent", () => {
beforeEach(() => {
mockFetch = vi.fn();
globalThis.fetch = withFetchPreconnect(mockFetch);
vi.spyOn(ssrf, "resolvePinnedHostnameWithPolicy").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
const addresses = ["93.184.216.34"];
return {
hostname: normalized,
addresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses }),
};
});
globalThis.fetch = mockFetch as unknown as typeof fetch;
mockPinnedHostnameResolution();
});
afterEach(() => {

View File

@@ -122,6 +122,7 @@ export async function fetchWithSlackAuth(url: string, token: string): Promise<Re
const SLACK_MEDIA_SSRF_POLICY = {
allowedHostnames: ["*.slack.com", "*.slack-edge.com", "*.slack-files.com"],
hostnameAllowlist: ["*.slack.com", "*.slack-edge.com", "*.slack-files.com"],
allowRfc2544BenchmarkRange: true,
};

View File

@@ -3,7 +3,7 @@ import * as ssrf from "../infra/net/ssrf.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
export function mockPinnedHostnameResolution(addresses: string[] = ["93.184.216.34"]) {
return vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
const resolve = async (hostname: string) => {
const normalized = normalizeLowercaseStringOrEmpty(hostname).replace(/\.$/, "");
const pinnedAddresses = [...addresses];
return {
@@ -11,5 +11,15 @@ export function mockPinnedHostnameResolution(addresses: string[] = ["93.184.216.
addresses: pinnedAddresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses: pinnedAddresses }),
};
});
};
const pinned = vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(resolve);
const pinnedWithPolicy = vi
.spyOn(ssrf, "resolvePinnedHostnameWithPolicy")
.mockImplementation(async (hostname) => resolve(hostname));
return {
mockRestore: () => {
pinned.mockRestore();
pinnedWithPolicy.mockRestore();
},
};
}