Skip to content

Commit 331eb0d

Browse files
authored
fix(huggingface): discover models when HTTP proxy is required (#110924)
1 parent b6f905b commit 331eb0d

2 files changed

Lines changed: 64 additions & 12 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Hugging Face proxy tests cover the live model discovery transport policy.
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
4+
const fetchWithSsrFGuardMock = vi.hoisted(() => vi.fn());
5+
6+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({
7+
...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()),
8+
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
9+
}));
10+
11+
import { discoverHuggingfaceModels } from "./models.js";
12+
13+
const ORIGINAL_VITEST = process.env.VITEST;
14+
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
15+
16+
function restoreEnv(key: "VITEST" | "NODE_ENV", value: string | undefined) {
17+
if (value === undefined) {
18+
delete process.env[key];
19+
} else {
20+
process.env[key] = value;
21+
}
22+
}
23+
24+
afterEach(() => {
25+
restoreEnv("VITEST", ORIGINAL_VITEST);
26+
restoreEnv("NODE_ENV", ORIGINAL_NODE_ENV);
27+
fetchWithSsrFGuardMock.mockReset();
28+
vi.restoreAllMocks();
29+
});
30+
31+
describe("Hugging Face model discovery proxy policy", () => {
32+
it("allows the guarded official catalog request to use an eligible HTTP proxy", async () => {
33+
process.env.VITEST = "false";
34+
process.env.NODE_ENV = "development";
35+
const response = new Response("unavailable", { status: 503 });
36+
const release = vi.fn(async () => undefined);
37+
fetchWithSsrFGuardMock.mockResolvedValue({ response, release });
38+
39+
await discoverHuggingfaceModels("hf_test_token");
40+
41+
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
42+
expect.objectContaining({
43+
mode: "trusted_env_proxy",
44+
url: "https://router.huggingface.co/v1/models",
45+
}),
46+
);
47+
expect(release).toHaveBeenCalledTimes(1);
48+
});
49+
});

‎extensions/huggingface/models.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Huggingface plugin module implements models behavior.
2+
import { withTrustedEnvProxyGuardedFetchMode } from "openclaw/plugin-sdk/fetch-runtime";
23
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
34
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
45
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-types";
@@ -139,19 +140,21 @@ export async function discoverHuggingfaceModels(
139140

140141
try {
141142
const requestTimeoutMs = resolveTimerTimeoutMs(timeoutMs, HUGGINGFACE_DISCOVERY_TIMEOUT_MS);
142-
const { response, release } = await fetchWithSsrFGuard({
143-
url: `${HUGGINGFACE_BASE_URL}/models`,
144-
init: {
145-
signal: AbortSignal.timeout(requestTimeoutMs),
146-
headers: {
147-
Authorization: `Bearer ${trimmedKey}`,
148-
"Content-Type": "application/json",
143+
const { response, release } = await fetchWithSsrFGuard(
144+
withTrustedEnvProxyGuardedFetchMode({
145+
url: `${HUGGINGFACE_BASE_URL}/models`,
146+
init: {
147+
signal: AbortSignal.timeout(requestTimeoutMs),
148+
headers: {
149+
Authorization: `Bearer ${trimmedKey}`,
150+
"Content-Type": "application/json",
151+
},
149152
},
150-
},
151-
timeoutMs: requestTimeoutMs,
152-
policy: ssrfPolicyFromHttpBaseUrlAllowedHostname(HUGGINGFACE_BASE_URL),
153-
auditContext: "huggingface-model-discovery",
154-
});
153+
timeoutMs: requestTimeoutMs,
154+
policy: ssrfPolicyFromHttpBaseUrlAllowedHostname(HUGGINGFACE_BASE_URL),
155+
auditContext: "huggingface-model-discovery",
156+
}),
157+
);
155158
try {
156159
if (!response.ok) {
157160
await response.body?.cancel().catch(() => undefined);

0 commit comments

Comments
 (0)