Skip to content

Commit

Permalink
CommonServerPython: truncate return_error messages (#34823)
Browse files Browse the repository at this point in the history
* return_error_message

* pre commit

* CR

* RN

* CR

* ignore

* move constant

* pre commit

* add verbose

* revert to master
  • Loading branch information
rshunim authored Jun 18, 2024
1 parent 99f2a1f commit 9ed2458
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions Packs/Base/.pack-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ CPE
CPEs
debug-entry
clickable
truncate

[tests_require_network]
CommonServerPython
Expand Down
6 changes: 6 additions & 0 deletions Packs/Base/ReleaseNotes/1_34_13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Scripts

##### CommonServerPython

- Updated the return_error function to truncate long error messages.
7 changes: 6 additions & 1 deletion Packs/Base/Scripts/CommonServerPython/CommonServerPython.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __line__():
MASK = '<XX_REPLACED>'
SEND_PREFIX = "send: b'"
SAFE_SLEEP_START_TIME = datetime.now()
MAX_ERROR_MESSAGE_LENGTH = 50000


def register_module_line(module_name, start_end, line, wrapper=0):
Expand Down Expand Up @@ -7247,7 +7248,7 @@ def return_error(message, error='', outputs=None):
Returns error entry with given message and exits the script
:type message: ``str``
:param message: The message to return in the entry (required)
:param message: The message to return to the entry (required)
:type error: ``str`` or Exception
:param error: The raw error message to log (optional)
Expand Down Expand Up @@ -7286,6 +7287,10 @@ def return_error(message, error='', outputs=None):
if is_server_handled:
raise Exception(message)
else:
if len(message) > MAX_ERROR_MESSAGE_LENGTH:
half_length = MAX_ERROR_MESSAGE_LENGTH // 2
message = message[:half_length] + "...This error body was truncated..." + message[half_length * (-1):]

demisto.results({
'Type': entryTypes['error'],
'ContentsFormat': formats['text'],
Expand Down
42 changes: 42 additions & 0 deletions Packs/Base/Scripts/CommonServerPython/CommonServerPython_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,48 @@ def test_is_mac_address():
assert (is_mac_address(mac_address_true))


def test_return_error_truncated_message(mocker):
"""
Given
- invalid error message due to longer than max length (50,000)
When
- return_error function is called
Then
- Return a truncated message that contains clarification about the truncation
"""
from CommonServerPython import return_error, MAX_ERROR_MESSAGE_LENGTH
err_msg = "1" * (MAX_ERROR_MESSAGE_LENGTH + 1)
results = mocker.spy(demisto, 'results')
mocker.patch.object(sys, 'exit')
return_error(err_msg)
assert len(results.call_args[0][0]["Contents"]) == MAX_ERROR_MESSAGE_LENGTH + \
len("...This error body was truncated...")
assert "This error body was truncated" in results.call_args[0][0]["Contents"]


def test_return_error_valid_message(mocker):
"""
Given
- A valid error message
When
- return_error function is called
Then
- Ensure the same message is returned
- Ensure the error message does not contain clarification about a truncation
"""
from CommonServerPython import return_error, MAX_ERROR_MESSAGE_LENGTH
err_msg = "1" * int(MAX_ERROR_MESSAGE_LENGTH * 0.9)
results = mocker.spy(demisto, 'results')
mocker.patch.object(sys, 'exit')
return_error(err_msg)
assert len(results.call_args[0][0]["Contents"]) == len(err_msg)
assert "This error body was truncated" not in results.call_args[0][0]["Contents"]


def test_return_error_command(mocker):
from CommonServerPython import return_error
err_msg = "Testing unicode Ё"
Expand Down
2 changes: 1 addition & 1 deletion Packs/Base/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Base",
"description": "The base pack for Cortex XSOAR.",
"support": "xsoar",
"currentVersion": "1.34.12",
"currentVersion": "1.34.13",
"author": "Cortex XSOAR",
"serverMinVersion": "6.0.0",
"url": "https://www.paloaltonetworks.com/cortex",
Expand Down

0 comments on commit 9ed2458

Please sign in to comment.