Skip to content

Commit cf24aea

Browse files
authored
Merge pull request #3405 from airween/v3/pmfromffix
fix: @pmFromFile with multiple files issue
2 parents 3150740 + 6089b6b commit cf24aea

File tree

5 files changed

+78
-24
lines changed

5 files changed

+78
-24
lines changed

src/operators/pm_from_file.cc

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#include "src/operators/operator.h"
2121
#include "src/utils/https_client.h"
2222
#include "src/utils/system.h"
23+
#include "src/utils/string.h"
2324

25+
using namespace modsecurity::utils::string;
2426

2527
namespace modsecurity {
2628
namespace operators {
@@ -44,39 +46,47 @@ bool PmFromFile::isComment(const std::string &s) {
4446
}
4547

4648
bool PmFromFile::init(const std::string &config, std::string *error) {
47-
std::istream *iss;
48-
49-
if (m_param.compare(0, 8, "https://") == 0) {
50-
Utils::HttpsClient client;
51-
bool ret = client.download(m_param);
52-
if (ret == false) {
53-
error->assign(client.error);
54-
return false;
55-
}
56-
iss = new std::stringstream(client.content);
57-
} else {
58-
std::string err;
59-
std::string resource = utils::find_resource(m_param, config, &err);
60-
iss = new std::ifstream(resource, std::ios::in);
49+
std::vector<std::string> tokens = split(m_param, ' ');
50+
51+
for (const auto& token : tokens) {
52+
if (! token.empty()) {
53+
54+
std::istream *iss;
55+
56+
if (token.compare(0, 8, "https://") == 0) {
57+
Utils::HttpsClient client;
58+
bool ret = client.download(token);
59+
if (ret == false) {
60+
error->assign(client.error);
61+
return false;
62+
}
63+
iss = new std::stringstream(client.content);
64+
} else {
65+
std::string err;
66+
std::string resource = utils::find_resource(token, config, &err);
67+
iss = new std::ifstream(resource, std::ios::in);
68+
69+
if (((std::ifstream *)iss)->is_open() == false) {
70+
error->assign("Failed to open file: '" + token + "'. " + err);
71+
delete iss;
72+
return false;
73+
}
74+
}
75+
76+
for (std::string line; std::getline(*iss, line); ) {
77+
if (isComment(line) == false) {
78+
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
79+
}
80+
}
6181

62-
if (((std::ifstream *)iss)->is_open() == false) {
63-
error->assign("Failed to open file: " + m_param + ". " + err);
6482
delete iss;
65-
return false;
6683
}
6784
}
6885

69-
for (std::string line; std::getline(*iss, line); ) {
70-
if (isComment(line) == false) {
71-
acmp_add_pattern(m_p, line.c_str(), NULL, NULL, line.length());
72-
}
73-
}
74-
7586
while (m_p->is_failtree_done == 0) {
7687
acmp_prepare(m_p);
7788
}
7889

79-
delete iss;
8090
return true;
8191
}
8292

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# comment
2+
pattern1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# comment
2+
pattern2
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"enabled": 1,
4+
"version_min": 300000,
5+
"version_max": 0,
6+
"title": "pmFromFile operator test",
7+
"client": {
8+
"ip": "10.20.30.40",
9+
"port": 2313
10+
},
11+
"server": {
12+
"ip": "1.2.3.4",
13+
"port": 80
14+
},
15+
"request": {
16+
"headers": {
17+
"Host": "foobar.com"
18+
},
19+
"uri": "\/test.php?param1=pattern2",
20+
"method": "GET",
21+
"http_version": 1.1,
22+
"body": ""
23+
},
24+
"response": {
25+
"headers": {
26+
"Content-Type": "text\/html; charset=utf-8\n\r",
27+
"Content-Length": "10\n\r"
28+
}
29+
},
30+
"expected": {
31+
"debug_log": "Rule returned 1",
32+
"http_code": 403
33+
},
34+
"rules": [
35+
"SecRuleEngine On",
36+
"SecRule ARGS \"@pmFromFile test-cases/data/pattern-file1.data test-cases/data/pattern-file2.data\" \"phase:1,id:999,deny\""
37+
]
38+
}
39+
]

test/test-suite.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ TESTS+=test/test-cases/regression/operator-fuzzyhash.json
8686
TESTS+=test/test-cases/regression/operator-inpectFile.json
8787
TESTS+=test/test-cases/regression/operator-ipMatchFromFile.json
8888
TESTS+=test/test-cases/regression/operator-pm.json
89+
TESTS+=test/test-cases/regression/operator-pmfromfile.json
8990
TESTS+=test/test-cases/regression/operator-rx.json
9091
TESTS+=test/test-cases/regression/operator-rxGlobal.json
9192
TESTS+=test/test-cases/regression/operator-UnconditionalMatch.json

0 commit comments

Comments
 (0)