mirror of
https://github.com/actions/runner.git
synced 2025-12-12 23:46:12 +00:00
Add 'http://' to http(s)_proxy if there is no protocol (#2663)
* Add 'http://' to http(s)_proxy if there is no protocol Before this commit, these URIs used to be ignored (nullproxy). The new behaviour aligns with go style http(s)_proxy * Update src/Runner.Sdk/RunnerWebProxy.cs Co-authored-by: Nikola Jokic <jokicnikola07@gmail.com> * Assert that original protocol is preserved * Use startsWith for testing protocol * Only modify proxy if useful --------- Co-authored-by: Nikola Jokic <jokicnikola07@gmail.com>
This commit is contained in:
@@ -69,6 +69,10 @@ namespace GitHub.Runner.Sdk
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(httpProxyAddress) && !Uri.TryCreate(httpProxyAddress, UriKind.Absolute, out var _))
|
||||||
|
{
|
||||||
|
httpProxyAddress = PrependHttpIfMissing(httpProxyAddress);
|
||||||
|
}
|
||||||
if (!string.IsNullOrEmpty(httpProxyAddress) && Uri.TryCreate(httpProxyAddress, UriKind.Absolute, out var proxyHttpUri))
|
if (!string.IsNullOrEmpty(httpProxyAddress) && Uri.TryCreate(httpProxyAddress, UriKind.Absolute, out var proxyHttpUri))
|
||||||
{
|
{
|
||||||
_httpProxyAddress = proxyHttpUri.OriginalString;
|
_httpProxyAddress = proxyHttpUri.OriginalString;
|
||||||
@@ -99,6 +103,10 @@ namespace GitHub.Runner.Sdk
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(httpsProxyAddress) && !Uri.TryCreate(httpsProxyAddress, UriKind.Absolute, out var _))
|
||||||
|
{
|
||||||
|
httpsProxyAddress = PrependHttpIfMissing(httpsProxyAddress);
|
||||||
|
}
|
||||||
if (!string.IsNullOrEmpty(httpsProxyAddress) && Uri.TryCreate(httpsProxyAddress, UriKind.Absolute, out var proxyHttpsUri))
|
if (!string.IsNullOrEmpty(httpsProxyAddress) && Uri.TryCreate(httpsProxyAddress, UriKind.Absolute, out var proxyHttpsUri))
|
||||||
{
|
{
|
||||||
_httpsProxyAddress = proxyHttpsUri.OriginalString;
|
_httpsProxyAddress = proxyHttpsUri.OriginalString;
|
||||||
@@ -240,5 +248,20 @@ namespace GitHub.Runner.Sdk
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string PrependHttpIfMissing(string proxyAddress)
|
||||||
|
{
|
||||||
|
// much like in golang, see https://github.com/golang/net/blob/f5464ddb689c015d1abf4df78a806a54af977e6c/http/httpproxy/proxy.go#LL156C31-L156C31
|
||||||
|
if (!proxyAddress.StartsWith("http://", StringComparison.Ordinal) && !proxyAddress.StartsWith("https://", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
var prependedProxyAddress = "http://" + proxyAddress;
|
||||||
|
if (Uri.TryCreate(prependedProxyAddress, UriKind.Absolute, out var _))
|
||||||
|
{
|
||||||
|
// if prepending http:// turns the proxyAddress into a valid Uri, then use that
|
||||||
|
return prependedProxyAddress;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return proxyAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,8 +186,8 @@ namespace GitHub.Runner.Common.Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Environment.SetEnvironmentVariable("http_proxy", "127.0.0.1:7777");
|
Environment.SetEnvironmentVariable("http_proxy", "#fragment");
|
||||||
Environment.SetEnvironmentVariable("https_proxy", "127.0.0.1");
|
Environment.SetEnvironmentVariable("https_proxy", "#fragment");
|
||||||
var proxy = new RunnerWebProxy();
|
var proxy = new RunnerWebProxy();
|
||||||
|
|
||||||
Assert.Null(proxy.HttpProxyAddress);
|
Assert.Null(proxy.HttpProxyAddress);
|
||||||
@@ -206,6 +206,68 @@ namespace GitHub.Runner.Common.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Level", "L0")]
|
||||||
|
[Trait("Category", "Common")]
|
||||||
|
public void WebProxyPrependsHTTPforHTTP_PROXY_IfNoProtocol()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Environment.SetEnvironmentVariable("http_proxy", "127.0.0.1:7777");
|
||||||
|
var proxy = new RunnerWebProxy();
|
||||||
|
|
||||||
|
Assert.Equal("http://127.0.0.1:7777", proxy.HttpProxyAddress);
|
||||||
|
Assert.Null(proxy.HttpProxyUsername);
|
||||||
|
Assert.Null(proxy.HttpProxyPassword);
|
||||||
|
|
||||||
|
Assert.Equal(0, proxy.NoProxyList.Count);
|
||||||
|
|
||||||
|
Environment.SetEnvironmentVariable("http_proxy", "http://127.0.0.1:7777");
|
||||||
|
proxy = new RunnerWebProxy();
|
||||||
|
|
||||||
|
Assert.Equal("http://127.0.0.1:7777", proxy.HttpProxyAddress);
|
||||||
|
Assert.Null(proxy.HttpProxyUsername);
|
||||||
|
Assert.Null(proxy.HttpProxyPassword);
|
||||||
|
|
||||||
|
Assert.Equal(0, proxy.NoProxyList.Count);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
CleanProxyEnv();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Level", "L0")]
|
||||||
|
[Trait("Category", "Common")]
|
||||||
|
public void WebProxyPrependsHTTPforHTTPS_PROXY_IfNoProtocol()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Environment.SetEnvironmentVariable("https_proxy", "127.0.0.1:7777");
|
||||||
|
var proxy = new RunnerWebProxy();
|
||||||
|
|
||||||
|
Assert.Equal("http://127.0.0.1:7777", proxy.HttpsProxyAddress);
|
||||||
|
Assert.Null(proxy.HttpProxyUsername);
|
||||||
|
Assert.Null(proxy.HttpProxyPassword);
|
||||||
|
|
||||||
|
Assert.Equal(0, proxy.NoProxyList.Count);
|
||||||
|
|
||||||
|
Environment.SetEnvironmentVariable("https_proxy", "https://127.0.0.1:7777");
|
||||||
|
proxy = new RunnerWebProxy();
|
||||||
|
|
||||||
|
Assert.Equal("https://127.0.0.1:7777", proxy.HttpsProxyAddress); // existing protocol 'https' is not removed
|
||||||
|
Assert.Null(proxy.HttpProxyUsername);
|
||||||
|
Assert.Null(proxy.HttpProxyPassword);
|
||||||
|
|
||||||
|
Assert.Equal(0, proxy.NoProxyList.Count);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
CleanProxyEnv();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
[Trait("Level", "L0")]
|
[Trait("Level", "L0")]
|
||||||
[Trait("Category", "Common")]
|
[Trait("Category", "Common")]
|
||||||
|
|||||||
Reference in New Issue
Block a user