bugfix: APPRISE_ATTACH_SIZE correctly manages attachment size (#177)

This commit is contained in:
Chris Caron 2024-03-13 08:47:00 -04:00 committed by GitHub
parent 6e29103046
commit 4bf5093d8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 20 deletions

View File

@ -121,11 +121,12 @@ class AttachmentTests(SimpleTestCase):
# Test a case where our attachment exceeds the maximum size we allow # Test a case where our attachment exceeds the maximum size we allow
# for # for
with override_settings(APPRISE_MAX_ATTACHMENT_SIZE=1): with override_settings(APPRISE_ATTACH_SIZE=1):
files_request = { files_request = {
'file1': SimpleUploadedFile( 'file1': SimpleUploadedFile(
"attach.txt", "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") content_type="text/plain")
} }
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -324,9 +325,10 @@ class AttachmentTests(SimpleTestCase):
# Test a case where our attachment exceeds the maximum size we allow # Test a case where our attachment exceeds the maximum size we allow
# for # 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 = \ attachment_payload = \
b"some content more then 1 byte in length to pass along." ("content" * 1024 * 1024).encode('utf-8')
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
parse_attachments(attachment_payload, {}) parse_attachments(attachment_payload, {})

View File

@ -338,8 +338,8 @@ def parse_attachments(attachment_payload, files_request):
# #
# Some Validation # Some Validation
# #
if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \ if settings.APPRISE_ATTACH_SIZE > 0 and \
attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE: attachment.size > settings.APPRISE_ATTACH_SIZE:
raise ValueError( raise ValueError(
"attachment %s's filesize is to large" % filename) "attachment %s's filesize is to large" % filename)
@ -387,8 +387,8 @@ def parse_attachments(attachment_payload, files_request):
# #
# Some Validation # Some Validation
# #
if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \ if settings.APPRISE_ATTACH_SIZE > 0 and \
attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE: attachment.size > settings.APPRISE_ATTACH_SIZE:
raise ValueError( raise ValueError(
"attachment %s's filesize is to large" % filename) "attachment %s's filesize is to large" % filename)

View File

@ -629,11 +629,11 @@ class NotifyView(View):
attach = parse_attachments( attach = parse_attachments(
content.get('attachment'), request.FILES) content.get('attachment'), request.FILES)
except (TypeError, ValueError): except (TypeError, ValueError) as e:
# Invalid entry found in list # Invalid entry found in list
logger.warning( logger.warning(
'NOTIFY - %s - Bad attachment specified', 'NOTIFY - %s - Bad attachment: %s',
request.META['REMOTE_ADDR']) request.META['REMOTE_ADDR'], str(e))
return HttpResponse( return HttpResponse(
_('Bad attachment'), _('Bad attachment'),
@ -1177,11 +1177,11 @@ class StatelessNotifyView(View):
attach = parse_attachments( attach = parse_attachments(
content.get('attachment'), request.FILES) content.get('attachment'), request.FILES)
except (TypeError, ValueError): except (TypeError, ValueError) as e:
# Invalid entry found in list # Invalid entry found in list
logger.warning( logger.warning(
'NOTIFY - %s - Bad attachment specified', 'NOTIFY - %s - Bad attachment: %s',
request.META['REMOTE_ADDR']) request.META['REMOTE_ADDR'], str(e))
return HttpResponse( return HttpResponse(
_('Bad attachment'), _('Bad attachment'),

View File

@ -146,7 +146,7 @@ APPRISE_CONFIG_DIR = os.environ.get(
APPRISE_ATTACH_DIR = os.environ.get( APPRISE_ATTACH_DIR = os.environ.get(
'APPRISE_ATTACH_DIR', os.path.join(BASE_DIR, 'var', 'attach')) '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 APPRISE_ATTACH_SIZE = int(os.environ.get('APPRISE_ATTACH_SIZE', 200)) * 1048576
# When set Apprise API Locks itself down so that future (configuration) # When set Apprise API Locks itself down so that future (configuration)
@ -205,8 +205,3 @@ APPRISE_PLUGIN_PATHS = os.environ.get(
# Define the number of attachments that can exist as part of a payload # Define the number of attachments that can exist as part of a payload
# Setting this to zero disables the limit # Setting this to zero disables the limit
APPRISE_MAX_ATTACHMENTS = int(os.environ.get('APPRISE_MAX_ATTACHMENTS', 6)) 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))