Compare commits

..

1 Commits

Author SHA1 Message Date
Markus Hartung
97a1db9cdb fix(providers): validate Dify keys against native /v1/chat-messages endpoint (#11002) 2026-08-21 19:52:13 -03:00
9 changed files with 197 additions and 42 deletions

View File

@@ -1 +0,0 @@
- fix(providers): mark the blackbox provider deprecated — api.blackbox.ai returns HTTP 404 on every path variant (sweep 2026-08-21), so the public inference surface is dead and the catalog entry now carries a deprecation notice. ([#10997](https://github.com/diegosouzapw/OmniRoute/issues/10997))

View File

@@ -0,0 +1 @@
- fix(providers): validate Dify keys against its native /v1/chat-messages endpoint (#11002)

View File

@@ -5,12 +5,6 @@ export const blackboxProvider: RegistryEntry = {
alias: "bb",
format: "openai",
executor: "default",
// NOTE: api.blackbox.ai returns HTTP 404 on /v1/chat/completions and /v1/models
// (empty body, all path variants) since sweep 2026-08-21; the public inference
// surface has moved to the gated enterprise.blackbox.ai/v1 endpoint. The provider
// is marked deprecated in src/shared/constants/providers/apikey/frontier-labs.ts —
// this registry entry is kept intact (registration/execution unaffected), so
// existing configured keys keep working if a restored/enterprise host is reachable.
baseUrl: "https://api.blackbox.ai/v1/chat/completions",
modelsUrl: "https://api.blackbox.ai/v1/models",
authType: "apikey",

View File

@@ -5,7 +5,11 @@ export const difyProvider: RegistryEntry = {
alias: "dify",
format: "openai",
executor: "default",
baseUrl: "https://api.dify.ai/v1/chat/completions",
// Dify does not serve /chat/completions — its native completion route is
// POST /v1/chat-messages (validated via the dedicated dify validator, #11002).
// Keep this as the bare API root so route suffixes build correctly and
// self-hosted instances can override the base URL per connection.
baseUrl: "https://api.dify.ai",
authType: "apikey",
authHeader: "bearer",
models: [{ id: "auto", name: "Auto" }],

View File

@@ -108,6 +108,7 @@ import {
validateBytezProvider,
} from "./validation/webCookie";
import { validateAiHordeProvider } from "./validation/aihorde";
import { validateDifyProvider } from "./validation/dify";
import { validateAdobeFireflyProvider } from "./validation/adobeFirefly";
import {
validateV0VercelProvider,
@@ -230,6 +231,10 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
freebuff: validateFreebuffProvider,
"command-code": validateCommandCodeProvider,
huggingface: validateHuggingFaceProvider,
// #11002: Dify serves no OpenAI-compatible route — only POST /v1/chat-messages.
// The generic OpenAI-like probe 404s on /v1/models and /v1/chat/completions,
// so every real app key was misreported as "endpoint not supported".
dify: validateDifyProvider,
// #5422: auth-only probe — Bytez 404s on every chat model until the account adds it to
// its catalog, so the generic chat probe can't validate a fresh key.
bytez: validateBytezProvider,

View File

@@ -0,0 +1,86 @@
/**
* Dify key check. Dify (multi-app LLM "LLMOps" platform) does NOT expose an
* OpenAI-compatible HTTP API. Its native completion endpoint is
* `POST {base}/v1/chat-messages` (body `inputs`/`query`/`response_mode`/`user`
* — no `model`/`messages` envelope). There is no `/v1/models` listing, so the
* generic OpenAI-like probe (GET /v1/models → POST /v1/chat/completions)
* always 404s and every real Dify app key is misreported as
* "Provider validation endpoint not supported" (#11002).
*
* Dify itself returns a clean 401 {"code":"unauthorized"} for a bad app key on
* `/v1/chat-messages`, and 200 for a valid key, so a single POST there is the
* correct auth probe.
*/
import { getRegistryEntry } from "@omniroute/open-sse/config/providerRegistry.ts";
import { normalizeBaseUrl } from "./urlHelpers";
import { toValidationErrorResult, validationWrite } from "./transport";
/**
* Shape a provider/connection base URL into the Dify native completion route.
* Accepts the cloud root (`https://api.dify.ai`), a `/v1` root, or a full
* `/v1/chat-messages` URL (e.g. a self-hosted instance) and always returns
* `{base}/v1/chat-messages`.
*/
export function resolveDifyChatMessagesUrl(baseUrl: string) {
const normalized = normalizeBaseUrl(baseUrl);
if (!normalized) return "";
const cleaned = normalized.replace(/\/chat-messages$/, "").replace(/\/v1$/, "");
return `${cleaned}/v1/chat-messages`;
}
/** Pure status→verdict mapping, unit-testable without network. */
export function difyValidationResultFromStatus(status: number) {
if (status === 401 || status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (status >= 200 && status < 300) {
return { valid: true, error: null };
}
return { valid: false, error: `Dify validation failed (${status})` };
}
export async function validateDifyProvider({
apiKey,
providerSpecificData = {},
fetchImpl = validationWrite,
}: {
apiKey?: unknown;
providerSpecificData?: Record<string, unknown>;
fetchImpl?: typeof validationWrite;
}) {
const key = typeof apiKey === "string" ? apiKey.trim() : "";
if (!key) {
return { valid: false, error: "API key required" };
}
const specificBase =
typeof providerSpecificData?.baseUrl === "string" ? providerSpecificData.baseUrl.trim() : "";
const entryBase = (getRegistryEntry("dify")?.baseUrl as string) || "";
const probeUrl = resolveDifyChatMessagesUrl(specificBase || entryBase);
if (!probeUrl) {
return { valid: false, error: "Dify requires a Base URL" };
}
try {
const response = await fetchImpl(
probeUrl,
{
method: "POST",
headers: {
Authorization: `Bearer ${key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
inputs: {},
query: "ping",
response_mode: "blocking",
user: "omniroute-key-check",
}),
},
false
);
return difyValidationResultFromStatus(response.status);
} catch (error) {
return toValidationErrorResult(error);
}
}

View File

@@ -91,11 +91,6 @@ export const APIKEY_PROVIDERS_FRONTIER = {
hasFree: true,
freeNote:
"Limited free access is available through Blackbox; model availability and account limits apply",
subscriptionRisk: true,
riskNoticeVariant: "deprecated",
deprecated: true,
deprecationReason:
"api.blackbox.ai returns HTTP 404 on every path variant (sweep 2026-08-21); the public inference surface has moved to the gated enterprise.blackbox.ai/v1 endpoint.",
},
xai: {
id: "xai",

View File

@@ -1,29 +0,0 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const __dirname = dirname(fileURLToPath(import.meta.url));
const blackboxRegistryPath = join(
__dirname,
"../../open-sse/config/providers/registry/blackbox/index.ts",
);
const blackboxMetaPath = join(
__dirname,
"../../src/shared/constants/providers/apikey/frontier-labs.ts",
);
test("probe: blackbox runtime registry still points at api.blackbox.ai (bug present)", () => {
const src = readFileSync(blackboxRegistryPath, "utf8");
assert.match(src, /api\.blackbox\.ai\/v1\/chat\/completions/);
assert.match(src, /api\.blackbox\.ai\/v1\/models/);
});
test("guard: blackbox metadata is marked deprecated because api.blackbox.ai is dead", () => {
const meta = readFileSync(blackboxMetaPath, "utf8");
const block = meta.split(/\n\s*blackbox:\s*\{/)[1]?.split(/\n\s*\w+:\s*\{/)[0] ?? "";
assert.ok(block.includes("deprecated: true"));
assert.ok(block.includes('riskNoticeVariant: "deprecated"'));
assert.ok(block.includes("deprecationReason"));
});

View File

@@ -0,0 +1,100 @@
import test from "node:test";
import assert from "node:assert/strict";
import { after, before } from "node:test";
import { createServer, type Server } from "node:http";
import { readFileSync } from "node:fs";
import { validateProviderApiKey } from "../../src/lib/providers/validation.ts";
import {
difyValidationResultFromStatus,
resolveDifyChatMessagesUrl,
} from "../../src/lib/providers/validation/dify.ts";
import { difyProvider } from "../../open-sse/config/providers/registry/dify/index.ts";
// #11002 — the `dify` provider is registered with format:"openai", so the generic OpenAI-like
// validation probe hits GET /v1/models then POST /v1/chat/completions. Dify's native API serves
// neither — it only exposes POST /v1/chat-messages (401 {"code":"unauthorized"} for a bad key).
// Every real Dify app key therefore fails validation with the generic
// "Provider validation endpoint not supported" instead of a clean invalid/valid verdict.
//
// The fake upstream below is Dify-faithful: /v1/models and /v1/chat/completions 404, while
// /v1/chat-messages is the only route and answers 401 for a bad key.
let server: Server;
let baseUrl = "";
before(async () => {
server = createServer((req, res) => {
const path = (req.url || "").split("?")[0];
if (path === "/v1/models") {
res.writeHead(404, { "content-type": "text/plain" });
res.end("Not Found");
} else if (path === "/v1/chat/completions") {
res.writeHead(404, { "content-type": "text/html" });
res.end("<html>404 Not Found</html>");
} else if (path === "/v1/chat-messages") {
res.writeHead(401, { "content-type": "application/json" });
res.end(JSON.stringify({ code: "unauthorized", message: "Access token is invalid" }));
} else {
res.writeHead(404, { "content-type": "text/plain" });
res.end("Not Found");
}
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
if (!address || typeof address === "string") throw new Error("no assigned port");
baseUrl = `http://127.0.0.1:${address.port}`;
});
after(async () => {
await new Promise<void>((resolve, reject) =>
server.close((err) => (err ? reject(err) : resolve()))
);
});
test("#11002 dify key validation probes /v1/chat-messages and rejects a bad key", async () => {
const result = await validateProviderApiKey({
provider: "dify",
apiKey: "app-test-key",
providerSpecificData: { baseUrl },
});
assert.equal(result.valid, false);
assert.equal(result.error, "Invalid API key");
});
test("#11002 dify status→result maps bad keys and valid keys", () => {
assert.deepEqual(difyValidationResultFromStatus(401), {
valid: false,
error: "Invalid API key",
});
assert.deepEqual(difyValidationResultFromStatus(403), {
valid: false,
error: "Invalid API key",
});
assert.deepEqual(difyValidationResultFromStatus(200), { valid: true, error: null });
assert.deepEqual(difyValidationResultFromStatus(500), {
valid: false,
error: "Dify validation failed (500)",
});
});
test("#11002 resolveDifyChatMessagesUrl always targets /v1/chat-messages", () => {
assert.equal(resolveDifyChatMessagesUrl("https://api.dify.ai"), "https://api.dify.ai/v1/chat-messages");
assert.equal(
resolveDifyChatMessagesUrl("https://selfhosted.example.com/v1"),
"https://selfhosted.example.com/v1/chat-messages"
);
assert.equal(
resolveDifyChatMessagesUrl("https://selfhosted.example.com/v1/chat-messages"),
"https://selfhosted.example.com/v1/chat-messages"
);
});
test("#11002 dify registry baseUrl is the bare API root, not /chat/completions", () => {
assert.equal(difyProvider.baseUrl, "https://api.dify.ai");
const src = readFileSync(
new URL("../../src/lib/providers/validation.ts", import.meta.url),
"utf8"
);
assert.match(src, /dify:\s*validateDifyProvider/);
});