fix(openai): treat Authorization header as case-insensitive

This commit is contained in:
Supra4E8C
2026-03-27 22:03:57 +08:00
parent 1bf7b23f75
commit 163c1ac29d
4 changed files with 13 additions and 9 deletions
+2 -2
View File
@@ -12,7 +12,7 @@ import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
import { useNotificationStore } from '@/stores';
import { apiCallApi, getApiCallErrorMessage } from '@/services/api';
import type { ApiKeyEntry } from '@/types';
import { buildHeaderObject } from '@/utils/headers';
import { buildHeaderObject, hasHeader } from '@/utils/headers';
import { buildApiKeyEntry, buildOpenAIChatCompletionsEndpoint } from '@/components/providers/utils';
import type { OpenAIEditOutletContext } from './AiProvidersOpenAIEditLayout';
import type { KeyTestStatus } from '@/stores/useOpenAIEditDraftStore';
@@ -213,7 +213,7 @@ export function AiProvidersOpenAIEditPage() {
'Content-Type': 'application/json',
...customHeaders,
};
if (!headers.Authorization && !headers['authorization']) {
if (!hasHeader(headers, 'authorization')) {
headers.Authorization = `Bearer ${keyEntry.apiKey.trim()}`;
}
+2 -2
View File
@@ -9,7 +9,7 @@ import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
import { modelsApi } from '@/services/api';
import type { ModelInfo } from '@/utils/models';
import { buildHeaderObject } from '@/utils/headers';
import { buildHeaderObject, hasHeader } from '@/utils/headers';
import { buildOpenAIModelsEndpoint } from '@/components/providers/utils';
import type { OpenAIEditOutletContext } from './AiProvidersOpenAIEditLayout';
import styles from './AiProvidersPage.module.scss';
@@ -68,7 +68,7 @@ export function AiProvidersOpenAIModelsPage() {
try {
const headerObject = buildHeaderObject(form.headers);
const firstKey = form.apiKeyEntries.find((entry) => entry.apiKey?.trim())?.apiKey?.trim();
const hasAuthHeader = Boolean(headerObject.Authorization || headerObject['authorization']);
const hasAuthHeader = hasHeader(headerObject, 'authorization');
const list = await modelsApi.fetchModelsViaApiCall(
trimmedBaseUrl,
hasAuthHeader ? undefined : firstKey,
+3 -5
View File
@@ -90,7 +90,7 @@ export const modelsApi = {
}
const resolvedHeaders = { ...headers };
if (apiKey) {
if (apiKey && !hasHeader(resolvedHeaders, 'authorization')) {
resolvedHeaders.Authorization = `Bearer ${apiKey}`;
}
@@ -116,8 +116,7 @@ export const modelsApi = {
}
const resolvedHeaders = { ...headers };
const hasAuthHeader = Boolean(resolvedHeaders.Authorization || resolvedHeaders.authorization);
if (apiKey && !hasAuthHeader) {
if (apiKey && !hasHeader(resolvedHeaders, 'authorization')) {
resolvedHeaders.Authorization = `Bearer ${apiKey}`;
}
@@ -149,8 +148,7 @@ export const modelsApi = {
}
const resolvedHeaders = { ...headers };
const hasAuthHeader = Boolean(resolvedHeaders.Authorization || resolvedHeaders.authorization);
if (apiKey && !hasAuthHeader) {
if (apiKey && !hasHeader(resolvedHeaders, 'authorization')) {
resolvedHeaders.Authorization = `Bearer ${apiKey}`;
}
+6
View File
@@ -31,6 +31,12 @@ export function buildHeaderObject(input?: HeaderEntry[] | Record<string, string
}, {});
}
export function hasHeader(headers: Record<string, unknown> | null | undefined, name: string): boolean {
if (!headers) return false;
const target = name.toLowerCase();
return Object.keys(headers).some((key) => key.toLowerCase() === target);
}
export function headersToEntries(headers?: Record<string, string | undefined | null>): HeaderEntry[] {
if (!headers || typeof headers !== 'object') return [];
return Object.entries(headers)