From 6bb902ffd4b1a3f043ace2adba3724f6c949d3a5 Mon Sep 17 00:00:00 2001 From: support Date: Sun, 22 Nov 2026 20:23:18 +0100 Subject: [PATCH] Encode the text customers enter when it goes into a message Message tokens with text a customer typed (names, addresses, e-mails, phone numbers, comments, reviews, gift voucher names) were inserted into the e-mail body as they were. A name with HTML in it became HTML in the e-mail to the store owner, and again in the admin panel when the queued e-mail was opened for editing. The drops now HTML-encode that text, as they already did for product names and the purchase order number. Text the callers had already encoded (contact enquiries, ask-a-question and e-mail-a-friend messages, notes) is left alone. A subject is plain text, so it is decoded after rendering and a name like O'Brien stays readable there. --- .../DotLiquidDrops/LiquidAskQuestion.cs | 7 ++- .../DotLiquidDrops/LiquidBlogComment.cs | 3 +- .../DotLiquidDrops/LiquidContactUs.cs | 5 +- .../DotLiquidDrops/LiquidCustomer.cs | 32 +++++------ .../DotLiquidDrops/LiquidEmailAFriend.cs | 5 +- .../DotLiquidDrops/LiquidExtensions.cs | 9 +++ .../DotLiquidDrops/LiquidGiftVoucher.cs | 9 +-- .../DotLiquidDrops/LiquidKnowledgebase.cs | 3 +- .../DotLiquidDrops/LiquidMerchandiseReturn.cs | 23 ++++---- .../DotLiquidDrops/LiquidNewsComment.cs | 5 +- .../LiquidNewsLetterSubscription.cs | 3 +- .../DotLiquidDrops/LiquidOrder.cs | 56 +++++++++---------- .../DotLiquidDrops/LiquidVendor.cs | 19 ++++--- .../DotLiquidDrops/LiquidVendorReview.cs | 7 ++- .../Services/CampaignMessageRenderer.cs | 2 +- .../Services/CatalogMessageProviderService.cs | 2 +- .../Services/MessageProviderService.cs | 2 +- .../DotLiquidDrops/LiquidCustomerTests.cs | 25 +++++++++ .../DotLiquidDrops/LiquidOrderTests.cs | 13 +++++ 19 files changed, 144 insertions(+), 86 deletions(-) diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidAskQuestion.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidAskQuestion.cs index d02ce67ae..e0d63f3e1 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidAskQuestion.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidAskQuestion.cs @@ -1,4 +1,5 @@ using DotLiquid; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -7,9 +8,9 @@ public class LiquidAskQuestion : Drop public LiquidAskQuestion(string message, string email, string fullName, string phone) { Message = message; - Email = email; - FullName = fullName; - Phone = phone; + Email = WebUtility.HtmlEncode(email); + FullName = WebUtility.HtmlEncode(fullName); + Phone = WebUtility.HtmlEncode(phone); AdditionalTokens = new Dictionary(); } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidBlogComment.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidBlogComment.cs index 169695076..88c882339 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidBlogComment.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidBlogComment.cs @@ -2,6 +2,7 @@ using Grand.Domain.Blogs; using Grand.Domain.Localization; using Grand.Domain.Stores; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -33,7 +34,7 @@ public LiquidBlogComment(BlogComment blogComment, BlogPost blogPost, Store store public string BlogPostURL => $"{url}/{_blogPost.SeName}"; - public string BlogPostCommentText => _blogComment?.CommentText; + public string BlogPostCommentText => WebUtility.HtmlEncode(_blogComment?.CommentText); public IDictionary AdditionalTokens { get; set; } } \ No newline at end of file diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidContactUs.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidContactUs.cs index 70a8714ec..60af5899b 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidContactUs.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidContactUs.cs @@ -1,4 +1,5 @@ using DotLiquid; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -6,8 +7,8 @@ public class LiquidContactUs : Drop { public LiquidContactUs(string senderEmail, string senderName, string body, string attributeDescription) { - SenderEmail = senderEmail; - SenderName = senderName; + SenderEmail = WebUtility.HtmlEncode(senderEmail); + SenderName = WebUtility.HtmlEncode(senderName); Body = body; AttributeDescription = attributeDescription; diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidCustomer.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidCustomer.cs index b7e50198f..938d98863 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidCustomer.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidCustomer.cs @@ -26,35 +26,35 @@ public LiquidCustomer(Customer customer, Store store, DomainHost host, CustomerN AdditionalTokens = new Dictionary(); } - public string Email => _customer.Email; + public string Email => WebUtility.HtmlEncode(_customer.Email); - public string Username => _customer.Username; + public string Username => WebUtility.HtmlEncode(_customer.Username); - public string FullName => _customer.GetFullName(); + public string FullName => WebUtility.HtmlEncode(_customer.GetFullName()); - public string FirstName => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.FirstName); + public string FirstName => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.FirstName)); - public string LastName => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.LastName); + public string LastName => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.LastName)); - public string Gender => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Gender); + public string Gender => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Gender)); - public string DateOfBirth => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.DateOfBirth); + public string DateOfBirth => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.DateOfBirth)); - public string Company => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Company); + public string Company => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Company)); - public string StreetAddress => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.StreetAddress); + public string StreetAddress => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.StreetAddress)); - public string StreetAddress2 => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.StreetAddress2); + public string StreetAddress2 => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.StreetAddress2)); - public string ZipPostalCode => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.ZipPostalCode); + public string ZipPostalCode => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.ZipPostalCode)); - public string City => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.City); + public string City => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.City)); - public string Phone => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Phone); + public string Phone => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Phone)); - public string Fax => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Fax); + public string Fax => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.Fax)); - public string VatNumber => _customer.GetUserFieldFromEntity(SystemCustomerFieldNames.VatNumber); + public string VatNumber => WebUtility.HtmlEncode(_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.VatNumber)); public string VatNumberStatus => ((VatNumberStatus)_customer.GetUserFieldFromEntity(SystemCustomerFieldNames.VatNumberStatusId)).ToString(); @@ -78,7 +78,7 @@ private string PasswordRecoveryAreaPath public string NewNoteText => FormatText.ConvertText(_customerNote.Note); - public string NewTitleText => _customerNote.Title; + public string NewTitleText => WebUtility.HtmlEncode(_customerNote.Title); public string CustomerNoteAttachmentUrl => $"{url}/download/customernotefile/{_customerNote.Id}"; diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidEmailAFriend.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidEmailAFriend.cs index 4dcf8538a..e1eea2153 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidEmailAFriend.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidEmailAFriend.cs @@ -1,4 +1,5 @@ using DotLiquid; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -7,8 +8,8 @@ public class LiquidEmailAFriend : Drop public LiquidEmailAFriend(string personalMessage, string customerEmail, string friendsEmail) { PersonalMessage = personalMessage; - Email = customerEmail; - FriendsEmail = friendsEmail; + Email = WebUtility.HtmlEncode(customerEmail); + FriendsEmail = WebUtility.HtmlEncode(friendsEmail); AdditionalTokens = new Dictionary(); } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs index c5e48b936..cbef635b7 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs @@ -1,6 +1,7 @@ using DotLiquid; using DotLiquid.NamingConventions; using Grand.Domain.Messages; +using System.Net; using System.Reflection; namespace Grand.Business.Messages.DotLiquidDrops; @@ -27,4 +28,12 @@ public static string Render(LiquidObject liquidObject, string source) return replaced; } + + /// + /// Renders a subject line: tokens are HTML-encoded for the body, and a subject is plain text + /// + public static string RenderSubject(LiquidObject liquidObject, string source) + { + return WebUtility.HtmlDecode(Render(liquidObject, source)); + } } \ No newline at end of file diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidGiftVoucher.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidGiftVoucher.cs index 22989f2df..f0ad81612 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidGiftVoucher.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidGiftVoucher.cs @@ -1,6 +1,7 @@ using DotLiquid; using Grand.Domain.Orders; using Grand.SharedKernel.Extensions; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -15,13 +16,13 @@ public LiquidGiftVoucher(GiftVoucher giftVoucher) AdditionalTokens = new Dictionary(); } - public string SenderName => _giftVoucher.SenderName; + public string SenderName => WebUtility.HtmlEncode(_giftVoucher.SenderName); - public string SenderEmail => _giftVoucher.SenderEmail; + public string SenderEmail => WebUtility.HtmlEncode(_giftVoucher.SenderEmail); - public string RecipientName => _giftVoucher.RecipientName; + public string RecipientName => WebUtility.HtmlEncode(_giftVoucher.RecipientName); - public string RecipientEmail => _giftVoucher.RecipientEmail; + public string RecipientEmail => WebUtility.HtmlEncode(_giftVoucher.RecipientEmail); public string Amount { get; set; } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidKnowledgebase.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidKnowledgebase.cs index 00adeb500..67cb01793 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidKnowledgebase.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidKnowledgebase.cs @@ -2,6 +2,7 @@ using Grand.Domain.Knowledgebase; using Grand.Domain.Localization; using Grand.Domain.Stores; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -31,7 +32,7 @@ public LiquidKnowledgebase(KnowledgebaseArticle article, KnowledgebaseArticleCom public string ArticleTitle => _article.Name; - public string ArticleCommentText => _articleComment.CommentText; + public string ArticleCommentText => WebUtility.HtmlEncode(_articleComment.CommentText); public string ArticleCommentUrl => $"{url}/{_article.SeName}"; diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidMerchandiseReturn.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidMerchandiseReturn.cs index ee6cb0489..e059358df 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidMerchandiseReturn.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidMerchandiseReturn.cs @@ -2,6 +2,7 @@ using Grand.Domain.Orders; using Grand.Domain.Stores; using Grand.SharedKernel.Extensions; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -60,29 +61,29 @@ public LiquidMerchandiseReturn(MerchandiseReturn merchandiseReturn, Store store, public DateTime PickupDateUtc => _merchandiseReturn.PickupDate; - public string PickupAddressFirstName => _merchandiseReturn.PickupAddress.FirstName; + public string PickupAddressFirstName => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.FirstName); - public string PickupAddressLastName => _merchandiseReturn.PickupAddress.LastName; + public string PickupAddressLastName => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.LastName); - public string PickupAddressPhoneNumber => _merchandiseReturn.PickupAddress.PhoneNumber; + public string PickupAddressPhoneNumber => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.PhoneNumber); - public string PickupAddressEmail => _merchandiseReturn.PickupAddress.Email; + public string PickupAddressEmail => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.Email); - public string PickupAddressFaxNumber => _merchandiseReturn.PickupAddress.FaxNumber; + public string PickupAddressFaxNumber => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.FaxNumber); - public string PickupAddressCompany => _merchandiseReturn.PickupAddress.Company; + public string PickupAddressCompany => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.Company); - public string PickupAddressVatNumber => _merchandiseReturn.PickupAddress.VatNumber; + public string PickupAddressVatNumber => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.VatNumber); - public string PickupAddressAddress1 => _merchandiseReturn.PickupAddress.Address1; + public string PickupAddressAddress1 => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.Address1); - public string PickupAddressAddress2 => _merchandiseReturn.PickupAddress.Address2; + public string PickupAddressAddress2 => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.Address2); - public string PickupAddressCity => _merchandiseReturn.PickupAddress.City; + public string PickupAddressCity => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.City); public string PickupAddressStateProvince { get; set; } - public string PickupAddressZipPostalCode => _merchandiseReturn.PickupAddress.ZipPostalCode; + public string PickupAddressZipPostalCode => WebUtility.HtmlEncode(_merchandiseReturn.PickupAddress.ZipPostalCode); public string PickupAddressCountry { get; set; } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsComment.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsComment.cs index da848198e..bbdd4494a 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsComment.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsComment.cs @@ -2,6 +2,7 @@ using Grand.Domain.Localization; using Grand.Domain.News; using Grand.Domain.Stores; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -31,9 +32,9 @@ public LiquidNewsComment(NewsItem newsItem, NewsComment newsComment, Store store public string NewsTitle => _newsItem.Title; - public string CommentText => _newsComment.CommentText; + public string CommentText => WebUtility.HtmlEncode(_newsComment.CommentText); - public string CommentTitle => _newsComment.CommentTitle; + public string CommentTitle => WebUtility.HtmlEncode(_newsComment.CommentTitle); public string NewsURL => $"{url}/{_newsItem.SeName}"; diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsLetterSubscription.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsLetterSubscription.cs index 50e6c569b..4c9d30444 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsLetterSubscription.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidNewsLetterSubscription.cs @@ -1,6 +1,7 @@ using DotLiquid; using Grand.Domain.Messages; using Grand.Domain.Stores; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -23,7 +24,7 @@ public LiquidNewsLetterSubscription(NewsLetterSubscription subscription, Store s AdditionalTokens = new Dictionary(); } - public string Email => _subscription.Email; + public string Email => WebUtility.HtmlEncode(_subscription.Email); public string ActivationUrl { get { diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidOrder.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidOrder.cs index df08b826a..0e024732a 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidOrder.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidOrder.cs @@ -52,35 +52,35 @@ public LiquidOrder(Order order, Customer customer, Language language, Currency c //the due date is a calendar date, so it is shown as stored, without a time zone shift public string PaymentDueDate => _order.PaymentDueDateUtc?.ToString("D") ?? ""; - public string CustomerFullName => $"{_order.BillingAddress.FirstName} {_order.BillingAddress.LastName}"; + public string CustomerFullName => WebUtility.HtmlEncode($"{_order.BillingAddress.FirstName} {_order.BillingAddress.LastName}"); - public string CustomerEmail => _order.BillingAddress.Email; + public string CustomerEmail => WebUtility.HtmlEncode(_order.BillingAddress.Email); - public string BillingAddressName => _order.BillingAddress.Name; + public string BillingAddressName => WebUtility.HtmlEncode(_order.BillingAddress.Name); - public string BillingFirstName => _order.BillingAddress.FirstName; + public string BillingFirstName => WebUtility.HtmlEncode(_order.BillingAddress.FirstName); - public string BillingLastName => _order.BillingAddress.LastName; + public string BillingLastName => WebUtility.HtmlEncode(_order.BillingAddress.LastName); - public string BillingPhoneNumber => _order.BillingAddress.PhoneNumber; + public string BillingPhoneNumber => WebUtility.HtmlEncode(_order.BillingAddress.PhoneNumber); - public string BillingEmail => _order.BillingAddress.Email; + public string BillingEmail => WebUtility.HtmlEncode(_order.BillingAddress.Email); - public string BillingFaxNumber => _order.BillingAddress.FaxNumber; + public string BillingFaxNumber => WebUtility.HtmlEncode(_order.BillingAddress.FaxNumber); - public string BillingCompany => _order.BillingAddress.Company; + public string BillingCompany => WebUtility.HtmlEncode(_order.BillingAddress.Company); - public string BillingVatNumber => _order.BillingAddress.VatNumber; + public string BillingVatNumber => WebUtility.HtmlEncode(_order.BillingAddress.VatNumber); - public string BillingAddress1 => _order.BillingAddress.Address1; + public string BillingAddress1 => WebUtility.HtmlEncode(_order.BillingAddress.Address1); - public string BillingAddress2 => _order.BillingAddress.Address2; + public string BillingAddress2 => WebUtility.HtmlEncode(_order.BillingAddress.Address2); - public string BillingCity => _order.BillingAddress.City; + public string BillingCity => WebUtility.HtmlEncode(_order.BillingAddress.City); public string BillingStateProvince { get; set; } - public string BillingZipPostalCode => _order.BillingAddress.ZipPostalCode; + public string BillingZipPostalCode => WebUtility.HtmlEncode(_order.BillingAddress.ZipPostalCode); public string BillingCountry { get; set; } @@ -90,29 +90,29 @@ public LiquidOrder(Order order, Customer customer, Language language, Currency c public string ShippingAdditionDescription => _order.ShippingOptionAttributeDescription; - public string ShippingAddressName => _order.ShippingAddress != null ? _order.ShippingAddress.Name : ""; + public string ShippingAddressName => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.Name : ""); - public string ShippingFirstName => _order.ShippingAddress != null ? _order.ShippingAddress.FirstName : ""; + public string ShippingFirstName => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.FirstName : ""); - public string ShippingLastName => _order.ShippingAddress != null ? _order.ShippingAddress.LastName : ""; + public string ShippingLastName => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.LastName : ""); - public string ShippingPhoneNumber => _order.ShippingAddress != null ? _order.ShippingAddress.PhoneNumber : ""; + public string ShippingPhoneNumber => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.PhoneNumber : ""); - public string ShippingEmail => _order.ShippingAddress != null ? _order.ShippingAddress.Email : ""; + public string ShippingEmail => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.Email : ""); - public string ShippingFaxNumber => _order.ShippingAddress != null ? _order.ShippingAddress.FaxNumber : ""; + public string ShippingFaxNumber => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.FaxNumber : ""); - public string ShippingCompany => _order.ShippingAddress != null ? _order.ShippingAddress.Company : ""; + public string ShippingCompany => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.Company : ""); - public string ShippingAddress1 => _order.ShippingAddress != null ? _order.ShippingAddress.Address1 : ""; + public string ShippingAddress1 => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.Address1 : ""); - public string ShippingAddress2 => _order.ShippingAddress != null ? _order.ShippingAddress.Address2 : ""; + public string ShippingAddress2 => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.Address2 : ""); - public string ShippingCity => _order.ShippingAddress != null ? _order.ShippingAddress.City : ""; + public string ShippingCity => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.City : ""); public string ShippingStateProvince { get; set; } - public string ShippingZipPostalCode => _order.ShippingAddress != null ? _order.ShippingAddress.ZipPostalCode : ""; + public string ShippingZipPostalCode => WebUtility.HtmlEncode(_order.ShippingAddress != null ? _order.ShippingAddress.ZipPostalCode : ""); public string ShippingCountry { get; set; } @@ -121,7 +121,7 @@ public LiquidOrder(Order order, Customer customer, Language language, Currency c public string PaymentMethod { get; set; } - public string VatNumber => _order.VatNumber; + public string VatNumber => WebUtility.HtmlEncode(_order.VatNumber); public string CreatedOn => _order.CreatedOnUtc.ToLocalTime().ToString("D"); @@ -137,9 +137,9 @@ public LiquidOrder(Order order, Customer customer, Language language, Currency c public string OrderNoteAttachmentUrl => $"{url}/download/ordernotefile/{_orderNote.Id}"; - public string VendorName => _vendor?.Name; + public string VendorName => WebUtility.HtmlEncode(_vendor?.Name); - public string VendorEmail => _vendor?.Email; + public string VendorEmail => WebUtility.HtmlEncode(_vendor?.Email); public ICollection OrderItems { get; } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendor.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendor.cs index dd578146a..5a2d80a52 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendor.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendor.cs @@ -1,5 +1,6 @@ using DotLiquid; using Grand.Domain.Vendors; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -13,25 +14,25 @@ public LiquidVendor(Vendor vendor) AdditionalTokens = new Dictionary(); } - public string Name => _vendor.Name; + public string Name => WebUtility.HtmlEncode(_vendor.Name); - public string Email => _vendor.Email; + public string Email => WebUtility.HtmlEncode(_vendor.Email); public string Description => _vendor.Description; - public string Address1 => _vendor.Address?.Address1; + public string Address1 => WebUtility.HtmlEncode(_vendor.Address?.Address1); - public string Address2 => _vendor.Address?.Address2; + public string Address2 => WebUtility.HtmlEncode(_vendor.Address?.Address2); - public string City => _vendor.Address?.City; + public string City => WebUtility.HtmlEncode(_vendor.Address?.City); - public string Company => _vendor.Address?.Company; + public string Company => WebUtility.HtmlEncode(_vendor.Address?.Company); - public string FaxNumber => _vendor.Address?.FaxNumber; + public string FaxNumber => WebUtility.HtmlEncode(_vendor.Address?.FaxNumber); - public string PhoneNumber => _vendor.Address?.PhoneNumber; + public string PhoneNumber => WebUtility.HtmlEncode(_vendor.Address?.PhoneNumber); - public string ZipPostalCode => _vendor.Address?.ZipPostalCode; + public string ZipPostalCode => WebUtility.HtmlEncode(_vendor.Address?.ZipPostalCode); public string StateProvince { get; set; } diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendorReview.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendorReview.cs index 9567534e8..aa33850c3 100644 --- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendorReview.cs +++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidVendorReview.cs @@ -1,5 +1,6 @@ using DotLiquid; using Grand.Domain.Vendors; +using System.Net; namespace Grand.Business.Messages.DotLiquidDrops; @@ -16,11 +17,11 @@ public LiquidVendorReview(Vendor vendor, VendorReview vendorReview) AdditionalTokens = new Dictionary(); } - public string VendorName => _vendor.Name; + public string VendorName => WebUtility.HtmlEncode(_vendor.Name); - public string Title => _vendorReview.Title; + public string Title => WebUtility.HtmlEncode(_vendorReview.Title); - public string ReviewText => _vendorReview.ReviewText; + public string ReviewText => WebUtility.HtmlEncode(_vendorReview.ReviewText); public IDictionary AdditionalTokens { get; set; } } \ No newline at end of file diff --git a/src/Business/Grand.Business.Messages/Services/CampaignMessageRenderer.cs b/src/Business/Grand.Business.Messages/Services/CampaignMessageRenderer.cs index b167a1a95..5da7988c7 100644 --- a/src/Business/Grand.Business.Messages/Services/CampaignMessageRenderer.cs +++ b/src/Business/Grand.Business.Messages/Services/CampaignMessageRenderer.cs @@ -34,7 +34,7 @@ public async Task Render(Campaign campaign, CampaignRecipient r liquidObject.Email = new LiquidEmail(recipient.QueuedEmailId); var body = LiquidExtensions.Render(liquidObject, campaign.Body); - var subject = LiquidExtensions.Render(liquidObject, campaign.Subject); + var subject = LiquidExtensions.RenderSubject(liquidObject, campaign.Subject); return new CampaignMessage(subject, body); } diff --git a/src/Business/Grand.Business.Messages/Services/CatalogMessageProviderService.cs b/src/Business/Grand.Business.Messages/Services/CatalogMessageProviderService.cs index 8edc41fbb..c73f25467 100644 --- a/src/Business/Grand.Business.Messages/Services/CatalogMessageProviderService.cs +++ b/src/Business/Grand.Business.Messages/Services/CatalogMessageProviderService.cs @@ -178,7 +178,7 @@ public virtual async Task SendProductQuestionMessage(Customer customer, Sto var subject = messageTemplate.GetTranslation(mt => mt.Subject, languageId); var body = messageTemplate.GetTranslation(mt => mt.Body, languageId); - var subjectReplaced = LiquidExtensions.Render(liquidObject, subject); + var subjectReplaced = LiquidExtensions.RenderSubject(liquidObject, subject); var bodyReplaced = LiquidExtensions.Render(liquidObject, body); await _mediator.Send(new InsertContactUsCommand { diff --git a/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs b/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs index 85b9093c7..c15e693ef 100644 --- a/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs +++ b/src/Business/Grand.Business.Messages/Services/MessageProviderService.cs @@ -49,7 +49,7 @@ public virtual async Task SendNotification(MessageTemplate messageTemplate, var email = new QueuedEmail(); liquidObject.Email = new LiquidEmail(email.Id); - var subjectReplaced = LiquidExtensions.Render(liquidObject, subject); + var subjectReplaced = LiquidExtensions.RenderSubject(liquidObject, subject); var bodyReplaced = LiquidExtensions.Render(liquidObject, body); var attachments = new List(); diff --git a/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidCustomerTests.cs b/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidCustomerTests.cs index 949321765..4d0878b93 100644 --- a/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidCustomerTests.cs +++ b/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidCustomerTests.cs @@ -1,3 +1,4 @@ +using Grand.Domain.Messages; using Grand.Business.Messages.DotLiquidDrops; using Grand.Domain.Common; using Grand.Domain.Customers; @@ -37,4 +38,28 @@ public void PasswordRecoveryURL_PanelArea_LeadsToThatPanel() Assert.AreEqual("https://shop.test/vendor/passwordrecovery/confirm?token=tok&email=a%2Bb%40example.com", liquid.PasswordRecoveryURL); } + + [TestMethod] + public void TextEnteredByCustomer_IsHtmlEncoded() + { + var customer = new Customer { Email = "a@example.com" }; + customer.UserFields.Add(new UserField + { Key = SystemCustomerFieldNames.FirstName, Value = "", StoreId = "" }); + var liquid = new LiquidCustomer(customer, new Store { Url = "https://shop.test/" }, null); + + Assert.AreEqual("<img src=x onerror=alert(1)>", liquid.FirstName); + Assert.DoesNotContain("<", liquid.FullName); + } + + [TestMethod] + public void RenderSubject_IsPlainText_WhileBodyIsEncoded() + { + var customer = new Customer(); + customer.UserFields.Add(new UserField + { Key = SystemCustomerFieldNames.FirstName, Value = "O'Brien & Co", StoreId = "" }); + var liquidObject = new LiquidObject { Customer = new LiquidCustomer(customer, new Store { Url = "https://shop.test/" }, null) }; + + Assert.AreEqual("Hi O'Brien & Co", LiquidExtensions.Render(liquidObject, "Hi {{Customer.FirstName}}")); + Assert.AreEqual("Hi O'Brien & Co", LiquidExtensions.RenderSubject(liquidObject, "Hi {{Customer.FirstName}}")); + } } diff --git a/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidOrderTests.cs b/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidOrderTests.cs index 22694f2f4..da52e0dd9 100644 --- a/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidOrderTests.cs +++ b/src/Tests/Grand.Business.Messages.Tests/DotLiquidDrops/LiquidOrderTests.cs @@ -39,4 +39,17 @@ public void PaymentDueDate_EmptyWhenNotOnCredit() Assert.AreEqual("", liquid.PaymentDueDate); Assert.AreEqual("", liquid.PurchaseOrderNumber); } + + [TestMethod] + public void BillingAndShippingText_IsHtmlEncoded() + { + var liquid = Liquid(new Order { + BillingAddress = new Grand.Domain.Common.Address { FirstName = "", LastName = "x" }, + ShippingAddress = new Grand.Domain.Common.Address { City = "" } + }); + + Assert.AreEqual("<b>", liquid.BillingFirstName); + Assert.AreEqual("<b> x", liquid.CustomerFullName); + Assert.AreEqual("<i>", liquid.ShippingCity); + } }