Skip to content

Commit 97c3d15

Browse files
authored
Merge pull request #3203 from airween/v3/sethostname
feat(api) new function: set hostname
2 parents a14cdc4 + c7efeb6 commit 97c3d15

File tree

8 files changed

+116
-2
lines changed

8 files changed

+116
-2
lines changed

headers/modsecurity/rule_message.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class RuleMessage {
6767
m_ruleLine(rule->getLineNumber()),
6868
m_saveMessage(true),
6969
m_serverIpAddress(trans->m_serverIpAddress),
70+
m_requestHostName(trans->m_requestHostName),
7071
m_severity(0),
7172
m_uriNoQueryStringDecoded(trans->m_uri_no_query_string_decoded),
7273
m_ver(rule->m_ver),
@@ -92,6 +93,7 @@ class RuleMessage {
9293
m_ruleLine(rule->m_ruleLine),
9394
m_saveMessage(rule->m_saveMessage),
9495
m_serverIpAddress(rule->m_serverIpAddress),
96+
m_requestHostName(rule->m_requestHostName),
9597
m_severity(rule->m_severity),
9698
m_uriNoQueryStringDecoded(rule->m_uriNoQueryStringDecoded),
9799
m_ver(rule->m_ver),
@@ -117,6 +119,7 @@ class RuleMessage {
117119
m_ruleLine(ruleMessage.m_ruleLine),
118120
m_saveMessage(ruleMessage.m_saveMessage),
119121
m_serverIpAddress(ruleMessage.m_serverIpAddress),
122+
m_requestHostName(ruleMessage.m_requestHostName),
120123
m_severity(ruleMessage.m_severity),
121124
m_uriNoQueryStringDecoded(ruleMessage.m_uriNoQueryStringDecoded),
122125
m_ver(ruleMessage.m_ver),
@@ -142,6 +145,7 @@ class RuleMessage {
142145
m_ruleLine = ruleMessage.m_ruleLine;
143146
m_saveMessage = ruleMessage.m_saveMessage;
144147
m_serverIpAddress = ruleMessage.m_serverIpAddress;
148+
m_requestHostName = ruleMessage.m_requestHostName;
145149
m_severity = ruleMessage.m_severity;
146150
m_uriNoQueryStringDecoded = ruleMessage.m_uriNoQueryStringDecoded;
147151
m_ver = ruleMessage.m_ver;
@@ -201,6 +205,7 @@ class RuleMessage {
201205
int m_ruleLine;
202206
bool m_saveMessage;
203207
std::shared_ptr<std::string> m_serverIpAddress;
208+
std::shared_ptr<std::string> m_requestHostName;
204209
int m_severity;
205210
std::shared_ptr<std::string> m_uriNoQueryStringDecoded;
206211
std::string m_ver;

headers/modsecurity/transaction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
393393
int processLogging();
394394
int updateStatusCode(int status);
395395

396+
int setRequestHostName(const std::string& hostname);
397+
396398
bool intervention(ModSecurityIntervention *it);
397399

398400
bool addArgument(const std::string& orig, const std::string& key,
@@ -443,6 +445,11 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
443445
*/
444446
std::shared_ptr<std::string> m_serverIpAddress;
445447

448+
/**
449+
* Holds the request's hostname
450+
*/
451+
std::shared_ptr<std::string> m_requestHostName;
452+
446453
/**
447454
* Holds the raw URI that was requested.
448455
*/
@@ -724,6 +731,9 @@ int msc_process_logging(Transaction *transaction);
724731
/** @ingroup ModSecurity_C_API */
725732
int msc_update_status_code(Transaction *transaction, int status);
726733

734+
/** @ingroup ModSecurity_C_API */
735+
int msc_set_request_hostname(Transaction *transaction, const unsigned char *hostname);
736+
727737
#ifdef __cplusplus
728738
}
729739
} // namespace modsecurity

src/rule_message.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ std::string RuleMessage::_details(const RuleMessage *rm) {
4242
msg.append(" [tag \"" + utils::string::toHexIfNeeded(a, true) + "\"]");
4343
}
4444

45-
msg.append(" [hostname \"" + *rm->m_serverIpAddress.get() \
46-
+ "\"]");
45+
msg.append(" [hostname \"" + *rm->m_requestHostName.get() + "\"]");
46+
4747
msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
4848
msg.append(" [unique_id \"" + *rm->m_id + "\"]");
4949
msg.append(" [ref \"" + utils::string::limitTo(200, rm->m_reference) + "\"]");

src/transaction.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
107107
m_clientIpAddress(std::make_shared<std::string>("")),
108108
m_httpVersion(""),
109109
m_serverIpAddress(std::make_shared<std::string>("")),
110+
m_requestHostName(std::make_shared<std::string>("")),
110111
m_uri(""),
111112
m_uri_no_query_string_decoded(std::make_shared<std::string>("")),
112113
m_ARGScombinedSizeDouble(0),
@@ -183,6 +184,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
183184
m_clientIpAddress(std::make_shared<std::string>("")),
184185
m_httpVersion(""),
185186
m_serverIpAddress(std::make_shared<std::string>("")),
187+
m_requestHostName(std::make_shared<std::string>("")),
186188
m_uri(""),
187189
m_uri_no_query_string_decoded(std::make_shared<std::string>("")),
188190
m_ARGScombinedSizeDouble(0),
@@ -319,6 +321,7 @@ int Transaction::processConnection(const char *client, int cPort,
319321
const char *server, int sPort) {
320322
m_clientIpAddress = std::unique_ptr<std::string>(new std::string(client));
321323
m_serverIpAddress = std::unique_ptr<std::string>(new std::string(server));
324+
m_requestHostName = std::unique_ptr<std::string>(new std::string(server));
322325
this->m_clientPort = cPort;
323326
this->m_serverPort = sPort;
324327
ms_dbg(4, "Transaction context created.");
@@ -2358,5 +2361,52 @@ extern "C" int msc_update_status_code(Transaction *transaction, int status) {
23582361
}
23592362

23602363

2364+
/**
2365+
* @name setRequestHostName
2366+
* @brief Set request's host name
2367+
*
2368+
* With this method it is possible to set the request's hostname.
2369+
*
2370+
* @note This function expects a NULL terminated string.
2371+
*
2372+
* @param hostname hostname.
2373+
*
2374+
* @returns If the operation was successful or not.
2375+
* @retval true Operation was successful.
2376+
* @retval false Operation failed.
2377+
*
2378+
*/
2379+
int Transaction::setRequestHostName(const std::string& hostname) {
2380+
2381+
if (hostname != "") {
2382+
m_requestHostName = std::unique_ptr<std::string>(new std::string(hostname));
2383+
}
2384+
2385+
return true;
2386+
}
2387+
2388+
2389+
/**
2390+
* @name msc_set_request_hostname
2391+
* @brief Set request's host name
2392+
*
2393+
* With this method it is possible to set request's hostname.
2394+
*
2395+
* @note This function expects a NULL terminated string.
2396+
*
2397+
* @param transaction ModSecurity transaction.
2398+
* @param hostname hostname.
2399+
*
2400+
* @returns If the operation was successful or not.
2401+
* @retval 1 Operation was successful.
2402+
* @retval 0 Operation failed.
2403+
*
2404+
*/
2405+
extern "C" int msc_set_request_hostname(Transaction *transaction,
2406+
const unsigned char *hostname) {
2407+
return transaction->setRequestHostName(reinterpret_cast<const char *>(hostname));
2408+
}
2409+
2410+
23612411
} // namespace modsecurity
23622412

test/regression/regression.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ void perform_unit_test(ModSecurityTest<RegressionTest> *test,
309309
modsec_transaction->processConnection(t->clientIp.c_str(),
310310
t->clientPort, t->serverIp.c_str(), t->serverPort);
311311

312+
if (t->hostname != "") {
313+
modsec_transaction->setRequestHostName(t->hostname);
314+
}
315+
312316
actions(&r, modsec_transaction, &serverLog);
313317
#if 0
314318
if (r.status != 200) {

test/regression/regression_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) {
134134
if (strcmp(key2, "port") == 0) {
135135
u->serverPort = YAJL_GET_INTEGER(val2);
136136
}
137+
if (strcmp(key2, "hostname") == 0) {
138+
u->hostname = YAJL_GET_STRING(val2);
139+
}
137140
}
138141
}
139142
if (strcmp(key, "request") == 0) {

test/regression/regression_test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class RegressionTest {
6161
std::string serverIp;
6262
int clientPort;
6363
int serverPort;
64+
std::string hostname;
6465

6566
std::string method;
6667
std::string httpVersion;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[
2+
{
3+
"enabled":1,
4+
"version_min":300000,
5+
"title":"Testing function :: setRequestHostName",
6+
"client": {
7+
"ip":"200.249.12.31"
8+
},
9+
"server":{
10+
"ip":"200.249.12.31",
11+
"port":80,
12+
"hostname":"modsecurity.org"
13+
},
14+
"request": {
15+
"headers": {
16+
"Host":"www.modsecurity.org"
17+
},
18+
"uri":"/foo?q=attack",
19+
"http_version": 1.1
20+
},
21+
"response":{
22+
"headers":{
23+
"Date":"Mon, 13 Jul 2015 20:02:41 GMT",
24+
"Last-Modified":"Sun, 26 Oct 2014 22:33:37 GMT",
25+
"Content-Type":"text/plain"
26+
},
27+
"body":[
28+
"denystring"
29+
]
30+
},
31+
"expected":{
32+
"http_code": 200,
33+
"debug_log": "[hostname: \"modsecurity.org\"]"
34+
},
35+
"rules":[
36+
"SecRuleEngine On",
37+
"SecResponseBodyAccess On",
38+
"SecRule ARGS_GET \"@contains attack\" \"id:1,phase:2,deny\""
39+
]
40+
}
41+
]

0 commit comments

Comments
 (0)