Skip to content

Commit

Permalink
Address CodeQL security concern
Browse files Browse the repository at this point in the history
Ensure that LoginRedirect can never be used to redirect to a different
site. In the future we may whitelist certain domains such as lexbox.org,
but for now we just strip off the hostname and make sure it has to be a
relative URL.
  • Loading branch information
rmunn committed Sep 25, 2024
1 parent 7e54c46 commit ca53296
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions backend/LexBoxApi/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ public async Task<ActionResult> LoginRedirect(

await HttpContext.SignInAsync(User,
new AuthenticationProperties { IsPersistent = true });
string destination = returnTo;
string destination = ValidateRedirectUrl(returnTo);
if (returnToIfEmailExists is not null && user.Email is not null)
{
var dbUser = await lexBoxDbContext.Users.FindByEmailOrUsername(user.Email);
if (dbUser is not null) destination = returnToIfEmailExists!;
if (dbUser is not null) destination = ValidateRedirectUrl(returnToIfEmailExists)!;
}
return Redirect(destination);

Check warning

Code scanning / CodeQL

URL redirection from remote source Medium

Untrusted URL redirection due to
user-provided value
.
Untrusted URL redirection due to
user-provided value
.
}

private string ValidateRedirectUrl(string url)
{
// Redirect URLs must be relative, to avoid phishing attacks where user is redirected to
// a lookalike site. So we strip off the host if there is one.
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
return uri.PathAndQuery;
}

[HttpGet("google")]
[AllowAnonymous]
public IActionResult GoogleLogin(string? redirectTo = null)
Expand Down

0 comments on commit ca53296

Please sign in to comment.