Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix(api): harden provider sync and stream escaping
diegosouzapw388e0fe845d9
diff --git a/.github/workflows/deploy-vps.yml b/.github/workflows/deploy-vps.ymlindex b49b2eae85..e1f572202c 100644--- a/.github/workflows/deploy-vps.yml+++ b/.github/workflows/deploy-vps.yml@@ -6,6 +6,9 @@ on:types: [completed]workflow_dispatch:+permissions:+ contents: read+jobs:deploy:if: >-diff --git a/open-sse/services/combo.ts b/open-sse/services/combo.tsindex 7f86b8e9f4..83d981d088 100644--- a/open-sse/services/combo.ts+++ b/open-sse/services/combo.ts@@ -618,7 +618,7 @@ export async function handleComboChat({// Inject tag at the beginning of the first content valueconst injected = text.replace(/"content":"([^"]+)/,- `"content":"${tagContent.replace(/"/g, '\\"')}$1`+ `"content":"${tagContent.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}$1`);tagInjected = true;controller.enqueue(encoder.encode(injected));diff --git a/open-sse/translator/request/openai-to-gemini.ts b/open-sse/translator/request/openai-to-gemini.tsindex d03ac45a50..0e61231eb2 100644--- a/open-sse/translator/request/openai-to-gemini.ts+++ b/open-sse/translator/request/openai-to-gemini.ts@@ -10,6 +10,8 @@ import {getDefaultThinkingBudget,} from "../../../src/shared/constants/modelSpecs.ts";+import * as crypto from "crypto";+function generateUUID() {return crypto.randomUUID();}diff --git a/src/app/(dashboard)/dashboard/providers/[id]/page.tsx b/src/app/(dashboard)/dashboard/providers/[id]/page.tsxindex 98257ca453..552ed965d7 100644--- a/src/app/(dashboard)/dashboard/providers/[id]/page.tsx+++ b/src/app/(dashboard)/dashboard/providers/[id]/page.tsx@@ -4278,9 +4278,9 @@ function ConnectionRow({{connection.lastError && connection.isActive !== false && (<spanclassName={`text-xs truncate max-w-[300px] ${statusPresentation.errorTextClass}`}- title={connection.lastError.replace(/<[^>]+>/gm, "")}+ title={connection.lastError.replace(/[<>]/g, "")}>- {connection.lastError.replace(/<[^>]+>/gm, "")}+ {connection.lastError.replace(/[<>]/g, "")}</span>)}<span className="text-xs text-text-muted">#{connection.priority}</span>diff --git a/tests/integration/_chatPipelineHarness.mjs b/tests/integration/_chatPipelineHarness.mjsindex 6c927e2765..ec0a9bb8f6 100644--- a/tests/integration/_chatPipelineHarness.mjs+++ b/tests/integration/_chatPipelineHarness.mjs@@ -232,7 +232,7 @@ export async function createChatPipelineHarness(prefix) {provider,authType: "apikey",name: overrides.name || `${provider}-primary`,- apiKey: overrides.apiKey || `sk-${provider}-${Math.random().toString(16).slice(2, 10)}`,+ apiKey: overrides.apiKey || `sk-${provider}-${crypto.randomUUID().slice(0, 8)}`,isActive: overrides.isActive ?? true,testStatus: overrides.testStatus || "active",priority: overrides.priority,diff --git a/tests/unit/chatcore-translation-paths.test.mjs b/tests/unit/chatcore-translation-paths.test.mjsindex e3d8ae0bf4..da5f1c8315 100644--- a/tests/unit/chatcore-translation-paths.test.mjs+++ b/tests/unit/chatcore-translation-paths.test.mjs@@ -820,7 +820,7 @@ test("chatCore refreshes GitHub credentials after 401 and retries with the refre}const providerCalls = seenCalls.filter((entry) =>- entry.url.includes("api.githubcopilot.com")+ entry.url.includes("api.githubcopilot.com/"));if (providerCalls.length === 1) {return new Response(@@ -839,7 +839,7 @@ test("chatCore refreshes GitHub credentials after 401 and retries with the refre});const payload = await result.response.json();- const providerCalls = calls.filter((entry) => entry.url.includes("api.githubcopilot.com"));+ const providerCalls = calls.filter((entry) => entry.url.includes("api.githubcopilot.com/"));assert.equal(result.success, true);assert.equal(providerCalls.length, 2);diff --git a/tests/unit/provider-validation-specialty.test.mjs b/tests/unit/provider-validation-specialty.test.mjsindex d1cc5ed944..3436da1c16 100644--- a/tests/unit/provider-validation-specialty.test.mjs+++ b/tests/unit/provider-validation-specialty.test.mjs@@ -15,21 +15,21 @@ test("specialty provider validators cover Deepgram, AssemblyAI, NanoBanana, Elevconst target = String(url);const headers = init.headers || {};- if (target.includes("deepgram")) {+ if (target.match(/deepgram/i)) {assert.equal(headers.Authorization, "Token dg-key");return new Response(JSON.stringify({ ok: true }), { status: 200 });}- if (target.includes("assemblyai")) {+ if (target.match(/assemblyai/i)) {assert.equal(headers.Authorization, "aa-key");return new Response(JSON.stringify({ error: "unauthorized" }), { status: 403 });}- if (target.includes("nanobanana")) {+ if (target.match(/nanobanana/i)) {return new Response(JSON.stringify({ error: "unauthorized" }), { status: 401 });}- if (target.includes("elevenlabs")) {+ if (target.match(/elevenlabs/i)) {return new Response(JSON.stringify({ voices: [] }), { status: 200 });}- if (target.includes("inworld")) {+ if (target.match(/inworld/i)) {return new Response(JSON.stringify({ error: "bad request" }), { status: 400 });}@@ -52,19 +52,19 @@ test("specialty provider validators cover Deepgram, AssemblyAI, NanoBanana, Elevtest("specialty providers surface network failures and non-auth upstream failures", async () => {globalThis.fetch = async (url) => {const target = String(url);- if (target.includes("deepgram")) {+ if (target.match(/deepgram/i)) {throw new Error("deepgram offline");}- if (target.includes("nanobanana")) {+ if (target.match(/nanobanana/i)) {throw new Error("nanobanana offline");}- if (target.includes("elevenlabs")) {+ if (target.match(/elevenlabs/i)) {return new Response(JSON.stringify({ error: "server" }), { status: 500 });}- if (target.includes("inworld")) {+ if (target.match(/inworld/i)) {return new Response(JSON.stringify({ error: "forbidden" }), { status: 403 });}- if (target.includes("longcat")) {+ if (target.match(/longcat/i)) {throw new Error("longcat offline");}throw new Error(`unexpected fetch: ${target}`);@@ -88,16 +88,16 @@ test("search provider validators cover success, client errors, server errors andglobalThis.fetch = async (url, init = {}) => {calls.push({ url: String(url), init });const target = String(url);- if (target.includes("search.brave.com")) {+ if (target.match(/search.brave.com/i)) {return new Response(JSON.stringify({ results: [] }), { status: 200 });}- if (target.includes("api.exa.ai")) {+ if (target.match(/api.exa.ai/i)) {return new Response(JSON.stringify({ error: "bad key" }), { status: 403 });}- if (target.includes("api.tavily.com")) {+ if (target.match(/api.tavily.com/i)) {return new Response(JSON.stringify({ error: "server" }), { status: 503 });}- if (target.includes("api.perplexity.ai")) {+ if (target.match(/api.perplexity.ai/i)) {throw new Error("perplexity offline");}throw new Error(`unexpected fetch: ${target}`);@@ -180,13 +180,13 @@ test("OpenAI-compatible validator covers /responses mode and final ping fallbacktest("Anthropic-compatible and Claude Code compatible validators cover direct success and bridge fallbacks", async () => {globalThis.fetch = async (url, init = {}) => {const target = String(url);- if (target.includes("anthropic-compatible.example.com") && init.method === "GET") {+ if (target.match(/anthropic-compatible.example.com/i) && init.method === "GET") {return new Response(JSON.stringify({ data: [] }), { status: 200 });}- if (target.includes("cc-compatible.example.com") && init.method === "GET") {+ if (target.match(/cc-compatible.example.com/i) && init.method === "GET") {return new Response(JSON.stringify({ error: "bridge unavailable" }), { status: 500 });}- if (target.includes("cc-compatible.example.com") && init.method === "POST") {+ if (target.match(/cc-compatible.example.com/i) && init.method === "POST") {return new Response(JSON.stringify({ error: "rate limited" }), { status: 429 });}throw new Error(`unexpected fetch: ${target}`);@@ -337,25 +337,25 @@ test("registry providers cover remaining OpenAI-like and Claude-like validationtest("specialty validators cover remaining status branches for Deepgram, AssemblyAI, NanoBanana, ElevenLabs, Inworld, Bailian and LongCat", async () => {globalThis.fetch = async (url) => {const target = String(url);- if (target.includes("deepgram")) {+ if (target.match(/deepgram/i)) {return new Response(JSON.stringify({ error: "server" }), { status: 500 });}- if (target.includes("assemblyai")) {+ if (target.match(/assemblyai/i)) {return new Response(JSON.stringify({ transcripts: [] }), { status: 200 });}- if (target.includes("nanobanana")) {+ if (target.match(/nanobanana/i)) {return new Response(JSON.stringify({ error: "bad request" }), { status: 400 });}- if (target.includes("elevenlabs")) {+ if (target.match(/elevenlabs/i)) {return new Response(JSON.stringify({ error: "forbidden" }), { status: 403 });}- if (target.includes("inworld")) {+ if (target.match(/inworld/i)) {throw new Error("inworld offline");}- if (target.includes("dashscope.aliyuncs.com")) {+ if (target.match(/dashscope.aliyuncs.com/i)) {return new Response(JSON.stringify({ error: "server" }), { status: 500 });}- if (target.includes("longcat")) {+ if (target.match(/longcat/i)) {return new Response(JSON.stringify({ error: "unauthorized" }), { status: 401 });}throw new Error(`unexpected fetch: ${target}`);@@ -376,10 +376,10 @@ test("specialty validators cover remaining status branches for Deepgram, Assemblconst longcatInvalid = await validateProviderApiKey({ provider: "longcat", apiKey: "lc-key" });globalThis.fetch = async (url) => {- if (String(url).includes("elevenlabs")) {+ if (String(url).match(/elevenlabs/i)) {throw new Error("elevenlabs offline");}- if (String(url).includes("longcat")) {+ if (String(url).match(/longcat/i)) {return new Response(JSON.stringify({ error: "unprocessable" }), { status: 422 });}throw new Error(`unexpected fetch: ${url}`);diff --git a/tests/unit/search-handler-extended.test.mjs b/tests/unit/search-handler-extended.test.mjsindex 844a37d66c..ccc25fb88d 100644--- a/tests/unit/search-handler-extended.test.mjs+++ b/tests/unit/search-handler-extended.test.mjs@@ -355,7 +355,7 @@ test("handleSearch fails over to the alternate provider on retriable upstream erglobalThis.fetch = async (url) => {calls.push(String(url));- if (String(url).startsWith("https://google.serper.dev")) {+ if (String(url).startsWith("https://google.serper.dev/")) {return new Response("upstream broken", { status: 500 });}