-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
airween
wants to merge
4
commits into
owasp-modsecurity:v2/master
Choose a base branch
from
airween:v2/ctlregexpatt
base: v2/master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be37489
Allow regular expressions in ctl:ruleRemoveTargetByX variable names II.
airween 2b2535c
Split error handling into two parts; add regular log in case of faile…
airween c796804
Pre-calculate string length instead of strlen()
airween 1790659
Statement simplification
airween File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,11 @@ 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; | ||
size_t value_len; | ||
|
||
if(msr == NULL) | ||
return 0; | ||
|
@@ -111,23 +115,59 @@ static int fetch_target_exception(msre_rule *rule, modsec_rec *msr, msre_var *va | |
value = NULL; | ||
} | ||
|
||
value_len = 0; | ||
if (value != NULL) { | ||
value_len = strlen(value); | ||
} | ||
|
||
if((strlen(myname) == strlen(name)) && | ||
(strncasecmp(myname, name,strlen(myname)) == 0)) { | ||
(strncasecmp(myname, name, strlen(myname)) == 0)) { | ||
|
||
if(value != NULL && myvalue != NULL) { | ||
if((strlen(myvalue) == strlen(value)) && | ||
if(value_len > 2 && value[0] == '/' && value[value_len - 1] == '/') { | ||
value[value_len - 1] = '\0'; | ||
#ifdef WITH_PCRE2 | ||
regex = msc_pregcomp(msr->mp, value + 1, | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If needed, logging should be added in msc_pregcomp(), so it's available for all calls |
||
if (msr->txcfg->debuglog_level >= 9) { | ||
#ifdef WITH_PCRE2 | ||
// in case of PCRE2 the errptr won't fill | ||
msr_log(msr, 9, "fetch_target_exception: Regexp /%s/ failed to compile at pos %d.", | ||
value + 1, erroffset); | ||
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: exclusion regexp /%s/ failed to compile at pos %d.", | ||
value + 1, erroffset); | ||
#else | ||
msr_log(msr, 9, "fetch_target_exception: Regexp /%s/ failed to compile at pos %d: %s.", | ||
value + 1, erroffset, errptr); | ||
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: exclusion regexp /%s/ failed to compile at pos %d: %s.", | ||
value + 1, erroffset, errptr); | ||
#endif | ||
} | ||
} else { | ||
#ifdef WITH_PCRE2 | ||
if (!(msc_regexec(regex, myvalue, strlen(myvalue), &errptr) == PCRE2_ERROR_NOMATCH)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct check is |
||
#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) == value_len) && | ||
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); | ||
} | ||
match = 1; | ||
} | ||
} else if (value == NULL && myvalue == NULL) { | ||
if (msr->txcfg->debuglog_level >= 9) { | ||
msr_log(msr, 9, "fetch_target_exception: Target %s will not be processed.", target); | ||
} | ||
match = 1; | ||
} else if (value == NULL && myvalue != NULL) { | ||
} else { | ||
if (msr->txcfg->debuglog_level >= 9) { | ||
msr_log(msr, 9, "fetch_target_exception: Target %s will not be processed.", target); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()).There was a problem hiding this comment.
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.