Skip to content

Commit 5a2a65b

Browse files
committed
Updated Env::evaluate to support case-insensitive environment variable names in Windows
- Env::evaluate - Environment variable names in Windows are case-insensitive, so in the Windows build we use strcasecmp to ignore case when matching variables in transaction->m_variableEnvs. - If the variable is found, we use the expected variable name to create the VariableValue instance, as further rule processing will look for the variable using case-sensitive comparisons. - This code is not limited to Windows to avoid another #ifdef block because for other platforms, because the env variable names are case-sensitive the value from either x.first and m_name will be the same. - In Windows build, avoid redefining environ, already defined by including stdlib.h.
1 parent b5c7624 commit 5a2a65b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/variables/env.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@
2525
#include <utility>
2626
#include <map>
2727

28+
#ifdef WIN32
29+
#include "src/compat/msvc.h"
30+
#endif
31+
2832
#include "modsecurity/transaction.h"
2933

34+
#ifndef WIN32
3035
extern char **environ;
36+
#endif
3137

3238
namespace modsecurity {
3339
namespace variables {
@@ -47,12 +53,20 @@ void Env::evaluate(Transaction *transaction,
4753
transaction->m_variableEnvs.insert(a);
4854
}
4955

56+
const auto hasName = m_name.length() > 0;
5057
for (auto& x : transaction->m_variableEnvs) {
51-
if (x.first != m_name && m_name.length() > 0) {
58+
#ifndef WIN32
59+
if (hasName && x.first != m_name) {
60+
#else
61+
if (hasName && strcasecmp(x.first.c_str(), m_name.c_str()) != 0) {
62+
#endif
5263
continue;
5364
}
54-
if (!m_keyExclusion.toOmit(x.first)) {
55-
l->push_back(new VariableValue(&m_collectionName, &x.first,
65+
// (Windows) we need to keep the case from the rule in case that from
66+
// the environment differs.
67+
const auto &key = hasName ? m_name : x.first;
68+
if (!m_keyExclusion.toOmit(key)) {
69+
l->push_back(new VariableValue(&m_collectionName, &key,
5670
&x.second));
5771
}
5872
}

0 commit comments

Comments
 (0)