Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion firru/cli/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]:
Expand Down
4 changes: 3 additions & 1 deletion firru/cli/mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_diff_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion tests/test_zero_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down