Skip to content

Commit

Permalink
[PM-3561] Clean the return url of any whitespace (#3696)
Browse files Browse the repository at this point in the history
* clean the return url of any whitespace

* ReplaceWhiteSpace helper

* tests for ReplaceWhiteSpace helper

---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
  • Loading branch information
kspearrin and withinfocus authored Feb 6, 2024
1 parent 7c4854f commit fc1d7c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bitwarden_license/src/Sso/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public IActionResult ExternalChallenge(string scheme, string returnUrl, string s
returnUrl = "~/";
}

// Clean the returnUrl
returnUrl = CoreHelpers.ReplaceWhiteSpace(returnUrl, string.Empty);
if (!Url.IsLocalUrl(returnUrl) && !_interaction.IsValidReturnUrl(returnUrl))
{
throw new Exception(_i18nService.T("InvalidReturnUrl"));
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Utilities/CoreHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static class CoreHelpers
private static readonly DateTime _max = new DateTime(9999, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly Random _random = new Random();
private static readonly string RealConnectingIp = "X-Connecting-IP";
private static readonly Regex _whiteSpaceRegex = new Regex(@"\s+");

/// <summary>
/// Generate sequential Guid for Sql Server.
Expand Down Expand Up @@ -868,4 +869,9 @@ public static string GetEmailDomain(string email)

return null;
}

public static string ReplaceWhiteSpace(string input, string newValue)
{
return _whiteSpaceRegex.Replace(input, newValue);
}
}
11 changes: 11 additions & 0 deletions test/Core.Test/Utilities/CoreHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,15 @@ public void GetEmailDomain_ReturnsNull(string wrongEmail)
{
Assert.Null(CoreHelpers.GetEmailDomain(wrongEmail));
}

[Theory]
[InlineData("hello world")]
[InlineData(" hello world ")]
[InlineData("hello\tworld")]
[InlineData("hello\r\nworld")]
[InlineData("hello\nworld")]
public void ReplaceWhiteSpace_Success(string email)
{
Assert.Equal("helloworld", CoreHelpers.ReplaceWhiteSpace(email, string.Empty));
}
}

0 comments on commit fc1d7c7

Please sign in to comment.