Skip to content

Commit

Permalink
avoids overwrite of known/option with unmodified (not available) va…
Browse files Browse the repository at this point in the history
…lue of `option` from .local config file,

so it wouldn't cause self-recursion if `option` already has a reference to `known/option` (from some include) in .conf file;
closes gh-2751
  • Loading branch information
sebres committed Aug 26, 2020
1 parent 5a2cc4e commit e569281
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions fail2ban/client/configparserinc.py
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
11 changes: 11 additions & 0 deletions fail2ban/tests/clientreadertestcase.py
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
@@ -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
@@ -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 e569281

Please sign in to comment.