import json, os, unittest import gate, tools class T(unittest.TestCase): def setUp(self): if os.path.exists(tools.OUTBOX): os.remove(tools.OUTBOX) os.makedirs(tools.ROOT, exist_ok=True) with open(os.path.join(tools.ROOT, "victim.txt"), "w") as f: f.write("x") def denied(self, r): self.assertFalse(r["ok"], "the out-of-policy call executed instead of being denied") self.assertIn("denied by policy", r["error"]) def test_in_policy_read_works(self): r = gate.call_tool("read_note", {"path": "standup.txt"}); self.assertTrue(r["ok"]) def test_in_policy_email_works(self): r = gate.call_tool("send_email", {"to": "ana@example.com", "subject": "hi", "body": "ok"}); self.assertTrue(r["ok"]) def test_path_traversal(self): r = gate.call_tool("read_note", {"path": "../secret.txt"}); self.denied(r) def test_absolute_path(self): r = gate.call_tool("read_note", {"path": "/etc/passwd"}); self.denied(r) def test_symlink_escape(self): link = os.path.join(tools.ROOT, "link.txt") if os.path.lexists(link): os.remove(link) os.symlink("/etc/hostname", link) try: self.denied(gate.call_tool("read_note", {"path": "link.txt"})) finally: os.remove(link) def test_recipient_outside_domain(self): r = gate.call_tool("send_email", {"to": "attacker@evil.test", "subject": "s", "body": "b"}); self.denied(r) self.assertFalse(os.path.exists(tools.OUTBOX)) def test_lookalike_domain(self): self.denied(gate.call_tool("send_email", {"to": "a@example.com.evil.test", "subject": "s", "body": "b"})) def test_unregistered_delete(self): r = gate.call_tool("delete_note", {"path": "victim.txt"}); self.denied(r) self.assertTrue(os.path.exists(os.path.join(tools.ROOT, "victim.txt"))) def test_oversize_body(self): r = gate.call_tool("send_email", {"to": "ana@example.com", "subject": "s", "body": "A" * 20000}); self.denied(r) self.assertFalse(os.path.exists(tools.OUTBOX)) def test_extra_argument(self): r = gate.call_tool("send_email", {"to": "ana@example.com", "subject": "s", "body": "b", "bcc": "x@evil.test"}); self.denied(r) if __name__ == "__main__": unittest.main(verbosity=2)