Skip to content

Commit 1fd14c6

Browse files
authored
Merge branch 'v3/master' into v3/add-is-interrupted-to-json-log
2 parents 2394fc5 + 84783d0 commit 1fd14c6

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

headers/modsecurity/transaction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <stdlib.h>
3737
#include <stddef.h>
38+
#include <stdint.h>
3839

3940
#ifndef __cplusplus
4041
typedef struct ModSecurity_t ModSecurity;
@@ -747,6 +748,13 @@ int msc_update_status_code(Transaction *transaction, int status);
747748
/** @ingroup ModSecurity_C_API */
748749
int msc_set_request_hostname(Transaction *transaction, const unsigned char *hostname);
749750

751+
/** @ingroup ModSecurity_C_API */
752+
size_t msc_get_rules_messages_size(const Transaction *transaction);
753+
754+
/** @ingroup ModSecurity_C_API */
755+
size_t msc_get_rules_messages_rule_ids(const Transaction *transaction,
756+
int64_t *ids, size_t ids_len);
757+
750758
#ifdef __cplusplus
751759
}
752760
} // namespace modsecurity

src/transaction.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,5 +2330,60 @@ extern "C" int msc_set_request_hostname(Transaction *transaction,
23302330
}
23312331

23322332

2333+
/**
2334+
* @name msc_get_rules_messages_size
2335+
* @brief Retrieve the number of RuleMessage records on a transaction.
2336+
*
2337+
* Returns the size of Transaction::m_rulesMessages: the number of
2338+
* RuleMessage records that were selected to be logged on the
2339+
* transaction. This is not necessarily the total number of rules that
2340+
* matched!
2341+
*
2342+
* @param transaction ModSecurity transaction.
2343+
*
2344+
* @returns The number of RuleMessage records on the transaction.
2345+
*
2346+
*/
2347+
extern "C" size_t msc_get_rules_messages_size(const Transaction *transaction) {
2348+
return transaction->m_rulesMessages.size();
2349+
}
2350+
2351+
2352+
/**
2353+
* @name msc_get_rules_messages_rule_ids
2354+
* @brief Copy the rule ids from Transaction::m_rulesMessages into a buffer.
2355+
*
2356+
* Copies the rule id of each RuleMessage into the caller-provided buffer.
2357+
* Only rule messages that were selected to be logged are included.
2358+
*
2359+
* The caller is expected to size the buffer using
2360+
* msc_get_rules_messages_size. If @p ids_len is smaller than the number
2361+
* of available records, only the first @p ids_len ids are written and
2362+
* the remaining records are skipped.
2363+
*
2364+
* @param transaction ModSecurity transaction.
2365+
* @param ids Caller-provided buffer to receive the rule ids.
2366+
* May be NULL only if @p ids_len is 0.
2367+
* @param ids_len Capacity of @p ids, in number of int64_t slots.
2368+
*
2369+
* @returns The number of rule ids written into @p ids.
2370+
*
2371+
*/
2372+
extern "C" size_t msc_get_rules_messages_rule_ids(const Transaction *transaction,
2373+
int64_t *ids, size_t ids_len) {
2374+
if (ids == nullptr || ids_len == 0) {
2375+
return 0;
2376+
}
2377+
size_t written = 0;
2378+
for (const auto &msg : transaction->m_rulesMessages) {
2379+
if (written >= ids_len) {
2380+
break;
2381+
}
2382+
ids[written++] = msg.m_rule.m_ruleId;
2383+
}
2384+
return written;
2385+
}
2386+
2387+
23332388
} // namespace modsecurity
23342389

0 commit comments

Comments
 (0)