From b375e6f66efaaf08ef24940374adbc60a68c79cc Mon Sep 17 00:00:00 2001 From: Hypercubies Date: Mon, 21 Sep 2026 14:07:00 +0800 Subject: [PATCH] fix(onebot): accept image request bodies and return explicit size errors HTTP POST used a body limit too small for ordinary base64 images and dropped the connection when it was exceeded. Raise the limit and return a structured size error without destroying the socket. --- .../onebot/src/network/http-server-adapter.ts | 16 +++++++++---- packages/onebot/tests/http-image-body.test.ts | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 packages/onebot/tests/http-image-body.test.ts diff --git a/packages/onebot/src/network/http-server-adapter.ts b/packages/onebot/src/network/http-server-adapter.ts index d546fb7b..4e8daef0 100644 --- a/packages/onebot/src/network/http-server-adapter.ts +++ b/packages/onebot/src/network/http-server-adapter.ts @@ -384,7 +384,8 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter respond(200, response); } catch (error) { const wording = error instanceof Error ? error.message : 'internal error'; - respond(500, { status: 'failed', retcode: 1200, data: null, wording }); + const tooLarge = error instanceof RequestBodyTooLargeError; + respond(tooLarge ? 413 : 500, { status: 'failed', retcode: tooLarge ? 1413 : 1200, data: null, wording }); } } @@ -435,15 +436,22 @@ function isAlreadyClosedError(error: Error): boolean { return (error as NodeJS.ErrnoException).code === 'ERR_SERVER_NOT_RUNNING'; } -function readRequestBody(req: IncomingMessage, maxBytes = 2 * 1024 * 1024): Promise { +class RequestBodyTooLargeError extends Error {} + +// Base64 media expands binary size by 4/3, before the JSON envelope. +function readRequestBody(req: IncomingMessage, maxBytes = 64 * 1024 * 1024): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; let total = 0; + let exceeded = false; req.on('data', (chunk: Buffer) => { + if (exceeded) return; total += chunk.length; if (total > maxBytes) { - reject(new Error('request body too large')); - req.destroy(); + exceeded = true; + chunks.length = 0; + reject(new RequestBodyTooLargeError('request body too large')); + // Drain without retaining bytes; keep the socket alive for the 413 response. return; } chunks.push(chunk); diff --git a/packages/onebot/tests/http-image-body.test.ts b/packages/onebot/tests/http-image-body.test.ts new file mode 100644 index 00000000..5af87f84 --- /dev/null +++ b/packages/onebot/tests/http-image-body.test.ts @@ -0,0 +1,24 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import http from 'http'; +import type { AddressInfo } from 'net'; +import { HttpServerAdapter } from '../src/network/http-server-adapter'; +let adapter: HttpServerAdapter | undefined; +afterEach(async () => { await adapter?.close(); }); +it('accepts base64 images above 2 MiB, returns 413 for oversize and stays usable', async () => { + const handle=vi.fn(async()=>({status:'ok',retcode:0,data:{}})); + adapter=new HttpServerAdapter('image-body',{name:'image-body',host:'127.0.0.1',port:0,path:'/',messageFormat:'array',reportSelfMessage:false},{uin:'10001',api:{isStreamAction:()=>false,handle} as never,buildLifecycleEvent:()=>({}),buildHeartbeatEvent:()=>({})}); + await adapter.open(); + const port=((adapter as unknown as {server:http.Server}).server.address() as AddressInfo).port; + async function post(body:string) { + return new Promise<{status:number,body:any}>((resolve,reject)=>{ + const req=http.request({hostname:'127.0.0.1',port,path:'/send_group_msg',method:'POST',headers:{'content-type':'application/json'}},res=>{ + const chunks:Buffer[]=[];res.on('data',c=>chunks.push(c));res.on('end',()=>resolve({status:res.statusCode!,body:JSON.parse(Buffer.concat(chunks).toString())})); + });req.on('error',reject);req.end(body); + }); + } + const image=JSON.stringify({group_id:1,message:[{type:'image',data:{file:'base64://'+Buffer.alloc(1644101).toString('base64')}}]}); + expect((await post(image)).status).toBe(200);expect(handle).toHaveBeenCalledTimes(1); + expect(await post(JSON.stringify({padding:'x'.repeat(64*1024*1024)}))).toMatchObject({status:413,body:{status:'failed',retcode:1413}}); + expect(handle).toHaveBeenCalledTimes(1); + expect((await post('{}')).status).toBe(200);expect(handle).toHaveBeenCalledTimes(2); +},20000);