Skip to content

Commit

Permalink
Support .zip as a supported file extension, raise unsupported file fo…
Browse files Browse the repository at this point in the history
…r regular zip files
  • Loading branch information
clach04 committed Nov 17, 2024
1 parent e692ecc commit 5e01bc6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 6 additions & 1 deletion puren_tonbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ class PurePyZipAES(ZipEncryptedFileBase):

description = 'AES-256 ZIP AE-1 DEFLATED (regular compression)'
extensions = ZipEncryptedFileBase.extensions + [
# TODO '.zip', see ZipAES() implementation
#'.zip', # any Zip file - NOTE if included, some tools may then attempt to treat regular zip files as potentially readable encrypted files and then fail; puren_tonbo.PurenTonboException: "There is no item named 'encrypted.md' in the archive"
]

Expand Down Expand Up @@ -702,7 +703,7 @@ class ZipAES(ZipEncryptedFileBase):
description = 'AES-256 ZIP AE-1 DEFLATED (regular compression), and read-only ZipCrypto'
extensions = ZipEncryptedFileBase.extensions + [
'.old.zip', # Zip file with old old ZipCrypto - reading/decrypting (writing not supported/implemented)
#'.zip', # any Zip file - NOTE if included, some tools may then attempt to treat regular zip files as potentially readable encrypted files and then fail; puren_tonbo.PurenTonboException: "There is no item named 'encrypted.md' in the archive"
'.zip', # any Zip file - NOTE if included, some tools may then attempt to treat regular zip files as potentially readable encrypted files and then fail; puren_tonbo.PurenTonboException: "There is no item named 'encrypted.md' in the archive"
]

def read_from(self, file_object):
Expand All @@ -714,6 +715,10 @@ def read_from(self, file_object):
except RuntimeError as info:
# so far only seen for; RuntimeError("Bad password for file 'encrypted.md'")
raise BadPassword(info)
except KeyError as info:
# Probably; KeyError: "There is no item named 'encrypted.md' in the archive"
raise UnsupportedFile(info)
# TODO chain exception...
except Exception as info:
# TODO chain exception...
#raise
Expand Down
2 changes: 2 additions & 0 deletions puren_tonbo/tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,8 @@ class TestFileSystemNotesWriteFunctionSaveEncryptedCompressedZlib(TestFileSystem
# TODO test write file with characters outside of encoding
# TODO test write file, then write 2nd time this time with characters outside of encoding - to generate error - existing file should be preserved
# TODO no filename, generator set to none. ensure reasonable error generated
# TODO zip specific, test attempting to decrypt a regular zip file without _filename / encrypted.md present
# TODO zip specific, gtrep a regular zip file without _filename / encrypted.md present - manually testing, ptgrep/ptig silently ignores BUT core library logs warning (to stderr)

def debugTestRunner(post_mortem=None):
"""unittest runner doing post mortem debugging on failing tests"""
Expand Down
33 changes: 30 additions & 3 deletions puren_tonbo/tools/ptrecrypt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
"""Command line tool to re-encrypt Puren Tonbo files from any format into any format, optionally with a new password
r"""Command line tool to re-encrypt Puren Tonbo files from any format into any format, optionally with a new password
python -m puren_tonbo.tools.ptrecrypt -h
ptrecrypt -h
Expand All @@ -22,14 +22,36 @@
python -m puren_tonbo.tools.ptrecrypt --cipher .v001_jenc --new_extension .jenc -p password --destination_directory 10k_v001_jenc "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .v002_jenc --new_extension .jenc -p password --destination_directory 10k_v002wip_jenc "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .aes256.zip --new_extension .aes256.zip -p password --destination_directory 10k_aes256_zip "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .zip -p password --destination_directory 10k_zip "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .zip --new_extension .zip -p password --destination_directory 10k_zip "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .chi --new_extension .chi -p password --destination_directory 10k_tombo_chi "10000 markdown files"
python -m puren_tonbo.tools.ptrecrypt --cipher .u001_jenc --new_extension .jenc -p password --destination_directory /tmp/all_jenc puren_tonbo/tests/data/merge3_base.txt
python -m puren_tonbo.tools.ptrecrypt --cipher .v001_jenc --new_extension .jenc -p password --destination_directory /tmp/all_jenc puren_tonbo/tests/data/merge3_base.txt
python -m puren_tonbo.tools.ptrecrypt --cipher .v002_jenc --new_extension .jenc -p password --destination_directory /tmp/all_jenc puren_tonbo/tests/data/merge3_base.txt
ptrecrypt --cipher .zip --new_extension .zip --skip_unencrypted --existing-files=delete .
ptrecrypt --cipher .zip --new_extension .zip --skip_unencrypted --existing-files=delete --force_recrypt_same_format_password .
python -m puren_tonbo.tools.ptrecrypt --simulate -p password --force_recrypt_same_format_password --existing_files replace --skip_unencrypted puren_tonbo/tests/data/
Tests:
ptrecrypt -p password --cipher .zip --existing-files=delete puren_tonbo/tests/demo_notes/secrets/accounts
ls -altr puren_tonbo/tests/demo_notes/secrets/accounts
dir /od puren_tonbo\tests\demo_notes\secrets\accounts
re-encrpt and rename
ptrecrypt -p password --cipher .zip --new_extension .zip --existing-files=delete puren_tonbo/tests/demo_notes/secrets/accounts
REM should be NOOP
ptrecrypt -p password --cipher .zip --new_extension .zip --existing-files=delete --force_recrypt_same_format_password puren_tonbo/tests/demo_notes/secrets/accounts
Restore text filses:
ptrecrypt -p password --cipher .txt --new_extension .txt --existing-files=delete --force_recrypt_same_format_password puren_tonbo/tests/demo_notes/secrets/accounts
"""

import datetime
Expand Down Expand Up @@ -118,8 +140,13 @@ def process_file(filename, password, new_password, handler_class_newfile, force_
in_handler_class = puren_tonbo.filename2handler(filename)
in_handler = in_handler_class(key=password)
in_file = open(filename_abs, 'rb')
plaintext_bytes = in_handler.read_from(in_file)
in_file.close()
try:
plaintext_bytes = in_handler.read_from(in_file)
except puren_tonbo.UnsupportedFile as info:
log.error('Skipping UnsupportedFile (TODO option to allow continue, and default to stopping?) %s, %r', filename, info)
return
finally:
in_file.close()
base_filename, original_extension = in_handler.split_extension(filename)
#"""
#print('\t\t %r' % plaintext_bytes)
Expand Down

0 comments on commit 5e01bc6

Please sign in to comment.