Skip to content

Commit

Permalink
Merge pull request #28 from Ostorlab/fix/whois_agent_crash
Browse files Browse the repository at this point in the history
Fix whois crash: 'str' object has no attribute extend
  • Loading branch information
najibraihan authored Feb 3, 2023
2 parents f5f9d87 + df441d3 commit 03f5b17
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
11 changes: 9 additions & 2 deletions agent/result_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,19 @@ def parse_results(results: whois.parser.WhoisCom) -> Iterator[Dict[str, Any]]:
scan_output_dict.get("expiration_date", [])
),
"name": name,
"emails": get_list_from_string(scan_output_dict.get("emails", "")),
"emails": get_list_from_string(
scan_output_dict.get("email", "")
if scan_output_dict.get("email", "") != ""
else scan_output_dict.get("emails", "")
),
"status": get_list_from_string(scan_output_dict.get("status", "")),
"name_servers": get_list_from_string(
scan_output_dict.get("name_servers", "")
),
"contact_name": contact_name,
# TODO(ticket: os-3017): change the proto of v3.asset.domain_name.whois to send contact names.
"contact_name": contact_name[0]
if isinstance(contact_name, list)
else contact_name,
"dnssec": get_list_from_string(scan_output_dict.get("dnssec", "")),
}
for field in OPTIONAL_FIELDS:
Expand Down
65 changes: 58 additions & 7 deletions tests/whois_domain_agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
"clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
],
"emails": ["abuse@godaddy.com"],
"email": ["abuse@godaddy.com"],
"dnssec": "unsigned",
"name": "REDACTED FOR PRIVACY",
"name": ["Catherine Shapiro", "Ivan SLY"],
"org": "Contact Privacy Inc. Customer 0139267634",
"address": "REDACTED FOR PRIVACY",
"city": "REDACTED FOR PRIVACY",
Expand All @@ -52,9 +52,9 @@
"clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
"clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
],
"emails": ["abuse@godaddy.com"],
"email": ["abuse@godaddy.com"],
"dnssec": "unsigned",
"name": "REDACTED FOR PRIVACY",
"name": ["Catherine Shapiro", "Ivan SLY"],
"org": "Contact Privacy Inc. Customer 0139267634",
"address": "REDACTED FOR PRIVACY",
"city": "REDACTED FOR PRIVACY",
Expand All @@ -80,9 +80,9 @@
"clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
"clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
],
"emails": ["compliance@tucows.com", "easydns@myprivacy.ca"],
"email": ["compliance@tucows.com", "easydns@myprivacy.ca"],
"dnssec": "unsigned",
"name": "REDACTED FOR PRIVACY",
"name": ["Catherine Shapiro", "Ivan SLY"],
"org": "Contact Privacy Inc. Customer 0139267634",
"address": "REDACTED FOR PRIVACY",
"city": "REDACTED FOR PRIVACY",
Expand All @@ -91,6 +91,25 @@
"country": "CA",
}

SCAN_OUTPUT_MULTIPLE_CONTACT_NAMES = {
"domain_name": "marksandspencer.at",
"registrar": "Key-Systems GmbH ( https://nic.at/registrar/404 )",
"name": ["Catherine Shapiro", "Ivan SLY"],
"org": ["Marks And Spencer P.l.c.", "IP TWINS S.A.S."],
"address": ["Waterside House", "35 North Wharf Road", "78 rue de Turbigo"],
"registrant_postal_code": ["W2 1NW", "75003"],
"city": ["London", "PARIS"],
"country": ["United Kingdom of Great Britain and Northern Ireland (the)", "France"],
"phone": ["+442087186494", "+33142789312"],
"fax": "+440207487267",
"updated_date": [
datetime.datetime(2021, 6, 23, 10, 10, 57),
datetime.datetime(2021, 6, 23, 10, 7, 2),
datetime.datetime(2023, 1, 4, 19, 30, 24),
],
"email": ["externaldnssupport@marks-and-spencer.com", "ivan.sly@iptwins.com"],
}


def testAgentWhois_whenDomainNameAsset_emitsMessages(
scan_message: message.Message,
Expand All @@ -115,6 +134,38 @@ def testAgentWhois_whenDomainNameAsset_emitsMessages(
assert agent_mock[0].data["emails"] == ["abuse@godaddy.com"]


def testAgentWhois_whenMultipleContactNames_emitsMessages(
scan_message: message.Message,
test_agent: whois_domain_agent.AgentWhoisDomain,
agent_persist_mock: Any,
mocker: plugin.MockerFixture,
agent_mock: List[message.Message],
) -> None:
"""Tests running the agent and emitting vulnerabilities."""
del agent_persist_mock

mock_whois = mocker.patch(
"whois.whois", return_value=SCAN_OUTPUT_MULTIPLE_CONTACT_NAMES
)
test_agent.start()
test_agent.process(scan_message)
mock_whois.assert_called_once()

assert len(agent_mock) > 0
assert agent_mock[0].selector == "v3.asset.domain_name.whois"
assert agent_mock[0].data["name"] == "marksandspencer.at"
assert agent_mock[0].data["updated_date"] == [
"2021-06-23T10:10:57",
"2021-06-23T10:07:02",
"2023-01-04T19:30:24",
]
assert agent_mock[0].data["emails"] == [
"externaldnssupport@marks-and-spencer.com",
"ivan.sly@iptwins.com",
]
assert agent_mock[0].data["contact_name"] == "Catherine Shapiro"


def testAgentWhois_whenDomainNameInputIsEmpty_NotEmitsMessages(
scan_message_not_valid: message.Message,
test_agent: whois_domain_agent.AgentWhoisDomain,
Expand All @@ -132,7 +183,7 @@ def testAgentWhois_whenDomainNameInputIsEmpty_NotEmitsMessages(
assert len(agent_mock) == 0


def testAgentWhois_whenDomainNameResultIsEmpty_NotEmitsMessages(
def testAgentWhois_whenDomainNameIsEmpty_notEmitsMessages(
scan_message: message.Message,
test_agent: whois_domain_agent.AgentWhoisDomain,
agent_persist_mock: Any,
Expand Down

0 comments on commit 03f5b17

Please sign in to comment.