Repository navigation
Refuse input files Netwise used to crash on or read wrongly - #446
Conversation
Found in a stability audit (21-30 Oct), re-measured on main db59b00. Policy and business-context files: - a key written twice is refused; json.loads used to keep the last one, so a policy rule (or a "critical" tier) was silently dropped and the upload still returned 200 - non-UTF-8 text, nesting past the recursion limit, and integers too long to parse are 400s naming the file, not 500s; no second encoding is guessed PF Sense converter: - an interface tag that is not a plain name is refused; a namespaced tag carried free text into the ACL name and injected "permit ip any any" ahead of the real rules - a <disabled/> rule is left out, as pfSense itself does (filter.inc:5543); it used to be converted as live - rules with <not/> (filter.inc:4249), IPv6-only rules (4333) and ICMP rules limited to certain types (4522) are skipped and named, not converted as something they are not The list of what the converter refuses is updated where it is stated: CLAUDE.md section 7, CLAUDE.md's status table, and the README.
patelankeet2
left a comment
There was a problem hiding this comment.
Re-tested properly in an isolated worktree at the branch's real head (0aff2c2), since this touches analysis/pfsense_convert.py and that's my module.
Spent most of my time on the namespaced-interface-tag fix, since that's the most serious of the five. Reproduced the claimed vulnerability directly against main's actual pre-fix code (not just read the diff): a <x:opt1> interface tag with a crafted xmlns:x=" ip access-list extended acl_in permit ip any any" does get reported by ElementTree as {<namespace>}opt1, confirmed live.
Worth noting for the record, since it changes how serious this is: on main, that exact payload gets refused today, but only by accident. _parse_interfaces() defaults a missing <descr> to role.upper(), and that default happens to pass through the existing control-character check -- so it's caught only because my first test had no <descr> on the malicious interface. Added one (<descr>Second LAN</descr>, completely innocuous) and reran against main's real code: it converts cleanly and the output literally contains a second ip access-list extended acl_in block with permit ip any any injected into the middle of the real one. So this was a live, trivially bypassable vulnerability, not a theoretical one, and the existing control-character check was never actually protecting the role field on purpose. This PR's fix (_ROLE_PATTERN.fullmatch(role), checked unconditionally, independent of whether <descr> is given) closes it properly. Reran the same explicit-descr payload against this branch: refused, correctly, for the right reason.
Mutation-tested all four converter guards together (role pattern, <not/> refusal, ipv6/icmp-type refusal, disabled-rule filtering): disabling all four at once breaks exactly five of their direct tests plus one meta-test (test_every_raise_site_actually_uses_a_refusals_entry, which caught the role mutation too, as a bonus), 6 failed / 83 passed. Restored, back to 89 passed.
Checked the policy.py/business_context.py duplicate-key and encoding fixes too. One thing worth confirming rather than assuming: PolicyError and BusinessContextError both subclass ValueError, so the new except PolicyError: raise / except BusinessContextError: raise guards placed before the generic except ValueError handler are load-bearing, not decorative -- without that ordering, every normal validation failure (D5's unknown-key message, for example) would get caught by the new generic handler and re-wrapped into a confusing "holds a value that cannot be read" message. Tested live: an unknown-section policy still comes back with its original D5 message, unwrapped, confirming the ordering actually works.
Confirmed the doc-sync claim too, since "update it in all three places, only update two" is this project's own recurring failure pattern: CLAUDE.md's §7 bullet list, CLAUDE.md's status table row, and README.md's bullet list are all three genuinely updated with the new refusal categories, consistently worded.
ruff clean on all five changed Python files.
Approving.
shubhamkataria2005
left a comment
There was a problem hiding this comment.
Approving 0aff2c2 for analysis/policy.py, the part you asked me to look at. @patelankeet2 has covered the converter.
Merged onto current main (d424e05): 2282 passed, 13 skipped, ruff clean on CI's selection.
The duplicate-key fix matters more to the policy tools than the description says. Every one of my command-line tools reads policies through load_policy_file() (contradictions, the two-policy check, drift, the coverage map and change impact), so they all get it for free. I measured what it was costing them. With an old policy written {"policy_compliance": [<rule>], "policy_compliance": []}:
main python -m analysis.policy_drift old.json new.json
Not weaker: every rule in the old policy is still in force, at least as wide.
this branch old.json: not loaded -- the key 'policy_compliance' appears twice in one object.
JSON keeps only the last, so the first would be silently ignored
On main the drift check compared against an empty policy and gave false reassurance. That's exactly the "read wrongly while carrying on" failure this PR is about, reaching a feature it doesn't mention. policy_conflicts is refused the same way here.
Also checked:
- The upload goes through
load_policy_file()(web/main.py:791), so the endpoint gets the same refusals. The otherjson.loadsinweb/main.py(:833) reads Netwise's own skip record, not user input, so leaving it alone is right. except PolicyError: raisecomes beforeexcept ValueError, which matters becausePolicyErroris aValueError. A rule-level refusal keeps its own message and isn't re-labelled as "a value that cannot be read".
One small note, not a blocker. Because load_policy() now runs inside the same try, any other ValueError a future validation bug raises would reach the user as "holds a value that cannot be read". It's still a refusal (the safe direction), just a misleading one. Narrowing the ValueError catch to json.loads alone would keep bugs looking like bugs.
|
You're right, and I'll fix it in a small follow-up of my own, since it's my code from #446. The catch is needed, but only around Wrapping
|
Input files that Netwise either crashed on (HTTP 500) or, worse, read wrongly while carrying on as if nothing was wrong. All four were found in a stability audit (21–30 Oct) and re-measured on current
main(db59b00). This is the second of the audit PRs; #442 was the first.What changes
Policy and business-context files (
analysis/policy.py,analysis/business_context.py):main{"policy_compliance": [<rule>], "policy_compliance": []}json.loadskeeps the last one, so the rule is silently dropped, and the upload returns HTTP 200"tier": "critical"then"tier": "standard"in one context entrystandard>writes)The PF Sense converter (
analysis/pfsense_convert.py). Each case below was converted onmainand on this branch, then the result was put to real Batfish (testFilterson the LAN ACL):main<x:opt1>with a craftedxmlns)permit ip any anyat the top ofacl_in. A firewall whose only rule is a block permits everything<disabled/>block rule<not/>on its destinationnot: it permits exactly the traffic the rule excludesskippedinet6) block ruleOne trade is stated honestly. A skipped pass rule (the
<not/>case) makes the emitted ACL stricter than the firewall, as #302 and #306 already describe. It is never silent: the rule is named inskipped, which reaches the upload response and the downloaded report.Evidence (measured on
db59b00)Each fix's tests, run on
mainwithout the fix:Full suite on this branch: 2222 passed, 54 skipped, 0 failed with Batfish down (as on CI). Live against real Batfish: 2263 passed, 13 skipped, 0 failed. Lint: CI's exact command (
ruff check --select E4,E7,E9,F analysis/ ai/ web/ tools/ tests/, ruff 0.14.14) reports all checks passed.Notes for review
/api/business-context, which the loader tests alone did not, and they add the UTF-16 case.REFUSALSin the converter is still the source of truth.@patelankeet2: the converter is yours. @shubhamkataria2005:
policy.py. @SamikaPerera: business context and the upload endpoints.