|
| 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 | +#ifndef HEADERS_MODSECURITY_RULE_REMOVE_TARGET_ENTRY_H_ |
| 17 | +#define HEADERS_MODSECURITY_RULE_REMOVE_TARGET_ENTRY_H_ |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "src/utils/regex.h" |
| 23 | + |
| 24 | +namespace modsecurity { |
| 25 | + |
| 26 | +/** |
| 27 | + * Entry for ctl:ruleRemoveTargetById exclusion. |
| 28 | + * Supports literal target (e.g. ARGS:pwd) or regex (e.g. ARGS:/^json\.\d+\.JobDescription$/). |
| 29 | + * Regex is compiled at config load (maintainer's approach). |
| 30 | + */ |
| 31 | +struct RuleRemoveTargetByIdEntry { |
| 32 | + int id; |
| 33 | + std::string literal; |
| 34 | + std::shared_ptr<Utils::Regex> regex; // shared: same compiled regex reused per request |
| 35 | + |
| 36 | + /** |
| 37 | + * Match VariableValue. For regex: match against key (dict element). |
| 38 | + * For literal: match against keyWithCollection (e.g. ARGS:mixpanel). |
| 39 | + */ |
| 40 | + bool matches(const std::string &key, const std::string &keyWithCollection) const { |
| 41 | + if (regex) { |
| 42 | + return regex->searchAll(key).size() > 0; |
| 43 | + } |
| 44 | + return literal == keyWithCollection; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Match Variable (for getFinalVars). Uses case-insensitive literal match. |
| 49 | + * Regex uses key from variable's fullName (extract part after colon). |
| 50 | + */ |
| 51 | + bool matchesVariable(const std::string &fullName) const { |
| 52 | + if (regex) { |
| 53 | + size_t colon = fullName.find(':'); |
| 54 | + std::string keyPart = (colon != std::string::npos && colon + 1 < fullName.size()) |
| 55 | + ? fullName.substr(colon + 1) : fullName; |
| 56 | + return regex->searchAll(keyPart).size() > 0; |
| 57 | + } |
| 58 | + if (literal.size() != fullName.size()) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + return std::equal(literal.begin(), literal.end(), fullName.begin(), |
| 62 | + [](char a, char b) { |
| 63 | + return std::tolower(static_cast<unsigned char>(a)) == |
| 64 | + std::tolower(static_cast<unsigned char>(b)); |
| 65 | + }); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +} // namespace modsecurity |
| 70 | + |
| 71 | +#endif // HEADERS_MODSECURITY_RULE_REMOVE_TARGET_ENTRY_H_ |
0 commit comments