Skip to content

Commit bfab2b4

Browse files
Show order and shipment notes to the customer the way merchandise return notes are shown (#1098)
1 parent 8fdaec8 commit bfab2b4

9 files changed

Lines changed: 87 additions & 22 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Grand.Infrastructure.Security;
2+
using Grand.Web.Extensions;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Moq;
5+
6+
namespace Grand.Web.Tests.Extensions;
7+
8+
[TestClass]
9+
public class NoteExtensionsTests
10+
{
11+
private readonly Mock<IHtmlSanitizationService> _sanitizer = new();
12+
13+
[TestMethod]
14+
public void CustomerNote_IsShownAsText()
15+
{
16+
_sanitizer.Setup(x => x.ContainsMarkup(It.IsAny<string>())).Returns(true);
17+
18+
var html = _sanitizer.Object.NoteHtml("<img src=x onerror=alert(1)>", createdByCustomer: true);
19+
20+
Assert.DoesNotContain("<img", html);
21+
Assert.Contains("&lt;img", html);
22+
}
23+
24+
[TestMethod]
25+
public void StaffNote_WithAllowedMarkup_IsKept()
26+
{
27+
_sanitizer.Setup(x => x.ContainsMarkup(It.IsAny<string>())).Returns(true);
28+
_sanitizer.Setup(x => x.ContainsDisallowedRichText(It.IsAny<string>())).Returns(false);
29+
30+
Assert.AreEqual("<a href=\"/form\">form</a>",
31+
_sanitizer.Object.NoteHtml("<a href=\"/form\">form</a>", createdByCustomer: false));
32+
}
33+
34+
[TestMethod]
35+
public void StaffNote_WithDisallowedMarkup_IsShownAsText()
36+
{
37+
_sanitizer.Setup(x => x.ContainsMarkup(It.IsAny<string>())).Returns(true);
38+
_sanitizer.Setup(x => x.ContainsDisallowedRichText(It.IsAny<string>())).Returns(true);
39+
40+
var html = _sanitizer.Object.NoteHtml("<script>x</script>", createdByCustomer: false);
41+
42+
Assert.DoesNotContain("<script", html);
43+
}
44+
}

‎src/Tests/Grand.Web.Tests/Features/Handlers/Orders/GetOrderDetailsHandlerApprovalTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Grand.Domain.Localization;
1313
using Grand.Domain.Orders;
1414
using Grand.Domain.Tax;
15+
using Grand.Infrastructure.Security;
1516
using Grand.Mediator;
1617
using Grand.Web.Common.Localization;
1718
using Grand.Web.Features.Handlers.Orders;
@@ -55,7 +56,7 @@ public void Init()
5556
new Mock<IOrderStatusService>().Object, new Mock<ICurrencyService>().Object,
5657
new Mock<IMediator>().Object, new CatalogSettings(), new OrderSettings(), new PdfSettings(),
5758
new TaxSettings(), new Mock<IEnumTranslationService>().Object, approvals.Object,
58-
new CustomerSettings());
59+
new CustomerSettings(), new Mock<IHtmlSanitizationService>().Object);
5960
}
6061

6162
private Task<OrderDetailsModel> Details(string customerId)

‎src/Tests/Grand.Web.Tests/Features/Handlers/Orders/GetOrderDetailsHandlerPaymentTermsTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Grand.Domain.Orders;
1414
using Grand.Domain.Payments;
1515
using Grand.Domain.Tax;
16+
using Grand.Infrastructure.Security;
1617
using Grand.Mediator;
1718
using Grand.Web.Common.Localization;
1819
using Grand.Web.Features.Handlers.Orders;
@@ -50,7 +51,7 @@ public void Init()
5051
new Mock<IOrderStatusService>().Object, new Mock<ICurrencyService>().Object,
5152
new Mock<IMediator>().Object, new CatalogSettings(), new OrderSettings(), new PdfSettings(),
5253
new TaxSettings(), new Mock<IEnumTranslationService>().Object, new Mock<IOrderApprovalService>().Object,
53-
new CustomerSettings());
54+
new CustomerSettings(), new Mock<IHtmlSanitizationService>().Object);
5455
}
5556

5657
private Task<OrderDetailsModel> Details(Order order)

