Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Merge pull request #130 from DuendeSoftware/brock/return-url-validation
Browse files Browse the repository at this point in the history
Add IReturnUrlValidator service
  • Loading branch information
leastprivilege authored Aug 16, 2022
2 parents b248b1e + 4ef7f4f commit 493d819
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Duende.Bff/BffServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static BffBuilder AddBff(this IServiceCollection services, Action<BffOpti
services.AddDistributedMemoryCache();
services.AddOpenIdConnectAccessTokenManagement();

services.AddTransient<IReturnUrlValidator, LocalUrlReturnUrlValidator>();

// management endpoints
services.AddTransient<ILoginService, DefaultLoginService>();
services.AddTransient<ISilentLoginService, DefaultSilentLoginService>();
Expand Down
19 changes: 13 additions & 6 deletions src/Duende.Bff/Endpoints/DefaultLoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ public class DefaultLoginService : ILoginService
/// <summary>
/// The BFF options
/// </summary>
protected readonly BffOptions _options;
protected readonly BffOptions Options;

/// <summary>
/// The return URL validator
/// </summary>
protected readonly IReturnUrlValidator ReturnUrlValidator;

/// <summary>
/// ctor
/// </summary>
/// <param name="options"></param>
public DefaultLoginService(IOptions<BffOptions> options)
/// <param name="returnUrlValidator"></param>
public DefaultLoginService(IOptions<BffOptions> options, IReturnUrlValidator returnUrlValidator)
{
_options = options.Value;
Options = options.Value;
ReturnUrlValidator = returnUrlValidator;
}

/// <inheritdoc />
public virtual async Task ProcessRequestAsync(HttpContext context)
{
context.CheckForBffMiddleware(_options);
context.CheckForBffMiddleware(Options);

var returnUrl = context.Request.Query[Constants.RequestParameters.ReturnUrl].FirstOrDefault();

if (!string.IsNullOrWhiteSpace(returnUrl))
{
if (!Util.IsLocalUrl(returnUrl))
if (!await ReturnUrlValidator.IsValidAsync(returnUrl))
{
throw new Exception("returnUrl is not application local: " + returnUrl);
throw new Exception("returnUrl is not valid: " + returnUrl);
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/Duende.Bff/Endpoints/DefaultLogoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ public class DefaultLogoutService : ILogoutService
/// </summary>
protected readonly IAuthenticationSchemeProvider AuthenticationSchemeProvider;

/// <summary>
/// The return URL validator
/// </summary>
protected readonly IReturnUrlValidator ReturnUrlValidator;

/// <summary>
/// Ctor
/// </summary>
/// <param name="options"></param>
/// <param name="authenticationAuthenticationSchemeProviderProvider"></param>
public DefaultLogoutService(IOptions<BffOptions> options, IAuthenticationSchemeProvider authenticationAuthenticationSchemeProviderProvider)
/// <param name="returnUrlValidator"></param>
public DefaultLogoutService(IOptions<BffOptions> options, IAuthenticationSchemeProvider authenticationAuthenticationSchemeProviderProvider, IReturnUrlValidator returnUrlValidator)
{
Options = options.Value;
AuthenticationSchemeProvider = authenticationAuthenticationSchemeProviderProvider;
ReturnUrlValidator = returnUrlValidator;
}

/// <inheritdoc />
Expand Down Expand Up @@ -67,9 +74,9 @@ public virtual async Task ProcessRequestAsync(HttpContext context)

if (!string.IsNullOrWhiteSpace(returnUrl))
{
if (!Util.IsLocalUrl(returnUrl))
if (!await ReturnUrlValidator.IsValidAsync(returnUrl))
{
throw new Exception("returnUrl is not application local: " + returnUrl);
throw new Exception("returnUrl is not valid: " + returnUrl);
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Duende.Bff/Endpoints/IReturnUrlValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.Threading.Tasks;

namespace Duende.Bff;

/// <summary>
/// Allows validating if the return URL for login and logout is valid.
/// </summary>
public interface IReturnUrlValidator
{
/// <summary>
/// Returns true is the returnUrl is valid and safe to redirect to.
/// </summary>
/// <param name="returnUrl"></param>
/// <returns></returns>
Task<bool> IsValidAsync(string returnUrl);
}

class LocalUrlReturnUrlValidator : IReturnUrlValidator
{
/// <inheritdoc/>
public Task<bool> IsValidAsync(string returnUrl)
{
return Task.FromResult(Util.IsLocalUrl(returnUrl));
}
}

0 comments on commit 493d819

Please sign in to comment.