diff --git a/src/main/services/urlschema/handle-providers.ts b/src/main/services/urlschema/handle-providers.ts index 9a598fc459..f8b0661370 100644 --- a/src/main/services/urlschema/handle-providers.ts +++ b/src/main/services/urlschema/handle-providers.ts @@ -3,6 +3,17 @@ import Logger from 'electron-log' import { windowService } from '../WindowService' +function ParseData(data: string) { + try { + const result = JSON.parse(Buffer.from(data, 'base64').toString('utf-8')) + + return JSON.stringify(result) + } catch (error) { + Logger.error('ParseData error:', { error }) + return null + } +} + export async function handleProvidersProtocolUrl(url: URL) { switch (url.pathname) { case '/api-keys': { @@ -19,7 +30,13 @@ export async function handleProvidersProtocolUrl(url: URL) { // replace + and / to _ and - because + and / are processed by URLSearchParams const processedSearch = url.search.replaceAll('+', '_').replaceAll('/', '-') const params = new URLSearchParams(processedSearch) - const data = params.get('data') + const data = ParseData(params.get('data')?.replaceAll('_', '+').replaceAll('-', '/') || '') + + if (!data) { + Logger.error('handleProvidersProtocolUrl data is null or invalid') + return + } + const mainWindow = windowService.getMainWindow() const version = params.get('v') if (version == '1') { @@ -33,7 +50,9 @@ export async function handleProvidersProtocolUrl(url: URL) { !mainWindow.isDestroyed() && (await mainWindow.webContents.executeJavaScript(`typeof window.navigate === 'function'`)) ) { - mainWindow.webContents.executeJavaScript(`window.navigate('/settings/provider?addProviderData=${data}')`) + mainWindow.webContents.executeJavaScript( + `window.navigate('/settings/provider?addProviderData=${encodeURIComponent(data)}')` + ) if (isMac) { windowService.showMainWindow() diff --git a/src/renderer/src/pages/settings/ProviderSettings/index.tsx b/src/renderer/src/pages/settings/ProviderSettings/index.tsx index a89719c178..d7a3469a86 100644 --- a/src/renderer/src/pages/settings/ProviderSettings/index.tsx +++ b/src/renderer/src/pages/settings/ProviderSettings/index.tsx @@ -238,16 +238,7 @@ const ProvidersList: FC = () => { } try { - const base64Decode = (base64EncodedString: string) => - new TextDecoder().decode(Uint8Array.from(atob(base64EncodedString), (m) => m.charCodeAt(0))) - const { - id, - apiKey: newApiKey, - baseUrl, - type, - name - } = JSON.parse(base64Decode(addProviderData.replaceAll('_', '+').replaceAll('-', '/'))) - + const { id, apiKey: newApiKey, baseUrl, type, name } = JSON.parse(addProviderData) if (!id || !newApiKey || !baseUrl) { window.message.error(t('settings.models.provider_key_add_failed_by_invalid_data')) window.navigate('/settings/provider')