From 75501b5270a4d7fe4540427f885448ee45024f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Mon, 23 Nov 2026 19:30:51 +0100 Subject: [PATCH 1/5] Harden identity redirect handling in Blazor template Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Components/Account/IdentityRedirectManager.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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..36864deb0406 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 @@ -11,13 +11,15 @@ public void RedirectTo(string? uri) uri ??= ""; // Prevent open redirects. - if (!Uri.IsWellFormedUriString(uri, UriKind.Relative) - || uri.StartsWith("//", StringComparison.Ordinal)) + // Resolve the URI the same way NavigationManager does and only allow destinations within the app's base URI. + var baseUri = new Uri(navigationManager.BaseUri); + if (!Uri.TryCreate(baseUri, uri, out var absoluteUri) + || !absoluteUri.AbsoluteUri.StartsWith(baseUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) { - uri = navigationManager.ToBaseRelativePath(uri); + absoluteUri = baseUri; } - navigationManager.NavigateTo(uri); + navigationManager.NavigateTo(absoluteUri.AbsoluteUri); } public void RedirectTo(string uri, Dictionary queryParameters) From d06e03409b7d07d6863ec9d53cb91219d45f67b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Mon, 23 Nov 2026 20:22:24 +0100 Subject: [PATCH 2/5] Allow app root without trailing slash in identity redirects Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Components/Account/IdentityRedirectManager.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 36864deb0406..74c5277bf1bf 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 @@ -12,9 +12,10 @@ public void RedirectTo(string? uri) // Prevent open redirects. // Resolve the URI the same way NavigationManager does and only allow destinations within the app's base URI. + // The base URI always ends with "/", so appending "/" also allows the app root without it (for example "/app?x=1"). var baseUri = new Uri(navigationManager.BaseUri); if (!Uri.TryCreate(baseUri, uri, out var absoluteUri) - || !absoluteUri.AbsoluteUri.StartsWith(baseUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) + || !(absoluteUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) { absoluteUri = baseUri; } From 51d6aeebcaddd1e923517afb4512d993372732f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 00:49:00 +0100 Subject: [PATCH 3/5] Compare app base path ordinally in identity redirects Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Components/Account/IdentityRedirectManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 74c5277bf1bf..97bed9edc8a6 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 @@ -15,7 +15,7 @@ public void RedirectTo(string? uri) // The base URI always ends with "/", so appending "/" also allows the app root without it (for example "/app?x=1"). var baseUri = new Uri(navigationManager.BaseUri); if (!Uri.TryCreate(baseUri, uri, out var absoluteUri) - || !(absoluteUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)) + || !(absoluteUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { absoluteUri = baseUri; } From 7f3a3230fac66bf06766c2daf95b9372cf416544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 02:11:13 +0100 Subject: [PATCH 4/5] Clarify identity redirect validation comments Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Account/IdentityRedirectManager.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 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 97bed9edc8a6..2e28830f7f3c 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,19 +8,17 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage public void RedirectTo(string? uri) { - uri ??= ""; - - // Prevent open redirects. - // Resolve the URI the same way NavigationManager does and only allow destinations within the app's base URI. - // The base URI always ends with "/", so appending "/" also allows the app root without it (for example "/app?x=1"). + // The URI often comes 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 absoluteUri) - || !(absoluteUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + if (!Uri.TryCreate(baseUri, uri, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - absoluteUri = baseUri; + redirectUri = baseUri; } - navigationManager.NavigateTo(absoluteUri.AbsoluteUri); + navigationManager.NavigateTo(redirectUri.AbsoluteUri); } public void RedirectTo(string uri, Dictionary queryParameters) From 2b9c56ae323b9dd273074123b87e09e966e62984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 18:44:09 +0100 Subject: [PATCH 5/5] Reword identity redirect comment --- .../Components/Account/IdentityRedirectManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2e28830f7f3c..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,7 +8,7 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage public void RedirectTo(string? uri) { - // The URI often comes from a "returnUrl" query string parameter, which anyone can set in a link to the app. + // 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);