Skip to content

Commit

Permalink
fix: make sure the return path is relative in the challenge endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
felixcicatt committed Oct 21, 2024
1 parent f8c601b commit 1be7900
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions ODPC.Server/Authentication/AuthenticationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,25 @@ 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 = GetSafeReturnPath(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,
});
}

private static string GetSafeReturnPath(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 1be7900

Please sign in to comment.