From 6895b1de95f6df25f8225de355207a0dcee337f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Pietik=C3=A4inen?= Date: Fri, 20 Nov 2026 18:04:00 +0200 Subject: [PATCH] diff -o sh: a widening is a comment, so `| sh` never widens Without --flows, a widened change was emitted as a runnable line with only a trailing `# widened` tag. `grep narrowed | sh` skipped it, but `| sh` or `firru mass` on the whole script applied it: on the LSPR24 example, dc1's r1 to r4 script opened AD Web Services to the DMZ's green team, 552 flows on d2. Every other line that must not run by accident (a change the traffic contradicts, a terminal swap, a scored removal) was already a comment; a widening now is too, tagged `# widened REVIEW`. The block-early pipeline needs no second grep for it, and firru mass's docstring drops the example that worked around it. Co-Authored-By: Claude Opus 5.5 --- firru/cli/edit.py | 9 ++++++++- firru/cli/mass.py | 4 +++- tests/test_diff_evidence.py | 8 ++++++++ tests/test_zero_evidence.py | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/firru/cli/edit.py b/firru/cli/edit.py index 933a34b..ae12d5f 100644 --- a/firru/cli/edit.py +++ b/firru/cli/edit.py @@ -789,7 +789,9 @@ def _emit_sh(old_path, new_path, defs_dir, result, direction=None, swaps=None, """Emit runnable firru commands that turn OLD into NEW, one per change, each naming its term so `| grep SERVICE | sh` applies a subset. Each line carries its class as a trailing comment, so `| grep narrowed | sh` takes - every safe tightening and nothing else. + every safe tightening and nothing else. A line that must not run by + accident (a widening, a change the traffic contradicts, a terminal swap) + is itself a comment, so `| sh` on the whole output never widens. A restricted diff emits restricted commands: the scope that made the finding has to travel with the command that acts on it, or running the @@ -838,6 +840,11 @@ def _emit_sh(old_path, new_path, defs_dir, result, direction=None, swaps=None, f"observed ({rec['keep_hits']} flows), left out never " f"answered: {' '.join(rec['left_out'])}") print(f"# {line} # widened: would admit {_fmt_silent(silent)}") + elif cls == "widened": + # It grants peers OLD did not. Review decides that, never a + # grep, so it is a comment like every other line that must + # not run by accident: `| sh` on the whole output is safe. + print(f"# {line} # widened REVIEW{_ev_tag(evidence, term)}") else: print(f"{line} # {cls}{_ev_tag(evidence, term)}") for term, _ in result["cosmetic"]: diff --git a/firru/cli/mass.py b/firru/cli/mass.py index 0b99698..f8c7f4a 100644 --- a/firru/cli/mass.py +++ b/firru/cli/mass.py @@ -10,7 +10,9 @@ firru diff pol.yml tightened.yml --flows w.jsonl -o sh | grep observed | firru mass firru mass out/edits/*.sh - grep -v '# widened' out/edits/host.sh | firru mass + +A widening, a change the traffic contradicts and a terminal swap are emitted +as comments, so a whole script runs only what is safe to take unreviewed. The input format is exactly the emitted script: `#` comment lines and blank lines are skipped, a trailing ` # tag` on a command is stripped, and a leading diff --git a/tests/test_diff_evidence.py b/tests/test_diff_evidence.py index a1b6d82..377ba0b 100644 --- a/tests/test_diff_evidence.py +++ b/tests/test_diff_evidence.py @@ -359,3 +359,11 @@ def test_cut_peers_are_named_by_the_inventory(tmp_path): "192.0.2.7": 3}), "silent": Counter()}} _name_peers(ev, str(d)) assert ev[1]["cut"] == Counter({"GT1": 7, "10.9.2.2 (NET_MGMT)": 1, "192.0.2.7": 3}) + + +def test_a_widening_without_evidence_is_a_comment(tmp_path, capsys): + """`| sh` on a whole script must never widen: a widening is for review.""" + out = _run(tmp_path, [_t("allow-tcp-SSH", "SSH", src="NET_MGMT")], + [_t("allow-tcp-SSH", "SSH", src="[NET_MGMT, NET_APP]")], None, capsys=capsys) + (line,) = [l for l in out.splitlines() if "set-source" in l] + assert line.startswith("# firru set-source ") and line.endswith("# widened REVIEW") diff --git a/tests/test_zero_evidence.py b/tests/test_zero_evidence.py index 613cd45..e88cf66 100644 --- a/tests/test_zero_evidence.py +++ b/tests/test_zero_evidence.py @@ -94,7 +94,7 @@ def test_review_grep_skips_it_block_early_takes_it(self, tmp_path, capsys): line = [l for l in out.splitlines() if "ISAKMP" in l][0] # review: grep 'observed (' → no match assert "observed (" not in line - # block-early: grep -v '^#' | grep -v '# widened' → runnable + # block-early: grep -v '^#' → runnable assert not line.startswith("#") and "# widened" not in line def test_a_scored_term_is_emitted_commented(self, tmp_path, capsys):