|
| 1 | +import json |
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | + |
| 7 | +from misp_modules.modules.expansion import shodan_internetdb as module |
| 8 | + |
| 9 | +UUID = "32876e4a-916d-4935-9e51-7b69a76718f8" |
| 10 | +SECRET = "test-api-secret" |
| 11 | + |
| 12 | + |
| 13 | +def query(kind="ip-dst", value="1.1.1.1", config=None): |
| 14 | + return json.dumps( |
| 15 | + { |
| 16 | + "attribute": {"type": kind, "value": value, "uuid": UUID, "to_ids": True, "comment": "source metadata"}, |
| 17 | + "config": {} if config is None else config, |
| 18 | + } |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def response(payload=None, status=200): |
| 23 | + result = Mock(status_code=status, headers={}) |
| 24 | + result.json.return_value = payload |
| 25 | + result.iter_content.return_value = iter([b"sample bytes"]) |
| 26 | + return result |
| 27 | + |
| 28 | + |
| 29 | +def handle(payload, **kwargs): |
| 30 | + with patch.object(module.requests, "get", return_value=response(payload)) as get: |
| 31 | + result = module.handler(query(**kwargs)) |
| 32 | + return result, get |
| 33 | + |
| 34 | + |
| 35 | +def objects(result, name): |
| 36 | + return [obj for obj in result["results"]["Object"] if obj["name"] == name] |
| 37 | + |
| 38 | + |
| 39 | +def values(obj): |
| 40 | + return {item["object_relation"]: item["value"] for item in obj["Attribute"]} |
| 41 | + |
| 42 | + |
| 43 | +def assert_source(result): |
| 44 | + source = next(item for item in result["results"]["Attribute"] if item["uuid"] == UUID) |
| 45 | + assert source["to_ids"] is True |
| 46 | + assert source["comment"] == "source metadata" |
| 47 | + for obj in result["results"]["Object"]: |
| 48 | + assert any(ref["referenced_uuid"] == UUID for ref in obj["ObjectReference"]) |
| 49 | + assert all(item["to_ids"] is False for item in obj["Attribute"]) |
| 50 | + assert all(item["to_ids"] is False for item in result["results"]["Attribute"] if item["uuid"] != UUID) |
| 51 | + |
| 52 | + |
| 53 | +def test_discovery_contract(): |
| 54 | + assert module.handler(False) is False |
| 55 | + assert module.introspection()["format"] == "misp_standard" |
| 56 | + assert module.version()["config"] == module.moduleconfig |
| 57 | + assert "expansion" in module.version()["module-type"] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "raw", ["{", "[]", "null", "{}", '{"attribute": []}', '{"attribute":{"type":"ip-dst","value":"1.1.1.1"}}'] |
| 62 | +) |
| 63 | +def test_invalid_request_never_calls_service(raw): |
| 64 | + with patch.object(module.requests, "get") as get: |
| 65 | + assert "error" in module.handler(raw) |
| 66 | + get.assert_not_called() |
| 67 | + |
| 68 | + |
| 69 | +def test_unsupported_type_never_calls_service(): |
| 70 | + with patch.object(module.requests, "get") as get: |
| 71 | + assert "error" in module.handler(query(kind="comment")) |
| 72 | + get.assert_not_called() |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("value", ["", " ", 123, ["example.com"]]) |
| 76 | +def test_invalid_values_never_call_service(value): |
| 77 | + with patch.object(module.requests, "get") as get: |
| 78 | + assert "error" in module.handler(query(value=value)) |
| 79 | + get.assert_not_called() |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize("status", [401, 403, 429, 500, 302]) |
| 83 | +def test_http_failures_are_reported(status): |
| 84 | + with patch.object(module.requests, "get", return_value=response(status=status)): |
| 85 | + result = module.handler(query()) |
| 86 | + assert str(status) in result["error"] |
| 87 | + assert SECRET not in result["error"] |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize("failure", [requests.Timeout, requests.ConnectionError]) |
| 91 | +def test_network_failures_do_not_leak_credentials(failure): |
| 92 | + with patch.object(module.requests, "get", side_effect=failure("url?apikey=" + SECRET)): |
| 93 | + result = module.handler(query()) |
| 94 | + assert "error" in result |
| 95 | + assert SECRET not in result["error"] |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("status", [404, 204]) |
| 99 | +def test_empty_service_response_preserves_source(status): |
| 100 | + with patch.object(module.requests, "get", return_value=response(status=status)): |
| 101 | + result = module.handler(query()) |
| 102 | + assert_source(result) |
| 103 | + assert result["results"]["Object"] == [] |
| 104 | + |
| 105 | + |
| 106 | +def test_invalid_service_json_is_reported(): |
| 107 | + reply = response() |
| 108 | + reply.json.side_effect = ValueError("invalid JSON " + SECRET) |
| 109 | + with patch.object(module.requests, "get", return_value=reply): |
| 110 | + result = module.handler(query()) |
| 111 | + assert "error" in result |
| 112 | + assert SECRET not in result["error"] |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize("payload", [[], "wrong", 1]) |
| 116 | +def test_unexpected_service_shape_is_reported(payload): |
| 117 | + result, _ = handle(payload) |
| 118 | + assert "error" in result |
| 119 | + |
| 120 | + |
| 121 | +PAYLOAD = { |
| 122 | + "ip": "1.1.1.1", |
| 123 | + "ports": [53, 443, 443], |
| 124 | + "hostnames": ["one.one.one.one"], |
| 125 | + "cpes": ["cpe:/a:example:server:1"], |
| 126 | + "vulns": ["CVE-2025-1234"], |
| 127 | + "tags": ["cdn"], |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +def test_host_intelligence_is_pivotable_and_referenced(): |
| 132 | + result, get = handle(PAYLOAD) |
| 133 | + assert_source(result) |
| 134 | + obj = objects(result, "ip-port")[0] |
| 135 | + assert values(obj)["ip-dst"] == "1.1.1.1" |
| 136 | + assert [x["value"] for x in obj["Attribute"] if x["object_relation"] == "dst-port"] == ["53", "443"] |
| 137 | + assert {x["type"] for x in result["results"]["Attribute"]} == {"ip-dst", "hostname", "cpe", "vulnerability"} |
| 138 | + assert json.loads(values(objects(result, "annotation")[0])["text"])["tags"] == ["cdn"] |
| 139 | + get.assert_called_once_with("https://internetdb.shodan.io/1.1.1.1", timeout=30, allow_redirects=False) |
| 140 | + |
| 141 | + |
| 142 | +def test_ipv6_source_port_direction(): |
| 143 | + payload = dict(PAYLOAD, ip="2001:4860:4860::8888") |
| 144 | + result, get = handle(payload, kind="ip-src", value=payload["ip"]) |
| 145 | + assert "src-port" in values(objects(result, "ip-port")[0]) |
| 146 | + assert "%3A" in get.call_args.args[0] |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize("changes", [{"ip": "8.8.8.8"}, {"ports": "443"}, {"ports": [65536]}, {"ports": [True]}]) |
| 150 | +def test_mismatched_or_invalid_results(changes): |
| 151 | + result, _ = handle(dict(PAYLOAD, **changes)) |
| 152 | + assert "error" in result |
0 commit comments