From 6daf4973aa9b5ec750390688a4879cee2cf71ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20Karata=C5=9F?= Date: Wed, 24 Feb 2021 11:08:18 +0300 Subject: [PATCH 1/3] Add attachment size validation and ATTACHMENTS_MAX_SIZE setting --- django_ses_plus/models.py | 4 ++++ django_ses_plus/settings.py | 1 + 2 files changed, 5 insertions(+) diff --git a/django_ses_plus/models.py b/django_ses_plus/models.py index dddb854..e7f4f51 100644 --- a/django_ses_plus/models.py +++ b/django_ses_plus/models.py @@ -1,4 +1,5 @@ import base64 +import sys import uuid from django.conf import settings @@ -78,6 +79,9 @@ def send_email(self, subject, template_path, context, from_email=None, language= assert all([key in attachment for key in ["filename", "content", "mimetype"]]), "Attachments should contain `filename`, `content` and `mimetype`." if isinstance(attachment["content"], bytes): + assert ( + sys.getsizeof(attachment["content"]) <= DJANGO_SES_PLUS_SETTINGS["ATTACHMENTS_MAX_SIZE"] + ), f'Attachment sizes should be smaller than {DJANGO_SES_PLUS_SETTINGS["ATTACHMENTS_MAX_SIZE"]} bytes.' # Since celery only accepts JSON serializable types and `bytes` is not JSON serializable, # Base64 encoding is used to be able to pass attachment content to the celery task, attachment["content"] = base64.b64encode(attachment["content"]).decode("utf-8") diff --git a/django_ses_plus/settings.py b/django_ses_plus/settings.py index 41eec9a..5f1231a 100644 --- a/django_ses_plus/settings.py +++ b/django_ses_plus/settings.py @@ -4,6 +4,7 @@ DJANGO_SES_PLUS_SETTINGS.setdefault("SEND_EMAIL", True) DJANGO_SES_PLUS_SETTINGS.setdefault("CELERY_TASK_RETRY_KWARGS", {'max_retries': 15, 'countdown': 60}) +DJANGO_SES_PLUS_SETTINGS.setdefault("ATTACHMENTS_MAX_SIZE", 10485760) # 10MB # Get default from email from django settings. DJANGO_SES_PLUS_SETTINGS.setdefault("DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL) From 514fc544b746e9dde4e262c04104bc9e57a4e36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20Karata=C5=9F?= Date: Wed, 24 Feb 2021 12:10:10 +0300 Subject: [PATCH 2/3] Change default to 100KB --- django_ses_plus/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_ses_plus/settings.py b/django_ses_plus/settings.py index 5f1231a..1868b22 100644 --- a/django_ses_plus/settings.py +++ b/django_ses_plus/settings.py @@ -4,7 +4,7 @@ DJANGO_SES_PLUS_SETTINGS.setdefault("SEND_EMAIL", True) DJANGO_SES_PLUS_SETTINGS.setdefault("CELERY_TASK_RETRY_KWARGS", {'max_retries': 15, 'countdown': 60}) -DJANGO_SES_PLUS_SETTINGS.setdefault("ATTACHMENTS_MAX_SIZE", 10485760) # 10MB +DJANGO_SES_PLUS_SETTINGS.setdefault("ATTACHMENTS_MAX_SIZE", 102400) # 100KB # Get default from email from django settings. DJANGO_SES_PLUS_SETTINGS.setdefault("DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL) From f959bf29b7e861ddd7bb78d08c300e4b983a962e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20Karata=C5=9F?= Date: Wed, 24 Feb 2021 12:19:00 +0300 Subject: [PATCH 3/3] Add new setting ATTACHMENTS_MAX_SIZE to README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b31da78..cdbc57f 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,10 @@ Please refer to django-ses package [documentation](https://github.com/django-ses DJANGO_SES_PLUS_SETTINGS = { "SEND_EMAIL": True, # True by default. "CELERY_TASK_RETRY_KWARGS": { - 'max_retries': 15, + 'max_retries': 15, 'countdown': 60 - } + }, + "ATTACHMENTS_MAX_SIZE": 102400, # in bytes. 100KB default. } ```