From 27f9eac26f46ff18370621edf406c987204c2c4d Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Fri, 17 Jan 2025 21:43:38 +0100 Subject: [PATCH] refactor: use "every" method instead of "forEach" for checking the written headers with ModSecurity --- src/index.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 009e565..9b21752 100644 --- a/src/index.js +++ b/src/index.js @@ -78,7 +78,7 @@ module.exports = (req, res, logFacilities, config, next) => { } writtenStatusCode = statusCode; let intervene = false; - Object.keys(writtenHeaders).forEach((key) => { + Object.keys(writtenHeaders).every((key) => { if (typeof writtenHeaders[key] == "string") { securityResponse = transaction.addResponseHeader( key, @@ -86,18 +86,20 @@ module.exports = (req, res, logFacilities, config, next) => { ); if (typeof securityResponse === "object") { intervene = true; - return; + return false; } } else if (Array.isArray(writtenHeaders[key])) { - writtenHeaders[key].forEach((value) => { + writtenHeaders[key].every((value) => { securityResponse = transaction.addResponseHeader(key, value); if (typeof securityResponse === "object") { intervene = true; - return; + return false; } + return true; }); - if (intervene) return; + if (intervene) return false; } + return true; }); if (intervene) { res.write = () => false; @@ -267,7 +269,7 @@ module.exports = (req, res, logFacilities, config, next) => { res.write = function (chunk, encoding, callback) { if (!headersSet) { let intervene = false; - Object.keys(writtenHeaders).forEach((key) => { + Object.keys(writtenHeaders).every((key) => { if (typeof writtenHeaders[key] == "string") { securityResponse = transaction.addResponseHeader( key, @@ -275,18 +277,20 @@ module.exports = (req, res, logFacilities, config, next) => { ); if (typeof securityResponse === "object") { intervene = true; - return; + return false; } } else if (Array.isArray(writtenHeaders[key])) { - writtenHeaders[key].forEach((value) => { + writtenHeaders[key].every((value) => { securityResponse = transaction.addResponseHeader(key, value); if (typeof securityResponse === "object") { intervene = true; - return; + return false; } + return true; }); - if (intervene) return; + if (intervene) return false; } + return true; }); if (intervene) { return false;