From 7f3ea4ae6d7805e0b3ead25e3327bcc2531e3f61 Mon Sep 17 00:00:00 2001 From: Wxw-Gu <84499436+Wxw-Gu@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:53:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B6=85=E9=99=90=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E5=85=88=E6=8E=92=E7=A9=BA=E5=86=8D=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=20413=EF=BC=8C=E9=81=BF=E5=85=8D=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E6=94=B6=E5=88=B0=E8=BF=9E=E6=8E=A5=E9=87=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原先看到 Content-Length 超限就 pause 请求并立即发 413 + Connection close, 客户端此时仍在写 body,在 Windows 上会以 ECONNRESET 收场而非读到该响应。 --- src/main/http-server.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/http-server.ts b/src/main/http-server.ts index 5174a8b..1a55837 100644 --- a/src/main/http-server.ts +++ b/src/main/http-server.ts @@ -239,7 +239,17 @@ function readBody(req: IncomingMessage, maxBytes = MAX_JSON_BODY_BYTES): Promise return new Promise((resolve, reject) => { const contentLength = Number(req.headers['content-length']) if (Number.isFinite(contentLength) && contentLength > maxBytes) { - req.pause() + /* + * 拒收,但**要先把请求体排空**。 + * + * 直接 `pause()` 会让剩余数据留在 socket 缓冲里,而我们紧接着就会 + * 发 413 + `Connection: close` —— 客户端此时仍在写,收到连接关闭后会以 + * 传输层错误收场(undici 报 `ECONNRESET`;Windows 的 TCP 栈比 macOS + * 更倾向回 RST,所以先在那里暴露)。改成 `resume()`:不再收集数据, + * 只是把客户端已经/正在发的字节丢掉,让它正常写完并读到这个 413。 + * 上限仍是 maxBytes,所以不会有人靠超大 body 拖住我们。 + */ + req.resume() reject(new RequestBodyTooLargeError()) return } @@ -249,7 +259,8 @@ function readBody(req: IncomingMessage, maxBytes = MAX_JSON_BODY_BYTES): Promise size += chunk.length if (size > maxBytes) { chunks.length = 0 - req.pause() + // 同上:排空而不是暂停,否则客户端拿不到这个 413。 + req.resume() reject(new RequestBodyTooLargeError()) return }