Skip to content

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

fix(oauth): handle null state in Cline exchange (#1016)

diegosouzapw76740598999d
diff --git a/src/app/api/oauth/[provider]/[action]/route.ts b/src/app/api/oauth/[provider]/[action]/route.ts
index 7ab57428ac..551b69c46a 100755
--- a/src/app/api/oauth/[provider]/[action]/route.ts
+++ b/src/app/api/oauth/[provider]/[action]/route.ts
@@ -229,13 +229,32 @@ export async function POST(
if (action === "exchange") {
const { code, redirectUri, codeVerifier, state } = body;
+ const normalizedState = typeof state === "string" && state.length > 0 ? state : undefined;
+ const providerData = getProvider(provider);
+
+ if (providerData.flowType === "authorization_code_pkce" && !codeVerifier) {
+ return NextResponse.json(
+ {
+ error: {
+ message: "Invalid request",
+ details: [
+ {
+ field: "codeVerifier",
+ message: `Code verifier is required for ${provider} OAuth exchange`,
+ },
+ ],
+ },
+ },
+ { status: 400 }
+ );
+ }
// Resolve proxy for this provider (provider-level → global → direct)
const proxy = await resolveProxyForProvider(provider);
// Exchange code for tokens (through proxy if configured)
const tokenData = await runWithProxyContext(proxy, () =>
- exchangeTokens(provider, code, redirectUri, codeVerifier, state)
+ exchangeTokens(provider, code, redirectUri, codeVerifier, normalizedState)
);
// Normalize: if name is missing, use email or displayName as fallback so accounts
diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json
index d5623cd60e..ff5a6d501b 100644
--- a/src/i18n/messages/en.json
+++ b/src/i18n/messages/en.json
@@ -1037,6 +1037,8 @@
"audioTranscriptionDesc": "Transcribe audio files to text (Whisper)",
"textToSpeech": "Text to Speech",
"textToSpeechDesc": "Convert text to natural-sounding speech",
+ "musicGeneration": "Music Generation",
+ "musicDesc": "Generate music and audio tracks via ComfyUI (Stable Audio, MusicGen)",
"moderations": "Moderations",
"moderationsDesc": "Content moderation and safety classification",
"responsesDesc": "OpenAI Responses API for Codex and advanced agentic workflows",
diff --git a/src/shared/components/OAuthModal.tsx b/src/shared/components/OAuthModal.tsx
index dd0eb04370..66b0c7b0b5 100644
--- a/src/shared/components/OAuthModal.tsx
+++ b/src/shared/components/OAuthModal.tsx
@@ -75,6 +75,14 @@ export default function OAuthModal({
async (code, state) => {
if (!authData) return;
try {
+ if (!authData.redirectUri || !authData.codeVerifier) {
+ throw new Error(
+ "OAuth session is incomplete (missing redirect URI or code verifier). Restart the connection and try again."
+ );
+ }
+
+ const normalizedState = typeof state === "string" && state.length > 0 ? state : undefined;
+
const res = await fetch(`/api/oauth/${provider}/exchange`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -82,18 +90,29 @@ export default function OAuthModal({
code,
redirectUri: authData.redirectUri,
codeVerifier: authData.codeVerifier,
- state,
+ ...(normalizedState ? { state: normalizedState } : {}),
}),
});
const data = await res.json();
if (!res.ok) {
- const errMsg =
+ const errorObject =
typeof data.error === "object" && data.error !== null
- ? ((data.error as Record<string, unknown>).message as string) ||
- JSON.stringify(data.error)
- : data.error || "Exchange failed";
- throw new Error(errMsg);
+ ? (data.error as Record<string, unknown>)
+ : null;
+ const errMsg = errorObject
+ ? (errorObject.message as string) || JSON.stringify(errorObject)
+ : data.error || "Exchange failed";
+ const details = Array.isArray(errorObject?.details)
+ ? (errorObject.details as Array<{ field?: string; message?: string }>)
+ .map((detail) => {
+ if (!detail?.message) return null;
+ return detail.field ? `${detail.field}: ${detail.message}` : detail.message;
+ })
+ .filter(Boolean)
+ .join("; ")
+ : "";
+ throw new Error(details ? `${errMsg} (${details})` : errMsg);
}
setStep("success");
@@ -213,6 +232,13 @@ export default function OAuthModal({
let forceManual = false;
+ // Claude Code and Cline OAuth flows can finish on provider-hosted pages that
+ // show an auth code instead of redirecting back to OmniRoute.
+ // Start directly in manual mode so users always have an input to paste code/url.
+ if (provider === "claude" || provider === "cline") {
+ forceManual = true;
+ }
+
// Codex: on localhost use callback server on port 1455,
// on remote use standard auth code flow (callback server is unreachable)
if (provider === "codex") {
@@ -497,6 +523,13 @@ export default function OAuthModal({
const handleManualSubmit = async () => {
try {
setError(null);
+
+ if (!authData) {
+ throw new Error(
+ "OAuth session not initialized. Restart the connection flow and try again."
+ );
+ }
+
const input = callbackUrl.trim();
let code = null;
let state = authData?.state || null;
@@ -661,14 +694,17 @@ export default function OAuthModal({
Step 2: Paste the callback URL or auth code here
</p>
<p className="text-xs text-text-muted mb-2">
- After authorization, paste the full callback URL. For Claude Code, you can also
- paste the Authentication Code directly, for example <code>code#state</code>.
+ After authorization, paste the full callback URL. For Claude Code and Cline, you
+ can also paste the Authentication Code directly, for example{" "}
+ <code>code#state</code>.
</p>
<Input
value={callbackUrl}
onChange={(e) => setCallbackUrl(e.target.value)}
placeholder={
- provider === "claude" ? "code#state or /callback?code=..." : placeholderUrl
+ provider === "claude" || provider === "cline"
+ ? "code#state or /callback?code=..."
+ : placeholderUrl
}
className="font-mono text-xs"
/>
@@ -676,7 +712,7 @@ export default function OAuthModal({
</div>
<div className="flex gap-2">
- <Button onClick={handleManualSubmit} fullWidth disabled={!callbackUrl}>
+ <Button onClick={handleManualSubmit} fullWidth disabled={!callbackUrl || !authData}>
Connect
</Button>
<Button onClick={onClose} variant="ghost" fullWidth>
diff --git a/src/shared/validation/schemas.ts b/src/shared/validation/schemas.ts
index e56d306ff0..6ea2a1fd71 100644
--- a/src/shared/validation/schemas.ts
+++ b/src/shared/validation/schemas.ts
@@ -829,8 +829,8 @@ export const translatorTranslateSchema = z
export const oauthExchangeSchema = z.object({
code: z.string().trim().min(1),
redirectUri: z.string().trim().min(1),
- codeVerifier: z.string().trim().min(1),
- state: z.string().optional(),
+ codeVerifier: z.string().trim().min(1).optional(),
+ state: z.string().nullable().optional(),
});
export const oauthPollSchema = z.object({