Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(providers): reject malformed successful model responses

Afloat73e79a2821de
diff --git a/server/provider-key-check.test.ts b/server/provider-key-check.test.ts
index c7602f4189..121ce640e6 100644
--- a/server/provider-key-check.test.ts
+++ b/server/provider-key-check.test.ts
@@ -110,6 +110,21 @@ describe("provider key check", () => {
expect(await checkProviderKey({ provider: "xai", key: "good-key", url: "http://127.0.0.1:1" })).toEqual({ ok: false, reason: "unreachable" });
});
+ it.each([{}, { error: { message: "invalid key" } }, { data: null }, { data: {} }, { models: "unavailable" }])(
+ "does not report a successful model check for HTTP 200 with %j",
+ async body => {
+ const provider: typeof fetch = async () => Response.json(body);
+ expect(await checkProviderKey({ provider: "openaiCompat", key: "fixture", url: "https://models.example.test/v1" }, provider))
+ .toEqual({ ok: false, reason: "unexpected", status: 200 });
+ },
+ );
+
+ it.each([[], { data: [] }, { models: [] }])("accepts a structurally valid empty catalog %j", async body => {
+ const provider: typeof fetch = async () => Response.json(body);
+ expect(await checkProviderKey({ provider: "mistral", key: "fixture" }, provider))
+ .toEqual({ ok: true, check: "models", models: [] });
+ });
+
it("never approves an absent key even when a provider's catalog is public", async () => {
let calls = 0;
const publicCatalog: typeof fetch = async () => { calls++; return Response.json({ data: [] }); };
diff --git a/server/provider-key-check.ts b/server/provider-key-check.ts
index 17f77dcabf..3af800d200 100644
--- a/server/provider-key-check.ts
+++ b/server/provider-key-check.ts
@@ -42,9 +42,10 @@ export function providerModelsUrl(provider: ProviderKeyKind, base?: string | nul
return `${root}/models`;
}
-function modelIds(body: unknown): string[] {
+function modelIds(body: unknown): string[] | null {
const record = body && typeof body === "object" ? (body as { data?: unknown; models?: unknown }) : null;
- const list = Array.isArray(body) ? body : Array.isArray(record?.data) ? record.data : Array.isArray(record?.models) ? record.models : [];
+ const list = Array.isArray(body) ? body : Array.isArray(record?.data) ? record.data : Array.isArray(record?.models) ? record.models : null;
+ if (!list) return null;
const ids: string[] = [];
for (const entry of list) {
const item = entry && typeof entry === "object" ? (entry as { id?: unknown; name?: unknown }) : null;
@@ -92,7 +93,10 @@ export async function checkProviderKey(
if (typeof freeTier !== "boolean") return { ok: false, reason: "unexpected", status: response.status };
return { ok: true, check: "authentication", models: [], freeTier };
}
- return { ok: true, check: "models", models: modelIds(body) };
+ const models = modelIds(body);
+ return models === null
+ ? { ok: false, reason: "unexpected", status: response.status }
+ : { ok: true, check: "models", models };
} catch {
return { ok: false, reason: "unreachable" };
} finally {