Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplified and ensure proper support of RFC 5322
Mikail KOCAK809770bb1162
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 25b5e12b50..e2f2721d26 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -99,6 +99,7 @@ All notable, unreleased changes to this project will be documented in this file.- Drop `manage_users` permission from the `permissions` query - #4854 by @maarcingebala- Fix ProductVariant with duplicated AttributeValues - #4829 by @fowczarek- Saleor can now be part of Apollo Federation, making seamless GraphQL schema extension possible - #4825 by @salwator+- Added the possibility to customize the sender address and name from the dashboard - #4820 by @NyanKiyoshi## 2.8.0diff --git a/saleor/account/emails.py b/saleor/account/emails.pyindex 155d7069da..f3c5fd7df1 100644--- a/saleor/account/emails.py+++ b/saleor/account/emails.py@@ -6,7 +6,7 @@ from templated_email import send_templated_mailfrom ..account import events as account_eventsfrom ..celeryconf import app-from ..core.emails import get_email_bases+from ..core.emails import get_email_contextfrom ..core.utils import build_absolute_uri@@ -41,7 +41,7 @@ def _send_password_reset_email_with_url(recipient_email, redirect_url, user_id,def _send_password_reset_email(recipient_email, reset_url, user_id):- send_kwargs, ctx = get_email_bases()+ send_kwargs, ctx = get_email_context()ctx["reset_url"] = reset_urlsend_templated_mail(template_name="account/password_reset",@@ -87,7 +87,7 @@ def _send_account_delete_confirmation_email(recipient_email, token):def _send_delete_confirmation_email(recipient_email, delete_url):- send_kwargs, ctx = get_email_bases()+ send_kwargs, ctx = get_email_context()ctx["delete_url"] = delete_urlsend_templated_mail(template_name="account/account_delete",diff --git a/saleor/core/emails.py b/saleor/core/emails.pyindex 2cfec51b31..8fe253e6e8 100644--- a/saleor/core/emails.py+++ b/saleor/core/emails.py@@ -4,7 +4,7 @@ from django.templatetags.static import staticfrom ..core.utils import build_absolute_uri-def get_email_bases():+def get_email_context():site: Site = Site.objects.get_current()logo_url = build_absolute_uri(static("images/logo-light.svg"))send_email_kwargs = {"from_email": site.settings.default_from_email}diff --git a/saleor/dashboard/emails.py b/saleor/dashboard/emails.pyindex 318fe7fb03..96242e8914 100644--- a/saleor/dashboard/emails.py+++ b/saleor/dashboard/emails.py@@ -8,7 +8,7 @@ from templated_email import send_templated_mailfrom ..account.models import Userfrom ..celeryconf import app-from ..core.emails import get_email_bases+from ..core.emails import get_email_contextfrom ..core.utils import build_absolute_uri@@ -52,7 +52,7 @@ def _send_set_user_password_email(recipient_email, user_pk, token, template_namedef _send_set_password_email(recipient_email, password_set_url, template_name):- send_kwargs, ctx = get_email_bases()+ send_kwargs, ctx = get_email_context()ctx["password_set_url"] = password_set_urlsend_templated_mail(template_name=template_name,@@ -65,7 +65,7 @@ def _send_set_password_email(recipient_email, password_set_url, template_name):@app.taskdef send_promote_customer_to_staff_email(staff_pk):staff = User.objects.get(pk=staff_pk)- send_kwargs, ctx = get_email_bases()+ send_kwargs, ctx = get_email_context()ctx["dashboard_url"] = build_absolute_uri(reverse("dashboard:index"))send_templated_mail(template_name="dashboard/staff/promote_customer",diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.pyindex 4d285dba92..81dff20673 100644--- a/saleor/graphql/shop/mutations.py+++ b/saleor/graphql/shop/mutations.py@@ -37,10 +37,10 @@ class ShopSettingsInput(graphene.InputObjectType):description="Default number of days which digital content URL will be valid.")default_mail_sender_name = graphene.String(- description="Default email sender's name"+ description="Default email sender's name.")default_mail_sender_address = graphene.String(- description="Default email sender's address"+ description="Default email sender's address.")diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.pyindex eae4e5f617..9a1f47a108 100644--- a/saleor/graphql/shop/types.py+++ b/saleor/graphql/shop/types.py@@ -90,10 +90,10 @@ class Shop(graphene.ObjectType):CountryDisplay, description="Shop's default country.")default_mail_sender_name = graphene.String(- description="Default shop's email sender's name"+ description="Default shop's email sender's name.")default_mail_sender_address = graphene.String(- description="Default shop's email sender's address"+ description="Default shop's email sender's address.")description = graphene.String(description="Shop's description.")domain = graphene.Field(Domain, required=True, description="Shop's domain data.")@@ -144,7 +144,7 @@ class Shop(graphene.ObjectType):description="Default number of max downloads per digital content URL.")default_digital_url_valid_days = graphene.Int(- description=("Default number of days which digital content URL will be valid.")+ description="Default number of days which digital content URL will be valid.")company_address = graphene.Field(Address, description="Company address.", required=Falsediff --git a/saleor/order/emails.py b/saleor/order/emails.pyindex 2108700212..161f4f006a 100644--- a/saleor/order/emails.py+++ b/saleor/order/emails.py@@ -2,7 +2,7 @@ from django.urls import reversefrom templated_email import send_templated_mailfrom ..celeryconf import app-from ..core.emails import get_email_bases+from ..core.emails import get_email_contextfrom ..core.utils import build_absolute_urifrom ..seo.schema.email import get_order_confirmation_markupfrom . import events@@ -24,7 +24,7 @@ def collect_data_for_email(order_pk, template):"""order = Order.objects.get(pk=order_pk)recipient_email = order.get_customer_email()- send_kwargs, email_context = get_email_bases()+ send_kwargs, email_context = get_email_context()email_context["order_details_url"] = build_absolute_uri(reverse("order:details", kwargs={"token": order.token}))diff --git a/saleor/site/models.py b/saleor/site/models.pyindex 60b37eae16..ec87170b60 100644--- a/saleor/site/models.py+++ b/saleor/site/models.py@@ -1,3 +1,5 @@+from email.headerregistry import Address+from email.utils import parseaddrfrom typing import Optionalfrom django.conf import settings@@ -82,20 +84,24 @@ class SiteSettings(models.Model):@propertydef default_from_email(self) -> Optional[str]:- sender_name: Optional[str] = self.default_mail_sender_name- sender_address: Optional[- str- ] = self.default_mail_sender_address or settings.DEFAULT_FROM_EMAIL+ sender_name: Optional[str] = self.default_mail_sender_name or ""+ sender_address: Optional[str] = self.default_mail_sender_address- # If no name was provided, only return the email address- if not sender_name:- return sender_address+ if not sender_address:+ sender_address = settings.DEFAULT_FROM_EMAIL- # If an email address was provided, return 'Sender Name <address>'- if sender_address:- return f"{sender_name} <{sender_address}>"+ if not sender_address:+ return None- return None+ sender_name, sender_address = parseaddr(sender_address)++ # Note: we only want to format the address in accordance to RFC 5322+ # but our job is not to sanitize the values. The sanitized value, encoding, etc.+ # will depend on the email backend being used.+ #+ # Refer to email.header.Header and django.core.mail.message.sanitize_address.+ value = str(Address(sender_name, addr_spec=sender_address))+ return valuedef available_backends(self):return self.authorizationkey_set.values_list("name", flat=True)diff --git a/tests/settings.py b/tests/settings.pyindex b4d69c550e..afb8bba23b 100644--- a/tests/settings.py+++ b/tests/settings.py@@ -17,6 +17,8 @@ ES_URL = NoneSEARCH_BACKEND = "saleor.search.backends.postgresql"INSTALLED_APPS = [a for a in INSTALLED_APPS if a != "django_elasticsearch_dsl"]+EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"+RECAPTCHA_PUBLIC_KEY = ""RECAPTCHA_PRIVATE_KEY = ""diff --git a/tests/test_emails.py b/tests/test_emails.pyindex 41ab750950..5f9d09dfc5 100644--- a/tests/test_emails.py+++ b/tests/test_emails.py@@ -1,16 +1,18 @@from unittest import mockimport pytest+from django.core import mailfrom django.templatetags.static import staticfrom templated_email import get_connection+import saleor.account.emails as account_emailsimport saleor.order.emails as emails-from saleor.core.emails import get_email_bases+from saleor.core.emails import get_email_contextfrom saleor.core.utils import build_absolute_urifrom saleor.order.utils import add_variant_to_order-def test_get_email_base_context(site_settings):+def test_get_email_context(site_settings):site = site_settings.sitelogo_url = build_absolute_uri(static("images/logo-light.svg"))@@ -21,7 +23,7 @@ def test_get_email_base_context(site_settings):"site_name": site.name,}- send_kwargs, received_context = get_email_bases()+ send_kwargs, received_context = get_email_context()assert send_kwargs == expected_send_kwargsassert proper_context == received_context@@ -163,3 +165,44 @@ def test_send_fulfillment_emails(# Render the email to ensure there is no erroremail_connection = get_connection()email_connection.get_email_message(to=recipients, **expected_call_kwargs)+++def test_email_having_display_name_in_settings(customer_user, site_settings, settings):+ expected_from_email = "Info <[email protected]>"++ site_settings.default_mail_sender_name = None+ site_settings.default_mail_sender_address = None++ settings.DEFAULT_FROM_EMAIL = expected_from_email++ assert site_settings.default_from_email == expected_from_email+++def test_send_dummy_email_with_utf_8(customer_user, site_settings):+ site_settings.default_mail_sender_address = "[email protected]"+ site_settings.default_mail_sender_name = "徐 欣"+ site_settings.save(+ update_fields=["default_mail_sender_address", "default_mail_sender_name"]+ )++ account_emails.send_account_delete_confirmation_email(customer_user)++ assert len(mail.outbox) > 0+ message: mail.EmailMessage = mail.outbox[-1]+ assert message.from_email == "徐 欣 <[email protected]>"+ assert message.extra_headers == {}+++ "sender_name, sender_address",+ (("徐 欣", "[email protected]\nOopsie: Hello"), ("徐\n欣", "[email protected]")),+)+def test_send_dummy_email_with_header_injection(+ customer_user, site_settings, sender_name, sender_address+):+ site_settings.default_mail_sender_address = sender_name+ site_settings.default_mail_sender_name = sender_address+ site_settings.save()++ account_emails.send_account_delete_confirmation_email(customer_user)+ assert len(mail.outbox) == 0