2 Commits
Author SHA1 Message Date
Defame1297 166e0eb3e0 docs(ufw): describe the base rule comment check where readers look
Build / build (pull_request) Successful in 33s
Lint / renovate-config (pull_request) Successful in 11s
Molecule / select (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 2m32s
Molecule / scenarios (pull_request) Successful in 13m34s
Molecule / Result (pull_request) Successful in 1s
Name the single-quote limit in the README's schema note, give the new
verify case a lead comment like its neighbours, and say why ufw refuses
the quote in the validate.yml comment. The fragment now says the check
runs before any rule is applied, since install and sysctl run earlier.

Refs: #221
Assisted-by: Claude Code
2026-12-06 21:53:58 +00:00
Defame1297 592b779ad8 fix(ufw): refuse a single quote in a base rule comment
The ufw module wraps the comment in single quotes without escaping, so a
quote in a ufw_rules comment failed the rules loop part-way, after earlier
rules had already changed. validate.yml now refuses it before any change.

Only the single quote is refused: backslash, non-ASCII and surrounding
whitespace work for base rules, which are never read back as tags.
Delete entries are skipped, as their comment is never sent.

Refs: #221
Assisted-by: Claude Code
2026-12-06 21:35:05 +00:00
5 changed files with 58 additions and 10 deletions

No files matched your search

@@ -0,0 +1,6 @@
---
bugfixes:
- >-
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.
+5 -5
View File
@@ -26,13 +26,13 @@ Main entry point:
| Variable | Default | Required | Description |
|---|---|---|---|
| `ufw_enabled` | `true` | no | Skip all UFW configuration when false (the package is still installed). |
| `ufw_manage_rules` | `true` | no | Skip the `ufw_rules` schema check, the lock-out guard and rule application when false. Before-rules validation and `before.rules`, defaults and enable still run (gated on `ufw_enabled`). |
| `ufw_manage_rules` | `true` | no | Skip the `ufw_rules` schema and comment checks, the lock-out guard and rule application when false. Before-rules validation and `before.rules`, defaults and enable still run (gated on `ufw_enabled`). |
| `ufw_defaults` | `incoming: deny`, `outgoing: allow`, `routed: deny` | no | Default policies. All three keys are read, so override all of them. |
| `ufw_logging` | `"on"` | no | UFW logging level. |
| `ufw_mask_nftables` | `true` | no | Mask `nftables.service` without stopping it, so a package upgrade cannot flush the rules ufw loaded (see Notes). Set false on a host that loads its own ruleset through that unit. Gated on `ufw_enabled`. |
| `ufw_require_ssh_allow` | `true` | no | Enable the SSH lock-out guard. |
| `ufw_ssh_port` | `ansible_port` or 22 | no | TCP port the guard looks for. |
| `ufw_rules` | `[]` | no | Base rule list: `rule` (allow, deny, reject, limit), `port`, `proto`, `from`, `to`, `interface`, `direction`, `comment`, `delete` (see Notes). |
| `ufw_rules` | `[]` | no | Base rule list: `rule` (allow, deny, reject, limit), `port`, `proto`, `from`, `to`, `interface`, `direction`, `comment`, `delete` (see Notes). A comment may not contain a single quote: the ufw module cannot pass one. |
| `ufw_before_rules` | `[]` | no | Entries added as ACCEPT lines to `before.rules`: `interface`, `proto`, `port`, `from`, `comment`. |
| `ufw_sysctl_overrides` | `[]` | no | `{name, value}` pairs written to `/etc/ufw/sysctl.conf`. |
@@ -141,7 +141,7 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_
- Prepare installs ufw and stores rules the role must handle: an untagged manual rule, a manual rule the base list deletes, and tagged rules of service owners that are stale, removed or orphaned, some of them looking like an SSH path (port 22 with a source, no port, a range, an application profile named like SSH).
- Converge applies the base `ufw_rules` (one entry with `delete: true`), then calls `service_rules` for several owners (purge on, purge off, `absent`), and `orphan_rules` with no list, an empty list, report only and purge.
- Verify first checks that each refusal stops the run with its message and changes nothing: default deny without an SSH rule (also when the SSH rule is only deleted, or added and deleted), a stray `*_ufw_rules` or `*_ufw_before_rules` variable, the invalid `service_rules` inputs (owner name, reserved `base`, a quote in a comment, a rule another owner holds, a rule that would retag the SSH rule, an unknown key, an interface without a direction, a purge with an empty list), and an invalid managed owner for `orphan_rules`.
- Verify first checks that each refusal stops the run with its message and changes nothing: default deny without an SSH rule (also when the SSH rule is only deleted, or added and deleted), a single quote in a base rule comment, a stray `*_ufw_rules` or `*_ufw_before_rules` variable, the invalid `service_rules` inputs (owner name, reserved `base`, a quote in a comment, a rule another owner holds, a rule that would retag the SSH rule, an unknown key, an interface without a direction, a purge with an empty list), and an invalid managed owner for `orphan_rules`.
- Verify then reads `ufw status verbose` and `ufw show added`: ufw is active with default deny incoming, the base rules are loaded, the deleted rule is gone, the manual and SSH rules are untouched, the service rules carry their owner tag, stale and orphan rules are deleted only where purging was on and only when they cannot carry SSH.
- Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules.
- Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change.
@@ -155,9 +155,9 @@ Not covered: `ufw_mask_nftables: false` is never set, `ufw_before_rules` entries
- Idempotent: yes.
- 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 not sent, 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.
- 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.
+6 -5
View File
@@ -22,7 +22,7 @@ argument_specs:
default: true
description:
- >-
When false the C(ufw_rules) schema check, the SSH lock-out guard and rule
When false the C(ufw_rules) schema and comment checks, the SSH lock-out guard and rule
application are skipped. Before-rules validation, C(before.rules), default policies and
enabling still run, gated on C(ufw_enabled).
ufw_defaults:
@@ -77,12 +77,13 @@ argument_specs:
- >-
Each item is a mapping with C(rule) (allow, deny, reject or limit) and at least one of C(port), C(from), C(to)
or C(interface). Optional keys are C(proto) (tcp or udp), C(direction), C(comment) and C(delete).
- A comment may not contain a single quote, because the ufw module cannot pass one.
- C(port) must be a positive integer value.
- >-
C(delete=true) (a boolean) removes the rule the other keys describe instead of adding it. Its comment is not
sent, so the rule matches whatever its comment. Deletes are applied after every other rule of the list, so a replacement exists before the old
rule goes; a rule that is both added and deleted in the list fails the play, and a delete entry never counts as
the SSH allow rule of the lock-out guard.
C(delete=true) (a boolean) removes the rule the other keys describe instead of adding it. Its comment is neither
sent nor checked, so the rule matches whatever its comment. Deletes are applied after every other rule of the
list, so a replacement exists before the old rule goes; a rule that is both added and deleted in the list fails
the play, and a delete entry never counts as the SSH allow rule of the lock-out guard.
ufw_before_rules:
type: raw
description:
+25
View File
@@ -7,6 +7,12 @@
- name: Refuse default-deny without an SSH allow rule
hosts: all
gather_facts: false
vars:
# A failed looped task reports "One or more items failed" in msg and the item
# messages in results; an unlooped one has the message in msg.
ufw_verify_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
- name: Run the role with only a non-SSH rule
block:
@@ -65,6 +71,25 @@
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
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
- {rule: allow, port: 8080, proto: tcp, comment: "it's"}
- name: Unreachable when base rule comments are checked
ansible.builtin.fail:
msg: a quote in a base rule comment was accepted
rescue:
- name: The comment check stopped the play
ansible.builtin.assert:
that: "'single quote' in ufw_verify_failure"
quiet: true
# Stray variable guard: variables named like <role>_ufw_rules must fail the play
# before anything changes, naming the variable. The vars sit on the include only, so they
# do not reach the plays below (the base ufw_rules and ufw_before_rules never match).
+16
View File
@@ -16,6 +16,22 @@
- ufw_manage_rules
- ufw_aggregated_rules | length > 0
# 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)"
fail_msg: >-
UFW rule comment contains a single quote, which the ufw module cannot pass to ufw: {{ ufw_rule }}
quiet: true
loop: "{{ ufw_aggregated_rules | default([]) }}"
loop_control:
loop_var: ufw_rule
when:
- ufw_manage_rules
- ufw_aggregated_rules | length > 0
- not ufw_rule.delete | default(false)
# A rule both added and deleted would be added, then deleted (deletes run last), and the
# lock-out guard below would still count the added one. Fields are normalised the way ufw
# stores them before comparing: no direction is in (incoming/outgoing are in/out), no from