Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect github.my-company-server.com as GitHub #241

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/shared/GitHub.Tests/GitHubHostProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,26 @@ public void GitHubHostProvider_IsGitHubDotCom(string input, bool expected)
// show a helpful error message in the call to `GenerateCredentialAsync` instead.
[InlineData("http://github.com", true)]
[InlineData("http://gist.github.com", true)]
[InlineData("https://github.com", true)]
[InlineData("https://gist.github.com", true)]
[InlineData("ssh://github.com", false)]
[InlineData("https://example.com", false)]

[InlineData("https://github.com", true)]
[InlineData("https://github.con", false)] // No support of phony similar tld.
[InlineData("https://gist.github.con", false)] // No support of phony similar tld.
[InlineData("https://foogithub.com", false)] // No support of non github.com domains.
[InlineData("https://api.github.com", false)] // No support of github.com subdomains.
[InlineData("https://gist.github.com", true)] // Except gists.

[InlineData("http://github.my-company-server.com", true)]
[InlineData("http://gist.github.my-company-server.com", true)]
[InlineData("https://github.my-company-server.com", true)]
[InlineData("https://gist.github.my-company-server.com", true)]
[InlineData("https://gist.my-company-server.com", false)]
[InlineData("https://my-company-server.com", false)]
[InlineData("https://github.my.company.server.com", true)]
[InlineData("https://foogithub.my-company-server.com", false)]
[InlineData("https://api.github.my-company-server.com", false)]
[InlineData("https://gist.github.my.company.server.com", true)]
public void GitHubHostProvider_IsSupported(string uriString, bool expected)
{
Uri uri = new Uri(uriString);
Expand All @@ -55,6 +71,10 @@ public void GitHubHostProvider_IsSupported(string uriString, bool expected)
[Theory]
[InlineData("https://github.com", "https://github.com")]
[InlineData("https://gist.github.com", "https://github.com")]
[InlineData("https://github.my-company-server.com", "https://github.my-company-server.com")]
[InlineData("https://gist.github.my-company-server.com", "https://github.my-company-server.com")]
[InlineData("https://github.my.company.server.com", "https://github.my.company.server.com")]
[InlineData("https://gist.github.my.company.server.com", "https://github.my.company.server.com")]
public void GitHubHostProvider_GetCredentialServiceUrl(string uriString, string expectedService)
{
Uri uri = new Uri(uriString);
Expand Down
42 changes: 31 additions & 11 deletions src/shared/GitHub/GitHubHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,35 @@ public override bool IsSupported(InputArguments input)
return false;
}

// Split port number and hostname from host input argument
input.TryGetHostAndPort(out string hostName, out _);

// We do not support unencrypted HTTP communications to GitHub,
// but we report `true` here for HTTP so that we can show a helpful
// error message for the user in `CreateCredentialAsync`.
return (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http") ||
StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "https")) &&
(StringComparer.OrdinalIgnoreCase.Equals(hostName, GitHubConstants.GitHubBaseUrlHost) ||
StringComparer.OrdinalIgnoreCase.Equals(hostName, GitHubConstants.GistBaseUrlHost));
if (!StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http") &&
!StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "https"))
{
return false;
}

// Split port number and hostname from host input argument
input.TryGetHostAndPort(out string hostName, out _);

if (StringComparer.OrdinalIgnoreCase.Equals(hostName, GitHubConstants.GitHubBaseUrlHost) ||
StringComparer.OrdinalIgnoreCase.Equals(hostName, GitHubConstants.GistBaseUrlHost))
{
return true;
}

string[] domains = hostName.Split(new char[] { '.' });

// github[.subdomain].domain.tld
if (domains.Length >= 3 && domains[0] == "github")
AlexanderLanin marked this conversation as resolved.
Show resolved Hide resolved
return true;

// gist.github[.subdomain].domain.tld
if (domains.Length >= 4 && domains[0] == "gist" && domains[1] == "github")
AlexanderLanin marked this conversation as resolved.
Show resolved Hide resolved
return true;

return false;
}

public override string GetServiceName(InputArguments input)
Expand Down Expand Up @@ -264,10 +283,11 @@ private static Uri NormalizeUri(Uri uri)
}

// Special case for gist.github.com which are git backed repositories under the hood.
// Credentials for these repositories are the same as the one stored with "github.com"
if (uri.DnsSafeHost.Equals(GitHubConstants.GistBaseUrlHost, StringComparison.OrdinalIgnoreCase))
{
return new Uri("https://" + GitHubConstants.GitHubBaseUrlHost);
// Credentials for these repositories are the same as the one stored with "github.com".
// Same for gist.github[.subdomain].domain.tld. The general form was already checked via IsSupported.
int firstDot = uri.DnsSafeHost.IndexOf(".");
if (uri.DnsSafeHost.Substring(0, firstDot).Equals("gist", StringComparison.OrdinalIgnoreCase)) {
AlexanderLanin marked this conversation as resolved.
Show resolved Hide resolved
return new Uri("https://" + uri.DnsSafeHost.Substring(firstDot+1));
}

return uri;
Expand Down