Skip to content

Commit eec1f00

Browse files
zimmerleFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Using a custom VariableMatch* implementation
Delay the variable name resolution till last minute. Fix one of the issues raised in #2376
1 parent 97762dc commit eec1f00

15 files changed

+710
-26
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
5+
- Using a custom VariableMatch* implementation
6+
[#2428 - @zimmerle, @martinhsv]
47
- Avoids to cleanup GeoIp on ModSecurity destructor
58
[#2041 - @zimmerle, @jptosso, @victorhora]
69
- Fix memory leak of RuleMessages objects
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2020 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+
#ifdef __cplusplus
17+
#include <vector>
18+
#include <utility>
19+
20+
#include "modsecurity/string_view.hpp"
21+
#endif
22+
23+
#include "modsecurity/variable_value.h"
24+
25+
26+
#ifndef HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_H_
27+
#define HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_H_
28+
29+
#ifdef __cplusplus
30+
31+
namespace modsecurity {
32+
class Transaction;
33+
namespace Utils {
34+
class Regex;
35+
}
36+
namespace variables {
37+
class KeyExclusions;
38+
}
39+
40+
41+
class AnchoredVariableMatchVars {
42+
public:
43+
explicit AnchoredVariableMatchVars(Transaction *t)
44+
: m_name("MATCHED_VARS"),
45+
m_transaction(t)
46+
{ }
47+
48+
AnchoredVariableMatchVars(const AnchoredVariableMatchVars &a) = delete;
49+
AnchoredVariableMatchVars &operator= (const AnchoredVariableMatchVars &a) = delete;
50+
51+
void set(std::shared_ptr<const VariableValue> v) noexcept {
52+
m_vvs.push_back(v);
53+
}
54+
55+
void unset() noexcept {
56+
m_vvs.clear();
57+
}
58+
59+
void resolve(VariableValues *l,
60+
const variables::KeyExclusions &ke) const noexcept;
61+
62+
void resolve(const std::string &key,
63+
VariableValues *l) const noexcept;
64+
65+
66+
void resolveRegularExpression(const Utils::Regex *r,
67+
VariableValues *l,
68+
const variables::KeyExclusions &ke) const noexcept;
69+
70+
std::unique_ptr<std::string> resolveFirst(const std::string &key) const noexcept;
71+
72+
private:
73+
std::vector<std::shared_ptr<const VariableValue>> m_vvs;
74+
const std::string m_name;
75+
const Transaction *m_transaction;
76+
};
77+
78+
79+
} // namespace modsecurity
80+
81+
#endif
82+
83+
84+
#endif // HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_H_
85+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2020 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+
#ifdef __cplusplus
17+
#include <vector>
18+
#include <utility>
19+
20+
#include "modsecurity/string_view.hpp"
21+
#endif
22+
23+
#include "modsecurity/variable_value.h"
24+
25+
26+
#ifndef HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_NAMES_H_
27+
#define HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_NAMES_H_
28+
29+
#ifdef __cplusplus
30+
31+
namespace modsecurity {
32+
class Transaction;
33+
namespace Utils {
34+
class Regex;
35+
}
36+
namespace variables {
37+
class KeyExclusions;
38+
}
39+
40+
41+
class AnchoredVariableMatchVarsNames {
42+
public:
43+
explicit AnchoredVariableMatchVarsNames(Transaction *t)
44+
: m_name("MATCHED_VARS_NAMES"),
45+
m_transaction(t)
46+
{ }
47+
48+
AnchoredVariableMatchVarsNames(const AnchoredVariableMatchVarsNames &a) = delete;
49+
AnchoredVariableMatchVarsNames &operator= (const AnchoredVariableMatchVarsNames &a) = delete;
50+
51+
void set(std::shared_ptr<const VariableValue> v) noexcept {
52+
m_vvs.push_back(v);
53+
}
54+
55+
void unset() noexcept {
56+
m_vvs.clear();
57+
}
58+
59+
void resolve(VariableValues *l,
60+
const variables::KeyExclusions &ke) const noexcept;
61+
62+
void resolve(const std::string &key,
63+
VariableValues *l) const noexcept;
64+
65+
66+
void resolveRegularExpression(const Utils::Regex *r,
67+
VariableValues *l,
68+
const variables::KeyExclusions &ke) const noexcept;
69+
70+
std::unique_ptr<std::string> resolveFirst(const std::string &key) const noexcept;
71+
72+
private:
73+
std::vector<std::shared_ptr<const VariableValue>> m_vvs;
74+
const std::string m_name;
75+
const Transaction *m_transaction;
76+
};
77+
78+
79+
} // namespace modsecurity
80+
81+
#endif
82+
83+
84+
#endif // HEADERS_MODSECURITY_ANCHORED_SET_VARIABLE_MATCH_VARS_NAMES_H_
85+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 - 2020 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+
#ifdef __cplusplus
17+
#include <ctime>
18+
#include <fstream>
19+
#include <iomanip>
20+
#include <iostream>
21+
#include <list>
22+
#include <map>
23+
#include <sstream>
24+
#include <string>
25+
#include <unordered_map>
26+
#include <utility>
27+
#include <vector>
28+
#include <memory>
29+
#include <cstring>
30+
31+
#include "modsecurity/string_view.hpp"
32+
#endif
33+
34+
#include "modsecurity/variable_value.h"
35+
36+
37+
#ifndef HEADERS_MODSECURITY_ANCHORED_VARIABLE_NAME_H_
38+
#define HEADERS_MODSECURITY_ANCHORED_VARIABLE_NAME_H_
39+
40+
#ifdef __cplusplus
41+
42+
43+
namespace modsecurity {
44+
class Transaction;
45+
46+
class AnchoredVariableMatchVarName {
47+
public:
48+
AnchoredVariableMatchVarName()
49+
: m_name("MATCHED_VAR_NAME")
50+
{ }
51+
52+
AnchoredVariableMatchVarName(const AnchoredVariableMatchVarName &a) = delete;
53+
AnchoredVariableMatchVarName &operator= (const AnchoredVariableMatchVarName &a) = delete;
54+
55+
void set(std::shared_ptr<const VariableValue> v) noexcept {
56+
m_vv = v;
57+
}
58+
59+
void unset() noexcept {
60+
m_vv = nullptr;
61+
}
62+
63+
void evaluate(VariableValues *l) const noexcept {
64+
if (!m_vv) {
65+
return;
66+
}
67+
const VariableValue *var = new VariableValue(
68+
std::unique_ptr<std::string>(new std::string(m_vv->getName())),
69+
&m_name
70+
);
71+
l->push_back(std::unique_ptr<const VariableValue>(var));
72+
}
73+
74+
std::unique_ptr<std::string> resolveFirst() const noexcept {
75+
if (m_vv) {
76+
return std::unique_ptr<std::string>(new std::string(m_vv->getName()));
77+
}
78+
return std::unique_ptr<std::string>(new std::string(""));
79+
}
80+
81+
private:
82+
std::shared_ptr<const VariableValue> m_vv;
83+
const std::string m_name;
84+
};
85+
86+
87+
} // namespace modsecurity
88+
89+
#endif
90+
91+
92+
#endif // HEADERS_MODSECURITY_ANCHORED_VARIABLE_NAME_H_

headers/modsecurity/transaction.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ typedef struct Rules_t RulesSet;
4242
#endif
4343

4444
#include "modsecurity/anchored_set_variable.h"
45+
#include "modsecurity/anchored_set_variable_match_vars_names.h"
46+
#include "modsecurity/anchored_set_variable_match_vars.h"
47+
#include "modsecurity/anchored_variable_match_var_name.h"
4548
#include "modsecurity/anchored_variable.h"
4649
#include "modsecurity/intervention.h"
4750
#include "modsecurity/collection/collections.h"
@@ -136,7 +139,7 @@ class TransactionAnchoredVariables {
136139
m_variableFullRequestLength(t, "FULL_REQUEST_LENGTH"),
137140
m_variableInboundDataError(t, "INBOUND_DATA_ERROR"),
138141
m_variableMatchedVar(t, "MATCHED_VAR"),
139-
m_variableMatchedVarName(t, "MATCHED_VAR_NAME"),
142+
m_variableMatchedVarName(),
140143
m_variableMultipartBoundaryQuoted(t, "MULTIPART_BOUNDARY_QUOTED"),
141144
m_variableMultipartBoundaryWhiteSpace(t,
142145
"MULTIPART_BOUNDARY_WHITESPACE"),
@@ -195,8 +198,8 @@ class TransactionAnchoredVariables {
195198
m_variableFilesTmpContent(t, "FILES_TMP_CONTENT"),
196199
m_variableMultipartFileName(t, "MULTIPART_FILENAME"),
197200
m_variableMultipartName(t, "MULTIPART_NAME"),
198-
m_variableMatchedVarsNames(t, "MATCHED_VARS_NAMES"),
199-
m_variableMatchedVars(t, "MATCHED_VARS"),
201+
m_variableMatchedVarsNames(t),
202+
m_variableMatchedVars(t),
200203
m_variableFiles(t, "FILES"),
201204
m_variableRequestCookies(t, "REQUEST_COOKIES"),
202205
m_variableRequestHeaders(t, "REQUEST_HEADERS"),
@@ -220,7 +223,7 @@ class TransactionAnchoredVariables {
220223
AnchoredVariable m_variableFullRequestLength;
221224
AnchoredVariable m_variableInboundDataError;
222225
AnchoredVariable m_variableMatchedVar;
223-
AnchoredVariable m_variableMatchedVarName;
226+
AnchoredVariableMatchVarName m_variableMatchedVarName;
224227
AnchoredVariable m_variableMultipartBoundaryQuoted;
225228
AnchoredVariable m_variableMultipartBoundaryWhiteSpace;
226229
AnchoredVariable m_variableMultipartCrlfLFLines;
@@ -276,8 +279,8 @@ class TransactionAnchoredVariables {
276279
AnchoredSetVariable m_variableFilesTmpContent;
277280
AnchoredSetVariable m_variableMultipartFileName;
278281
AnchoredSetVariable m_variableMultipartName;
279-
AnchoredSetVariable m_variableMatchedVarsNames;
280-
AnchoredSetVariable m_variableMatchedVars;
282+
AnchoredVariableMatchVarsNames m_variableMatchedVarsNames;
283+
AnchoredVariableMatchVars m_variableMatchedVars;
281284
AnchoredSetVariable m_variableFiles;
282285
AnchoredSetVariable m_variableRequestCookies;
283286
AnchoredSetVariable m_variableRequestHeaders;

src/Makefile.am

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ MAINTAINERCLEANFILES = \
3535

3636

3737
pkginclude_HEADERS = \
38-
../headers/modsecurity/anchored_set_variable.h \
39-
../headers/modsecurity/anchored_variable.h \
38+
../headers/modsecurity/anchored_set_variable.h \
39+
../headers/modsecurity/anchored_set_variable_match_vars.h \
40+
../headers/modsecurity/anchored_set_variable_match_vars_names.h \
41+
../headers/modsecurity/anchored_variable.h \
42+
../headers/modsecurity/anchored_variable_match_var_name.h \
4043
../headers/modsecurity/audit_log.h \
4144
../headers/modsecurity/debug_log.h \
4245
../headers/modsecurity/intervention.h \
@@ -272,6 +275,8 @@ libmodsecurity_la_SOURCES = \
272275
parser/driver.cc \
273276
transaction.cc \
274277
anchored_set_variable.cc \
278+
anchored_set_variable_match_vars.cc \
279+
anchored_set_variable_match_vars_names.cc \
275280
anchored_variable.cc \
276281
audit_log/audit_log.cc \
277282
audit_log/writer/writer.cc \

0 commit comments

Comments
 (0)