|
| 1 | +import time |
| 2 | +import requests |
| 3 | +from .server.check_events_from_mock import ( |
| 4 | + fetch_events_from_mock, |
| 5 | + filter_on_event_type, |
| 6 | + validate_started_event, |
| 7 | + validate_heartbeat, |
| 8 | +) |
| 9 | + |
| 10 | +base_url_fw = "http://localhost:8114" |
| 11 | +base_url_nofw = "http://localhost:8115" |
| 12 | + |
| 13 | + |
| 14 | +def test_firewall_started_okay(): |
| 15 | + events = fetch_events_from_mock("http://localhost:5000") |
| 16 | + started_events = filter_on_event_type(events, "started") |
| 17 | + assert len(started_events) == 1 |
| 18 | + validate_started_event(started_events[0], None) |
| 19 | + |
| 20 | + |
| 21 | +def test_homepage_with_firewall(): |
| 22 | + res = requests.get(f"{base_url_fw}/") |
| 23 | + assert res.status_code == 200 |
| 24 | + |
| 25 | + |
| 26 | +def test_homepage_without_firewall(): |
| 27 | + res = requests.get(f"{base_url_nofw}/") |
| 28 | + assert res.status_code == 200 |
| 29 | + |
| 30 | + |
| 31 | +def test_sync_route_with_firewall(): |
| 32 | + res = requests.get(f"{base_url_fw}/sync_route") |
| 33 | + assert res.status_code == 200 |
| 34 | + |
| 35 | + |
| 36 | +def test_sync_route_without_firewall(): |
| 37 | + res = requests.get(f"{base_url_nofw}/sync_route") |
| 38 | + assert res.status_code == 200 |
| 39 | + |
| 40 | + |
| 41 | +def test_shell_injection_with_firewall(): |
| 42 | + res = requests.get(f"{base_url_fw}/shell/ls;echo test") |
| 43 | + assert res.status_code == 500 |
| 44 | + |
| 45 | + time.sleep(5) |
| 46 | + events = fetch_events_from_mock("http://localhost:5000") |
| 47 | + attacks = filter_on_event_type(events, "detected_attack") |
| 48 | + |
| 49 | + assert len(attacks) == 1 |
| 50 | + assert attacks[0]["attack"]["blocked"] == True |
| 51 | + assert attacks[0]["attack"]["kind"] == "shell_injection" |
| 52 | + assert attacks[0]["attack"]["operation"] == "subprocess.Popen" |
| 53 | + assert attacks[0]["attack"]["source"] == "routeParams" |
| 54 | + |
| 55 | + |
| 56 | +def test_shell_injection_without_firewall(): |
| 57 | + res = requests.get(f"{base_url_nofw}/shell/ls") |
| 58 | + assert res.status_code == 200 |
| 59 | + |
| 60 | + |
| 61 | +def test_initial_heartbeat(): |
| 62 | + time.sleep(55) |
| 63 | + events = fetch_events_from_mock("http://localhost:5000") |
| 64 | + heartbeat_events = filter_on_event_type(events, "heartbeat") |
| 65 | + assert len(heartbeat_events) == 1 |
| 66 | + routes = heartbeat_events[0]["routes"] |
| 67 | + route_paths = sorted([r["path"] for r in routes]) |
| 68 | + assert "/" in route_paths |
| 69 | + assert "/sync_route" in route_paths |
0 commit comments