diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 61224f8..276f52e 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -767,6 +767,7 @@ "api_key_added": "API key added successfully", "api_key_updated": "API key updated successfully", "api_key_deleted": "API key deleted successfully", + "api_key_invalid_chars": "API key can only contain letters, numbers, and symbols", "gemini_key_added": "Gemini key added successfully", "gemini_key_updated": "Gemini key updated successfully", "gemini_key_deleted": "Gemini key deleted successfully", diff --git a/src/i18n/locales/zh-CN.json b/src/i18n/locales/zh-CN.json index b84e8d0..34fbb2c 100644 --- a/src/i18n/locales/zh-CN.json +++ b/src/i18n/locales/zh-CN.json @@ -767,6 +767,7 @@ "api_key_added": "API密钥添加成功", "api_key_updated": "API密钥更新成功", "api_key_deleted": "API密钥删除成功", + "api_key_invalid_chars": "API密钥仅支持英文字母、数字和符号", "gemini_key_added": "Gemini密钥添加成功", "gemini_key_updated": "Gemini密钥更新成功", "gemini_key_deleted": "Gemini密钥删除成功", diff --git a/src/pages/ApiKeysPage.tsx b/src/pages/ApiKeysPage.tsx index fb9eb63..3461151 100644 --- a/src/pages/ApiKeysPage.tsx +++ b/src/pages/ApiKeysPage.tsx @@ -9,6 +9,7 @@ import { LoadingSpinner } from '@/components/ui/LoadingSpinner'; import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores'; import { apiKeysApi } from '@/services/api'; import { maskApiKey } from '@/utils/format'; +import { isValidApiKeyCharset } from '@/utils/validation'; import styles from './ApiKeysPage.module.scss'; export function ApiKeysPage() { @@ -83,6 +84,10 @@ export function ApiKeysPage() { showNotification(`${t('notification.please_enter')} ${t('notification.api_key')}`, 'error'); return; } + if (!isValidApiKeyCharset(trimmed)) { + showNotification(t('notification.api_key_invalid_chars'), 'error'); + return; + } const isEdit = editingIndex !== null; const nextKeys = isEdit diff --git a/src/utils/validation.ts b/src/utils/validation.ts index a92ad16..e1cf66f 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -35,6 +35,14 @@ export function isValidApiKey(key: string): boolean { return !/\s/.test(key); } +/** + * 验证 API Key 字符集(仅允许 ASCII 可见字符) + */ +export function isValidApiKeyCharset(key: string): boolean { + if (!key) return false; + return /^[\x21-\x7E]+$/.test(key); +} + /** * 验证 JSON 格式 */