Skip to content

Commit

Permalink
Merge pull request #75 from GeneriekPublicatiePlatformWoo/71-fix-codeql
Browse files Browse the repository at this point in the history
fix: make sure the return path is relative in the challenge endpoint
  • Loading branch information
felixcicatt authored Oct 21, 2024
2 parents 5886da4 + a5ee766 commit 421fa5e
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions ODPC.Server/Authentication/AuthenticationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,32 @@ private static Task HandleLoggedOut<TOptions>(RedirectContext<TOptions> ctx) whe
private static Task ChallengeAsync(HttpContext httpContext)
{
var request = httpContext.Request;
var returnUrl = (request.Query["returnUrl"].FirstOrDefault() ?? string.Empty)
.AsSpan()
.TrimStart('/');

var fullReturnUrl = $"{request.Scheme}://{request.Host}{request.PathBase}/{returnUrl}";
var returnPath = GetRelativeReturnUrl(request);

if (httpContext.User.Identity?.IsAuthenticated ?? false)
{
httpContext.Response.Redirect(fullReturnUrl);
httpContext.Response.Redirect(returnPath);
return Task.CompletedTask;
}

return httpContext.ChallengeAsync(new AuthenticationProperties
{
RedirectUri = fullReturnUrl,
RedirectUri = returnPath,
});
}

/// <summary>
/// We gebruiken een query parameter om te bepalen waar we naartoe moeten redirecten na inlog.
/// Dat is gebruikersinput. Daarom willen we valideren dat die query parameter daadwerkelijk een relatieve url is.
/// Zo niet, redirecten we naar de root van de applicatie.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private static string GetRelativeReturnUrl(HttpRequest request)
{
var returnUrl = request.Query["returnUrl"].FirstOrDefault();
if (string.IsNullOrWhiteSpace(returnUrl) || new Uri(returnUrl, UriKind.RelativeOrAbsolute).IsAbsoluteUri) return "/";
return $"/{returnUrl.AsSpan().TrimStart('/')}";
}
}
}

0 comments on commit 421fa5e

Please sign in to comment.