Skip to content

Commit

Permalink
Use fmt to format strings
Browse files Browse the repository at this point in the history
  • Loading branch information
eduar-hte committed Aug 28, 2024
1 parent 488dbc4 commit 4672801
Show file tree
Hide file tree
Showing 72 changed files with 831 additions and 867 deletions.
1 change: 0 additions & 1 deletion headers/modsecurity/rules_set_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ class RulesSetProperties {
PropertyNotSetConfigBoolean
};


/**
*
* The RuleEngine enumerator consists in mapping the different states
Expand Down
6 changes: 4 additions & 2 deletions src/actions/accuracy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "src/actions/accuracy.h"

#include <fmt/format.h>

#include "modsecurity/rule_with_actions.h"


Expand All @@ -25,8 +27,8 @@ bool Accuracy::init(std::string *error) {
try {
m_accuracy = std::stoi(m_parser_payload);
} catch (...) {
error->assign("Accuracy: The input \"" + m_parser_payload + "\" is " \
"not a number.");
error->assign(fmt::format("Accuracy: The input \"{}\" is " \
"not a number.", m_parser_payload));
return false;
}
return true;
Expand Down
13 changes: 5 additions & 8 deletions src/actions/ctl/audit_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "src/actions/ctl/audit_engine.h"

#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set_properties.h"
#include "modsecurity/rules_set.h"
Expand All @@ -37,21 +38,17 @@ bool AuditEngine::init(std::string *error) {
} else if (what == "relevantonly") {
m_auditEngine = audit_log::AuditLog::AuditLogStatus::RelevantOnlyAuditLogStatus;
} else {
error->assign("Internal error. Expected: On, Off or RelevantOnly; " \
"got: " + m_parser_payload);
error->assign(fmt::format("Internal error. Expected: On, Off or RelevantOnly; " \
"got: {}", m_parser_payload));
return false;
}

return true;
}

bool AuditEngine::evaluate(RuleWithActions *rule, Transaction *transaction) {
std::stringstream a;
a << "Setting SecAuditEngine to ";
a << std::to_string(m_auditEngine);
a << " as requested by a ctl:auditEngine action";

ms_dbg_a(transaction, 8, a.str());
ms_dbg_a(transaction, 8, fmt::format("Setting SecAuditEngine to {} " \
" as requested by a ctl:auditEngine action", (int)m_auditEngine));

transaction->m_ctlAuditEngine = m_auditEngine;
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/actions/ctl/request_body_access.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set_properties.h"
#include "modsecurity/transaction.h"
Expand All @@ -34,8 +35,8 @@ bool RequestBodyAccess::init(std::string *error) {
} else if (what == "false") {
m_request_body_access = false;
} else {
error->assign("Internal error. Expected: true or false, got: " \
+ m_parser_payload);
error->assign(fmt::format("Internal error. Expected: true or false, got: {}",
m_parser_payload));
return false;
}

Expand Down
21 changes: 8 additions & 13 deletions src/actions/ctl/rule_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set_properties.h"
#include "modsecurity/rules_set.h"
#include "modsecurity/transaction.h"

namespace modsecurity {
namespace actions {
namespace ctl {
namespace modsecurity::actions::ctl {


bool RuleEngine::init(std::string *error) {
Expand All @@ -37,27 +36,23 @@ bool RuleEngine::init(std::string *error) {
} else if (what == "detectiononly") {
m_ruleEngine = RulesSetProperties::DetectionOnlyRuleEngine;
} else {
error->assign("Internal error. Expected: On, Off or DetectionOnly; " \
"got: " + m_parser_payload);
error->assign(fmt::format("Internal error. Expected: On, Off or DetectionOnly; " \
"got: {}", m_parser_payload));
return false;
}

return true;
}

bool RuleEngine::evaluate(RuleWithActions *rule, Transaction *transaction) {
std::stringstream a;
a << "Setting SecRuleEngine to ";
a << modsecurity::RulesSetProperties::ruleEngineStateString(m_ruleEngine);
a << " as requested by a ctl:ruleEngine action";
const auto a = fmt::format("Setting SecRuleEngine to {} as requested by a ctl:ruleEngine action",
modsecurity::RulesSetProperties::ruleEngineStateString(m_ruleEngine));

ms_dbg_a(transaction, 8, a.str());
ms_dbg_a(transaction, 8, a);

transaction->m_secRuleEngine = m_ruleEngine;
return true;
}


} // namespace ctl
} // namespace actions
} // namespace modsecurity
} // namespace modsecurity::actions::ctl
11 changes: 6 additions & 5 deletions src/actions/ctl/rule_remove_by_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/transaction.h"
#include "src/utils/string.h"
Expand Down Expand Up @@ -46,19 +47,19 @@ bool RuleRemoveById::init(std::string *error) {
n1n = std::stoi(n1s);
added = true;
} catch (...) {
error->assign("Not a number: " + n1s);
error->assign(fmt::format("Not a number: {}", n1s));
return false;
}
try {
n2n = std::stoi(n2s);
added = true;
} catch (...) {
error->assign("Not a number: " + n2s);
error->assign(fmt::format("Not a number: {}", n2s));
return false;
}

if (n1n > n2n) {
error->assign("Invalid range: " + b);
error->assign(fmt::format("Invalid range: {}", b));
return false;
}
m_ranges.push_back(std::make_pair(n1n, n2n));
Expand All @@ -69,7 +70,7 @@ bool RuleRemoveById::init(std::string *error) {
m_ids.push_back(num);
added = true;
} catch (...) {
error->assign("Not a number or range: " + b);
error->assign(fmt::format("Not a number or range: {}", b));
return false;
}
}
Expand All @@ -79,7 +80,7 @@ bool RuleRemoveById::init(std::string *error) {
return true;
}

error->assign("Not a number or range: " + what);
error->assign(fmt::format("Not a number or range: {}", what));
return false;
}

Expand Down
7 changes: 4 additions & 3 deletions src/actions/ctl/rule_remove_target_by_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <vector>
#include <utility>
#include <fmt/format.h>

#include "modsecurity/transaction.h"
#include "src/utils/string.h"
Expand All @@ -34,15 +35,15 @@ bool RuleRemoveTargetById::init(std::string *error) {
std::vector<std::string> param = utils::string::split(what, ';');

if (param.size() < 2) {
error->assign(what + " is not a valid `ID;VARIABLE'");
error->assign(fmt::format("{} is not a valid `ID;VARIABLE'", what));
return false;
}

try {
m_id = std::stoi(param[0]);
} catch(...) {
error->assign("Not able to convert '" + param[0] +
"' into a number");
error->assign(fmt::format("Not able to convert '{}' into a number",
param[0]));
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/actions/ctl/rule_remove_target_by_tag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <vector>
#include <utility>
#include <fmt/format.h>

#include "modsecurity/transaction.h"
#include "src/utils/string.h"
Expand All @@ -34,7 +35,7 @@ bool RuleRemoveTargetByTag::init(std::string *error) {
std::vector<std::string> param = utils::string::split(what, ';');

if (param.size() < 2) {
error->assign(what + " is not a valid `TAG;VARIABLE'");
error->assign(fmt::format("{} is not a valid `TAG;VARIABLE'", what));
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/actions/data/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <iostream>
#include <string>
#include <memory>
#include <fmt/format.h>

#include "modsecurity/transaction.h"

Expand All @@ -30,7 +31,7 @@ bool Status::init(std::string *error) {
try {
m_status = std::stoi(m_parser_payload);
} catch (...) {
error->assign("Not a valid number: " + m_parser_payload);
error->assign(fmt::format("Not a valid number: {}", m_parser_payload));
return false;
}

Expand Down
7 changes: 4 additions & 3 deletions src/actions/disruptive/allow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set.h"
#include "modsecurity/transaction.h"
Expand Down Expand Up @@ -50,9 +51,9 @@ bool Allow::init(std::string *error) {


bool Allow::evaluate(RuleWithActions *rule, Transaction *transaction) {
ms_dbg_a(transaction, 4, "Dropping the evaluation of upcoming rules " \
"in favor of an `allow' action of type: " \
+ allowTypeToName(m_allowType));
ms_dbg_a(transaction, 4, fmt::format("Dropping the evaluation of upcoming rules " \
"in favor of an `allow' action of type: {}",
allowTypeToName(m_allowType)));

transaction->m_allowType = m_allowType;

Expand Down
7 changes: 4 additions & 3 deletions src/actions/exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set.h"
#include "modsecurity/actions/action.h"
Expand All @@ -36,12 +37,12 @@ bool Exec::init(std::string *error) {
m_script = utils::find_resource(m_parser_payload, "", &err);

if (m_script.size() == 0) {
error->assign("exec: Script not found: " + err);
error->assign(fmt::format("exec: Script not found: {}", err));
return false;
}

if (engine::Lua::isCompatible(m_script, &m_lua, &err) == false) {
error->assign("exec: " + err);
error->assign(fmt::format("exec: {}", err));
return false;
}

Expand All @@ -50,7 +51,7 @@ bool Exec::init(std::string *error) {


bool Exec::evaluate(RuleWithActions *rule, Transaction *t) {
ms_dbg_a(t, 8, "Running script... " + m_script);
ms_dbg_a(t, 8, fmt::format("Running script... {}", m_script));
m_lua.run(t);
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/actions/expire_var.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "src/actions/expire_var.h"

#include <string>
#include <fmt/format.h>

#include "modsecurity/rules_set.h"
#include "modsecurity/transaction.h"
Expand Down Expand Up @@ -81,7 +82,7 @@ bool ExpireVar::evaluate(RuleWithActions *rule, Transaction *t) {
} else {
ms_dbg_a(t, 5, "Invalid collection found in expirevar expression: collection must be `ip', `global', `resource', `user' or `session'");
}
ms_dbg_a(t, 9, "Setting variable `" + variable_name + "' to expire in " + std::to_string(expirySeconds) + " seconds.");
ms_dbg_a(t, 9, fmt::format("Setting variable `{}' to expire in {} seconds.", variable_name, expirySeconds));

return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/actions/init_col.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <iostream>
#include <string>
#include <fmt/format.h>

#include "modsecurity/actions/action.h"
#include "modsecurity/transaction.h"
Expand Down Expand Up @@ -67,8 +68,8 @@ bool InitCol::evaluate(RuleWithActions *rule, Transaction *t) {
return false;
}

ms_dbg_a(t, 5, "Collection `" + m_collection_key + "' initialized with " \
"value: " + collectionName);
ms_dbg_a(t, 5, fmt::format("Collection `{}' initialized with " \
"value: {}", m_collection_key, collectionName));

return true;
}
Expand Down
6 changes: 4 additions & 2 deletions src/actions/maturity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "src/actions/maturity.h"

#include <fmt/format.h>

#include "modsecurity/rule_with_actions.h"


Expand All @@ -25,8 +27,8 @@ bool Maturity::init(std::string *error) {
try {
m_maturity = std::stoi(m_parser_payload);
} catch (...) {
error->assign("Maturity: The input \"" + m_parser_payload + "\" is " \
"not a number.");
error->assign(fmt::format("Maturity: The input \"{}\" is " \
"not a number.", m_parser_payload));
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/actions/msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <iostream>
#include <string>
#include <memory>
#include <fmt/format.h>

#include "modsecurity/actions/action.h"
#include "modsecurity/transaction.h"
Expand Down Expand Up @@ -49,7 +50,7 @@ namespace actions {
bool Msg::evaluate(RuleWithActions *rule, Transaction *transaction, RuleMessage &ruleMessage) {
const auto msg = data(transaction);
ruleMessage.m_message = msg;
ms_dbg_a(transaction, 9, "Saving msg: " + msg);
ms_dbg_a(transaction, 9, fmt::format("Saving msg: {}", msg));

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/actions/phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "src/actions/phase.h"

#include <fmt/format.h>

#include "modsecurity/rule_with_actions.h"
#include "src/utils/string.h"

Expand Down Expand Up @@ -47,7 +49,7 @@ bool Phase::init(std::string *error) {
m_phase = modsecurity::Phases::LoggingPhase;
m_secRulesPhase = 5;
} else {
error->assign("Unknown phase: " + m_parser_payload);
error->assign(fmt::format("Unknown phase: {}", m_parser_payload));
return false;
}
} catch (...) {
Expand Down
Loading

0 comments on commit 4672801

Please sign in to comment.