From 0afa610d8f2c4659a5833cdebd31d6430fc0eb77 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 18:58:32 +0100 Subject: [PATCH 1/4] Replace the sign-in token on sign-out, so a copied cookie stops working --- .../CookieAuthenticationServiceTests.cs | 41 +++++++++++++++++++ .../CookieAuthenticationService.cs | 35 +++++++++++----- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs index 7c1499c65..6f1ee7add 100644 --- a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs +++ b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs @@ -385,6 +385,47 @@ public async Task SignOut_EndsOidcSession() _oidcSessionService.Verify(x => x.End("session-1", OidcSessionEndReason.SignOut), Times.Once); } + //a copy of the cookie taken before the sign-out is not accepted after it + [TestMethod] + public async Task SignOut_ReplacesThePasswordToken() + { + var customer = RegisteredCustomer(); + _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) + .ReturnsAsync(Ticket()); + + await _cookieAuthService.SignOut(); + + _customerServiceMock.Verify(x => x.UpdateUserField(customer, SystemCustomerFieldNames.PasswordToken, + It.Is(v => !string.IsNullOrEmpty(v) && v != "token"), It.IsAny()), Times.Once); + } + + //a cookie whose token was replaced already does not end the sign-ins made after it + [TestMethod] + public async Task SignOut_OldCookie_PasswordTokenKept() + { + var customer = RegisteredCustomer(); + customer.UserFields.Single(f => f.Key == SystemCustomerFieldNames.PasswordToken).Value = "newer"; + _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) + .ReturnsAsync(Ticket()); + + await _cookieAuthService.SignOut(); + + _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken, + It.IsAny(), It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task SignOut_NotSignedIn_PasswordTokenKept() + { + _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) + .ReturnsAsync(AuthenticateResult.NoResult()); + + await _cookieAuthService.SignOut(); + + _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken, + It.IsAny(), It.IsAny()), Times.Never); + } + [TestMethod] public async Task HasOidcSession_SessionCookie_True() { diff --git a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs index 8cf36060b..7fc161035 100644 --- a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs +++ b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs @@ -213,6 +213,8 @@ public virtual async Task SignOut() var oidcSessionId = authenticateResult?.Succeeded == true ? OidcSessionId(authenticateResult.Principal) : null; if (!string.IsNullOrEmpty(oidcSessionId)) await EndOidcSession(oidcSessionId); + if (authenticateResult?.Succeeded == true) + await ReplacePasswordToken(authenticateResult.Principal); await _httpContextAccessor.HttpContext.SignOutAsync(GrandCookieAuthenticationDefaults .AuthenticationScheme); @@ -291,16 +293,8 @@ private async Task RetrieveCustomer(ClaimsPrincipal principal) private async Task IsValidCustomer(Customer customer, ClaimsPrincipal principal) { var passwordToken = customer.GetUserFieldFromEntity(SystemCustomerFieldNames.PasswordToken); - if (!string.IsNullOrEmpty(passwordToken)) - { - var tokenClaim = principal - .FindFirst(claim => - claim.Type == ClaimTypes.UserData && - claim.Issuer.Equals(_securityConfig.CookieClaimsIssuer, StringComparison.InvariantCultureIgnoreCase)); - - if (tokenClaim == null || tokenClaim.Value != passwordToken) - return false; - } + if (!string.IsNullOrEmpty(passwordToken) && PasswordTokenOf(principal) != passwordToken) + return false; if (!customer.Active || customer.Deleted || !await _groupService.IsRegistered(customer)) return false; @@ -319,6 +313,27 @@ private async Task IsValidCustomer(Customer customer, ClaimsPrincipal prin return (await _externalSignInPolicy.PasswordSignIn(customer, SignInSurface.AdminPanel)).Allowed; } + /// + /// The cookie is a ticket the server keeps no record of, so a copy taken before the sign-out would still be + /// accepted: the token it carries is replaced, which ends every sign-in of the account, on other devices too + /// + private async Task ReplacePasswordToken(ClaimsPrincipal principal) + { + var customer = await RetrieveCustomer(principal); + var passwordToken = customer?.GetUserFieldFromEntity(SystemCustomerFieldNames.PasswordToken); + //an old cookie, whose token was replaced already, does not end the sign-ins made after it + if (string.IsNullOrEmpty(passwordToken) || PasswordTokenOf(principal) != passwordToken) + return; + + await _customerService.UpdateUserField(customer, SystemCustomerFieldNames.PasswordToken, + Guid.NewGuid().ToString()); + } + + private string PasswordTokenOf(ClaimsPrincipal principal) => + principal.FindFirst(claim => + claim.Type == ClaimTypes.UserData && + claim.Issuer.Equals(_securityConfig.CookieClaimsIssuer, StringComparison.InvariantCultureIgnoreCase))?.Value; + private async Task EndOidcSession(string sessionId) { try From 69db8fdea008dbb4eec7dabd70eaa967d24e83ff Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 21:33:44 +0100 Subject: [PATCH 2/4] Sign out only by POST; ending the sign-ins on other devices is a customer setting --- .../Customers/CustomerSettings.cs | 5 +++++ .../HomeControllerAttributeTests.cs | 11 ++++++++++ .../CookieAuthenticationServiceTests.cs | 19 ++++++++++++++++- .../StateChangingActionVerbTests.cs | 1 + .../Areas/Admin/Views/Login/Index.cshtml | 8 ++++--- .../Customer.TabCustomerSecurity.cshtml | 5 +++++ .../Admin/Views/Shared/_AdminLayout.cshtml | 4 +++- .../Controllers/BaseHomeController.cs | 2 ++ .../Models/Settings/CustomerSettingsModel.cs | 3 +++ .../CookieAuthenticationService.cs | 2 +- .../Areas/Store/Views/Login/Index.cshtml | 8 ++++--- .../Customer.TabCustomerSecurity.cshtml | 5 +++++ .../Store/Views/Shared/_StoreLayout.cshtml | 4 +++- .../Areas/Vendor/Views/Login/Index.cshtml | 8 ++++--- .../Vendor/Views/Shared/_VendorLayout.cshtml | 4 +++- .../App_Data/Resources/DefaultLanguage.xml | Bin 1839674 -> 1839940 bytes .../App_Data/Resources/Upgrade/en_250.xml | 3 +++ .../Controllers/AccountController.cs | 2 ++ src/Web/Grand.Web/Views/Account/Login.cshtml | 8 ++++--- .../Views/Shared/Partials/HeaderLinks.cshtml | 20 +++++++++++------- 20 files changed, 97 insertions(+), 25 deletions(-) diff --git a/src/Core/Grand.Domain/Customers/CustomerSettings.cs b/src/Core/Grand.Domain/Customers/CustomerSettings.cs index 3d8cb525c..b074e001c 100644 --- a/src/Core/Grand.Domain/Customers/CustomerSettings.cs +++ b/src/Core/Grand.Domain/Customers/CustomerSettings.cs @@ -170,6 +170,11 @@ public class CustomerSettings : ISettings /// public int FailedPasswordLockoutMinutes { get; set; } + /// + /// Gets or sets a value indicating whether signing out ends every sign-in of the account, on other devices too + /// + public bool SignOutOnAllDevices { get; set; } + /// /// Gets or sets a number of days for password expiration /// diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/HomeControllerAttributeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/HomeControllerAttributeTests.cs index 89171811a..a9d24dcc4 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/HomeControllerAttributeTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/HomeControllerAttributeTests.cs @@ -42,6 +42,17 @@ public void HasAutoValidateAntiforgeryTokenAttribute() Assert.AreEqual(1, attr.Length); } + //a GET could be triggered by a link or an image on another site + [TestMethod] + public void Logout_AcceptsPostOnly() + { + var verbs = typeof(HomeController).GetMethod(nameof(HomeController.Logout))! + .GetCustomAttributes(typeof(HttpPostAttribute), inherit: true); + Assert.AreEqual(1, verbs.Length); + Assert.AreEqual(0, typeof(HomeController).GetMethod(nameof(HomeController.Logout))! + .GetCustomAttributes(typeof(HttpGetAttribute), inherit: true).Length); + } + [TestMethod] public void HasAuthorizeMenuAttribute() { diff --git a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs index 6f1ee7add..ee47b3428 100644 --- a/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs +++ b/src/Tests/Grand.Web.Common.Tests/Security/Authentication/CookieAuthenticationServiceTests.cs @@ -387,8 +387,9 @@ public async Task SignOut_EndsOidcSession() //a copy of the cookie taken before the sign-out is not accepted after it [TestMethod] - public async Task SignOut_ReplacesThePasswordToken() + public async Task SignOut_OnAllDevices_ReplacesThePasswordToken() { + _customerSettings.SignOutOnAllDevices = true; var customer = RegisteredCustomer(); _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) .ReturnsAsync(Ticket()); @@ -403,6 +404,7 @@ public async Task SignOut_ReplacesThePasswordToken() [TestMethod] public async Task SignOut_OldCookie_PasswordTokenKept() { + _customerSettings.SignOutOnAllDevices = true; var customer = RegisteredCustomer(); customer.UserFields.Single(f => f.Key == SystemCustomerFieldNames.PasswordToken).Value = "newer"; _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) @@ -414,9 +416,24 @@ public async Task SignOut_OldCookie_PasswordTokenKept() It.IsAny(), It.IsAny()), Times.Never); } + //by default the other devices stay signed in + [TestMethod] + public async Task SignOut_ThisDeviceOnly_PasswordTokenKept() + { + RegisteredCustomer(); + _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) + .ReturnsAsync(Ticket()); + + await _cookieAuthService.SignOut(); + + _customerServiceMock.Verify(x => x.UpdateUserField(It.IsAny(), SystemCustomerFieldNames.PasswordToken, + It.IsAny(), It.IsAny()), Times.Never); + } + [TestMethod] public async Task SignOut_NotSignedIn_PasswordTokenKept() { + _customerSettings.SignOutOnAllDevices = true; _authServiceMock.Setup(x => x.AuthenticateAsync(It.IsAny(), GrandCookieAuthenticationDefaults.AuthenticationScheme)) .ReturnsAsync(AuthenticateResult.NoResult()); diff --git a/src/Tests/Grand.Web.Tests/Controllers/StateChangingActionVerbTests.cs b/src/Tests/Grand.Web.Tests/Controllers/StateChangingActionVerbTests.cs index 26b98c10b..e66154192 100644 --- a/src/Tests/Grand.Web.Tests/Controllers/StateChangingActionVerbTests.cs +++ b/src/Tests/Grand.Web.Tests/Controllers/StateChangingActionVerbTests.cs @@ -12,6 +12,7 @@ public class StateChangingActionVerbTests [TestMethod] [DataRow(typeof(OrderController), nameof(OrderController.CancelOrder))] [DataRow(typeof(ShoppingCartController), nameof(ShoppingCartController.ClearCart))] + [DataRow(typeof(AccountController), nameof(AccountController.Logout))] public void Action_AcceptsPostOnly(Type controller, string action) { var method = controller.GetMethod(action, BindingFlags.Public | BindingFlags.Instance)!; diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Login/Index.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Login/Index.cshtml index d4b214977..79a739abc 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Login/Index.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Login/Index.cshtml @@ -69,9 +69,11 @@ @if (Model.SignedInWithoutAccess) {
@Loc["Admin.Login.SignedInWithoutAccess"]
- - @Loc["Admin.Header.Logout"] - +
+ +
} @if (Model.ExternalSignInUnavailable) { diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml index e0eb2be83..69396390e 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml @@ -81,3 +81,8 @@ + +
+ +
+
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/_AdminLayout.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/_AdminLayout.cshtml index cc4e269b5..91a9bf528 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/_AdminLayout.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/_AdminLayout.cshtml @@ -175,7 +175,9 @@ }
  • - + @* signing out is a POST, so a link on another site cannot sign staff out *@ +
    +
    @Loc["Admin.Header.Logout"]
  • diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseHomeController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseHomeController.cs index 03a2e8410..cbe6a9668 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BaseHomeController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseHomeController.cs @@ -61,6 +61,8 @@ public async Task GetStatesByCountryId(string countryId, bool? ad return Json(result); } + //a POST, so a link or an image on another site cannot sign staff out + [HttpPost] public async Task Logout([FromServices] IProviderSignOut providerSignOut, [FromServices] IAuditEventRecorder auditEventRecorder) { diff --git a/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs b/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs index f641b2902..62c966cca 100644 --- a/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs +++ b/src/Web/Grand.Web.AdminShared/Models/Settings/CustomerSettingsModel.cs @@ -80,6 +80,9 @@ public class CustomersSettingsModel : BaseModel [GrandResourceDisplayName("Admin.Settings.Customer.FailedPasswordLockoutMinutes")] public int FailedPasswordLockoutMinutes { get; set; } + [GrandResourceDisplayName("Admin.Settings.Customer.SignOutOnAllDevices")] + public bool SignOutOnAllDevices { get; set; } + [GrandResourceDisplayName("Admin.Settings.Customer.NewsletterEnabled")] public bool NewsletterEnabled { get; set; } diff --git a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs index 7fc161035..f1f15250e 100644 --- a/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs +++ b/src/Web/Grand.Web.Common/Security/Authentication/CookieAuthenticationService.cs @@ -213,7 +213,7 @@ public virtual async Task SignOut() var oidcSessionId = authenticateResult?.Succeeded == true ? OidcSessionId(authenticateResult.Principal) : null; if (!string.IsNullOrEmpty(oidcSessionId)) await EndOidcSession(oidcSessionId); - if (authenticateResult?.Succeeded == true) + if (authenticateResult?.Succeeded == true && _customerSettings.SignOutOnAllDevices) await ReplacePasswordToken(authenticateResult.Principal); await _httpContextAccessor.HttpContext.SignOutAsync(GrandCookieAuthenticationDefaults diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/Login/Index.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/Login/Index.cshtml index bd77e1ac7..d2ccecc75 100644 --- a/src/Web/Grand.Web.Store/Areas/Store/Views/Login/Index.cshtml +++ b/src/Web/Grand.Web.Store/Areas/Store/Views/Login/Index.cshtml @@ -69,9 +69,11 @@ @if (Model.SignedInWithoutAccess) {
    @Loc["Admin.Login.SignedInWithoutAccess"]
    - - @Loc["Admin.Header.Logout"] - +
    + +
    } @if (Model.ExternalSignInUnavailable) { diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml index ffb19e528..dad19e09f 100644 --- a/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml +++ b/src/Web/Grand.Web.Store/Areas/Store/Views/Setting/Partials/Customer.TabCustomerSecurity.cshtml @@ -79,3 +79,8 @@ + +
    + +
    +
    diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/Shared/_StoreLayout.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/Shared/_StoreLayout.cshtml index 585be022f..ffcab5040 100644 --- a/src/Web/Grand.Web.Store/Areas/Store/Views/Shared/_StoreLayout.cshtml +++ b/src/Web/Grand.Web.Store/Areas/Store/Views/Shared/_StoreLayout.cshtml @@ -121,7 +121,9 @@