Skip to content

Commit

Permalink
Fixed bug when handling same input and output file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobellaccini committed Sep 30, 2018
1 parent 5ca00e7 commit e10eb09
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
History
===============

0.4.2 (Sep 2018)
~~~~~~~~~~~~~~~~~~
* Fixed bug when handling same input and output file

0.4.1 (Sep 2018)
~~~~~~~~~~~~~~~~~~
* Preventing users from specifying the same input and output file
Expand Down
24 changes: 15 additions & 9 deletions pyAesCrypt/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from os import stat, remove, path

# pyAesCrypt version
version = "0.4.1"
version = "0.4.2"

# encryption/decryption buffer size - 64K
bufferSize = 64 * 1024
Expand Down Expand Up @@ -81,12 +81,15 @@ def stretch(passw, iv1):
def encryptFile(infile, outfile, passw, bufferSize):
try:
with open(infile, "rb") as fIn:
# check that output file does not exist
# or that, if exists, is not the same as the input file
# (i.e.: overwrite if it seems safe)
if path.isfile(outfile):
if path.samefile(infile, outfile):
raise ValueError("Input and output files "
"are the same.")
try:
with open(outfile, "wb") as fOut:
# check if input and output files are the same
if path.samefile(infile, outfile):
raise ValueError("Input and output files "
"are the same.")
# encrypt file stream
encryptStream(fIn, fOut, passw, bufferSize)

Expand Down Expand Up @@ -244,12 +247,15 @@ def encryptStream(fIn, fOut, passw, bufferSize):
def decryptFile(infile, outfile, passw, bufferSize):
try:
with open(infile, "rb") as fIn:
# check that output file does not exist
# or that, if exists, is not the same as the input file
# (i.e.: overwrite if it seems safe)
if path.isfile(outfile):
if path.samefile(infile, outfile):
raise ValueError("Input and output files "
"are the same.")
try:
with open(outfile, "wb") as fOut:
# check if input and output files are the same
if path.samefile(infile, outfile):
raise ValueError("Input and output files "
"are the same.")
# get input file size
inputFileSize = stat(infile).st_size
try:
Expand Down
11 changes: 10 additions & 1 deletion pyAesCrypt/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ def test_bs_quick(self):
class TestExceptions(unittest.TestCase):

# test file path
tfile = prefix+'test.txt'
tfile = prefix + 'test.txt'

# path of the copy of the test file
tfilebak = tfile + '.bak'

# fixture for preparing the environment
def setUp(self):
Expand All @@ -216,6 +219,8 @@ def setUp(self):
# generate a test file
with open(self.tfile, 'wb') as fout:
fout.write(os.urandom(4))
# copy of the test file
shutil.copyfile(self.tfile, self.tfilebak)

def tearDown(self):
# remove whole directory tree
Expand Down Expand Up @@ -327,13 +332,17 @@ def test_samefile_enc(self):
pyAesCrypt.encryptFile,
self.tfile, self.tfile,
'pass', bufferSize)
# check that the original file was not modified
self.assertTrue(filecmp.cmp(self.tfile, self.tfilebak))
# test same input and output file - decryption
def test_samefile_dec(self):
self.assertRaisesRegex(ValueError, ("Input and output files "
"are the same."),
pyAesCrypt.decryptFile,
self.tfile, self.tfile,
'pass', bufferSize)
# check that the original file was not modified
self.assertTrue(filecmp.cmp(self.tfile, self.tfilebak))

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
README = readme.read()

setup(name='pyAesCrypt',
version='0.4.1',
version='0.4.2',
packages = find_packages(),
include_package_data=True,
description='Encrypt and decrypt files and streams in AES Crypt format (version 2)',
Expand Down

0 comments on commit e10eb09

Please sign in to comment.