Fix rule ordering and protocol 'any' validation#252
Merged
opoplawski merged 1 commit intopfsensible:masterfrom Mar 30, 2026
Merged
Fix rule ordering and protocol 'any' validation#252opoplawski merged 1 commit intopfsensible:masterfrom
opoplawski merged 1 commit intopfsensible:masterfrom
Conversation
1. Allow protocol 'any' with destination/source ports in validation. pfSense accepts this combination in the UI but the module rejected it. Added 'any' to the allowed protocol list in _params_to_obj(). 2. New pass/match rules without explicit 'after' or 'before' are now inserted before the first block/reject rule on the same interface. Previously they appended to the end, placing allow rules below deny rules and making them ineffective. Deny rules still append to the end. Existing rules are never moved or deleted. Includes unit tests for both fixes and a block rule in the LAN test fixture for ordering validation.
64a2861 to
f364d52
Compare
Contributor
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two issues with
pfsense_rule:Protocol validation rejects
anywith ports. Settingprotocol: anywithdestination_portfails with "you can't use ports on protocols other than tcp, udp or tcp/udp", even though pfSense itself accepts this combination in the UI (Protocol dropdown includes "Any" alongside TCP, UDP, etc.).New pass rules append below existing block rules. When creating a new pass rule without explicit
afterorbefore,_get_expected_rule_xml_index()returns_get_last_rule_xml_index() + 1, which appends the rule to the end of the interface's rule list. If a block/reject rule already exists, the new pass rule ends up below it and is never evaluated by pf.Fix
Protocol validation (
_params_to_obj, line 121): Added'any'to the allowed protocol list. Other non-port protocols (icmp, esp, gre, etc.) are still correctly rejected.Rule ordering (
_get_expected_rule_xml_index): When noafter/beforeis specified and the rule doesn't already exist, pass/match rules now check for the first block/reject rule on the same interface via a new_get_first_deny_rule_xml_index()helper. If one exists, the pass rule is inserted before it. If no block/reject rules exist, it appends to the end as before. Block/reject rules still append to the end. Existing rules are never moved or deleted.The
afterandbeforeparameters continue to work exactly as before for users who want explicit positioning.Changes
plugins/module_utils/rule.py— 20 insertions, 2 deletionschangelogs/fragments/— 2 new fragmentstests/unit/plugins/modules/fixtures/pfsense_rule_config.xml— added ablock_all_lanrule to the LAN interface for ordering teststests/unit/plugins/modules/test_pfsense_rule_create.py— 4 new tests, 1 updated error messageTests
All 678 existing unit tests pass. New tests:
test_rule_create_protocol_any_with_dst_port— protocolanywith port is acceptedtest_rule_create_protocol_icmp_with_dst_port— protocolicmpwith port is still rejectedtest_rule_create_pass_before_block— new pass rule is positioned before existing block ruletest_rule_create_block_appends_to_end— new block rule appends after existing block ruleTested against pfSense 2.7.2 with 239 rules across 20 VLAN interfaces.