Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow regular expressions in ctl:ruleRemoveTargetByX variable names II. #3121

Open
wants to merge 4 commits into
base: v2/master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion apache2/re.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ static int fetch_target_exception(msre_rule *rule, modsec_rec *msr, msre_var *va
char *c = NULL, *name = NULL, *value = NULL;
char *variable = NULL, *myvar = NULL;
char *myvalue = NULL, *myname = NULL;
msc_regex_t *regex;
char *errptr;
int erroffset;
int match = 0;

if(msr == NULL)
Expand Down Expand Up @@ -115,7 +118,33 @@ static int fetch_target_exception(msre_rule *rule, modsec_rec *msr, msre_var *va
(strncasecmp(myname, name,strlen(myname)) == 0)) {

if(value != NULL && myvalue != NULL) {
if((strlen(myvalue) == strlen(value)) &&
if(strlen(value) > 2 && value[0] == '/' && value[strlen(value) - 1] == '/') {
value[strlen(value) - 1] = '\0';
#ifdef WITH_PCRE2
regex = msc_pregcomp(msr->mp, value + 1,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (copied) code contains a memory leak: the regex variable compiled in each cycle during the exclusion check, but never freed (never called msc_pcre_cleanup()).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reorganize this part. I would expect that regex variable above must be compiled during startup, and not when the engine inspect the exclusion.

PCRE2_DOTALL | PCRE2_CASELESS | PCRE2_DOLLAR_ENDONLY, (const char **)&errptr, &erroffset);
#else
regex = msc_pregcomp(msr->mp, value + 1,
PCRE_DOTALL | PCRE_CASELESS | PCRE_DOLLAR_ENDONLY, (const char **)&errptr, &erroffset);
#endif
if (regex == NULL) {
if (msr->txcfg->debuglog_level >= 9) {
msr_log(msr, 9, "fetch_target_exception: Regexp /%s/ failed to compile at pos %d: %s.",
value + 1, erroffset, errptr);
}
} else {
#ifdef WITH_PCRE2
if (!(msc_regexec(regex, myvalue, strlen(myvalue), &errptr) == PCRE2_ERROR_NOMATCH)) {
#else
if (!(msc_regexec(regex, myvalue, strlen(myvalue), &errptr) == PCRE_ERROR_NOMATCH)) {
#endif
if (msr->txcfg->debuglog_level >= 9) {
msr_log(msr, 9, "fetch_target_exception: Target %s will not be processed.", var->name);
}
match = 1;
}
}
} else if((strlen(myvalue) == strlen(value)) &&
strncasecmp(myvalue,value,strlen(myvalue)) == 0) {
if (msr->txcfg->debuglog_level >= 9) {
msr_log(msr, 9, "fetch_target_exception: Target %s will not be processed.", target);
Expand Down