Skip to content

Commit 65358e2

Browse files
authored
fix getcredentialembeddedurl() (#643)
1 parent 5a79eea commit 65358e2

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/Microsoft.VisualStudio.Services.Agent/Util/UrlUtil.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ public static Uri GetCredentialEmbeddedUrl(Uri baseUrl, string username, string
2323

2424
UriBuilder credUri = new UriBuilder(baseUrl);
2525

26-
// escape chars in username for uri
27-
if (!string.IsNullOrEmpty(username))
26+
// ensure we have a username, uribuild will throw if username is empty but password is not.
27+
if (string.IsNullOrEmpty(username))
2828
{
29-
credUri.UserName = Uri.EscapeDataString(username);
29+
username = "emptyusername";
3030
}
3131

32+
// escape chars in username for uri
33+
credUri.UserName = Uri.EscapeDataString(username);
34+
3235
// escape chars in password for uri
3336
if (!string.IsNullOrEmpty(password))
3437
{

src/Test/L0/Util/UrlUtilL0.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using Microsoft.TeamFoundation.DistributedTask.WebApi;
3+
using Microsoft.VisualStudio.Services.Agent.Util;
4+
using Xunit;
5+
6+
namespace Microsoft.VisualStudio.Services.Agent.Tests.Util
7+
{
8+
public class UrlUtilL0
9+
{
10+
[Fact]
11+
[Trait("Level", "L0")]
12+
[Trait("Category", "Common")]
13+
public void GetCredentialEmbeddedUrl_NoUsernameAndPassword()
14+
{
15+
// Act.
16+
Uri result = UrlUtil.GetCredentialEmbeddedUrl(new Uri("https://github.com/Microsoft/vsts-agent.git"), string.Empty, string.Empty);
17+
// Actual
18+
Assert.Equal("https://github.com/Microsoft/vsts-agent.git", result.AbsoluteUri);
19+
}
20+
21+
[Fact]
22+
[Trait("Level", "L0")]
23+
[Trait("Category", "Common")]
24+
public void GetCredentialEmbeddedUrl_NoUsername()
25+
{
26+
// Act.
27+
Uri result = UrlUtil.GetCredentialEmbeddedUrl(new Uri("https://github.com/Microsoft/vsts-agent.git"), string.Empty, "password123");
28+
// Actual
29+
Assert.Equal("https://emptyusername:[email protected]/Microsoft/vsts-agent.git", result.AbsoluteUri);
30+
}
31+
32+
[Fact]
33+
[Trait("Level", "L0")]
34+
[Trait("Category", "Common")]
35+
public void GetCredentialEmbeddedUrl_NoPassword()
36+
{
37+
// Act.
38+
Uri result = UrlUtil.GetCredentialEmbeddedUrl(new Uri("https://github.com/Microsoft/vsts-agent.git"), "user123", string.Empty);
39+
// Actual
40+
Assert.Equal("https://[email protected]/Microsoft/vsts-agent.git", result.AbsoluteUri);
41+
}
42+
43+
[Fact]
44+
[Trait("Level", "L0")]
45+
[Trait("Category", "Common")]
46+
public void GetCredentialEmbeddedUrl_HasUsernameAndPassword()
47+
{
48+
// Act.
49+
Uri result = UrlUtil.GetCredentialEmbeddedUrl(new Uri("https://github.com/Microsoft/vsts-agent.git"), "user123", "password123");
50+
// Actual
51+
Assert.Equal("https://user123:[email protected]/Microsoft/vsts-agent.git", result.AbsoluteUri);
52+
}
53+
54+
[Fact]
55+
[Trait("Level", "L0")]
56+
[Trait("Category", "Common")]
57+
public void GetCredentialEmbeddedUrl_UsernameAndPasswordEncoding()
58+
{
59+
// Act.
60+
Uri result = UrlUtil.GetCredentialEmbeddedUrl(new Uri("https://github.com/Microsoft/vsts-agent.git"), "user 123", "password 123");
61+
// Actual
62+
Assert.Equal("https://user%20123:password%[email protected]/Microsoft/vsts-agent.git", result.AbsoluteUri);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)