Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Fix get_count_total(). Accept -1 value set by the server. #355

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix target_to_ipv4_short(). [#338](https://github.com/greenbone/ospd/pull/338)
- Fix malformed target. [#341](https://github.com/greenbone/ospd/pull/341)
- Initialize end_time with create_scan. [#354](https://github.com/greenbone/ospd/pull/354)
- Fix get_count_total(). Accept -1 value set by the server. [#355](https://github.com/greenbone/ospd/pull/355)

[20.8.2]: https://github.com/greenbone/ospd/compare/v20.8.1...ospd-20.08

Expand Down
15 changes: 13 additions & 2 deletions ospd/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,18 @@ def get_count_total(self, scan_id: str) -> int:
""" Get a scan's total host count. """

count_total = self.scans_table[scan_id]['count_total']
if not count_total:

# The value set by the server has priority over the value
# calculated from the original target list by ospd.
# As ospd is not intelligent enough to check the amount of valid
# hosts, check for duplicated or invalid hosts, consider a negative
# value set for the server, in case it detects an invalid target string
# or a different amount than the orignal amount in the target list.
if count_total == -1:
count_total = 0
# If the server does not set the total host count
# ospd set the amount of host from the original host list.
elif not count_total:
count_total = self.get_host_count(scan_id)
self.update_count_total(scan_id, count_total)

Expand Down Expand Up @@ -458,7 +469,7 @@ def calculate_target_progress(self, scan_id: str) -> int:
)
except ZeroDivisionError:
# Consider the case in which all hosts are dead or excluded
LOGGER.debug('%s: All hosts dead or excluded.')
LOGGER.debug('%s: All hosts dead or excluded.', scan_id)
t_prog = ScanProgress.FINISHED.value

return t_prog
Expand Down
26 changes: 26 additions & 0 deletions tests/test_scan_and_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,32 @@ def test_set_scan_total_hosts(self):
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 3)

def test_set_scan_total_hosts_invalid_target(self):

fs = FakeStream()
self.daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost1, localhost2, localhost3, localhost4</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
self.daemon.start_queued_scans()

response = fs.get_response()
scan_id = response.findtext('id')

count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 4)

# The total host is set by the server as -1, because invalid target
self.daemon.set_scan_total_hosts(scan_id, -1)
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 0)

def test_get_scan_progress_xml(self):

fs = FakeStream()
Expand Down