From e83c47acfff70267e5bf31ee67ee7875b1c0c9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Wed, 25 Nov 2026 12:33:30 +0100 Subject: [PATCH] Harden identity redirect handling in Blazor template (#70796) Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Components/Account/IdentityRedirectManager.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Account/IdentityRedirectManager.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Account/IdentityRedirectManager.cs index f6b1066f15a5..e953ff21024b 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Account/IdentityRedirectManager.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Account/IdentityRedirectManager.cs @@ -8,16 +8,17 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage public void RedirectTo(string? uri) { - uri ??= ""; - - // Prevent open redirects. - if (!Uri.IsWellFormedUriString(uri, UriKind.Relative) - || uri.StartsWith("//", StringComparison.Ordinal)) + // The URI can come from a "returnUrl" query string parameter, which anyone can set in a link to the app. + // To prevent open redirects, resolve it to an absolute URI and only use it if it's within the app. + // Appending "/" to the path also accepts the app's root URI without a trailing slash, such as "/app?page=2". + var baseUri = new Uri(navigationManager.BaseUri); + if (!Uri.TryCreate(baseUri, uri, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - uri = navigationManager.ToBaseRelativePath(uri); + redirectUri = baseUri; } - navigationManager.NavigateTo(uri); + navigationManager.NavigateTo(redirectUri.AbsoluteUri); } public void RedirectTo(string uri, Dictionary queryParameters)