From f0977c9319f0b1ff48cbceabea99946e6518a383 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Wed, 21 Feb 2024 00:49:44 +0100 Subject: [PATCH] Prevented "write after end" SVR.JS crashes --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 74693ed..6ae3343 100644 --- a/index.js +++ b/index.js @@ -166,7 +166,11 @@ function createFastCGIHandler(options) { var processedPacket = parseFastCGIPacket(packet); if (processedPacket.requestID != requestID) return; //Drop the packet if (processedPacket.type == STDOUT) { - if(processedPacket.content.length > 0) emulatedStdout.push(processedPacket.content); + try { + if(processedPacket.content.length > 0) emulatedStdout.push(processedPacket.content); + } catch (err) { + //STDOUT will be lost instead of crashing the server + } } else if (processedPacket.type == STDERR) { try { if(processedPacket.content.length > 0) emulatedStderr.push(processedPacket.content); @@ -189,7 +193,7 @@ function createFastCGIHandler(options) { err = new Error("Multiplexed connections not supported by the FastCGI application"); } emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) { + if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("error", err); @@ -200,7 +204,7 @@ function createFastCGIHandler(options) { } } else { emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null) { + if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("exit", appStatus, null);