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
Fix cross-store product export IDOR in ExportExcelAll
KrzysztofPajaka414f972dd1b
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.csindex c783cacd7..50c839a6d 100644--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs@@ -2774,9 +2774,8 @@ public class BaseProductControllerTests{var model = new ProductListModel();var products = new List<Product> { new() { Id = "p1" } };- // No scope filtering here: productViewModelService.PrepareProducts is host-specific and already- // returns only the caller's products (Vendor's implementation constrains by CurrentVendor.Id- // internally) - the controller trusts it, same as ProductList trusts PrepareProductsModel.+ // vendorId is forced inside PrepareProducts itself; storeId is forced at the call site below,+ // same as every other PrepareProductsModel/PrepareProducts caller in this controller._productViewModelServiceMock.Setup(s => s.PrepareProducts(model)).ReturnsAsync(products);var exportManagerMock = new Mock<IExportManager<Product>>();exportManagerMock.Setup(e => e.Export(products)).ReturnsAsync([1, 2, 3]);@@ -2790,6 +2789,38 @@ public class BaseProductControllerTestsCollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, file.FileContents);}+ [TestMethod]+ public async Task ExportExcelAll_UsesScopeDefaultStoreId()+ {+ // Regression test: found during a post-Phase-2 tenant-isolation audit that this action was the+ // one PrepareProducts/PrepareProductsModel caller in the controller NOT forcing+ // model.SearchStoreId = scope.DefaultStoreId, letting a Store-scoped user export other stores'+ // products by posting an arbitrary SearchStoreId.+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");+ var model = new ProductListModel();+ _productViewModelServiceMock.Setup(s => s.PrepareProducts(model)).ReturnsAsync([]);+ var exportManagerMock = new Mock<IExportManager<Product>>();+ exportManagerMock.Setup(e => e.Export(It.IsAny<IEnumerable<Product>>())).ReturnsAsync([1, 2, 3]);++ await _controller.ExportExcelAll(model, exportManagerMock.Object);++ Assert.AreEqual("store-1", model.SearchStoreId);+ }++ [TestMethod]+ public async Task ExportExcelAll_NoDefaultStoreId_DoesNotOverrideModelSearchStoreId()+ {+ _scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);+ var model = new ProductListModel { SearchStoreId = "explicit" };+ _productViewModelServiceMock.Setup(s => s.PrepareProducts(model)).ReturnsAsync([]);+ var exportManagerMock = new Mock<IExportManager<Product>>();+ exportManagerMock.Setup(e => e.Export(It.IsAny<IEnumerable<Product>>())).ReturnsAsync([1, 2, 3]);++ await _controller.ExportExcelAll(model, exportManagerMock.Object);++ Assert.AreEqual("explicit", model.SearchStoreId);+ }+[TestMethod]public async Task ExportExcelAll_ExportThrows_ReturnsRedirectToList(){diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.csindex 5250db92d..1b9756218 100644--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs@@ -1848,10 +1848,13 @@ public abstract class BaseProductController(public async Task<IActionResult> ExportExcelAll(ProductListModel model,[FromServices] IExportManager<Product> exportManager){- // No explicit scope filter needed here: productViewModelService is host-specific (Admin's- // implementation returns all products matching the search model; Vendor's PrepareProducts always- // constrains the SearchProducts call to WorkContext.CurrentVendor.Id regardless of what's in- // model), so scoping is already enforced inside the polymorphic call, same as ProductList above.+ // vendorId is forced inside PrepareProducts itself (scope.DefaultVendorId ?? model.SearchVendorId),+ // but storeId is not - every other caller of PrepareProductsModel/PrepareProducts in this file+ // forces it here, at the call site, the same way ProductList does above. This one was missing it:+ // a Store-scoped user could POST an arbitrary (or blank) SearchStoreId and export every store's+ // products, not just their own. Found during a post-Phase-2 tenant-isolation audit.+ if (scope.DefaultStoreId is not null) model.SearchStoreId = scope.DefaultStoreId;+var products = await productViewModelService.PrepareProducts(model);try{