Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion 10.0/BlazorSample_BlazorWebApp/Components/Pages/PetDetail.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
@page "/pet-detail/{id:int}"
@inject NavigationManager Navigation

<PageTitle>Pet Detail</PageTitle>

<h1>Pet Detail Record</h1>

<p>Pet ID: @Id</p>

<p><a href="@ReturnUrl">Return</a></p>
<p><a href="@returnLink">Return</a></p>

@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;
}
}
20 changes: 19 additions & 1 deletion 10.0/BlazorSample_BlazorWebAssembly/Pages/PetDetail.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
@page "/pet-detail/{id:int}"
@inject NavigationManager Navigation

<PageTitle>Pet Detail</PageTitle>

<h1>Pet Detail Record</h1>

<p>Pet ID: @Id</p>

<p><a href="@ReturnUrl">Return</a></p>
<p><a href="@returnLink">Return</a></p>

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, Dictionary<string, object?> queryParameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
@page "/pet-detail/{id:int}"
@inject NavigationManager Navigation

<PageTitle>Pet Detail</PageTitle>

<h1>Pet Detail Record</h1>

<p>Pet ID: @Id</p>

<p><a href="@ReturnUrl">Return</a></p>
<p><a href="@returnLink">Return</a></p>

@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;
}
}
Loading
Loading