Repository navigation
Compare IP fact rules with address facts by address instead of regex prefix - #3425
Open
bacescuandrei wants to merge 1 commit into
Open
bacescuandrei wants to merge 1 commit into
bacescuandrei wants to merge 1 commit into
Conversation
…refix RuleSet.is_fact_allowed() fell back to re.match() whenever the IP comparison did not match. An address rule is a valid regex that also matches longer addresses, so ALLOW 10.0.0.1 after DENY .* also allowed 10.0.0.10-19 and 10.0.0.100-199, and DENY 127.0.0.1 also denied 127.0.0.10. When the rule is an IPv4 address or network and the fact value is an IPv4 address, decide by the IP comparison only. Facts that are networks keep the regex fallback, so a DENY address rule still blocks a network fact that covers it. Regex rules, including .*, are unchanged.
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.
Description
The fieldmanual documents a rule's
matchas a regex.RuleSetalso has IP-specific handling:_is_ip_rule_matchcompares addresses and subnets, and its docstring says only string-equivalent addresses should match. However,is_fact_allowed()still evaluates the rule asre.match(rule.match, fact.value)whenever that IP comparison does not match. An IPv4 address is a regex prefix of longer addresses, so:DENY .*followed byALLOW 10.0.0.1also allows10.0.0.10–10.0.0.19and10.0.0.100–10.0.0.199. Rules bound the fact values the planner may use, so an allow-listed host widens the operation's scope.DENY 127.0.0.1also denies127.0.0.10.This change makes IP-shaped rules compare as addresses when the fact value is an IPv4 address. The rule can be an address or a network. In that case the IP comparison decides alone and the regex fallback is not used.
Behaviour change:
DENY 127.0.0.1no longer denies127.0.0.10, andALLOW 10.0.0.1no longer allows10.0.0.15.Unchanged:
DENY 10.0.0.1still denies10.0.0.1/32, andDENY 10.0.0.0still denies10.0.0.0/24..*, are unchanged.Users who relied on prefix matching with an IP-shaped pattern can keep that behaviour with an explicit regex, for example
10\.0\.0\.1\d*.Type of change
How Has This Been Tested?
Added four tests to
tests/utility/test_ip_rule.py:ALLOWof a single address afterDENY .*;On master, the first two fail and the other two pass; on this branch all pass.
I compared allow/deny decisions on master and on this branch for these cases:
/32and/24facts;DENY 0.0.0.0/1against0.0.0.0/16;DENY .*;ALLOW 10.0.0.0against10.0.0.0/24;DENY .*.Only the two address-against-longer-address rows change.
Full suite on Python 3.12 (
python -m pytest --asyncio-mode=auto tests): 565 passed, 1 xfailed. Master: 561 passed, 1 xfailed.flake8 7.0.0 with flake8-bugbear: clean on the changed files.
bandit -r app -ll --skip=B303: no issues.Checklist: