From 715b3e4ef120c999fdb05a32041fdcdacb67a95b Mon Sep 17 00:00:00 2001 From: Andres Morey Date: Tue, 25 Jun 2024 15:17:31 +0300 Subject: [PATCH] Resets request body after reading in node-http library (#51) fixes https://github.com/kubetail-org/edge-csrf/issues/50 --- packages/node-http/src/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/node-http/src/index.ts b/packages/node-http/src/index.ts index 16ac33a..5f08ae3 100644 --- a/packages/node-http/src/index.ts +++ b/packages/node-http/src/index.ts @@ -15,8 +15,20 @@ export { CsrfError }; function getRequestBody(req: IncomingMessage): Promise { return new Promise((resolve, reject) => { let body = ''; - req.on('data', (chunk) => { body += chunk.toString(); }); - req.on('end', () => resolve(body)); + + req.on('data', (chunk) => { + body += chunk.toString(); + }); + + req.on('end', () => { + // reset body + req.push(body); + req.push(null); + + // resolve promise + resolve(body); + }); + req.on('error', (err) => reject(err)); }); }