From 4bf5093d8fbc0360c5b472f64ffd3dbb6e6d5069 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Wed, 13 Mar 2024 08:47:00 -0400 Subject: [PATCH] bugfix: APPRISE_ATTACH_SIZE correctly manages attachment size (#177) --- apprise_api/api/tests/test_attachment.py | 10 ++++++---- apprise_api/api/utils.py | 8 ++++---- apprise_api/api/views.py | 12 ++++++------ apprise_api/core/settings/__init__.py | 7 +------ 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/apprise_api/api/tests/test_attachment.py b/apprise_api/api/tests/test_attachment.py index 023dd9d..4ca6405 100644 --- a/apprise_api/api/tests/test_attachment.py +++ b/apprise_api/api/tests/test_attachment.py @@ -121,11 +121,12 @@ def test_form_file_attachment_parsing(self): # Test a case where our attachment exceeds the maximum size we allow # for - with override_settings(APPRISE_MAX_ATTACHMENT_SIZE=1): + with override_settings(APPRISE_ATTACH_SIZE=1): files_request = { 'file1': SimpleUploadedFile( "attach.txt", - b"some content more then 1 byte in length to pass along.", + # More then 1 MB in size causing error to trip + ("content" * 1024 * 1024).encode('utf-8'), content_type="text/plain") } with self.assertRaises(ValueError): @@ -324,9 +325,10 @@ def test(*args, **kwargs): # Test a case where our attachment exceeds the maximum size we allow # for - with override_settings(APPRISE_MAX_ATTACHMENT_SIZE=1): + with override_settings(APPRISE_ATTACH_SIZE=1): + # More then 1 MB in size causing error to trip attachment_payload = \ - b"some content more then 1 byte in length to pass along." + ("content" * 1024 * 1024).encode('utf-8') with self.assertRaises(ValueError): parse_attachments(attachment_payload, {}) diff --git a/apprise_api/api/utils.py b/apprise_api/api/utils.py index 195976e..32f86b4 100644 --- a/apprise_api/api/utils.py +++ b/apprise_api/api/utils.py @@ -338,8 +338,8 @@ def parse_attachments(attachment_payload, files_request): # # Some Validation # - if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \ - attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE: + if settings.APPRISE_ATTACH_SIZE > 0 and \ + attachment.size > settings.APPRISE_ATTACH_SIZE: raise ValueError( "attachment %s's filesize is to large" % filename) @@ -387,8 +387,8 @@ def parse_attachments(attachment_payload, files_request): # # Some Validation # - if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \ - attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE: + if settings.APPRISE_ATTACH_SIZE > 0 and \ + attachment.size > settings.APPRISE_ATTACH_SIZE: raise ValueError( "attachment %s's filesize is to large" % filename) diff --git a/apprise_api/api/views.py b/apprise_api/api/views.py index 6f9c586..c122589 100644 --- a/apprise_api/api/views.py +++ b/apprise_api/api/views.py @@ -629,11 +629,11 @@ def post(self, request, key): attach = parse_attachments( content.get('attachment'), request.FILES) - except (TypeError, ValueError): + except (TypeError, ValueError) as e: # Invalid entry found in list logger.warning( - 'NOTIFY - %s - Bad attachment specified', - request.META['REMOTE_ADDR']) + 'NOTIFY - %s - Bad attachment: %s', + request.META['REMOTE_ADDR'], str(e)) return HttpResponse( _('Bad attachment'), @@ -1177,11 +1177,11 @@ def post(self, request): attach = parse_attachments( content.get('attachment'), request.FILES) - except (TypeError, ValueError): + except (TypeError, ValueError) as e: # Invalid entry found in list logger.warning( - 'NOTIFY - %s - Bad attachment specified', - request.META['REMOTE_ADDR']) + 'NOTIFY - %s - Bad attachment: %s', + request.META['REMOTE_ADDR'], str(e)) return HttpResponse( _('Bad attachment'), diff --git a/apprise_api/core/settings/__init__.py b/apprise_api/core/settings/__init__.py index 5dcaa59..0f58976 100644 --- a/apprise_api/core/settings/__init__.py +++ b/apprise_api/core/settings/__init__.py @@ -146,7 +146,7 @@ APPRISE_ATTACH_DIR = os.environ.get( 'APPRISE_ATTACH_DIR', os.path.join(BASE_DIR, 'var', 'attach')) -# The file attachment size allowed by the API +# The maximum file attachment size allowed by the API (defined in MB) APPRISE_ATTACH_SIZE = int(os.environ.get('APPRISE_ATTACH_SIZE', 200)) * 1048576 # When set Apprise API Locks itself down so that future (configuration) @@ -205,8 +205,3 @@ # Define the number of attachments that can exist as part of a payload # Setting this to zero disables the limit APPRISE_MAX_ATTACHMENTS = int(os.environ.get('APPRISE_MAX_ATTACHMENTS', 6)) - -# Defines the maximum size each attachment can be -# 8388608 == 8MB -APPRISE_MAX_ATTACHMENT_SIZE = int( - os.environ.get('APPRISE_MAX_ATTACHMENT_SIZE', 8388608))