Skip to content

Commit 6fbe217

Browse files
Fix DBotPredictURLPhishing failing on rasterize errors (demisto#34060)
* init * fixed * RN * RN * added unit-tests; fixed CR changes * use list comprehension
1 parent cc34ef6 commit 6fbe217

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Scripts
3+
4+
##### DBotPredictURLPhishing
5+
6+
- Fixed an issue in which an error was raised when more than one URL failed to be rasterized.

Packs/PhishingURL/Scripts/DBotPredictURLPhishing/DBotPredictURLPhishing.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,14 @@ def extract_created_date(entry: dict):
460460

461461
def weed_rasterize_errors(urls: list[str], res_rasterize: list[dict]):
462462
'''Remove the URLs that failed rasterization and return them.'''
463-
464-
error_idx = [i for (i, res) in enumerate(res_rasterize) if isinstance(res['Contents'], str)]
463+
if len(urls) != len(res_rasterize):
464+
demisto.debug(f'{res_rasterize=}')
465+
raise DemistoException('Unexpected response from the "rasterize" command. '
466+
'Please make sure the Rasterize pack version is above 2.0.7')
467+
error_idx = [
468+
i for (i, res) in enumerate(res_rasterize)
469+
if isinstance(res['Contents'], str)
470+
][::-1] # reverse the list as it will be used to remove elements.
465471
if error_idx:
466472
return_results(CommandResults(readable_output=tableToMarkdown(
467473
'The following URLs failed rasterize and were skipped:',

Packs/PhishingURL/Scripts/DBotPredictURLPhishing/dbotpredicturlphishing_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from DBotPredictURLPhishing import *
22
import pytest
33
import DBotPredictURLPhishing
4+
from pytest_mock import MockerFixture
45

56
DBotPredictURLPhishing.isCommandAvailable = lambda _: True
67
CORRECT_DOMAINS = ['google.com']
@@ -315,3 +316,37 @@ def test_extract_created_date_with_empty_entry():
315316
"""
316317
from DBotPredictURLPhishing import extract_created_date
317318
assert not extract_created_date({"EntryContext": None, "Type": 1})
319+
320+
321+
def test_weed_rasterize_errors(mocker: MockerFixture):
322+
"""
323+
Given: the results from calling rasterize include errors.
324+
When: looking for rasterize error responses in the weed_rasterize_errors function.
325+
Then: Make sure the correct errors are weeded out and returned to the user and the rest are used.
326+
"""
327+
return_results_mock = mocker.patch('DBotPredictURLPhishing.return_results')
328+
urls = ['1', '2', '3']
329+
res_rasterize = [
330+
{'Contents': 'error 1'},
331+
{'Contents': {'success': True}},
332+
{'Contents': 'error 3'},
333+
]
334+
335+
weed_rasterize_errors(urls, res_rasterize)
336+
337+
assert urls == ['2']
338+
assert res_rasterize == [{'Contents': {'success': True}}]
339+
assert 'error 1' in return_results_mock.call_args_list[0].args[0].readable_output
340+
assert 'error 3' in return_results_mock.call_args_list[0].args[0].readable_output
341+
342+
343+
def test_weed_rasterize_errors_bad_rasterize_response():
344+
"""
345+
Given: the results from calling rasterize are less than the amount of URLs given.
346+
When: looking for rasterize error responses in the weed_rasterize_errors function.
347+
Then: Make sure the correct error is raised.
348+
"""
349+
with pytest.raises(DemistoException, match=(
350+
'Unexpected response from the "rasterize" command. Please make sure the Rasterize pack version is above 2.0.7')
351+
):
352+
weed_rasterize_errors(['1', '2'], [{}])

Packs/PhishingURL/pack_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Phishing URL",
33
"description": "Phishing URL is a project with the goal of detecting phishing URLs using machine learning",
44
"support": "xsoar",
5-
"currentVersion": "1.1.12",
5+
"currentVersion": "1.1.13",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

0 commit comments

Comments
 (0)