diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index 0d7d8ba9..220753a7 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -282,7 +282,18 @@ def excepthook(exctype, value, traceback): "Unhandled exception in Fail2Ban:", exc_info=True) return sys.__excepthook__(exctype, value, traceback) -def splitwords(s): +RE_REM_COMMENTS = re.compile(r'(?m)(?:^|\s)[\#;].*') +def removeComments(s): + """Helper to remove comments: + # comment ... + ; comment ... + no comment # comment ... + no comment ; comment ... + """ + return RE_REM_COMMENTS.sub('', s) + +RE_SPLT_WORDS = re.compile(r'[\s,]+') +def splitwords(s, ignoreComments=False): """Helper to split words on any comma, space, or a new line Returns empty list if input is empty (or None) and filters @@ -290,7 +301,9 @@ def splitwords(s): """ if not s: return [] - return list(filter(bool, [v.strip() for v in re.split(r'[\s,]+', s)])) + if ignoreComments: + s = removeComments(s) + return list(filter(bool, [v.strip() for v in RE_SPLT_WORDS.split(s)])) def _merge_dicts(x, y): """Helper to merge dicts. diff --git a/fail2ban/server/ipdns.py b/fail2ban/server/ipdns.py index cff8d60d..665bddc6 100644 --- a/fail2ban/server/ipdns.py +++ b/fail2ban/server/ipdns.py @@ -778,7 +778,7 @@ class FileIPAddrSet(IPAddrSet): self._fileStats = stats with open(self.fileName, 'r') as f: ips = f.read() - ips = splitwords(ips) + ips = splitwords(ips, ignoreComments=True) self.set(ips) except Exception as e: # pragma: no cover if not noError: raise e diff --git a/fail2ban/tests/misctestcase.py b/fail2ban/tests/misctestcase.py index bfce434f..6de4ff09 100644 --- a/fail2ban/tests/misctestcase.py +++ b/fail2ban/tests/misctestcase.py @@ -34,7 +34,7 @@ from io import StringIO from .utils import LogCaptureTestCase, logSys as DefLogSys from ..helpers import formatExceptionInfo, mbasename, TraceBack, FormatterWithTraceBack, getLogger, \ - getVerbosityFormat, splitwords, uni_decode, uni_string + getVerbosityFormat, removeComments, splitwords, uni_decode, uni_string from ..server.mytime import MyTime @@ -68,6 +68,16 @@ class HelpersTest(unittest.TestCase): self.assertEqual(splitwords(' 1\n 2, 3'), ['1', '2', '3']) self.assertEqual(splitwords('\t1\t 2,\r\n 3\n'), ['1', '2', '3']); # other spaces + def testSplitNoComments(self): + s = ''' + # comment ... + ; comment ... + line1 A # comment ... + line2 B ; comment ... + ''' + self.assertEqual(splitwords(s, ignoreComments=True), ['line1', 'A', 'line2', 'B']) + self.assertEqual(splitwords(removeComments(s)), ['line1', 'A', 'line2', 'B']) + def _sh_call(cmd): import subprocess