Skip to content

Commit

Permalink
fix bug by convert the password to bytes (#27283)
Browse files Browse the repository at this point in the history
* fix bug by convert the password to bytes

* commit

* update RN and Docker

* comment corrections

* commit
  • Loading branch information
israelpoli authored and ostolero committed Jun 14, 2023
1 parent efcf177 commit b9dd936
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
7 changes: 7 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_11_85.md
@@ -0,0 +1,7 @@

#### Scripts

##### UnzipFile

- Fixed an issue where the script failed running on password-protected files, when using the `zipfile` tool.
- Updated the Docker image to: *demisto/unzip:1.0.0.61858*.
7 changes: 3 additions & 4 deletions Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py
Expand Up @@ -28,10 +28,7 @@ def get_zip_path(args):
fn = demisto.get(entry, 'File')

# We check the python version to prevent encoding issues. Effects Demisto 4.5+
if sys.version_info > (3, 0):
is_text = type(fn) is str
else:
is_text = type(fn) in [unicode, str] # pylint: disable=E0602
is_text = type(fn) is str # pylint: disable=E0602

is_correct_file = args.get('fileName', '').lower() == fn.lower()
is_zip = fn.lower().endswith('.zip')
Expand Down Expand Up @@ -105,6 +102,8 @@ def extract(file_info, dir_path, zip_tool='7z', password=None):
if zip_tool == '7z':
stdout = extract_using_7z(file_path, dir_path, password=password)
elif zip_tool == 'zipfile':
if password:
password = bytes(password, 'utf-8')
extract_using_zipfile(file_path, dir_path, password=password)
else:
return_error(f'There is no zipTool named: {zip_tool}')
Expand Down
2 changes: 1 addition & 1 deletion Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.yml
Expand Up @@ -60,7 +60,7 @@ tags:
- file
timeout: '0'
type: python
dockerimage: demisto/unzip:1.0.0.44723
dockerimage: demisto/unzip:1.0.0.61858
runonce: false
tests:
- ZipFile-Test
Expand Down
66 changes: 29 additions & 37 deletions Packs/CommonScripts/Scripts/UnzipFile/UnzipFile_test.py
@@ -1,7 +1,6 @@
from tempfile import mkdtemp
from UnzipFile import *
import os
import sys
import pytest

data_test_unzip_no_password = ['testZip.yml', 'ScanSummary.txt', 'item.png']
Expand Down Expand Up @@ -45,17 +44,13 @@ def test_unzip_no_password(file_name):
assert expected_data == actual_file_data, 'failed extracting ' + zipped_file_path


data_test_unzip_with_password = [
('fix_unzip.png', 'demisto'),
]


@pytest.mark.parametrize('file_name, password', data_test_unzip_with_password)
def test_unzip_with_password(file_name, password):
@pytest.mark.parametrize('zip_tool', ('7z', 'zipfile'))
def test_unzip_with_password(zip_tool: str):
"""
Given
- valid zip file - with password required
- empty folder _dir
- the tool to extract files
When
- run extract on that zip file and export the internal files to _dir
Then
Expand All @@ -64,6 +59,8 @@ def test_unzip_with_password(file_name, password):
"""
# Given
# - valid zip file - no password required
file_name = 'fix_unzip.png'
password = 'demisto'
main_dir = '/'.join(__file__.split('/')[0:-1])
expected_file_unzipped = os.path.join(main_dir + '/data_test', file_name)
zipped_file_path = expected_file_unzipped + '.zip'
Expand All @@ -76,7 +73,7 @@ def test_unzip_with_password(file_name, password):
_dir = mkdtemp()
# When
# - run extract on that zip file and export the internal files to _dir
extract(zipped_file_object, _dir, password=password)
extract(zipped_file_object, _dir, password=password, zip_tool=zip_tool)
# Then
# - ensure zip file content have been saved at _dir directory with the original filename
with open(_dir + '/' + file_name, 'rb') as f:
Expand Down Expand Up @@ -139,34 +136,29 @@ def test_unrar_no_password():
- ensure rar file content has been saved at _dir directory with the original filename
- ensure that the saved file has expected content
"""
if sys.version_info > (3, 0):
# Given
# - valid rar file - no password required
file_name = 'Untitled_document.pdf'
main_dir = '/'.join(__file__.split('/')[0:-1])
expected_file_unzipped = os.path.join(main_dir + '/data_test', file_name)
zipped_file_path = expected_file_unzipped + '.rar'
# Creation of file object
zipped_file_object = {
'name': 'Untitled_document.pdf.rar',
'path': zipped_file_path
}
# - empty folder _di
_dir = mkdtemp()
# When
# - run extract on that zip file and export the internal files to _dir
extract(zipped_file_object, _dir)
# Then
# - ensure rar file content have been saved at _dir directory with the original filename
with open(_dir + '/' + file_name, 'rb') as f:
actual_file_data = f.read()
with open(expected_file_unzipped, 'rb') as f:
expected_data = f.read()
shutil.rmtree(_dir)
# - ensure that the saved file has expected content data
assert expected_data == actual_file_data, 'failed extracting ' + zipped_file_path
else:
assert len("This doesn't work on the old docker image") > 1
file_name = 'Untitled_document.pdf'
main_dir = '/'.join(__file__.split('/')[0:-1])
expected_file_unzipped = os.path.join(main_dir + '/data_test', file_name)
zipped_file_path = expected_file_unzipped + '.rar'
# Creation of file object
zipped_file_object = {
'name': 'Untitled_document.pdf.rar',
'path': zipped_file_path
}
# - empty folder _di
_dir = mkdtemp()
# When
# - run extract on that zip file and export the internal files to _dir
extract(zipped_file_object, _dir)
# Then
# - ensure rar file content have been saved at _dir directory with the original filename
with open(_dir + '/' + file_name, 'rb') as f:
actual_file_data = f.read()
with open(expected_file_unzipped, 'rb') as f:
expected_data = f.read()
shutil.rmtree(_dir)
# - ensure that the saved file has expected content data
assert expected_data == actual_file_data, 'failed extracting ' + zipped_file_path


def test_extract_tarfile():
Expand Down
2 changes: 1 addition & 1 deletion Packs/CommonScripts/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Common Scripts",
"description": "Frequently used scripts pack.",
"support": "xsoar",
"currentVersion": "1.11.84",
"currentVersion": "1.11.85",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit b9dd936

Please sign in to comment.