fix(fail2ban): refuse a scalar or non-string fail2ban_ignoreip
argument_specs accepts a plain string for a list option and leaves it a string, and accepts a list holding a number or null. The pattern check then iterated a string per character and rendered "1 . 2 . 3 . 4", or died with a TypeError on null. Check the type first (assert stops at the first false item). Also refuse ";" and "%" in an entry: Fail2Ban's config parser treats ";" after whitespace as an inline comment, which drops the rest of the ignoreip line without an error, and "%" as interpolation syntax, which fails the whole config after jail.local is written. Add Molecule cases for a scalar string and a semicolon entry. Document what review found: service_jail does not read the variable, the [DEFAULT] value replaces an ignoreip set in jail.conf or jail.d/*.conf, a failed hostname lookup leaves the entry ineffective with a log warning (addresses and networks are safer), and --tags fail2ban_configure skips the validation. Assisted-by: Claude Code
This commit is contained in:
1 parent
57b15df1ac
commit
2b994363d7
5 files changed
+73
-10
No files matched your search
@@ -12,11 +12,19 @@
|
||||
fail_msg: "Invalid Fail2Ban jail: {{ item }}"
|
||||
loop: "{{ fail2ban_jails }}"
|
||||
|
||||
# Entries are joined with spaces into one ignoreip line, so an entry holding whitespace would silently become two. The
|
||||
# pattern ends in \Z, not $: Python's $ also matches before a trailing newline. The type of the list is argument_specs' job.
|
||||
# argument_specs does not reject a scalar string (it accepts "1.2.3.4" and leaves it a string, which the filters below would
|
||||
# then iterate per character), a mapping, or a list holding a number or null, so the type is checked here first. assert stops at
|
||||
# the first false item, so the later ones never see a value they cannot handle.
|
||||
# Entries are joined with spaces into one ignoreip line, so an entry holding whitespace would silently become two. Fail2Ban's
|
||||
# config parser treats ";" after whitespace as an inline comment (the rest of the line is dropped without an error, and
|
||||
# `fail2ban-client -t` cannot see it) and "%" as interpolation syntax (the whole config then fails to load, after jail.local is
|
||||
# written), so neither is allowed. Addresses, networks and hostnames contain none of these. The pattern ends in \Z, not $:
|
||||
# Python's $ also matches before a trailing newline.
|
||||
- name: Validate the Fail2Ban ignoreip entries
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- fail2ban_ignoreip | select('match', '^\S+\Z') | list | length == fail2ban_ignoreip | length
|
||||
fail_msg: "fail2ban_ignoreip entries must be non-empty and hold no whitespace: {{ fail2ban_ignoreip }}"
|
||||
- fail2ban_ignoreip is sequence and fail2ban_ignoreip is not string and fail2ban_ignoreip is not mapping
|
||||
- fail2ban_ignoreip | select('string') | list | length == fail2ban_ignoreip | length
|
||||
- fail2ban_ignoreip | select('match', '^[^\s;%]+\Z') | list | length == fail2ban_ignoreip | length
|
||||
fail_msg: "fail2ban_ignoreip must be a list of non-empty strings without whitespace, semicolons or percent signs: {{ fail2ban_ignoreip }}"
|
||||
quiet: true
|
||||
Reference in new issue
Block a user