From bd1003285768d54713a5028cc5e60e17f4ec39ca Mon Sep 17 00:00:00 2001 From: shaw Date: Thu, 21 Aug 2025 09:29:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4Unicode=E6=B8=85?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E6=81=A2=E5=A4=8D=E4=B8=8E?= =?UTF-8?q?main=E5=88=86=E6=94=AF=E4=B8=80=E8=87=B4=E7=9A=84=E8=BD=AC?= =?UTF-8?q?=E5=8F=91=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除app.js中的JSON解析错误处理中间件 - 移除api.js中的cleanUnicodeString和cleanUnicodeInObject函数 - 移除handleMessagesRequest中的Unicode清理调用 - 确保转发逻辑与main远程分支完全一致 问题原因: - Unicode清理逻辑会修改请求体,可能导致某些情况下的JSON解析错误 - Claude API本身能够处理Unicode问题,不需要在中转服务中预处理 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/app.js | 30 ------------------------------ src/routes/api.js | 41 ----------------------------------------- 2 files changed, 71 deletions(-) diff --git a/src/app.js b/src/app.js index 9fca57eb..2f6d09cb 100644 --- a/src/app.js +++ b/src/app.js @@ -141,40 +141,10 @@ class Application { if (buf && buf.length && !buf.toString(encoding || 'utf8').trim()) { throw new Error('Invalid JSON: empty body') } - // 注意:不在这里修改buffer,避免导致JSON解析错误 } }) ) this.app.use(express.urlencoded({ extended: true, limit: '10mb' })) - - // 🧹 JSON 解析错误处理中间件 - this.app.use((err, req, res, next) => { - if (err instanceof SyntaxError && err.status === 400 && 'body' in err) { - // JSON 解析错误统一处理 - logger.warn('🚨 JSON parsing error detected:', err.message) - - // 检查是否是常见的JSON解析错误 - if ( - err.message.includes('Unexpected end of JSON input') || - err.message.includes('Unexpected token') || - err.message.includes('Expected property name') || - err.message.includes('in JSON at position') || - err.message.includes('surrogate') || - err.message.includes('UTF-16') || - err.message.includes('invalid character') - ) { - return res.status(400).json({ - type: 'error', - error: { - type: 'invalid_request_error', - message: 'Invalid JSON format in request body. Please ensure the request contains valid JSON data.' - } - }) - } - } - next(err) - }) - this.app.use(securityMiddleware) // 🎯 信任代理 diff --git a/src/routes/api.js b/src/routes/api.js index 9f66bc6d..3b1c4160 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -12,52 +12,11 @@ const sessionHelper = require('../utils/sessionHelper') const router = express.Router() -// 🧹 Unicode 字符清理函数 -function cleanUnicodeString(str) { - if (typeof str !== 'string') { - return str - } - - // 移除无效的 UTF-16 代理对字符 - // 匹配无效的低代理字符 (0xDC00-0xDFFF) 没有对应的高代理字符 - // 匹配无效的高代理字符 (0xD800-0xDBFF) 没有对应的低代理字符 - return str.replace( - /[\uDC00-\uDFFF](?![\uD800-\uDBFF])|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/g, - '\uFFFD' - ) -} - -// 🧹 递归清理对象中的 Unicode 字符 -function cleanUnicodeInObject(obj) { - if (typeof obj === 'string') { - return cleanUnicodeString(obj) - } - - if (Array.isArray(obj)) { - return obj.map((item) => cleanUnicodeInObject(item)) - } - - if (obj && typeof obj === 'object') { - const cleaned = {} - for (const [key, value] of Object.entries(obj)) { - cleaned[cleanUnicodeString(key)] = cleanUnicodeInObject(value) - } - return cleaned - } - - return obj -} - // 🔧 共享的消息处理函数 async function handleMessagesRequest(req, res) { try { const startTime = Date.now() - // Unicode 字符清理 - 在输入验证之前清理请求体 - if (req.body) { - req.body = cleanUnicodeInObject(req.body) - } - // 严格的输入验证 if (!req.body || typeof req.body !== 'object') { return res.status(400).json({