|
| 1 | +/* |
| 2 | + * ModSecurity, http://www.modsecurity.org/ |
| 3 | + * Copyright (c) 2023 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 [email protected]. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include "src/actions/expire_var.h" |
| 17 | + |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "modsecurity/rules_set.h" |
| 21 | +#include "modsecurity/transaction.h" |
| 22 | +#include "modsecurity/rule.h" |
| 23 | +#include "src/utils/string.h" |
| 24 | +#include "src/variables/global.h" |
| 25 | +#include "src/variables/ip.h" |
| 26 | +#include "src/variables/resource.h" |
| 27 | +#include "src/variables/session.h" |
| 28 | +#include "src/variables/user.h" |
| 29 | +#include "src/variables/variable.h" |
| 30 | + |
| 31 | +namespace modsecurity { |
| 32 | +namespace actions { |
| 33 | + |
| 34 | + |
| 35 | +bool ExpireVar::init(std::string *error) { |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +bool ExpireVar::evaluate(RuleWithActions *rule, Transaction *t) { |
| 41 | + |
| 42 | + std::string expireExpressionExpanded(m_string->evaluate(t)); |
| 43 | + |
| 44 | + std::string fully_qualified_var; |
| 45 | + int expirySeconds = 0; |
| 46 | + size_t posEquals = expireExpressionExpanded.find("="); |
| 47 | + if (posEquals == std::string::npos) { |
| 48 | + fully_qualified_var = expireExpressionExpanded; |
| 49 | + } else { |
| 50 | + fully_qualified_var = expireExpressionExpanded.substr(0, posEquals); |
| 51 | + std::string expiry = expireExpressionExpanded.substr(posEquals+1); |
| 52 | + if (expiry.find_first_not_of("0123456789") == std::string::npos) { |
| 53 | + expirySeconds = atoi(expiry.c_str()); |
| 54 | + } else { |
| 55 | + ms_dbg_a(t, 5, "Non-numeric expiry seconds found in expirevar expression."); |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + size_t posDot = fully_qualified_var.find("."); |
| 61 | + if (posDot == std::string::npos) { |
| 62 | + ms_dbg_a(t, 5, "No collection found in expirevar expression."); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + std::string collection = fully_qualified_var.substr(0, posDot); |
| 67 | + std::string variable_name = fully_qualified_var.substr(posDot+1); |
| 68 | + std::unique_ptr<RunTimeString> runTimeString(new RunTimeString()); |
| 69 | + runTimeString->appendText(fully_qualified_var); |
| 70 | + |
| 71 | + if (collection == "ip") { |
| 72 | + std::unique_ptr<modsecurity::variables::Ip_DynamicElement> ip_dynamicElement(new modsecurity::variables::Ip_DynamicElement(std::move(runTimeString))); |
| 73 | + ip_dynamicElement->setExpiry(t, variable_name, expirySeconds); |
| 74 | + } else if (collection == "global") { |
| 75 | + std::unique_ptr<modsecurity::variables::Global_DynamicElement> global_dynamicElement(new modsecurity::variables::Global_DynamicElement(std::move(runTimeString))); |
| 76 | + global_dynamicElement->setExpiry(t, variable_name, expirySeconds); |
| 77 | + } else if (collection == "resource") { |
| 78 | + std::unique_ptr<modsecurity::variables::Resource_DynamicElement> resource_dynamicElement(new modsecurity::variables::Resource_DynamicElement(std::move(runTimeString))); |
| 79 | + resource_dynamicElement->setExpiry(t, variable_name, expirySeconds); |
| 80 | + } else if (collection == "session") { |
| 81 | + std::unique_ptr<modsecurity::variables::Session_DynamicElement> session_dynamicElement(new modsecurity::variables::Session_DynamicElement(std::move(runTimeString))); |
| 82 | + session_dynamicElement->setExpiry(t, variable_name, expirySeconds); |
| 83 | + } else if (collection == "user") { |
| 84 | + std::unique_ptr<modsecurity::variables::User_DynamicElement> user_dynamicElement(new modsecurity::variables::User_DynamicElement(std::move(runTimeString))); |
| 85 | + user_dynamicElement->setExpiry(t, variable_name, expirySeconds); |
| 86 | + } else { |
| 87 | + ms_dbg_a(t, 5, "Invalid collection found in expirevar expression: collection must be `ip', `global', `resource', `user' or `session'"); |
| 88 | + } |
| 89 | + ms_dbg_a(t, 9, "Setting variable `" + variable_name + "' to expire in " + std::to_string(expirySeconds) + " seconds."); |
| 90 | + |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace actions |
| 95 | +} // namespace modsecurity |
0 commit comments