Skip to content

Commit 1d6e72e

Browse files
authored
Merge pull request #3212 from eduar-hte/defensive-intervention
Do not assume ModSecurityIntervention argument to transaction::intervention has been initialized/cleaned
2 parents e8db92e + cf643d6 commit 1d6e72e

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
matrix:
1313
os: [ubuntu-22.04]
1414
platform:
15-
- {label: "x64", arch: "amd64", configure: "--enable-assertions=yes"}
16-
- {label: "x32", arch: "i386", configure: "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --enable-assertions=yes"}
15+
- {label: "x64", arch: "amd64", configure: ""}
16+
- {label: "x32", arch: "i386", configure: "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32"}
1717
compiler:
1818
- {label: "gcc", cc: "gcc", cxx: "g++"}
1919
- {label: "clang", cc: "clang", cxx: "clang++"}
@@ -66,7 +66,7 @@ jobs:
6666
env:
6767
CC: ${{ matrix.compiler.cc }}
6868
CXX: ${{ matrix.compiler.cxx }}
69-
run: ./configure ${{ matrix.platform.configure }} ${{ matrix.configure.opt }}
69+
run: ./configure ${{ matrix.platform.configure }} ${{ matrix.configure.opt }} --enable-assertions=yes
7070
- uses: ammaraskar/gcc-problem-matcher@master
7171
- name: make
7272
run: make -j `nproc`

src/transaction.cc

+23-9
Original file line numberDiff line numberDiff line change
@@ -1469,31 +1469,40 @@ int Transaction::processLogging() {
14691469
* @brief Check if ModSecurity has anything to ask to the server.
14701470
*
14711471
* Intervention can generate a log event and/or perform a disruptive action.
1472+
*
1473+
* If the return value is true, all fields in the ModSecurityIntervention
1474+
* parameter have been initialized and are safe to access.
1475+
* If the return value is false, no changes to the ModSecurityIntervention
1476+
* parameter have been made.
14721477
*
1473-
* @param Pointer ModSecurityIntervention structure
1478+
* @param it Pointer to ModSecurityIntervention structure
14741479
* @retval true A intervention should be made.
14751480
* @retval false Nothing to be done.
14761481
*
14771482
*/
14781483
bool Transaction::intervention(ModSecurityIntervention *it) {
1484+
const auto disruptive = m_it.disruptive;
14791485
if (m_it.disruptive) {
14801486
if (m_it.url) {
14811487
it->url = strdup(m_it.url);
1488+
} else {
1489+
it->url = NULL;
14821490
}
14831491
it->disruptive = m_it.disruptive;
14841492
it->status = m_it.status;
14851493

14861494
if (m_it.log != NULL) {
1487-
std::string log("");
1488-
log.append(m_it.log);
1489-
utils::string::replaceAll(&log, std::string("%d"),
1495+
std::string log(m_it.log);
1496+
utils::string::replaceAll(log, "%d",
14901497
std::to_string(it->status));
14911498
it->log = strdup(log.c_str());
1499+
} else {
1500+
it->log = NULL;
14921501
}
14931502
intervention::reset(&m_it);
14941503
}
14951504

1496-
return it->disruptive;
1505+
return disruptive;
14971506
}
14981507

14991508

@@ -2260,11 +2269,16 @@ extern "C" void msc_transaction_cleanup(Transaction *transaction) {
22602269
*
22612270
* Intervention can generate a log event and/or perform a disruptive action.
22622271
*
2263-
* @param transaction ModSecurity transaction.
2272+
* If the return value is not zero, all fields in the ModSecurityIntervention
2273+
* parameter have been initialized and are safe to access.
2274+
* If the return value is zero, no changes to the ModSecurityIntervention
2275+
* parameter have been made.
22642276
*
2265-
* @return Pointer to ModSecurityIntervention structure
2266-
* @retval >0 A intervention should be made.
2267-
* @retval NULL Nothing to be done.
2277+
* @param transaction ModSecurity transaction.
2278+
* @param it Pointer to ModSecurityIntervention structure
2279+
* @returns If an intervention should be made
2280+
* @retval >0 A intervention should be made.
2281+
* @retval 0 Nothing to be done.
22682282
*
22692283
*/
22702284
extern "C" int msc_intervention(Transaction *transaction,

src/utils/string.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ unsigned char *c2x(unsigned what, unsigned char *where) {
254254
}
255255

256256

257-
void replaceAll(std::string *str, const std::string& from,
258-
const std::string& to) {
257+
void replaceAll(std::string &str, std::string_view from,
258+
std::string_view to) {
259259
size_t start_pos = 0;
260-
while ((start_pos = str->find(from, start_pos)) != std::string::npos) {
261-
str->replace(start_pos, from.length(), to);
260+
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
261+
str.replace(start_pos, from.length(), to);
262262
start_pos += to.length();
263263
}
264264
}

src/utils/string.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ std::vector<std::string> ssplit(std::string str, char delimiter);
6868
std::pair<std::string, std::string> ssplit_pair(const std::string& str, char delimiter);
6969
std::vector<std::string> split(std::string str, char delimiter);
7070
void chomp(std::string *str);
71-
void replaceAll(std::string *str, const std::string& from,
72-
const std::string& to);
71+
void replaceAll(std::string &str, std::string_view from,
72+
std::string_view to);
7373
std::string removeWhiteSpacesIfNeeded(std::string a);
7474
std::string parserSanitizer(std::string a);
7575

0 commit comments

Comments
 (0)