Fix UTF-8 request body handling

This commit is contained in:
2026-06-25 08:41:33 +09:00
parent 6ee328e4fc
commit efb5d0cd73
3 changed files with 194 additions and 18 deletions

View File

@@ -92,17 +92,19 @@ async function writeDatabase(data) {
function readRequestBody(request) {
return new Promise((resolve, reject) => {
let body = '';
const chunks = [];
let bodyLength = 0;
request.on('data', (chunk) => {
body += chunk;
if (body.length > 5_000_000) {
chunks.push(chunk);
bodyLength += chunk.length;
if (bodyLength > 5_000_000) {
reject(new Error('Request body is too large'));
request.destroy();
}
});
request.on('end', () => resolve(body));
request.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
request.on('error', reject);
});
}