From 7c1591b4f50310598dcf5f0d027828c86b54aed9 Mon Sep 17 00:00:00 2001 From: support Date: Thu, 26 Nov 2026 20:14:16 +0100 Subject: [PATCH] Do not let an admin shut themselves out of the admin panel Saving the permission page with the admin's own groups unchecked took away the admin panel and the permission page from everyone who could give them back, and an allowed IP list for the admin area without the address the admin saves from shut them out at the next request. The permission page now refuses a save that leaves the saving admin without the admin panel or the permissions, and the general settings keep the old IP list, with a message, when the new one leaves out the admin's address; the other settings are still saved. --- .../Controllers/PermissionControllerTests.cs | 56 +++++++++++++++++- .../Controllers/PermissionController.cs | 10 ++++ .../Controllers/SettingController.cs | 15 ++--- .../App_Data/Resources/DefaultLanguage.xml | Bin 1839940 -> 1840928 bytes .../App_Data/Resources/Upgrade/en_250.xml | 6 ++ 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs index eb4341880..e7af7e0c6 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs @@ -1,5 +1,6 @@ using Grand.Business.Core.Interfaces.Common.Directory; using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Domain; using Grand.Domain.Customers; using Grand.Domain.Localization; using Grand.Domain.Permissions; @@ -7,7 +8,9 @@ using Grand.Web.Admin.Controllers; using Grand.Web.AdminShared.Models.Permissions; using Grand.Web.Common.Localization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -28,6 +31,10 @@ public void Init() { var workContextMock = new Mock(); workContextMock.Setup(w => w.WorkingLanguage).Returns(new Language { Id = "lang-1" }); + //the admin who saves the permissions belongs to the group "admins" + var admin = new Customer(); + admin.Groups.Add("admins"); + workContextMock.Setup(w => w.CurrentCustomer).Returns(admin); var contextAccessorMock = new Mock(); contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object); @@ -35,7 +42,20 @@ public void Init() .ReturnsAsync(new List()); _controller = new PermissionController(contextAccessorMock.Object, _permissionServiceMock.Object, - _groupServiceMock.Object, new Mock().Object); + _groupServiceMock.Object, new Mock().Object) { + TempData = new TempDataDictionary(new DefaultHttpContext(), new Mock().Object) + }; + } + + private void PermissionsOfTheAdmins() + { + _groupServiceMock.Setup(g => g.GetAllCustomerGroups("", 0, int.MaxValue, true)) + .ReturnsAsync(new PagedList(new[] { new CustomerGroup { Id = "admins", Active = true } }, 0, 10)); + _permissionServiceMock.Setup(p => p.GetAllPermissions()).ReturnsAsync(new List { + new() { SystemName = PermissionSystemName.AccessAdminPanel, CustomerGroups = { "admins" } }, + new() { SystemName = PermissionSystemName.Acl, CustomerGroups = { "admins" } }, + new() { SystemName = SystemName, CustomerGroups = { "admins" } } + }); } private void GroupExists() => @@ -148,4 +168,38 @@ public async Task PermissionsActionPost_UnknownPermission_IsNotFoundAndWritesNot Assert.IsInstanceOfType(result); VerifyNothingWritten(); } + + [TestMethod] + public async Task PermissionsSave_AdminTakesAwayOwnAdminPanel_IsRefusedAndWritesNothing() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary { + ["allow_admins"] = [PermissionSystemName.Acl, SystemName] + }); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task PermissionsSave_EverythingUnchecked_IsRefusedAndWritesNothing() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary()); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task PermissionsSave_AdminKeepsOwnAccess_SavesTheOtherChanges() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary { + ["allow_admins"] = [PermissionSystemName.AccessAdminPanel, PermissionSystemName.Acl] + }); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.Is(x => x.SystemName == SystemName)), Times.Once); + } } diff --git a/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs b/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs index 524d667ce..852ae838c 100644 --- a/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs @@ -115,6 +115,16 @@ public async Task PermissionsSave(IDictionary m var permissionRecords = await _permissionService.GetAllPermissions(); var customerGroups = await _groupService.GetAllCustomerGroups(showHidden: true); + //whoever saves keeps the admin panel and this page through one of their groups: nobody else may be left to give them back + var ownGroups = customerGroups.Where(g => g.Active && _contextAccessor.WorkContext.CurrentCustomer.Groups.Contains(g.Id)).ToList(); + bool KeepsOwn(string permission) => + ownGroups.Any(g => model.TryGetValue($"allow_{g.Id}", out var allowed) && allowed.Contains(permission)); + if (!KeepsOwn(PermissionSystemName.AccessAdminPanel) || !KeepsOwn(PermissionSystemName.Acl)) + { + Error(_translationService.GetResource("Admin.Configuration.Permissions.CantRemoveOwnAccess")); + return RedirectToAction("Index"); + } + foreach (var cr in customerGroups) { model.TryGetValue($"allow_{cr.Id}", out var permissionIds); diff --git a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs index 009d80ea9..f5ca407e3 100644 --- a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs @@ -714,13 +714,14 @@ public async Task GeneralCommon(GeneralCommonSettingsModel model) //security settings var securitySettings = await settingService.LoadSetting(storeScope); - securitySettings.AdminAreaAllowedIpAddresses ??= []; - securitySettings.AdminAreaAllowedIpAddresses.Clear(); - if (!string.IsNullOrEmpty(model.SecuritySettings.AdminAreaAllowedIpAddresses)) - foreach (var s in model.SecuritySettings.AdminAreaAllowedIpAddresses.Split([','], - StringSplitOptions.RemoveEmptyEntries)) - if (!string.IsNullOrWhiteSpace(s)) - securitySettings.AdminAreaAllowedIpAddresses.Add(s.Trim()); + var allowedIpAddresses = (model.SecuritySettings.AdminAreaAllowedIpAddresses ?? "") + .Split([','], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + //a list without the address the admin saves from would shut them out of the admin panel at once: keep the old one + var currentIp = HttpContext.Connection.RemoteIpAddress?.ToString(); + if (allowedIpAddresses.Count > 0 && !allowedIpAddresses.Contains(currentIp, StringComparer.OrdinalIgnoreCase)) + Error(string.Format(translationService.GetResource("Admin.Settings.GeneralCommon.AdminAreaAllowedIpAddresses.WithoutOwn"), currentIp)); + else + securitySettings.AdminAreaAllowedIpAddresses = allowedIpAddresses; await settingService.SaveSetting(securitySettings); diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml index 6793f972d64c97cd067088bbf9e96c9c46556975..f8d8411778c60e7c12984f3869127a9d05d74b98 100644 GIT binary patch delta 525 zcmX|-%S#(!6veN3jLEcSGR~w)CI-iL5i61k6^m97mu}i|+bEQ)%~0Y1QhmT$bDN zSl)fXT4HlSh{Wb8O$Dk@nX;6pBGqsfsSYj86Z_r?HeR~aXBD{K;YG(;2MydB@Ky1j z-u83f*If}tr z->8l0N=m$jPUP^z>=e_K?9w@2mC+; zkHBNlbztTLo0ok2MjBRF7M2#)Eo?1W)BW<;l%`Mk%(!Iw1q)Uw2=~J)7Qg8emT>S) zKQNu6X}Ur#n^k*39vcv|12G2>a{@6J5OV`D4-oTiFUaEycsxBJf}f-PLNGrN3jnbo h5DNjZFc6CXu_zFW0kJp`O8~JX5KC>p5G);V7yyESKl=ax diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml index c8225fc40..b661b5ca1 100644 --- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml +++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml @@ -2829,4 +2829,10 @@ Sign out on all devices + + You can't take away your own access to the admin panel or to the permissions. Another administrator can change them. + + + The allowed IP addresses were not changed: they don't include your address ({0}), so you would be shut out of the admin panel. +