mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-12 17:51:22 +00:00
test: isolate media network fetches
This commit is contained in:
@@ -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(() => {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user