‎src/Tests/Grand.Web.Tests/Features/Handlers/Orders/GetOrderDetailsHandlerRecurringTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Grand.Domain.Localization;
1313
using Grand.Domain.Orders;
1414
using Grand.Domain.Tax;
15+
using Grand.Infrastructure.Security;
1516
using Grand.Mediator;
1617
using Grand.Web.Common.Localization;
1718
using Grand.Web.Features.Handlers.Orders;
@@ -54,7 +55,7 @@ public void Init()
5455
new Mock<IOrderStatusService>().Object, new Mock<ICurrencyService>().Object,
5556
new Mock<IMediator>().Object, new CatalogSettings(), new OrderSettings(), new PdfSettings(),
5657
new TaxSettings(), new Mock<IEnumTranslationService>().Object, new Mock<IOrderApprovalService>().Object,
57-
_customerSettings);
58+
_customerSettings, new Mock<IHtmlSanitizationService>().Object);
5859
}
5960

6061
private Task<OrderDetailsModel> Details(Order order, string viewerId = "buyer", string storeId = "s1")

‎src/Tests/Grand.Web.Tests/Features/Handlers/Orders/GetOrderDetailsHandlerUnitsTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Grand.Domain.Orders;
1414
using Grand.Domain.Shipping;
1515
using Grand.Domain.Tax;
16+
using Grand.Infrastructure.Security;
1617
using Grand.Mediator;
1718
using Grand.Web.Common.Localization;
1819
using Grand.Web.Features.Handlers.Orders;
@@ -56,7 +57,7 @@ public void Init()
5657
new Mock<IOrderStatusService>().Object, new Mock<ICurrencyService>().Object,
5758
new Mock<IMediator>().Object, new CatalogSettings(), new OrderSettings(), new PdfSettings(),
5859
new TaxSettings(), new Mock<IEnumTranslationService>().Object, new Mock<IOrderApprovalService>().Object,
59-
new CustomerSettings());
60+
new CustomerSettings(), new Mock<IHtmlSanitizationService>().Object);
6061
}
6162

6263
private void Shipped(DateTime? shippedDateUtc, ShipmentItem item)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Grand.Infrastructure.Security;
2+
using Grand.SharedKernel.Extensions;
3+
4+
namespace Grand.Web.Extensions;
5+
6+
public static class NoteExtensions
7+
{
8+
/// <summary>
9+
/// What the customer typed is text, as the e-mails and the panels show it. A staff note is text too, unless it
10+
/// is markup the rich-text allowlist keeps whole, e.g. a link to a form: such notes were shown as HTML before
11+
/// </summary>
12+
public static string NoteHtml(this IHtmlSanitizationService htmlSanitizationService, string note,
13+
bool createdByCustomer)
14+
{
15+
if (!createdByCustomer && htmlSanitizationService.ContainsMarkup(note) &&
16+
!htmlSanitizationService.ContainsDisallowedRichText(note))
17+
return note;
18+
19+
return FormatText.ConvertText(note);
20+
}
21+
}

‎src/Web/Grand.Web/Features/Handlers/Orders/GetMerchandiseReturnDetailsHandler.cs‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,9 @@ private async Task PrepareMerchandiseReturnNotes(GetMerchandiseReturnDetails req
256256
Id = merchandiseReturnNote.Id,
257257
MerchandiseReturnId = merchandiseReturnNote.MerchandiseReturnId,
258258
HasDownload = !string.IsNullOrEmpty(merchandiseReturnNote.DownloadId),
259-
Note = NoteHtml(merchandiseReturnNote),
259+
Note = _htmlSanitizationService.NoteHtml(merchandiseReturnNote.Note, merchandiseReturnNote.CreatedByCustomer),
260260
CreatedByCustomer = merchandiseReturnNote.CreatedByCustomer,
261261
CreatedOn = _dateTimeService.ConvertToUserTime(merchandiseReturnNote.CreatedOnUtc, DateTimeKind.Utc)
262262
});
263263
}
264-
265-
/// <summary>
266-
/// What the customer typed is text, as the e-mails and the panels show it. A staff note is text too, unless it
267-
/// is markup the rich-text allowlist keeps whole, e.g. a link to a form: such notes were shown as HTML before
268-
/// </summary>
269-
private string NoteHtml(MerchandiseReturnNote note)
270-
{
271-
if (!note.CreatedByCustomer && _htmlSanitizationService.ContainsMarkup(note.Note) &&
272-
!_htmlSanitizationService.ContainsDisallowedRichText(note.Note))
273-
return note.Note;
274-
275-
return FormatText.ConvertText(note.Note);
276-
}
277264
}

