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

[BUG] archive.extracted and archive.unzip with password on Windows causes Python Exception Error #61422

Closed
3 of 6 tasks
digitalsleuth opened this issue Jan 5, 2022 · 4 comments · Fixed by #62774
Closed
3 of 6 tasks
Assignees
Labels
Bug broken, incorrect, or confusing behavior Confirmed Salt engineer has confirmed bug/feature - often including a MCVE Execution-Module Sulfur v3006.0 release code name and version

Comments

@digitalsleuth
Copy link

digitalsleuth commented Jan 5, 2022

Description
Creating a state to extract a zip file with a password on it causes the following error:
Exception encountered unpacking zipfile: pwd: expected bytes, got str

Setup
(Please provide relevant configs and/or SLS files (be sure to remove sensitive info. There is no general set-up of Salt.)
Sample SLS:

{% set version = '1.23.55' %}
nirsoft-extract:
  archive.extracted:
    - name: C:\\standalone\\nirsoft\\
    - source: C:\\salt\\tempdownload\\nirsoft_package_enc_{{ version }}.zip
    - enforce_toplevel: false
    - password: nirsoft9876$

nirsoft-env-vars:
  win_path.exists:
    - name: 'C:\standalone\nirsoft\'

Password has been entered with single and double-quotes with the same issue.

Command line sample:

salt-call --local archive.unzip nirsoft_package_enc_1.23.55.zip 'C:\Users\forensics\Downloads\nirsoft' password="nirsoft9876$"
Error running 'archive.unzip': Exception encountered unpacking zipfile: pwd: expected bytes, got str
  • on-prem machine
  • VM (Virtualbox, KVM, etc. please specify) - Windows 10 VMware VM, Up-to-date, version 2004 (19041.388) 8GB RAM, 2CPU, 60GB HDD.
  • VM running on a cloud service, please be explicit and add details
  • container (Kubernetes, Docker, containerd, etc. please specify)
  • or a combination, please be explicit
  • jails if it is FreeBSD

Ubuntu 18.04 and 20.04 docker

Steps to Reproduce the behavior
Use either the above state or the salt command above to reproduce. Debug logs here:

[DEBUG   ] LazyLoaded archive.unzip
[DEBUG   ] LazyLoaded direct_call.execute
Traceback (most recent call last):
  File "C:\Program Files\Salt Project\Salt\bin\Lib\site-packages\salt-3004-py3.8.egg\salt\modules\archive.py", line 1076, in unzip
    zfile.extract(target, dest, password)
  File "C:\Program Files\Salt Project\Salt\bin\lib\zipfile.py", line 1630, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Program Files\Salt Project\Salt\bin\lib\zipfile.py", line 1700, in _extract_member
    with self.open(member, pwd=pwd) as source, \
  File "C:\Program Files\Salt Project\Salt\bin\lib\zipfile.py", line 1497, in open
    raise TypeError("pwd: expected bytes, got %s" % type(pwd).__name__)
TypeError: pwd: expected bytes, got str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\cli\caller.py", line 218, in call
    ret["return"] = self.minion.executors[fname](
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 149, in __call__
    return self.loader.run(run_func, *args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 1201, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 1216, in _run_as
    return _func_or_method(*args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\Lib\site-packages\salt-3004-py3.8.egg\salt\executors\direct_call.py", line 10, in execute
    return func(*args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 149, in __call__
    return self.loader.run(run_func, *args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 1201, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\lib\site-packages\salt-3004-py3.8.egg\salt\loader\lazy.py", line 1216, in _run_as
    return _func_or_method(*args, **kwargs)
  File "C:\Program Files\Salt Project\Salt\bin\Lib\site-packages\salt-3004-py3.8.egg\salt\modules\archive.py", line 1098, in unzip
    raise CommandExecutionError(
salt.exceptions.CommandExecutionError: Exception encountered unpacking zipfile: pwd: expected bytes, got str
Error running 'archive.unzip': Exception encountered unpacking zipfile: pwd: expected bytes, got str

Expected behavior
Expected the file to be unzipped in the desired location and the password used to complete this task.

Screenshots
N/A

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
          Salt: 3004

Dependency Versions:
          cffi: 1.14.6
      cherrypy: 18.6.1
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: 4.0.7
     gitpython: 3.1.18
        Jinja2: 2.10.1
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: 1.1.4
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.20
      pycrypto: Not Installed
  pycryptodome: 3.10.1
        pygit2: Not Installed
        Python: 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)]
  python-gnupg: 0.4.7
        PyYAML: 5.4.1
         PyZMQ: 19.0.0
         smmap: 4.0.0
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.2

System Versions:
          dist:
        locale: cp1252
       machine: AMD64
       release: 10
        system: Windows
       version: 10 10.0.19041 SP0 Multiprocessor Free

Additional context
POTENTIAL RESOLUTION:
By changing line 1076 in salt/modules/archive.py from:
zfile.extract(target, dest, password)
to
zfile.extract(target, dest, password.encode())
Resolves the issue for me.

@digitalsleuth digitalsleuth added Bug broken, incorrect, or confusing behavior needs-triage labels Jan 5, 2022
@welcome
Copy link

welcome bot commented Jan 5, 2022

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at [email protected]. We’re glad you’ve joined our community and look forward to doing awesome things with you!

@digitalsleuth
Copy link
Author

Just checking to see if there's any progress on this?

@digitalsleuth
Copy link
Author

@twangboy Just checking to see what the status is for this?

@twangboy twangboy added Execution-Module Confirmed Salt engineer has confirmed bug/feature - often including a MCVE and removed needs-triage labels Sep 28, 2022
@twangboy twangboy added this to the Sulphur v3006.0 milestone Sep 28, 2022
@twangboy twangboy added the Sulfur v3006.0 release code name and version label Sep 28, 2022
@twangboy
Copy link
Contributor

Yep, this looks like an issue. The zipfile.extract function is expecting a binary value for the password. I submitted a PR with the fix, but I still need to figure out how to write tests for it.

@OrangeDog OrangeDog mentioned this issue Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior Confirmed Salt engineer has confirmed bug/feature - often including a MCVE Execution-Module Sulfur v3006.0 release code name and version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants