From ca923f126fc05869faf359a7421b930a71815fdd Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sun, 1 Nov 2026 08:29:43 +0200 Subject: [PATCH] Fixes the login URL being broken after the admin account registration --- .../Common/Services/UrlService.cs | 17 +++++- .../BookLibrary/Books/Read.cshtml | 9 ++- .../Common/Services/UrlServiceTests.cs | 57 +++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/Lumina.Presentation.Web/Common/Services/UrlService.cs b/src/Lumina.Presentation.Web/Common/Services/UrlService.cs index 2c6ac95a..04ef0c09 100644 --- a/src/Lumina.Presentation.Web/Common/Services/UrlService.cs +++ b/src/Lumina.Presentation.Web/Common/Services/UrlService.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Reflection; +using System.Text.RegularExpressions; #endregion namespace Lumina.Presentation.Web.Common.Services; @@ -20,6 +21,11 @@ public class UrlService : IUrlService /// private static readonly HashSet s_knownRouteTemplates = GetKnownRouteTemplates(); + /// + /// Matches an optional route parameter segment, such as /{returnUrl?}, that was left without a value. + /// + private static readonly Regex s_unfilledOptionalSegmentPattern = new(@"/?\{[^{}?]+\?\}", RegexOptions.Compiled | RegexOptions.CultureInvariant); + /// /// Initializes a new instance of the class. /// @@ -62,7 +68,16 @@ public class UrlService : IUrlService string path = routeTemplate.Replace("{culture}", culture, StringComparison.OrdinalIgnoreCase); if (additionalRouteValues is not null) foreach (PropertyInfo property in additionalRouteValues.GetType().GetProperties()) - path = path.Replace($"{{{property.Name}}}", property.GetValue(additionalRouteValues)?.ToString(), StringComparison.OrdinalIgnoreCase); + { + string? routeValue = property.GetValue(additionalRouteValues)?.ToString(); + path = path.Replace($"{{{property.Name}}}", routeValue, StringComparison.OrdinalIgnoreCase); + // An optional parameter is only filled in when a value is supplied, otherwise its segment is removed below. + if (!string.IsNullOrEmpty(routeValue)) + path = path.Replace($"{{{property.Name}?}}", routeValue, StringComparison.OrdinalIgnoreCase); + } + // Optional parameters that received no value would otherwise end up literally in the URL (and be percent encoded by the + // browser, like "/auth/login/%7BreturnUrl?%7D"), producing a link that does not match the intended route. + path = s_unfilledOptionalSegmentPattern.Replace(path, string.Empty); // Ensure the path starts with a slash, so that it is relative to the server root when concatenated with the path base. if (!path.StartsWith('/')) path = "/" + path; diff --git a/src/Lumina.Presentation.Web/Core/Views/Library/WrittenContentLibrary/BookLibrary/Books/Read.cshtml b/src/Lumina.Presentation.Web/Core/Views/Library/WrittenContentLibrary/BookLibrary/Books/Read.cshtml index b17fbfaa..f78a9a4c 100644 --- a/src/Lumina.Presentation.Web/Core/Views/Library/WrittenContentLibrary/BookLibrary/Books/Read.cshtml +++ b/src/Lumina.Presentation.Web/Core/Views/Library/WrittenContentLibrary/BookLibrary/Books/Read.cshtml @@ -46,9 +46,9 @@ const manifestUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_MANIFEST, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}`; const availabilityUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_AVAILABILITY, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}`; const libraryIndexUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.INDEX)'; - // The login route takes the page to return to as an optional route value. The template is kept, so the reader can append - // the current page to it when a JSON request discovers that the session ended. - const signInUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW)'; + // The login page URL, without the optional return URL segment. The reader appends the current page to it as a query value + // when a JSON request discovers that the session ended. + const signInUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW)'; const sectionUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_SECTION, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}&locationRef=`; const resourceUrlTemplate = '@_urlService.GetAbsoluteUrl(WebRoutes.Books.GET_READING_RESOURCE, new { bookId = default(Guid) })'.replace('/@(default(Guid))', `/${bookId}`) + `?libraryId=${libraryId}&resourceKey=`; const startConsumptionUrl = '@_urlService.GetAbsoluteUrl(WebRoutes.MediaConsumption.START_CONSUMPTION, new { libraryId = Model.LibraryId, mediaKind = "Book", mediaItemId = Model.BookId })'; @@ -289,10 +289,9 @@ // redirects of the authentication middleware do, so the reader page is not encoded into the path of the login route. function redirectIfSessionEnded(response) { if (response.status === 401) { - if (!signInUrlTemplate) + if (!signInUrl) return true; const returnUrl = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash); - const signInUrl = signInUrlTemplate.replace('/{returnUrl?}', ''); window.location.href = `${signInUrl}?returnUrl=${returnUrl}`; return true; } diff --git a/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Services/UrlServiceTests.cs b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Services/UrlServiceTests.cs index b924abbf..893eaeaf 100644 --- a/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Services/UrlServiceTests.cs +++ b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Services/UrlServiceTests.cs @@ -214,6 +214,63 @@ public class UrlServiceTests Assert.Equal("/lumina/de-de/auth/register", url); } + [Fact] + public void GetRelativeUrl_WhenOptionalRouteValueIsNotSupplied_ShouldRemoveOptionalSegment() + { + // Arrange + _mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us")); + + // Act + string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW); + + // Assert + Assert.Equal("/en-us/auth/login", url); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public void GetRelativeUrl_WhenOptionalRouteValueIsEmpty_ShouldRemoveOptionalSegment(string? returnUrl) + { + // Arrange + _mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us")); + + // Act + string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW, new { returnUrl }); + + // Assert + Assert.Equal("/en-us/auth/login", url); + } + + [Fact] + public void GetRelativeUrl_WhenOptionalRouteValueIsSupplied_ShouldReplaceOptionalPlaceholder() + { + // Arrange + _mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us")); + + // Act + string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN_VIEW, new { returnUrl = "libraries" }); + + // Assert + Assert.Equal("/en-us/auth/login/libraries", url); + } + + [Fact] + public void GetAbsoluteUrl_WhenOptionalRouteValueIsNotSupplied_ShouldBuildWellFormedUrl() + { + // Arrange + DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us"); + httpContext.Request.Host = new HostString("localhost:5012"); + httpContext.Request.Scheme = "http"; + _mockHttpContextAccessor.HttpContext.Returns(httpContext); + + // Act + string? url = _sut.GetAbsoluteUrl(WebRoutes.Authentication.LOGIN_VIEW); + + // Assert + Assert.Equal("http://localhost:5012/en-us/auth/login", url); + } + [Fact] public void GetAbsoluteUrl_WhenCalled_ShouldComposeSchemeAndHostWithRelativeUrl() {