‎src/Web/Grand.Web/Features/Handlers/Orders/GetOrderDetailsHandler.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Grand.Domain.Payments;
1818
using Grand.Domain.Shipping;
1919
using Grand.Domain.Tax;
20+
using Grand.Infrastructure.Security;
2021
using Grand.Web.Common.Localization;
2122
using Grand.Web.Extensions;
2223
using Grand.Web.Features.Models.Common;
@@ -31,6 +32,7 @@ public class GetOrderDetailsHandler : IRequestHandler<GetOrderDetails, OrderDeta
3132
{
3233
private readonly CatalogSettings _catalogSettings;
3334
private readonly CustomerSettings _customerSettings;
35+
private readonly IHtmlSanitizationService _htmlSanitizationService;
3436
private readonly ICurrencyService _currencyService;
3537
private readonly IDateTimeService _dateTimeService;
3638
private readonly IGiftVoucherService _giftVoucherService;
@@ -72,8 +74,10 @@ public GetOrderDetailsHandler(
7274
TaxSettings taxSettings,
7375
IEnumTranslationService enumTranslationService,
7476
IOrderApprovalService orderApprovalService,
75-
CustomerSettings customerSettings)
77+
CustomerSettings customerSettings,
78+
IHtmlSanitizationService htmlSanitizationService)
7679
{
80+
_htmlSanitizationService = htmlSanitizationService;
7781
_customerSettings = customerSettings;
7882
_orderApprovalService = orderApprovalService;
7983
_dateTimeService = dateTimeService;
@@ -403,7 +407,7 @@ private async Task PrepareOrderNotes(GetOrderDetails request, OrderDetailsModel
403407
Id = orderNote.Id,
404408
OrderId = orderNote.OrderId,
405409
HasDownload = !string.IsNullOrEmpty(orderNote.DownloadId),
406-
Note = orderNote.Note,
410+
Note = _htmlSanitizationService.NoteHtml(orderNote.Note, orderNote.CreatedByCustomer),
407411
CreatedOn = _dateTimeService.ConvertToUserTime(orderNote.CreatedOnUtc, DateTimeKind.Utc)
408412
});
409413
}

‎src/Web/Grand.Web/Features/Handlers/Orders/GetShipmentDetailsHandler.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Grand.Business.Core.Interfaces.Common.Directory;
66
using Grand.Domain.Catalog;
77
using Grand.Domain.Shipping;
8+
using Grand.Infrastructure.Security;
9+
using Grand.Web.Extensions;
810
using Grand.Web.Features.Models.Common;
911
using Grand.Web.Features.Models.Orders;
1012
using Grand.Web.Models.Orders;
@@ -19,6 +21,7 @@ public class GetShipmentDetailsHandler : IRequestHandler<GetShipmentDetails, Shi
1921
private readonly IMediator _mediator;
2022
private readonly IProductService _productService;
2123
private readonly IProductUnitService _productUnitService;
24+
private readonly IHtmlSanitizationService _htmlSanitizationService;
2225
private readonly IShipmentService _shipmentService;
2326
private readonly ShippingProviderSettings _shippingProviderSettings;
2427
private readonly IShippingService _shippingService;
@@ -34,8 +37,10 @@ public GetShipmentDetailsHandler(
3437
ShippingSettings shippingSettings,
3538
CatalogSettings catalogSettings,
3639
ShippingProviderSettings shippingProviderSettings,
37-
IProductUnitService productUnitService)
40+
IProductUnitService productUnitService,
41+
IHtmlSanitizationService htmlSanitizationService)
3842
{
43+
_htmlSanitizationService = htmlSanitizationService;
3944
_productUnitService = productUnitService;
4045
_dateTimeService = dateTimeService;
4146
_productService = productService;
@@ -147,7 +152,7 @@ public async Task<ShipmentDetailsModel> Handle(GetShipmentDetails request, Cance
147152
Id = shipmentNote.Id,
148153
ShipmentId = shipmentNote.ShipmentId,
149154
HasDownload = !string.IsNullOrEmpty(shipmentNote.DownloadId),
150-
Note = shipmentNote.Note,
155+
Note = _htmlSanitizationService.NoteHtml(shipmentNote.Note, shipmentNote.CreatedByCustomer),
151156
CreatedOn = _dateTimeService.ConvertToUserTime(shipmentNote.CreatedOnUtc, DateTimeKind.Utc)
152157
});
153158
return notes;

0 commit comments

Comments
 (0)