From 6e882fdbc8bde26a17681819e1be5cddd55829ea Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Wed, 21 Feb 2024 01:13:03 +0100 Subject: [PATCH] Prevented "write after end" SVR.JS crashes (even more) --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6ae3343..f23a13f 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,10 @@ function createFastCGIHandler(options) { //STDERR will be lost anyway... } } else if (processedPacket.type == END_REQUEST && processedPacket.content.length > 5) { - if (typeof socket !== "undefined") socket.removeListener("data", fastCGISocketHandler); + if (typeof socket !== "undefined") { + socket.removeListener("data", fastCGISocketHandler); + processFastCGIPacket = function() {}; + } var appStatus = processedPacket.content.readUInt32BE(0); var protocolStatus = processedPacket.content.readUInt8(4); if (protocolStatus != REQUEST_COMPLETE) { @@ -283,6 +286,8 @@ function createFastCGIHandler(options) { eventEmitter.emit("error", err); }).on("data", fastCGISocketHandler); + eventEmitter.socket = socket; + return eventEmitter; } @@ -465,7 +470,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e } }); - handler.on("connect", function () { + function handlerConnection() { handler.init(); handler.stdout.on("data", dataHandler); handler.stderr.on("data", function (data) { @@ -491,7 +496,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e res.end(); } }); - }); + } + if(typeof handler.socket.connecting == "undefined" || handler.socket.connecting) handler.on("connect", handlerConnection); + else if(!handler.socket.destroyed) handlerConnection(); } function executeFastCGIWithEnv(a, b, req, res, pubip, port, software, dh, user, cPath) {