summaryrefslogtreecommitdiff
path: root/fail2ban
diff options
context:
space:
mode:
authorsebres <info@sebres.de>2022-06-21 16:55:57 +0200
committersebres <info@sebres.de>2022-06-21 16:55:57 +0200
commit7e2ab36d86998575853150c0a57de5e22518cf66 (patch)
tree444755151c1fe365194c68ba74dfb27b960c3b6b /fail2ban
parent500895dcfa31f11c81b3c9128781a49a05e3bd05 (diff)
downloadfail2ban-7e2ab36d86998575853150c0a57de5e22518cf66.tar.gz
move global groups to start of expression (python 3.11 compat)
Diffstat (limited to 'fail2ban')
-rw-r--r--fail2ban/client/fail2banregex.py2
-rw-r--r--fail2ban/server/datetemplate.py8
2 files changed, 9 insertions, 1 deletions
diff --git a/fail2ban/client/fail2banregex.py b/fail2ban/client/fail2banregex.py
index 8c03b2dd..de7cde60 100644
--- a/fail2ban/client/fail2banregex.py
+++ b/fail2ban/client/fail2banregex.py
@@ -334,7 +334,7 @@ class Fail2banRegex(object):
fltFile = None
fltOpt = {}
if regextype == 'fail':
- if re.search(r'^(?ms)/{0,3}[\w/_\-.]+(?:\[.*\])?$', value):
+ if re.search(r'(?ms)^/{0,3}[\w/_\-.]+(?:\[.*\])?$', value):
try:
fltName, fltOpt = extractOptions(value)
if "." in fltName[~5:]:
diff --git a/fail2ban/server/datetemplate.py b/fail2ban/server/datetemplate.py
index 8f1aaeb4..e02772d8 100644
--- a/fail2ban/server/datetemplate.py
+++ b/fail2ban/server/datetemplate.py
@@ -35,6 +35,7 @@ logSys = getLogger(__name__)
# check already grouped contains "(", but ignores char "\(" and conditional "(?(id)...)":
RE_GROUPED = re.compile(r'(?<!(?:\(\?))(?<!\\)\((?!\?)')
RE_GROUP = ( re.compile(r'^((?:\(\?\w+\))?\^?(?:\(\?\w+\))?)(.*?)(\$?)$'), r"\1(\2)\3" )
+RE_GLOBALFLAGS = re.compile(r'((?:^|(?!<\\))\(\?[a-z]+\))')
RE_EXLINE_NO_BOUNDS = re.compile(r'^\{UNB\}')
RE_EXLINE_BOUND_BEG = re.compile(r'^\{\^LN-BEG\}')
@@ -110,6 +111,11 @@ class DateTemplate(object):
# because it may be very slow in negative case (by long log-lines not matching pattern)
regex = regex.strip()
+ # cut global flags like (?iu) from RE in order to pre-set it after processing:
+ gf = RE_GLOBALFLAGS.search(regex)
+ if gf:
+ regex = RE_GLOBALFLAGS.sub('', regex, count=1)
+ # check word boundaries needed:
boundBegin = wordBegin and not RE_NO_WRD_BOUND_BEG.search(regex)
boundEnd = wordEnd and not RE_NO_WRD_BOUND_END.search(regex)
# if no group add it now, should always have a group(1):
@@ -135,6 +141,8 @@ class DateTemplate(object):
self.flags |= DateTemplate.LINE_END
# remove possible special pattern "**" in front and end of regex:
regex = RE_DEL_WRD_BOUNDS[0].sub(RE_DEL_WRD_BOUNDS[1], regex)
+ if gf: # restore global flags:
+ regex = gf.group(1) + regex
self._regex = regex
logSys.log(4, ' constructed regex %s', regex)
self._cRegex = None