refactor: use "every" method instead of "forEach" for checking the written headers with ModSecurity

This commit is contained in:
Dorian Niemiec 2025-01-17 21:43:38 +01:00
parent e4518b934c
commit 27f9eac26f

View file

@ -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;