From 9154faf351a504b0a277c041a853bf165b047d58 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 2 Jan 2018 09:36:57 +1100 Subject: [PATCH 1/3] Check the file is a valid file or dir (not just that it exists) --- onionshare/__init__.py | 5 ++++- onionshare_gui/__init__.py | 5 ++++- share/locale/en.json | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 99beb0e0..4b64d420 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -68,12 +68,15 @@ def main(cwd=None): # Validation valid = True for filename in filenames: - if not os.path.exists(filename): + if not os.path.isfile(filename) and not os.path.isdir(filename): print(strings._("not_a_file").format(filename)) valid = False if not os.access(filename, os.R_OK): print(strings._("not_a_readable_file").format(filename)) valid = False + if os.path.isdir(filename) and os.path.getsize(filename) < 4096: + print(strings._("not_a_file").format(filename)) + valid = False if not valid: sys.exit() diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index 83d03cc0..9d105f6a 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -91,12 +91,15 @@ def main(): if filenames: valid = True for filename in filenames: - if not os.path.exists(filename): + if not os.path.isfile(filename) and not os.path.isdir(filename): Alert(strings._("not_a_file", True).format(filename)) valid = False if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) valid = False + if os.path.isdir(filename) and os.path.getsize(filename) < 4096: + Alert(strings._("not_a_file", True).format(filename)) + valid = False if not valid: sys.exit() diff --git a/share/locale/en.json b/share/locale/en.json index 0756843e..3fc0c313 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -8,7 +8,7 @@ "give_this_url": "Give this URL to the person you're sending the file to:", "give_this_url_stealth": "Give this URL and HidServAuth line to the person you're sending the file to:", "ctrlc_to_stop": "Press Ctrl-C to stop server", - "not_a_file": "{0:s} is not a file.", + "not_a_file": "{0:s} is not a valid file.", "not_a_readable_file": "{0:s} is not a readable file.", "no_available_port": "Could not start the Onion service as there was no available port.", "download_page_loaded": "Download page loaded", From 2e5b9b37a9c0a3d57659bb229fdc839b77fca442 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 2 Jan 2018 09:45:56 +1100 Subject: [PATCH 2/3] 4096 byte dir check is not consistent across platforms, remove it for now --- onionshare/__init__.py | 3 --- onionshare_gui/__init__.py | 3 --- 2 files changed, 6 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 4b64d420..705ecc79 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -74,9 +74,6 @@ def main(cwd=None): if not os.access(filename, os.R_OK): print(strings._("not_a_readable_file").format(filename)) valid = False - if os.path.isdir(filename) and os.path.getsize(filename) < 4096: - print(strings._("not_a_file").format(filename)) - valid = False if not valid: sys.exit() diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index 9d105f6a..14c76617 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -97,9 +97,6 @@ def main(): if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) valid = False - if os.path.isdir(filename) and os.path.getsize(filename) < 4096: - Alert(strings._("not_a_file", True).format(filename)) - valid = False if not valid: sys.exit() From f656bafd4c48735305eebdac63e6a4259959fc95 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Sun, 14 Jan 2018 20:36:43 +1100 Subject: [PATCH 3/3] Catch the OSError in CLI mode when a file/folder can't be zipped up, and error out more gracefully --- onionshare/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 705ecc79..3f8d8cad 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -98,8 +98,12 @@ def main(cwd=None): # Prepare files to share print(strings._("preparing_files")) - web.set_file_info(filenames) - app.cleanup_filenames.append(web.zip_filename) + try: + web.set_file_info(filenames) + app.cleanup_filenames.append(web.zip_filename) + except OSError as e: + print(e.strerror) + sys.exit(1) # Warn about sending large files over Tor if web.zip_filesize >= 157286400: # 150mb