diff --git a/changelogs/fragments/221-ufw-base-rule-comment.yml b/changelogs/fragments/221-ufw-base-rule-comment.yml index 8b3afd7..4ebcf70 100644 --- a/changelogs/fragments/221-ufw-base-rule-comment.yml +++ b/changelogs/fragments/221-ufw-base-rule-comment.yml @@ -1,6 +1,6 @@ --- bugfixes: - >- - ufw - A single quote in the ``comment`` of a ``ufw_rules`` entry is now refused before anything changes. The - ufw module cannot pass it to ufw, so the play failed while applying the rules, after earlier rules of the list - had been changed. The comment of an entry with ``delete: true`` is not checked, as it is never sent. + ufw - A single quote in the ``comment`` of a ``ufw_rules`` entry is now refused before any rule is applied. + The ufw module cannot pass it to ufw, so the play failed while applying the rules, after earlier rules of the + list had been changed. The comment of an entry with ``delete: true`` is not checked, as it is never sent. diff --git a/roles/ufw/README.md b/roles/ufw/README.md index db07855..8c23f6f 100644 --- a/roles/ufw/README.md +++ b/roles/ufw/README.md @@ -157,7 +157,7 @@ Not covered: `ufw_mask_nftables: false` is never set, `ufw_before_rules` entries - Lock-out guard: `defaults.yml` sets the deny policy before `rules.yml` adds any rule. When `ufw_enabled`, `ufw_manage_rules` and `ufw_require_ssh_allow` are true and the incoming default is deny or reject, the play fails unless `ufw_rules` holds an allow or limit rule for `ufw_ssh_port` that is TCP (or has no `proto`) and has `direction: in` or no `direction`. `direction: incoming`, which the module accepts, is not counted, so write `in`. Any `from`, `to` or `interface` restriction still counts; the guard cannot know the control host's address. An entry with `delete: true` never counts. - Replacing a base rule: list the new rule and the old one with `delete: true` (a boolean). Deletes are applied after every other rule, whatever their place in the list, so the replacement exists before the old rule goes, which matters when narrowing the SSH rule. The comment of a delete entry is neither sent nor checked, so the rule matches whatever its comment, and a delete of a rule that is already gone changes nothing. An entry that deletes a rule the same list adds (same `rule`, `port`, `proto`, `from`, `to`, `interface` and `direction`, where no `direction` is `in` and no `from` or `to` is `any`) fails the play. A delete without an address removes the IPv4 and the IPv6 rule; a replacement with an IPv4 `from` allows IPv4 only, so check that the hosts you connect from are covered before narrowing SSH. `delete` is for `ufw_rules` only; `service_rules` removes its own stale rules. - `before.rules` is a template derived from UFW's own `before.rules` (GPL-3.0-only, see `NOTICE`) that fully replaces the stock file, including a loopback accept, conntrack (established accept and INVALID-state drop), ICMP and mDNS/UPnP entries. It is deployed whenever `ufw_enabled` is true, even with no before-rules. -- Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, and each rule needs `port`, `from`, `to` or `interface`. +- Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, each rule needs `port`, `from`, `to` or `interface`, and a `comment` must not contain a single quote. - UFW is only re-enabled when `ufw status verbose` shows it inactive or the logging line differs, because the module rewrites rules and re-applies sysctl on every call. - The `Reload UFW` handler runs `ufw reload`. - nftables: when `nftables.service` is enabled (a preset can do that), the `nftables` package's postinst restarts it on upgrade (`dh_installsystemd --no-enable --no-start --restart-after-upgrade` emits `deb-systemd-invoke try-restart`), and both the restart and the unit's `ExecStop=/usr/sbin/nft flush ruleset` delete every rule ufw (and Docker) loaded, since the stock `/etc/nftables.conf` begins with `flush ruleset`. The role masks the unit and does not stop it, so nothing is flushed during the run. systemd refuses every job but a stop on a masked unit, so the postinst's restart fails before anything runs and the postinst ignores the failure (`|| true`); at boot the unit is not started either. The mask also applies when the unit or the package is absent, so a later install cannot enable it; the package's postinst removes only a mask its own helper created. The role never unmasks: with `ufw_mask_nftables: false` or `ufw_enabled: false` an existing mask stays. `--check` reports the mask without applying it. When `/etc/systemd/system/nftables.service` is a unit file of its own rather than a link, `systemctl mask` refuses with "already exists" and the play fails; set `ufw_mask_nftables: false` on such a host. diff --git a/roles/ufw/molecule/default/verify.yml b/roles/ufw/molecule/default/verify.yml index 6a42a5c..b89a2cc 100644 --- a/roles/ufw/molecule/default/verify.yml +++ b/roles/ufw/molecule/default/verify.yml @@ -71,6 +71,7 @@ that: "'both added and deleted' in ansible_failed_result.msg | default('')" quiet: true + # ufw's parser refuses a quote in a comment; the check must stop the play before any rule is applied. - name: A single quote in a base rule comment block: - name: Include ufw diff --git a/roles/ufw/tasks/validate.yml b/roles/ufw/tasks/validate.yml index 693e1f5..5778617 100644 --- a/roles/ufw/tasks/validate.yml +++ b/roles/ufw/tasks/validate.yml @@ -16,8 +16,8 @@ - ufw_manage_rules - ufw_aggregated_rules | length > 0 -# The ufw module puts the comment in single quotes unescaped, so one inside it fails mid-loop. -# Delete entries are skipped: rules.yml never sends their comment. +# The module wraps the comment in unescaped single quotes, which ufw's parser refuses, so the rules +# loop would fail part-way. Delete entries are skipped: rules.yml never sends their comment. - name: Validate the ufw_rules comments ansible.builtin.assert: that: "\"'\" not in (ufw_rule.comment | default('', true) | string)"