Skip to content

Commit

Permalink
CLI: Ask password with getpass standard library (#79)
Browse files Browse the repository at this point in the history
* CLI: Ask password with getpass standard library

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Feb 22, 2020
1 parent 0f9d599 commit 3255ad8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Removed
Security
--------

* CLI: Use 'getpass' standard library to input password.(#59)


`v0.6b4`_
=========

Expand Down
13 changes: 11 additions & 2 deletions py7zr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import argparse
import getpass
import os
import sys
from lzma import CHECK_CRC64, CHECK_SHA256, is_check_supported
Expand Down Expand Up @@ -50,7 +51,8 @@ def _create_parser(self):
extract_parser.set_defaults(func=self.run_extract)
extract_parser.add_argument("arcfile", help="7z archive file")
extract_parser.add_argument("odir", nargs="?", help="output directory")
extract_parser.add_argument("--password", help="Extraction with password")
extract_parser.add_argument("-P", "--password", action="store_true",
help="Password protected archive(you will be asked a password).")
create_parser = subparsers.add_parser('c')
create_parser.set_defaults(func=self.run_create)
create_parser.add_argument("arcfile", help="7z archive file")
Expand Down Expand Up @@ -192,7 +194,14 @@ def run_extract(self, args: argparse.Namespace) -> int:
if not py7zr.is_7zfile(target):
print('not a 7z file')
return(1)
password = args.password
if not args.password:
password = None # type: Optional[str]
else:
try:
password = getpass.getpass()
except getpass.GetPassWarning:
sys.stderr.write('Warning: your password may be shown.\n')
return(1)
a = py7zr.SevenZipFile(target, 'r', password=password)
if args.odir:
a.extractall(path=args.odir)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getpass
import lzma
import os
import re
Expand Down Expand Up @@ -286,10 +287,16 @@ def test_cli_extract(tmp_path):


@pytest.mark.cli
def test_cli_encrypted_extract(tmp_path):
def test_cli_encrypted_extract(monkeypatch, tmp_path):

def _getpasswd():
return 'secret'

monkeypatch.setattr(getpass, "getpass", _getpasswd)

arcfile = os.path.join(testdata_path, "encrypted_1.7z")
cli = py7zr.cli.Cli()
cli.run(["x", "--password", "secret", arcfile, str(tmp_path.resolve())])
cli.run(["x", "--password", arcfile, str(tmp_path.resolve())])
expected = [{'filename': 'test1.txt', 'mode': 33188,
'digest': '0f16b2f4c3a74b9257cd6229c0b7b91855b3260327ef0a42ecf59c44d065c5b2'},
{'filename': 'test/test2.txt', 'mode': 33188,
Expand Down

0 comments on commit 3255ad8

Please sign in to comment.