Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Force VendorId on InsertProductModel/UpdateProductModel for vendor scope (ARCH-001 Phase 1 Task 10)
KrzysztofPajakc1eb4b0cc652
diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.csindex daf3706d3..367f93b1c 100644--- a/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs@@ -24,7 +24,10 @@ using Grand.Domain.Stores;using Grand.Domain.Tax;using Grand.Domain.Vendors;using Grand.Infrastructure;+using Grand.Infrastructure.Mapper;+using Grand.Mapping;using Grand.Web.AdminShared.Interfaces;+using Grand.Web.AdminShared.Mapper;using Grand.Web.AdminShared.Models.Catalog;using Grand.Web.AdminShared.Services;using Grand.Web.Common.Localization;@@ -56,6 +59,9 @@ public class ProductViewModelServiceTests[TestInitialize]public void Setup(){+ var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile<ProductProfile>(); });+ AutoMapperConfig.Init(mapperConfig);+_discountServiceMock = new Mock<IDiscountService>();_enumTranslationServiceMock = new Mock<IEnumTranslationService>();_groupServiceMock = new Mock<IGroupService>();@@ -413,4 +419,58 @@ public class ProductViewModelServiceTestsAssert.IsTrue(model.AvailableVendors.Any(x => x.Value == "vendor1"),"Admin/Store should keep offering a vendor picker.");}++ [TestMethod]+ public async Task InsertProductModel_VendorScope_ForcesVendorId()+ {+ _scopeMock.Setup(s => s.DefaultVendorId).Returns("vendor1");+ var model = new ProductModel { Name = "Test product" };++ var product = await _productViewModelService.InsertProductModel(model);++ Assert.AreEqual("vendor1", product.VendorId,+ "Vendor's original InsertProductModel always set product.VendorId = CurrentVendor.Id - without " ++ "this, a vendor-created product would map whatever (usually blank) VendorId the model carries " ++ "and end up orphaned, inaccessible to the vendor who just created it.");+ }++ [TestMethod]+ public async Task InsertProductModel_GlobalScope_DoesNotForceVendorId()+ {+ // Default Setup() scope: Admin's Global scope (DefaultVendorId null).+ var model = new ProductModel { Name = "Test product", VendorId = "vendor2" };++ var product = await _productViewModelService.InsertProductModel(model);++ Assert.AreEqual("vendor2", product.VendorId,+ "Admin/Store should keep whatever vendor ownership the create form assigns.");+ }++ [TestMethod]+ public async Task UpdateProductModel_VendorScope_ForcesVendorId_PreventsCrossVendorReassignment()+ {+ _scopeMock.Setup(s => s.DefaultVendorId).Returns("vendor1");+ var product = new Product { Id = "p1", VendorId = "vendor1" };+ var model = new ProductModel { Id = "p1", VendorId = "vendor-attacker-supplied" };++ var result = await _productViewModelService.UpdateProductModel(product, model);++ Assert.AreEqual("vendor1", result.VendorId,+ "AdminShared's shared ProductProfile does not ignore ProductModel.VendorId -> Product.VendorId " ++ "(Admin's edit form legitimately reassigns vendor ownership); without re-forcing it after mapping, " ++ "a vendor could reassign their own product to a different vendor id via a tampered VendorId field.");+ }++ [TestMethod]+ public async Task UpdateProductModel_GlobalScope_UsesModelVendorId()+ {+ // Default Setup() scope: Admin's Global scope (DefaultVendorId null).+ var product = new Product { Id = "p1", VendorId = "vendor1" };+ var model = new ProductModel { Id = "p1", VendorId = "vendor2" };++ var result = await _productViewModelService.UpdateProductModel(product, model);++ Assert.AreEqual("vendor2", result.VendorId,+ "Admin should keep being able to reassign a product's vendor ownership via the edit form.");+ }}diff --git a/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.csindex 8706d44f1..d072b167a 100644--- a/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs+++ b/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs@@ -712,6 +712,16 @@ public class ProductViewModelService(//productvar product = model.ToEntity(dateTimeService);+ // VendorId: forced onto new products when scope.DefaultVendorId is set - mirrors Vendor's original+ // InsertProductModel, which always set product.VendorId = CurrentVendor?.Id explicitly and ignored+ // VendorId in its AutoMapper profile for exactly this reason. AdminShared's shared ProductProfile+ // does *not* ignore ProductModel.VendorId -> Product.VendorId (Admin's create form legitimately+ // assigns vendor ownership), so without this, a vendor host would map whatever (usually blank)+ // VendorId the model carries, leaving newly-created products belonging to no vendor at all -+ // orphaned and inaccessible to the vendor who just created them once VendorProductDataScope.HasAccess+ // gates on VendorId equality.+ if (scope.DefaultVendorId is not null) product.VendorId = scope.DefaultVendorId;+//discountsvar allDiscounts = await discountService.GetDiscountsQuery(DiscountType.AssignedToSkus, model.StoreId);foreach (Discount discount in allDiscounts)@@ -751,6 +761,16 @@ public class ProductViewModelService(//productproduct = model.ToEntity(product, dateTimeService);product.AutoAddRequiredProducts = model.AutoAddRequiredProducts;++ // VendorId: re-forced to scope.DefaultVendorId (when set) after mapping, not merely left alone -+ // AdminShared's shared ProductProfile does not ignore ProductModel.VendorId -> Product.VendorId, so+ // without this, mapping a caller-supplied model onto the existing entity would let a vendor+ // reassign their own product to a different vendor id via a tampered/mass-assigned VendorId field.+ // Vendor's original UpdateProductModel never touched VendorId at all (its own AutoMapper profile+ // ignores it), relying on the pre-loaded entity's existing value - forcing it here is strictly+ // equivalent for legitimately-owned products (already gated by scope.HasAccess before reaching this+ // method) while also closing that mass-assignment gap for Admin/Store's shared profile.+ if (scope.DefaultVendorId is not null) product.VendorId = scope.DefaultVendorId;product.Locales = await seNameService.TranslationSeNameProperties(model.Locales, product, x => x.Name);product.SeName = await seNameService.ValidateSeName(product, model.SeName, product.Name, true);//discounts