Skip to content

Commit

Permalink
Fix DBotPredictURLPhishing failing on rasterize errors (demisto#34060)
Browse files Browse the repository at this point in the history
* init

* fixed

* RN

* RN

* added unit-tests; fixed CR changes

* use list comprehension
  • Loading branch information
jlevypaloalto authored Apr 18, 2024
1 parent cc34ef6 commit 6fbe217
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Packs/PhishingURL/ReleaseNotes/1_1_13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Scripts

##### DBotPredictURLPhishing

- Fixed an issue in which an error was raised when more than one URL failed to be rasterized.
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,14 @@ def extract_created_date(entry: dict):

def weed_rasterize_errors(urls: list[str], res_rasterize: list[dict]):
'''Remove the URLs that failed rasterization and return them.'''

error_idx = [i for (i, res) in enumerate(res_rasterize) if isinstance(res['Contents'], str)]
if len(urls) != len(res_rasterize):
demisto.debug(f'{res_rasterize=}')
raise DemistoException('Unexpected response from the "rasterize" command. '
'Please make sure the Rasterize pack version is above 2.0.7')
error_idx = [
i for (i, res) in enumerate(res_rasterize)
if isinstance(res['Contents'], str)
][::-1] # reverse the list as it will be used to remove elements.
if error_idx:
return_results(CommandResults(readable_output=tableToMarkdown(
'The following URLs failed rasterize and were skipped:',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from DBotPredictURLPhishing import *
import pytest
import DBotPredictURLPhishing
from pytest_mock import MockerFixture

DBotPredictURLPhishing.isCommandAvailable = lambda _: True
CORRECT_DOMAINS = ['google.com']
Expand Down Expand Up @@ -315,3 +316,37 @@ def test_extract_created_date_with_empty_entry():
"""
from DBotPredictURLPhishing import extract_created_date
assert not extract_created_date({"EntryContext": None, "Type": 1})


def test_weed_rasterize_errors(mocker: MockerFixture):
"""
Given: the results from calling rasterize include errors.
When: looking for rasterize error responses in the weed_rasterize_errors function.
Then: Make sure the correct errors are weeded out and returned to the user and the rest are used.
"""
return_results_mock = mocker.patch('DBotPredictURLPhishing.return_results')
urls = ['1', '2', '3']
res_rasterize = [
{'Contents': 'error 1'},
{'Contents': {'success': True}},
{'Contents': 'error 3'},
]

weed_rasterize_errors(urls, res_rasterize)

assert urls == ['2']
assert res_rasterize == [{'Contents': {'success': True}}]
assert 'error 1' in return_results_mock.call_args_list[0].args[0].readable_output
assert 'error 3' in return_results_mock.call_args_list[0].args[0].readable_output


def test_weed_rasterize_errors_bad_rasterize_response():
"""
Given: the results from calling rasterize are less than the amount of URLs given.
When: looking for rasterize error responses in the weed_rasterize_errors function.
Then: Make sure the correct error is raised.
"""
with pytest.raises(DemistoException, match=(
'Unexpected response from the "rasterize" command. Please make sure the Rasterize pack version is above 2.0.7')
):
weed_rasterize_errors(['1', '2'], [{}])
2 changes: 1 addition & 1 deletion Packs/PhishingURL/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Phishing URL",
"description": "Phishing URL is a project with the goal of detecting phishing URLs using machine learning",
"support": "xsoar",
"currentVersion": "1.1.12",
"currentVersion": "1.1.13",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit 6fbe217

Please sign in to comment.