Skip to content

Commit 426ecf0

Browse files
committed
Uses unique_ptr on REMOTE_USER
1 parent 066182e commit 426ecf0

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

headers/modsecurity/transaction.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,6 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
625625

626626
int m_secRuleEngine;
627627

628-
std::string m_variableRemoteUser;
629-
630628
private:
631629
/**
632630
* Pointer to the callback function that will be called to fill

src/transaction.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
187187
m_json(NULL),
188188
#endif
189189
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
190-
m_variableRemoteUser(""),
191190
m_logCbData(logCbData),
192191
TransactionAnchoredVariables(this),
193192
TransactionRuleMessageManagement(this) {
@@ -253,7 +252,6 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
253252
m_json(NULL),
254253
#endif
255254
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
256-
m_variableRemoteUser(""),
257255
m_logCbData(logCbData),
258256
TransactionAnchoredVariables(this),
259257
TransactionRuleMessageManagement(this) {
@@ -1467,14 +1465,8 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename,
14671465
m_variableRequestHeaders.resolveFirst("Host").get())
14681466
<< " ";
14691467
ss << utils::string::dash_if_empty(this->m_clientIpAddress->c_str()) << " ";
1470-
/** TODO: Check variable */
1471-
variables::RemoteUser *r = new variables::RemoteUser("REMOTE_USER");
1472-
VariableValues l;
1473-
r->evaluate(this, &l);
1474-
delete r;
14751468

1476-
ss << utils::string::dash_if_empty(
1477-
m_variableRemoteUser.c_str());
1469+
ss << utils::string::dash_if_empty(variables::RemoteUser::parserRemoteUser(this).first.c_str());
14781470
ss << " ";
14791471
/** TODO: Check variable */
14801472
//ss << utils::string::dash_if_empty(

src/variables/remote_user.cc

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,22 @@
3030
#include <memory>
3131

3232
#include "modsecurity/transaction.h"
33-
#include "src/utils/base64.h"
33+
3434

3535
namespace modsecurity {
3636
namespace variables {
3737

3838

39+
3940
void RemoteUser::evaluate(Transaction *transaction,
4041
VariableValues *l) {
41-
size_t pos;
42-
std::string base64;
43-
std::string header;
44-
45-
VariableValues l2;
46-
transaction->m_variableRequestHeaders.resolve("authorization", &l2);
47-
48-
if (l2.size() < 1) {
49-
return;
50-
}
51-
52-
header = std::string(l2.at(0)->getValue());
53-
54-
if (header.compare(0, 6, "Basic ") == 0) {
55-
base64 = std::string(header, 6, header.length());
56-
}
57-
58-
base64 = Utils::Base64::decode(base64);
59-
60-
pos = base64.find(":");
61-
if (pos == std::string::npos) {
62-
return;
63-
}
64-
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
6542

66-
const std::string name = l2[0]->getName();
67-
auto var = std::make_shared<VariableValue>(&name, &transaction->m_variableRemoteUser);
43+
auto userName = parserRemoteUser(transaction);
44+
auto var = std::make_shared<VariableValue>(
45+
std::unique_ptr<std::string>(new std::string(userName.first)),
46+
&m_retName);
47+
var->addOrigin(userName.second);
6848

69-
for (auto &i : l2[0]->getOrigin()) {
70-
var->addOrigin(i);
71-
}
7249
l->push_back(std::move(var));
7350
}
7451

src/variables/remote_user.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define SRC_VARIABLES_REMOTE_USER_H_
2424

2525
#include "src/variables/variable.h"
26+
#include "src/utils/base64.h"
2627

2728
namespace modsecurity {
2829

@@ -38,6 +39,38 @@ class RemoteUser : public Variable {
3839

3940
void evaluate(Transaction *transaction,
4041
VariableValues *l) override;
42+
43+
static std::pair<std::string, VariableOrigin> parserRemoteUser(Transaction *transaction) {
44+
size_t pos;
45+
std::string base64;
46+
std::string header;
47+
48+
VariableValues l2;
49+
transaction->m_variableRequestHeaders.resolve("authorization", &l2);
50+
51+
if (l2.size() < 1) {
52+
goto err;
53+
}
54+
55+
header = std::string(l2.at(0)->getValue());
56+
57+
if (header.compare(0, 6, "Basic ") == 0) {
58+
base64 = std::string(header, 6, header.length());
59+
}
60+
61+
base64 = Utils::Base64::decode(base64);
62+
63+
pos = base64.find(":");
64+
if (pos == std::string::npos) {
65+
goto err;
66+
}
67+
68+
return std::make_pair(std::string(base64, 0, pos), l2[0]->getOrigin()[0]);
69+
err:
70+
return std::make_pair(std::string(""), VariableOrigin());
71+
72+
}
73+
4174
std::string m_retName;
4275
};
4376

0 commit comments

Comments
 (0)