Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle script timeout as script preferences instead of server preference #509

Merged
merged 4 commits into from
Oct 21, 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: 0 additions & 1 deletion ospd_openvas/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"NVT_BIDS_POS",
"NVT_XREFS_POS",
"NVT_CATEGORY_POS",
"NVT_TIMEOUT_POS",
"NVT_FAMILY_POS",
"NVT_NAME_POS",
]
Expand Down
37 changes: 4 additions & 33 deletions ospd_openvas/nvticache.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,22 @@ def get_nvt_metadata(self, oid: str) -> Optional[Dict[str, str]]:
'bid',
'xref',
'category',
'timeout',
'family',
'name',
]

custom = dict()
custom['refs'] = dict()
custom['vt_params'] = dict()
for child, res in zip(subelem, resp):
if child not in ['cve', 'bid', 'xref', 'tag', 'timeout'] and res:
if child not in ['cve', 'bid', 'xref', 'tag'] and res:
custom[child] = res
elif child == 'tag':
custom.update(self._parse_metadata_tags(res, oid))
elif child in ['cve', 'bid', 'xref'] and res:
custom['refs'][child] = res.split(", ")
elif child == 'timeout':
if res is None:
continue
vt_params = {}
if int(res) > 0:
_param_id = '0'
vt_params[_param_id] = dict()
vt_params[_param_id]['id'] = _param_id
vt_params[_param_id]['type'] = 'entry'
vt_params[_param_id]['name'] = 'timeout'
vt_params[_param_id]['description'] = 'Script Timeout'
vt_params[_param_id]['default'] = res
custom['vt_params'] = vt_params
custom['vt_params'].update(self.get_nvt_params(oid))

custom['vt_params'] = dict()
custom['vt_params'].update(self.get_nvt_params(oid))

return custom

Expand Down Expand Up @@ -267,22 +254,6 @@ def get_nvt_prefs(self, oid: str) -> Optional[List[str]]:
key = f'oid:{oid}:prefs'
return OpenvasDB.get_list_item(self.ctx, key)

def get_nvt_timeout(self, oid: str) -> Optional[str]:
"""Get NVT timeout

Arguments:
ctx: Redis context to be used.
oid: OID of VT from which to get the script timeout.

Returns:
The timeout.
"""
return OpenvasDB.get_single_item(
self.ctx,
f"nvt:{oid}",
index=NVT_META_FIELDS.index("NVT_TIMEOUT_POS"),
)

def get_nvt_tags(self, oid: str) -> Optional[Dict[str, str]]:
"""Get Tags of the given OID.

Expand Down
9 changes: 3 additions & 6 deletions ospd_openvas/preferencehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,9 @@ def _process_vts(
if type_aux == 'checkbox':
vt_param_value = _from_bool_to_str(int(vt_param_value))

if vt_param_id == '0':
vts_params[f"timeout.{vtid}"] = str(vt_param_value)
else:
vts_params[
f"{vtid}:{vt_param_id}:{param_type}:{param_name}"
] = str(vt_param_value)
vts_params[
f'{vtid}:{vt_param_id}:{param_type}:{param_name}'
] = str(vt_param_value)

return vts_list, vts_params

Expand Down
15 changes: 5 additions & 10 deletions tests/test_nvti_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ def test_get_nvt_metadata(self, MockOpenvasDB):
'',
'URL:http://www.mantisbt.org/',
'3',
'10',
'Product detection',
'Mantis Detection',
]
Expand Down Expand Up @@ -204,7 +203,7 @@ def test_get_nvt_metadata(self, MockOpenvasDB):
'id': '0',
'type': 'entry',
'name': 'timeout',
'description': 'Script Timeout',
'description': 'Description',
'default': '10',
},
'1': {
Expand All @@ -217,7 +216,10 @@ def test_get_nvt_metadata(self, MockOpenvasDB):
},
}

prefs1 = ['1|||dns-fuzz.timelimit|||entry|||default']
prefs1 = [
'0|||timeout|||entry|||10',
'1|||dns-fuzz.timelimit|||entry|||default',
]

MockOpenvasDB.get_list_item.side_effect = [metadata, prefs1]
resp = self.nvti.get_nvt_metadata('1.2.3.4')
Expand Down Expand Up @@ -261,13 +263,6 @@ def test_get_nvt_prefs(self, MockOpenvasDB):

self.assertEqual(resp, prefs)

def test_get_nvt_timeout(self, MockOpenvasDB):
MockOpenvasDB.get_single_item.return_value = '300'

resp = self.nvti.get_nvt_timeout('1.2.3.4')

self.assertEqual(resp, '300')

def test_get_nvt_tags(self, MockOpenvasDB):
tag = (
'last_modification=1533906565'
Expand Down