|
| 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 | + |
| 17 | +#ifdef __cplusplus |
| 18 | +#include <string> |
| 19 | +#include <algorithm> |
| 20 | +#include <memory> |
| 21 | +#include <functional> |
| 22 | +#include <iostream> |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "modsecurity/variable_value.h" |
| 26 | +#include "modsecurity/anchored_set_variable.h" |
| 27 | + |
| 28 | + |
| 29 | +#ifndef HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_TRANSLATION_PROXY_H_ |
| 30 | +#define HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_TRANSLATION_PROXY_H_ |
| 31 | + |
| 32 | +#ifdef __cplusplus |
| 33 | + |
| 34 | +namespace modsecurity { |
| 35 | + |
| 36 | + |
| 37 | +class AnchoredSetVariableTranslationProxy { |
| 38 | + public: |
| 39 | + AnchoredSetVariableTranslationProxy( |
| 40 | + const std::string &name, |
| 41 | + AnchoredSetVariable *fount) |
| 42 | + : m_name(name), |
| 43 | + m_fount(fount) |
| 44 | + { |
| 45 | + m_translate = [](std::string *name, std::vector<const VariableValue *> *l) { |
| 46 | + for (int i = 0; i < l->size(); ++i) { |
| 47 | + VariableValue *newVariableValue = new VariableValue(name, &l->at(i)->getKey()); |
| 48 | + const VariableValue *oldVariableValue = l->at(i); |
| 49 | + l->at(i) = newVariableValue; |
| 50 | + for (auto &oldOrigin : oldVariableValue->getOrigin()) { |
| 51 | + std::unique_ptr<VariableOrigin> newOrigin(new VariableOrigin); |
| 52 | + newOrigin->m_length = oldVariableValue->getKey().size(); |
| 53 | + newOrigin->m_offset = oldOrigin->m_offset - oldVariableValue->getKey().size() - 1; |
| 54 | + newVariableValue->addOrigin(std::move(newOrigin)); |
| 55 | + } |
| 56 | + delete oldVariableValue; |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + virtual ~AnchoredSetVariableTranslationProxy() |
| 62 | + { } |
| 63 | + |
| 64 | + void resolve(std::vector<const VariableValue *> *l) { |
| 65 | + m_fount->resolve(l); |
| 66 | + m_translate(&m_name, l); |
| 67 | + } |
| 68 | + |
| 69 | + void resolve(std::vector<const VariableValue *> *l, |
| 70 | + variables::KeyExclusions &ke) { |
| 71 | + m_fount->resolve(l, ke); |
| 72 | + m_translate(&m_name, l); |
| 73 | + } |
| 74 | + |
| 75 | + void resolve(const std::string &key, |
| 76 | + std::vector<const VariableValue *> *l) { |
| 77 | + m_fount->resolve(key, l); |
| 78 | + m_translate(&m_name, l); |
| 79 | + }; |
| 80 | + |
| 81 | + void resolveRegularExpression(Utils::Regex *r, |
| 82 | + std::vector<const VariableValue *> *l) { |
| 83 | + m_fount->resolveRegularExpression(r, l); |
| 84 | + m_translate(&m_name, l); |
| 85 | + }; |
| 86 | + |
| 87 | + void resolveRegularExpression(Utils::Regex *r, |
| 88 | + std::vector<const VariableValue *> *l, |
| 89 | + variables::KeyExclusions &ke) { |
| 90 | + m_fount->resolveRegularExpression(r, l, ke); |
| 91 | + m_translate(&m_name, l); |
| 92 | + }; |
| 93 | + |
| 94 | + std::unique_ptr<std::string> resolveFirst(const std::string &key) { |
| 95 | + std::vector<const VariableValue *> l; |
| 96 | + resolve(&l); |
| 97 | + |
| 98 | + if (l.empty()) { |
| 99 | + return nullptr; |
| 100 | + } |
| 101 | + |
| 102 | + std::unique_ptr<std::string> ret(new std::string("")); |
| 103 | + |
| 104 | + ret->assign(l.at(0)->getValue()); |
| 105 | + |
| 106 | + while (!l.empty()) { |
| 107 | + auto &a = l.back(); |
| 108 | + l.pop_back(); |
| 109 | + delete a; |
| 110 | + } |
| 111 | + |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + std::string m_name; |
| 116 | + private: |
| 117 | + AnchoredSetVariable *m_fount; |
| 118 | + std::function<void(std::string *name, std::vector<const VariableValue *> *l)> m_translate; |
| 119 | +}; |
| 120 | + |
| 121 | +} // namespace modsecurity |
| 122 | + |
| 123 | +#endif |
| 124 | + |
| 125 | + |
| 126 | +#endif // HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_TRANSLATION_PROXY_H_ |
0 commit comments