aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_parse.py
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-08 13:34:43 +0200
committerDanilo M. <danix@danix.xyz>2026-09-08 13:34:43 +0200
commit96bd15309df4bb3a46051718ef30ae023f310b06 (patch)
tree33fa9b4d4dda6f2a5d8cd7edb681a7a902d97d1b /tests/test_parse.py
parentc734feaf769c936698a52f38a2099152a6098c43 (diff)
downloadabusectl-96bd15309df4bb3a46051718ef30ae023f310b06.tar.gz
abusectl-96bd15309df4bb3a46051718ef30ae023f310b06.zip
feat: walk the Received chain to the trust boundary
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KphFXTc2QajxXsHWyvGJ4R
Diffstat (limited to 'tests/test_parse.py')
-rw-r--r--tests/test_parse.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/test_parse.py b/tests/test_parse.py
new file mode 100644
index 0000000..92095fd
--- /dev/null
+++ b/tests/test_parse.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+"""Tests for the Received-chain trust boundary walk."""
+
+import pathlib
+import unittest
+
+from abusectl import parse
+
+FIXTURES = pathlib.Path(__file__).parent / "fixtures"
+
+
+def load(name: str) -> bytes:
+ return (FIXTURES / name).read_bytes()
+
+
+class TestReceivedChain(unittest.TestCase):
+ def test_hops_are_returned_outermost_first(self):
+ hops = parse.received_hops(load("simple.eml"))
+ self.assertEqual([h.ip for h in hops], ["192.0.2.11", "203.0.113.42"])
+
+ def test_the_first_untrusted_hop_is_the_sender(self):
+ ip = parse.sending_ip(load("simple.eml"), trusted=["192.0.2.0/24"])
+ self.assertEqual(ip, "203.0.113.42")
+
+ def test_a_forged_chain_stops_at_the_first_untrusted_hop(self):
+ # The attacker prepended two hops naming an innocent third party.
+ # Walking past the boundary would report 198.51.100.7, which is
+ # someone else's address in a header the attacker wrote.
+ ip = parse.sending_ip(load("forged-chain.eml"), trusted=["192.0.2.0/24"])
+ self.assertEqual(ip, "203.0.113.99")
+
+ def test_hops_below_the_boundary_are_still_recorded(self):
+ # Recorded, but as untrusted: they may be useful and must not be
+ # presented as fact.
+ hops = parse.received_hops(load("forged-chain.eml"))
+ self.assertEqual(
+ [h.ip for h in hops],
+ ["192.0.2.11", "203.0.113.99", "198.51.100.7", "198.51.100.8"],
+ )
+
+ def test_no_trusted_relays_is_an_error_not_a_guess(self):
+ # Guessing the outermost public IP is wrong in exactly the case that
+ # matters, and a confident wrong answer gets a third party reported.
+ with self.assertRaises(parse.NoTrustBoundary):
+ parse.sending_ip(load("simple.eml"), trusted=[])
+
+ def test_a_chain_entirely_inside_the_boundary_has_no_sender(self):
+ ip = parse.sending_ip(
+ load("simple.eml"), trusted=["192.0.2.0/24", "203.0.113.0/24"]
+ )
+ self.assertIsNone(ip)
+
+
+if __name__ == "__main__":
+ unittest.main()