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 ID: @Id
- + @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 NavigationPet ID: @Id
- + @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/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..d224bb4a0 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 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, DictionaryPet ID: @Id
- + @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 NavigationPet ID: @Id
- + @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 NavigationPet ID: @Id
- + @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 NavigationPet ID: @Id
- + @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/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/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 NavigationPet ID: @Id
- + @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 NavigationPet ID: @Id
- + @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/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..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,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 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; } // 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."); }