Skip to content

Commit

Permalink
Merge branch '0.10' into 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
sebres committed Aug 26, 2020
2 parents e9071b6 + e569281 commit b2036c1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
7 changes: 4 additions & 3 deletions fail2ban/client/configparserinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import sys
from ..helpers import getLogger

if sys.version_info >= (3,2):
if sys.version_info >= (3,): # pragma: 2.x no cover

# SafeConfigParser deprecated from Python 3.2 (renamed to ConfigParser)
from configparser import ConfigParser as SafeConfigParser, BasicInterpolation, \
Expand Down Expand Up @@ -61,7 +61,7 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
return super(BasicInterpolationWithName, self)._interpolate_some(
parser, option, accum, rest, section, map, *args, **kwargs)

else: # pragma: no cover
else: # pragma: 3.x no cover
from ConfigParser import SafeConfigParser, \
InterpolationMissingOptionError, NoOptionError, NoSectionError

Expand Down Expand Up @@ -372,7 +372,8 @@ def read(self, filenames, get_includes=True):
s2 = alls.get(n)
if isinstance(s2, dict):
# save previous known values, for possible using in local interpolations later:
self.merge_section('KNOWN/'+n, s2, '')
self.merge_section('KNOWN/'+n,
dict(filter(lambda i: i[0] in s, s2.iteritems())), '')
# merge section
s2.update(s)
else:
Expand Down
14 changes: 8 additions & 6 deletions fail2ban/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ def splitWithOptions(option):
# tags (<tag>) in tagged options.
#

# max tag replacement count:
MAX_TAG_REPLACE_COUNT = 10
# max tag replacement count (considering tag X in tag Y repeat):
MAX_TAG_REPLACE_COUNT = 25

# compiled RE for tag name (replacement name)
TAG_CRE = re.compile(r'<([^ <>]+)>')
Expand Down Expand Up @@ -433,6 +433,7 @@ def substituteRecursiveTags(inptags, conditional='',
done = set()
noRecRepl = hasattr(tags, "getRawItem")
# repeat substitution while embedded-recursive (repFlag is True)
repCounts = {}
while True:
repFlag = False
# substitute each value:
Expand All @@ -444,7 +445,7 @@ def substituteRecursiveTags(inptags, conditional='',
value = orgval = uni_string(tags[tag])
# search and replace all tags within value, that can be interpolated using other tags:
m = tre_search(value)
refCounts = {}
rplc = repCounts.get(tag, {})
#logSys.log(5, 'TAG: %s, value: %s' % (tag, value))
while m:
# found replacement tag:
Expand All @@ -454,13 +455,13 @@ def substituteRecursiveTags(inptags, conditional='',
m = tre_search(value, m.end())
continue
#logSys.log(5, 'found: %s' % rtag)
if rtag == tag or refCounts.get(rtag, 1) > MAX_TAG_REPLACE_COUNT:
if rtag == tag or rplc.get(rtag, 1) > MAX_TAG_REPLACE_COUNT:
# recursive definitions are bad
#logSys.log(5, 'recursion fail tag: %s value: %s' % (tag, value) )
raise ValueError(
"properties contain self referencing definitions "
"and cannot be resolved, fail tag: %s, found: %s in %s, value: %s" %
(tag, rtag, refCounts, value))
(tag, rtag, rplc, value))
repl = None
if conditional:
repl = tags.get(rtag + '?' + conditional)
Expand All @@ -480,14 +481,15 @@ def substituteRecursiveTags(inptags, conditional='',
value = value.replace('<%s>' % rtag, repl)
#logSys.log(5, 'value now: %s' % value)
# increment reference count:
refCounts[rtag] = refCounts.get(rtag, 0) + 1
rplc[rtag] = rplc.get(rtag, 0) + 1
# the next match for replace:
m = tre_search(value, m.start())
#logSys.log(5, 'TAG: %s, newvalue: %s' % (tag, value))
# was substituted?
if orgval != value:
# check still contains any tag - should be repeated (possible embedded-recursive substitution):
if tre_search(value):
repCounts[tag] = rplc
repFlag = True
# copy return tags dict to prevent modifying of inptags:
if id(tags) == id(inptags):
Expand Down
2 changes: 1 addition & 1 deletion fail2ban/tests/actiontestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def testReplaceTagSelfRecursion(self):
delattr(self.__action, 'ac')
# produce self-referencing query except:
self.assertRaisesRegexp(ValueError, r"possible self referencing definitions in query",
lambda: self.__action.replaceTag("<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x<x>>>>>>>>>>>>>>>>>>>>>",
lambda: self.__action.replaceTag("<x"*30+">"*30,
self.__action._properties, conditional="family=inet6")
)

Expand Down
11 changes: 11 additions & 0 deletions fail2ban/tests/clientreadertestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ def testFilterReaderSubstitionDefault(self):
c = filterReader.convert()
self.assertSortedEqual(c, output)

def testFilterReaderSubstKnown(self):
# testcase02.conf + testcase02.local, test covering that known/option is not overridden
# with unmodified (not available) value of option from .local config file, so wouldn't
# cause self-recursion if option already has a reference to known/option in .conf file.
filterReader = FilterReader('testcase02', "jailname", {},
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
filterReader.read()
filterReader.getOptions(None)
opts = filterReader.getCombined()
self.assertTrue('sshd' in opts['failregex'])

def testFilterReaderSubstitionSet(self):
output = [['set', 'jailname', 'addfailregex', 'to=sour@example.com fromip=<IP>']]
filterReader = FilterReader('substition', "jailname", {'honeypot': 'sour@example.com'},
Expand Down
12 changes: 12 additions & 0 deletions fail2ban/tests/files/filter.d/testcase02.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = testcase-common.conf

[Definition]

_daemon = sshd
__prefix_line = %(known/__prefix_line)s(?:\w{14,20}: )?

failregex = %(__prefix_line)s test
4 changes: 4 additions & 0 deletions fail2ban/tests/files/filter.d/testcase02.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Definition]

# no options here, coverage for testFilterReaderSubstKnown:
# avoid to overwrite known/option with unmodified (not available) value of option from .local config file

0 comments on commit b2036c1

Please sign in to comment.