Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug by convert the password to bytes #27283

Merged
merged 5 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_11_85.md
@@ -0,0 +1,7 @@

#### Scripts

##### UnzipFile

- Fixed an issue where run the script failed when used with `zipfile` tool.
israelpoli marked this conversation as resolved.
Show resolved Hide resolved
- Updated the Docker image to: *demisto/unzip:1.0.0.61858*.
2 changes: 2 additions & 0 deletions Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py
Expand Up @@ -87,7 +87,7 @@
excluded_files: the excludedfiles which are in dir_path
"""
# remembering which files and dirs we currently have so we add them later as newly extracted files.
file_path = file_info['path']

Check failure on line 90 in Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (PTH112)

Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py:90:52: PTH112 `os.path.isdir()` should be replaced by `Path.is_dir()`
file_name = file_info['name']
excluded_files = [f for f in os.listdir('.') if isfile(f)]
excluded_dirs = [d for d in os.listdir('.') if isdir(d)]
Expand All @@ -105,6 +105,8 @@
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 Expand Up @@ -226,7 +228,7 @@
})
else:
results = []
# extracted files can be in sub directories so we save the base names of

Check failure on line 231 in Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (PTH119)

Packs/CommonScripts/Scripts/UnzipFile/UnzipFile.py:231:33: PTH119 `os.path.basename()` should be replaced by `Path.name`
# the files and also the full path of the file
files_base_names = [os.path.basename(file_path) for file_path in filenames] # noqa[F812]
files_dic = {file_path: os.path.basename(file_path) for file_path in filenames}
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
12 changes: 8 additions & 4 deletions Packs/CommonScripts/Scripts/UnzipFile/UnzipFile_test.py
Expand Up @@ -46,21 +46,25 @@


data_test_unzip_with_password = [
('fix_unzip.png', 'demisto'),
('fix_unzip.png', 'demisto', '7z'),
('fix_unzip.png', 'demisto', 'zipfile')
israelpoli marked this conversation as resolved.
Show resolved Hide resolved
]


@pytest.mark.parametrize('file_name, password', data_test_unzip_with_password)
def test_unzip_with_password(file_name, password):
@pytest.mark.parametrize('file_name, password, zip_tool', data_test_unzip_with_password)
def test_unzip_with_password(file_name, password, zip_tool):
israelpoli marked this conversation as resolved.
Show resolved Hide resolved
"""
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
- ensure zip file content have be saved at _dir directory with the original filename
- ensure that the saved file has expected content
- the extraction worked as expected with the password converted from str to bytes
when the extraction tool is `zipfile`
israelpoli marked this conversation as resolved.
Show resolved Hide resolved
"""
# Given
# - valid zip file - no password required
Expand All @@ -76,7 +80,7 @@
_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 @@ -230,7 +234,7 @@
- arguments for the script
When
- running the script on a password locked file
Then

Check failure on line 237 in Packs/CommonScripts/Scripts/UnzipFile/UnzipFile_test.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (PT015)

Packs/CommonScripts/Scripts/UnzipFile/UnzipFile_test.py:237:13: PT015 Assertion always fails, replace with `pytest.fail()`
- ensure that only one of the arguments 'password' or 'nonsensitive_password' is given or if they are identical.
"""
with pytest.raises(ValueError) as e:
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