fix(validation): guard non-string apiKey/modelsUrl in connection test (#2463)

A corrupted or mis-typed credential (non-string apiKey, or a non-string
modelsUrl from providerSpecificData/registry) could throw
'TypeError: ... is not a function' when validation called .startsWith()/.trim()
during a provider connection test. Adds typeof guards in validateOpenAILikeProvider,
validateGeminiLikeProvider and validateSnowflakeProvider so validation returns a
clean { valid } result instead of crashing. Does not pinpoint the NVIDIA NIM
e.startsWith report (needs a stack trace), but hardens the whole class.
This commit is contained in:
diegosouzapw
2026-05-21 09:49:47 -03:00
parent 8f63c72396
commit 2e85d80adf
44 changed files with 83 additions and 3 deletions

View File

@@ -304,7 +304,10 @@ async function validateOpenAILikeProvider({
isLocal = false,
}: any) {
try {
const customModelsUrl = modelsUrl?.trim() || "";
// Guard against a non-string modelsUrl reaching .trim()/.startsWith() — a malformed
// providerSpecificData / registry value would otherwise throw a TypeError mid-validation
// ("trim is not a function" / "startsWith is not a function"). See #2463 class.
const customModelsUrl = (typeof modelsUrl === "string" ? modelsUrl.trim() : "") || "";
const endpointUrl = customModelsUrl
? customModelsUrl.startsWith("http")
? customModelsUrl
@@ -754,7 +757,7 @@ async function validateGeminiLikeProvider({
if (authType === "header" || authType === "apikey") {
headers["x-goog-api-key"] = apiKey;
} else if (authType === "oauth" || apiKey.startsWith("ya29.")) {
} else if (authType === "oauth" || (typeof apiKey === "string" && apiKey.startsWith("ya29."))) {
headers["Authorization"] = `Bearer ${apiKey}`;
}
@@ -1179,7 +1182,7 @@ async function validateSnowflakeProvider({ apiKey, providerSpecificData = {} }:
return { valid: false, error: "Missing base URL" };
}
const usesProgrammaticAccessToken = apiKey.startsWith("pat/");
const usesProgrammaticAccessToken = typeof apiKey === "string" && apiKey.startsWith("pat/");
return validateDirectChatProvider({
url: normalizeSnowflakeChatUrl(baseUrl),
headers: {