From e30e0576aeca12be91fee706fd74268e54bfc0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 13:43:54 +0100 Subject: [PATCH 1/3] Validate return URLs in authentication samples Resolve return URLs against the app's base URI and only redirect to them if they stay within the app. This aligns the OIDC, Entra, standalone WebAssembly Identity and MAUI Blazor Web Identity samples with dotnet/aspnetcore#70796. Fixes #692 --- ...ginLogoutEndpointRouteBuilderExtensions.cs | 26 ++++++++++------- ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ .../BlazorWasmAuth/Pages/Identity/Login.razor | 10 ++++++- .../Account/IdentityRedirectManager.cs | 14 +++++---- ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ .../Components/Identity/Login.razor | 10 ++++++- ...ginLogoutEndpointRouteBuilderExtensions.cs | 26 ++++++++++------- ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ ...ginLogoutEndpointRouteBuilderExtensions.cs | 29 +++++++------------ .../Components/Identity/Login.razor | 10 ++++++- .../Account/IdentityRedirectManager.cs | 14 +++++---- 18 files changed, 194 insertions(+), 235 deletions(-) diff --git a/10.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs b/10.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs index 7e34d6c92..f483d24d4 100644 --- a/10.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/10.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,26 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(string.Empty); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out with both the Cookie and OIDC authentication schemes. Users who have not signed out with the OIDC scheme will // automatically get signed back in as the same user the next time they visit a page that requires authentication // with no opportunity to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); return group; } - // Prevent open redirects. Non-empty returnUrls are absolute URIs provided by NavigationManager.Uri. - private static AuthenticationProperties GetAuthProperties(string? returnUrl) => - new() + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) + { + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - RedirectUri = returnUrl switch - { - string => new Uri(returnUrl, UriKind.Absolute).PathAndQuery, - null => "/", - } - }; + redirectUri = baseUri; + } + + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; + } } diff --git a/10.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs b/10.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs index df9c1aa88..6aaf1a6c4 100644 --- a/10.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/10.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/10.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/10.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/10.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/10.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/10.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/10.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index 3d8e93f19..be0be8958 100644 --- a/10.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/10.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -10,37 +10,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/10.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs b/10.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/10.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/10.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/10.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Pages/Identity/Login.razor b/10.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Pages/Identity/Login.razor index 8d8825757..27cc0f7dc 100644 --- a/10.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Pages/Identity/Login.razor +++ b/10.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Pages/Identity/Login.razor @@ -94,7 +94,15 @@ if (formResult.Succeeded && !string.IsNullOrEmpty(ReturnUrl)) { - Navigation.NavigateTo(ReturnUrl); + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only navigate to 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(Navigation.BaseUri); + if (Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + && (returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + Navigation.NavigateTo(returnUri.AbsoluteUri); + } } } diff --git a/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs b/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs index a3e6dbf28..7919e268a 100644 --- a/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs +++ b/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs @@ -18,15 +18,17 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage public void RedirectTo(string? uri) { - uri ??= ""; - - // Prevent open redirects. - if (!Uri.IsWellFormedUriString(uri, UriKind.Relative)) + // 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 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) diff --git a/8.0/BlazorWebAppOidc/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/8.0/BlazorWebAppOidc/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/8.0/BlazorWebAppOidc/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/8.0/BlazorWebAppOidc/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index 3d8e93f19..be0be8958 100644 --- a/8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -10,37 +10,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/8.0/BlazorWebAppOidcServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs b/8.0/BlazorWebAppOidcServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/8.0/BlazorWebAppOidcServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/8.0/BlazorWebAppOidcServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/8.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor b/8.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor index 9e9823a6a..6e9a0f7ea 100644 --- a/8.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor +++ b/8.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor @@ -85,7 +85,15 @@ if (formResult.Succeeded && !string.IsNullOrEmpty(ReturnUrl)) { - Navigation.NavigateTo(ReturnUrl); + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only navigate to 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(Navigation.BaseUri); + if (Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + && (returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + Navigation.NavigateTo(returnUri.AbsoluteUri); + } } } diff --git a/9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs b/9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs index 7e34d6c92..f483d24d4 100644 --- a/9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,26 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(string.Empty); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out with both the Cookie and OIDC authentication schemes. Users who have not signed out with the OIDC scheme will // automatically get signed back in as the same user the next time they visit a page that requires authentication // with no opportunity to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); return group; } - // Prevent open redirects. Non-empty returnUrls are absolute URIs provided by NavigationManager.Uri. - private static AuthenticationProperties GetAuthProperties(string? returnUrl) => - new() + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) + { + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - RedirectUri = returnUrl switch - { - string => new Uri(returnUrl, UriKind.Absolute).PathAndQuery, - null => "/", - } - }; + redirectUri = baseUri; + } + + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; + } } diff --git a/9.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs b/9.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs index df9c1aa88..6aaf1a6c4 100644 --- a/9.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/9.0/BlazorWebAppEntraBffYarpAspire/BlazorWebAppEntra/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/9.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/9.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/9.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/9.0/BlazorWebAppOidcBffAuto/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/9.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs b/9.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs index 3d8e93f19..be0be8958 100644 --- a/9.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/9.0/BlazorWebAppOidcBffAutoYarpAspire/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -10,37 +10,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/9.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs b/9.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs index dc2b4f351..c55449309 100644 --- a/9.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs +++ b/9.0/BlazorWebAppOidcBffServer/BlazorWebAppOidcServer/LoginLogoutEndpointRouteBuilderExtensions.cs @@ -11,37 +11,30 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute { var group = endpoints.MapGroup(""); - group.MapGet("/login", (string? returnUrl) => TypedResults.Challenge(GetAuthProperties(returnUrl))) + group.MapGet("/login", (HttpRequest request, string? returnUrl) => TypedResults.Challenge(GetAuthProperties(request, returnUrl))) .AllowAnonymous(); // Sign out of the Cookie and OIDC handlers. If you do not sign out with the OIDC handler, // the user will automatically be signed back in the next time they visit a page that requires authentication // without being able to choose another account. - group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl), + group.MapPost("/logout", (HttpRequest request, [FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(request, returnUrl), [CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"])); return group; } - private static AuthenticationProperties GetAuthProperties(string? returnUrl) + private static AuthenticationProperties GetAuthProperties(HttpRequest request, string? returnUrl) { - // TODO: Use HttpContext.Request.PathBase instead. - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') + // The return URL comes from the query string or a form field, which anyone can set. + // 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($"{request.Scheme}://{request.Host}{request.PathBase}/"); + if (!Uri.TryCreate(baseUri, returnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - returnUrl = $"{pathBase}{returnUrl}"; + redirectUri = baseUri; } - return new AuthenticationProperties { RedirectUri = returnUrl }; + return new AuthenticationProperties { RedirectUri = redirectUri.AbsoluteUri }; } } diff --git a/9.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor b/9.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor index aebf19cc2..80726b4a4 100644 --- a/9.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor +++ b/9.0/BlazorWebAssemblyStandaloneWithIdentity/BlazorWasmAuth/Components/Identity/Login.razor @@ -85,7 +85,15 @@ if (formResult.Succeeded && !string.IsNullOrEmpty(ReturnUrl)) { - Navigation.NavigateTo(ReturnUrl); + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only navigate to 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(Navigation.BaseUri); + if (Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + && (returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + Navigation.NavigateTo(returnUri.AbsoluteUri); + } } } diff --git a/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs b/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs index e45b4da3f..30329ce82 100644 --- a/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs +++ b/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs @@ -18,17 +18,19 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage [DoesNotReturn] public void RedirectTo(string? uri) { - uri ??= ""; - - // Prevent open redirects. - if (!Uri.IsWellFormedUriString(uri, UriKind.Relative)) + // 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 redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) { - uri = navigationManager.ToBaseRelativePath(uri); + redirectUri = baseUri; } // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect. // So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown. - navigationManager.NavigateTo(uri); + navigationManager.NavigateTo(redirectUri.AbsoluteUri); throw new InvalidOperationException($"{nameof(IdentityRedirectManager)} can only be used during static rendering."); } From 453e5d0d4fb91a9eef05db006d2f9d804277edeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 16:56:57 +0100 Subject: [PATCH 2/3] Validate return URL in PetDetail sample Only link back to ReturnUrl when it resolves to a URI within the app, so the Return link can't point to another site or run a javascript: URL. Contributes to #692 --- .../Components/Pages/PetDetail.razor | 20 ++++++++++++++++++- .../Pages/PetDetail.razor | 20 ++++++++++++++++++- .../templated-components/PetDetail.razor | 20 ++++++++++++++++++- .../templated-components/PetDetail.razor | 20 ++++++++++++++++++- .../Components/Pages/PetDetail.razor | 20 ++++++++++++++++++- .../Pages/PetDetail.razor | 20 ++++++++++++++++++- .../Components/Pages/PetDetail.razor | 20 ++++++++++++++++++- .../Pages/PetDetail.razor | 20 ++++++++++++++++++- 8 files changed, 152 insertions(+), 8 deletions(-) diff --git a/10.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor b/10.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/10.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor +++ b/10.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/10.0/BlazorSample_BlazorWebAssembly/Pages/PetDetail.razor b/10.0/BlazorSample_BlazorWebAssembly/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/10.0/BlazorSample_BlazorWebAssembly/Pages/PetDetail.razor +++ b/10.0/BlazorSample_BlazorWebAssembly/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/7.0/BlazorSample_Server/Pages/templated-components/PetDetail.razor b/7.0/BlazorSample_Server/Pages/templated-components/PetDetail.razor index e44dd9324..17dcc7e69 100644 --- a/7.0/BlazorSample_Server/Pages/templated-components/PetDetail.razor +++ b/7.0/BlazorSample_Server/Pages/templated-components/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [Parameter, SupplyParameterFromQuery] public string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/7.0/BlazorSample_WebAssembly/Pages/templated-components/PetDetail.razor b/7.0/BlazorSample_WebAssembly/Pages/templated-components/PetDetail.razor index e44dd9324..17dcc7e69 100644 --- a/7.0/BlazorSample_WebAssembly/Pages/templated-components/PetDetail.razor +++ b/7.0/BlazorSample_WebAssembly/Pages/templated-components/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [Parameter, SupplyParameterFromQuery] public string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/8.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/8.0/BlazorSample_WebAssembly/Pages/PetDetail.razor b/8.0/BlazorSample_WebAssembly/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/8.0/BlazorSample_WebAssembly/Pages/PetDetail.razor +++ b/8.0/BlazorSample_WebAssembly/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/9.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor b/9.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/9.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor +++ b/9.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } diff --git a/9.0/BlazorSample_WebAssembly/Pages/PetDetail.razor b/9.0/BlazorSample_WebAssembly/Pages/PetDetail.razor index 99a200743..93df6fd3a 100644 --- a/9.0/BlazorSample_WebAssembly/Pages/PetDetail.razor +++ b/9.0/BlazorSample_WebAssembly/Pages/PetDetail.razor @@ -1,4 +1,5 @@ @page "/pet-detail/{id:int}" +@inject NavigationManager Navigation Pet Detail @@ -6,12 +7,29 @@

Pet ID: @Id

-

Return

+

Return

@code { + private string? returnLink; + [Parameter] public int Id { get; set; } [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } + + protected override void OnParametersSet() + { + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only link to 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(Navigation.BaseUri); + if (!Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + || !(returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + returnUri = baseUri; + } + + returnLink = returnUri.AbsoluteUri; + } } From d215a6039c8f4e5a34aa113e7ca494794066e926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Rozto=C4=8Dil?= Date: Tue, 24 Nov 2026 18:45:45 +0100 Subject: [PATCH 3/3] Reword redirect comment in MAUI samples --- .../Components/Account/IdentityRedirectManager.cs | 2 +- .../Components/Account/IdentityRedirectManager.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs b/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs index 7919e268a..d224bb4a0 100644 --- a/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs +++ b/10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs @@ -18,7 +18,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); diff --git a/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs b/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs index 30329ce82..c4cdc4488 100644 --- a/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs +++ b/9.0/MauiBlazorWebIdentity/MauiBlazorWeb/MauiBlazorWeb.Web/Components/Account/IdentityRedirectManager.cs @@ -18,7 +18,7 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage [DoesNotReturn] 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);