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

FIX: [CodeQL: SM02184] Server certificate validation disabled in VssUtil.cs #5068

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/Agent.Sdk/Util/VssUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Net.Security;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
Expand Down Expand Up @@ -163,21 +164,23 @@ public static bool IsCustomServerCertificateValidationSupported(ITraceWriter tra
return true;
}

// The function is to check if the custom server certificate validation is supported on the current platform.
private static bool CheckSupportOfCustomServerCertificateValidation(ITraceWriter trace)
{
using (var handler = new HttpClientHandler())
{
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return errors == SslPolicyErrors.None; };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is equivalent to not handling the event at all. I agree with the concerns about the impact on self-signed certificates with this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be good to add a clarifying comment specifically that we set ServerCertificateCustomValidationCallback only to check to see if GetAsync throws an exception when ServerCertificateCustomValidationCallback - the body of the handler doesn't matter.

not blocking


using (var client = new HttpClient(handler))
{
try
{
client.GetAsync(_testUri).GetAwaiter().GetResult();
trace.Verbose("Custom Server Validation Callback Successful, SSL diagnostic data collection is enabled.");
}
catch (Exception e)
{
trace.Verbose($"SSL diagnostic data collection is disabled, due to issue:\n{e.Message}");
trace.Verbose($"Custom Server Validation Callback Unsuccessful, SSL diagnostic data collection is disabled, due to issue:\n{e.Message}");
return false;
}
return true;
Expand Down
33 changes: 33 additions & 0 deletions src/Test/L0/Util/VssUtilL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,38 @@ public void VerifyOverwriteVssConnectionSetting()
}
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public void VerifyVSSConnectionUsingLegacyHandler()
{
Regex _serverSideAgentPlatformMatchingRegex = new Regex("vstsagentcore-(.+)(?=/)", RegexOptions.Compiled | RegexOptions.IgnoreCase);

using (TestHostContext hc = new TestHostContext(this))
{
Tracing trace = hc.GetTrace();
// Act.
try
{
Environment.SetEnvironmentVariable("AZP_AGENT_USE_LEGACY_HTTP", "true");

var exception = Record.Exception(() =>
{
var connection = VssUtil.CreateConnection(
new Uri("https://github.com/Microsoft/vsts-agent"),
new VssCredentials(),
trace);
});

Assert.Null(exception);
}
finally
{
Environment.SetEnvironmentVariable("AZP_AGENT_USE_LEGACY_HTTP", "");
}
}

}
}
}
Loading