Skip to content

Commit da0b7f0

Browse files
talzichcontent-botContent Bot
authored
Fix CS Falcon TPB and add them to nightly tests (demisto#42842)
* Add passing test to nightly * Possible fix to unzip file * Fix unzip to allow for spaces in entryID * Add retrieve file to nightly * Update conf.json * revert file exclusion changes * RN * Trigger AI Reviewer * Bump pack from version CommonScripts to 1.20.76. * Add UT * ruff * Remove danny fried from owner file --------- Co-authored-by: Content Bot <Content-Bot@users.noreply.github.com> Co-authored-by: Content Bot <bot@demisto.com>
1 parent 1a4403d commit da0b7f0

5 files changed

Lines changed: 53 additions & 13 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#### Scripts
3+
4+
##### UnzipFile
5+
6+
- Fixed an issue where UnzipFile would crash if input had spaces.

Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import shlex
32
import shutil
43
import sys
54
import zipfile as z
@@ -124,10 +123,10 @@ def extract_using_unrar(file_path, dir_path, password=None):
124123
:param password: password if the zip file is encrypted
125124
"""
126125
if password:
127-
cmd = f"unrar x -p{password} {file_path} {dir_path}"
126+
cmd = ["unrar", "x", f"-p{password}", file_path, dir_path]
128127
else:
129-
cmd = f"unrar x -p- {file_path} {dir_path}"
130-
process = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
128+
cmd = ["unrar", "x", "-p-", file_path, dir_path]
129+
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
131130
# process = Popen([cmd], shell=True, stdout=PIPE, stderr=PIPE)
132131
stdout, stderr = process.communicate()
133132
stdout = str(stdout)
@@ -141,13 +140,13 @@ def extract_using_unrar(file_path, dir_path, password=None):
141140

142141
def extract_using_tarfile(file_path: str, dir_path: str, file_name: str) -> str:
143142
if ".tar.gz" in file_name:
144-
cmd = f"tar -xzvf {file_path} -C {dir_path}"
143+
cmd = ["tar", "-xzvf", file_path, "-C", dir_path]
145144
elif file_name.endswith(".tar"):
146-
cmd = f"tar -xf {file_path} -C {dir_path}"
145+
cmd = ["tar", "-xf", file_path, "-C", dir_path]
147146
else:
148-
cmd = ""
147+
cmd = []
149148
demisto.debug(f"{file_name=} didn't match any condition. {cmd=}")
150-
process = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
149+
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
151150
stdout, stderr = process.communicate()
152151
stdout = str(stdout)
153152
if stderr:
@@ -161,8 +160,8 @@ def extract_using_7z(file_path, dir_path, password=None):
161160
:param dir_path: directory that the file will be extract to
162161
:param password: password if the zip file is encrypted
163162
"""
164-
cmd = f"7z x -p{password} -o{dir_path} {file_path}"
165-
process = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
163+
cmd = ["7z", "x", f"-p{password}", f"-o{dir_path}", file_path]
164+
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
166165
# process = Popen([cmd], shell=True, stdout=PIPE, stderr=PIPE)
167166
stdout, stderr = process.communicate()
168167
stdout = str(stdout)

Packs/CommonScripts/Scripts/UnzipFile/UnzipFile_test.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_archive_with_slash_in_path():
240240

241241
@pytest.fixture
242242
def mock_popen():
243-
with patch('UnzipFile.Popen') as mock:
243+
with patch("UnzipFile.Popen") as mock:
244244
yield mock
245245

246246

@@ -263,7 +263,7 @@ def test_extract_with_errors_in_stdout(mock_popen):
263263
mock_process = MagicMock()
264264
mock_process.communicate.return_value = (
265265
b"Hello_World_Errors.yml\nHello_World.yml", # stdout
266-
b"" # stderr (no error)
266+
b"", # stderr (no error)
267267
)
268268

269269
# Mock the Popen constructor to return our mock process
@@ -282,3 +282,35 @@ def test_extract_with_errors_in_stdout(mock_popen):
282282
assert "Hello_World.yml" in result
283283

284284

285+
def test_unzip_with_space_in_path(mocker):
286+
"""
287+
Given
288+
- A zip file path with spaces
289+
- empty folder _dir
290+
When
291+
- run extract on that zip file
292+
Then
293+
- ensure the command passed to Popen is a list and contains the path correctly (not split)
294+
"""
295+
import UnzipFile as unzip
296+
297+
# Given
298+
zip_path = "/path/to/directory with spaces/test.zip"
299+
zipped_file_object = {"name": "test.zip", "path": zip_path}
300+
_dir = "/tmp/extract_dir"
301+
302+
# Mock Popen
303+
mock_popen = mocker.patch.object(unzip, "Popen")
304+
mock_process = MagicMock()
305+
mock_process.communicate.return_value = (b"", b"")
306+
mock_popen.return_value = mock_process
307+
308+
# When
309+
extract(zipped_file_object, _dir, zip_tool="7z")
310+
311+
# Then
312+
args, _ = mock_popen.call_args
313+
cmd_list = args[0]
314+
315+
assert isinstance(cmd_list, list)
316+
assert zip_path in cmd_list

Packs/CommonScripts/pack_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Common Scripts",
33
"description": "Frequently used scripts pack.",
44
"support": "xsoar",
5-
"currentVersion": "1.20.75",
5+
"currentVersion": "1.20.76",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

Tests/conf.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6399,6 +6399,9 @@
63996399
"Whois A new layout implemented with python-whois service",
64006400
"Detonate URL - WildFire-v2 - Test",
64016401
"CrowdStrike Falcon Basic Test",
6402+
"Test Playbook - CrowdStrike Falcon - Get Endpoint Forensics Data",
6403+
"Test Playbook - CrowdStrike Falcon - Retrieve File",
6404+
"Test Playbook - CrowdStrike Falcon Malware - Verify Containment Actions",
64026405
"TestDemistoRestAPI",
64036406
"Test XDR Playbook execute script commands",
64046407
"Test XDR Playbook quarantine file command",

0 commit comments

Comments
 (0)