From 1d85ff8ed934c46b08b2ea0d0d9fdc5cde64ec65 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 21 Aug 2026 09:42:37 -0700 Subject: [PATCH] Captures a fresh antiforgery token after login in the web test helper --- .../Common/TestHelpers/WebTestHelpers.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/Common/Lumina.Presentation.Web.Fixtures/Common/TestHelpers/WebTestHelpers.cs b/tests/Common/Lumina.Presentation.Web.Fixtures/Common/TestHelpers/WebTestHelpers.cs index ba8de2a2..edf90f82 100644 --- a/tests/Common/Lumina.Presentation.Web.Fixtures/Common/TestHelpers/WebTestHelpers.cs +++ b/tests/Common/Lumina.Presentation.Web.Fixtures/Common/TestHelpers/WebTestHelpers.cs @@ -18,7 +18,7 @@ namespace Lumina.Presentation.Web.Fixtures.Common.TestHelpers; /// Represents an HTTP client that has been authenticated through the real login flow of the Web application. /// /// The authenticated HTTP client. -/// The antiforgery token captured before the login, valid for subsequent mutating requests. +/// The antiforgery token captured after the login, valid for subsequent mutating requests made by the authenticated user. [ExcludeFromCodeCoverage] public record AuthenticatedWebClient(HttpClient Client, string AntiforgeryToken); @@ -30,6 +30,8 @@ public static partial class WebTestHelpers { private const string LOGIN_PAGE_PATH = "/en-us/auth/login"; private const string LOGIN_ENDPOINT_PATH = "/en-us/auth/api-login"; + // the change password page renders an antiforgery token for the authenticated user without calling the remote API + private const string AUTHENTICATED_TOKEN_PAGE_PATH = "/en-us/auth/change-password"; [GeneratedRegex("name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]*)\"")] private static partial Regex AntiforgeryTokenRegex(); @@ -61,7 +63,11 @@ public static partial class WebTestHelpers }); string antiforgeryToken = await GetAntiforgeryTokenAsync(client); await LoginAsync(client, username!, password!, antiforgeryToken); - return new AuthenticatedWebClient(client, antiforgeryToken); + // the antiforgery token is bound to the authenticated user, because the antiforgery middleware runs after + // authentication; the token captured from the anonymous login page is rejected with a 400 for authenticated + // mutating requests, so a fresh token must be captured after the login + string authenticatedAntiforgeryToken = await GetAntiforgeryTokenAsync(client, AUTHENTICATED_TOKEN_PAGE_PATH); + return new AuthenticatedWebClient(client, authenticatedAntiforgeryToken); } /// @@ -86,18 +92,19 @@ public static partial class WebTestHelpers } /// - /// Retrieves the antiforgery token and cookie by requesting the login page. + /// Retrieves the antiforgery token and cookie by requesting a page that renders an antiforgery token. /// /// The HTTP client to retrieve the token through. + /// The page that renders the antiforgery token, defaults to the login page. /// The antiforgery token value. - public static async Task GetAntiforgeryTokenAsync(HttpClient client) + public static async Task GetAntiforgeryTokenAsync(HttpClient client, string? pagePath = null) { - HttpResponseMessage loginPageResponse = await client.GetAsync(LOGIN_PAGE_PATH); + HttpResponseMessage loginPageResponse = await client.GetAsync(pagePath ?? LOGIN_PAGE_PATH); loginPageResponse.EnsureSuccessStatusCode(); string loginPageHtml = await loginPageResponse.Content.ReadAsStringAsync(); Match match = AntiforgeryTokenRegex().Match(loginPageHtml); if (!match.Success) - throw new InvalidOperationException("Could not find the antiforgery token on the login page."); + throw new InvalidOperationException("Could not find the antiforgery token on the page."); return System.Net.WebUtility.HtmlDecode(match.Groups[1].Value); }