1 /* 2 * ModSecurity, http://www.modsecurity.org/ 3 * Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/) 4 * 5 * You may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * If any of the files related to licensing are missing or if you have any 11 * other questions related to licensing please contact Trustwave Holdings, Inc. 12 * directly using the email address security@modsecurity.org. 13 * 14 */ 15 16 #include <string> 17 #include <memory> 18 #include <utility> 19 20 #ifndef SRC_OPERATORS_OPERATOR_H__ 21 #define SRC_OPERATORS_OPERATOR_H__ 22 23 #include "modsecurity/transaction.h" 24 #include "modsecurity/rule.h" 25 #include "modsecurity/rule_message.h" 26 #include "src/run_time_string.h" 27 28 namespace modsecurity { 29 namespace operators { 30 31 class Operator { 32 public: 33 /** @ingroup ModSecurity_Operator */ Operator()34 Operator() 35 : m_match_message(""), 36 m_negation(false), 37 m_op(""), 38 m_param(""), 39 m_couldContainsMacro(false) { 40 if (m_couldContainsMacro == false && m_string) { 41 m_param = m_string->evaluate(); 42 } 43 } 44 Operator(const std::string & opName,const std::string & param,bool negation)45 Operator(const std::string &opName, const std::string ¶m, bool negation) 46 : m_match_message(""), 47 m_negation(negation), 48 m_op(opName), 49 m_param(param), 50 m_couldContainsMacro(false) { 51 if (m_couldContainsMacro == false && m_string) { 52 m_param = m_string->evaluate(); 53 } 54 } 55 Operator(const std::string & opName,std::unique_ptr<RunTimeString> param,bool negation)56 Operator(const std::string &opName, std::unique_ptr<RunTimeString> param, 57 bool negation) 58 : m_match_message(""), 59 m_negation(negation), 60 m_op(opName), 61 m_param(""), 62 m_string(std::move(param)), 63 m_couldContainsMacro(false) { 64 if (m_couldContainsMacro == false && m_string) { 65 m_param = m_string->evaluate(); 66 } 67 } 68 Operator(const std::string & opName,const std::string & param)69 Operator(const std::string &opName, const std::string ¶m) 70 : m_match_message(""), 71 m_negation(false), 72 m_op(opName), 73 m_param(param), 74 m_couldContainsMacro(false) { 75 if (m_couldContainsMacro == false && m_string) { 76 m_param = m_string->evaluate(); 77 } 78 } 79 Operator(const std::string & opName,std::unique_ptr<RunTimeString> param)80 Operator(const std::string &opName, std::unique_ptr<RunTimeString> param) 81 : m_match_message(""), 82 m_negation(false), 83 m_op(opName), 84 m_param(""), 85 m_string(std::move(param)), 86 m_couldContainsMacro(false) { 87 if (m_couldContainsMacro == false && m_string) { 88 m_param = m_string->evaluate(); 89 } 90 } 91 Operator(const std::string & opName)92 explicit Operator(const std::string &opName) 93 : m_match_message(""), 94 m_negation(false), 95 m_op(opName), 96 m_param(), 97 m_couldContainsMacro(false) { 98 if (m_couldContainsMacro == false && m_string) { 99 m_param = m_string->evaluate(); 100 } 101 } 102 ~Operator()103 virtual ~Operator() { } 104 static Operator *instantiate(std::string opName, std::string param); 105 init(const std::string & arg,std::string * error)106 virtual bool init(const std::string &arg, std::string *error) { 107 return true; 108 } 109 110 virtual std::string resolveMatchMessage(Transaction *t, 111 std::string key, std::string value); 112 113 bool evaluateInternal(Transaction *t, const std::string& a); 114 bool evaluateInternal(Transaction *t, RuleWithActions *rule, 115 const std::string& a); 116 bool evaluateInternal(Transaction *t, RuleWithActions *rule, 117 const std::string& a, std::shared_ptr<RuleMessage> ruleMessage); 118 119 120 virtual bool evaluate(Transaction *transaction, const std::string &str); evaluate(Transaction * transaction,RuleWithActions * rule,const std::string & str)121 virtual bool evaluate(Transaction *transaction, RuleWithActions *rule, 122 const std::string &str) { 123 return evaluate(transaction, str); 124 } evaluate(Transaction * transaction,RuleWithActions * rule,const std::string & str,std::shared_ptr<RuleMessage> ruleMessage)125 virtual bool evaluate(Transaction *transaction, RuleWithActions *rule, 126 const std::string &str, std::shared_ptr<RuleMessage> ruleMessage) { 127 return evaluate(transaction, str); 128 } 129 logOffset(std::shared_ptr<RuleMessage> ruleMessage,int offset,int len)130 static void logOffset(std::shared_ptr<RuleMessage> ruleMessage, int offset, int len) { 131 if (ruleMessage) { 132 ruleMessage->m_reference.append("o" 133 + std::to_string(offset) + "," 134 + std::to_string(len)); 135 } 136 } 137 138 std::string m_match_message; 139 bool m_negation; 140 std::string m_op; 141 std::string m_param; 142 std::unique_ptr<RunTimeString> m_string; 143 bool m_couldContainsMacro; 144 }; 145 146 } // namespace operators 147 } // namespace modsecurity 148 149 150 #endif // SRC_OPERATORS_OPERATOR_H__ 151