Skip to content

Commit dcfcb88

Browse files
committed
C#: Load Dependabot Proxy certificate in DependabotProxy, and implement IDisposable
1 parent 5a4f77f commit dcfcb88

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Security.Cryptography.X509Certificates;
45
using Semmle.Util;
56
using Semmle.Util.Logging;
67

78
namespace Semmle.Extraction.CSharp.DependencyFetching
89
{
9-
public class DependabotProxy
10+
public class DependabotProxy : IDisposable
1011
{
1112
private readonly string? host;
1213
private readonly string? port;
@@ -20,6 +21,10 @@ public class DependabotProxy
2021
/// The path to the temporary file where the certificate is stored.
2122
/// </summary>
2223
internal readonly string? CertificatePath;
24+
/// <summary>
25+
/// The certificate used for the Dependabot proxy.
26+
/// </summary>
27+
internal readonly X509Certificate2? Certificate;
2328

2429
/// <summary>
2530
/// Gets a value indicating whether a Dependabot proxy is configured.
@@ -60,6 +65,8 @@ internal DependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory
6065
writer.Write(cert);
6166

6267
logger.LogInfo($"Stored Dependabot proxy certificate at {this.CertificatePath}");
68+
69+
this.Certificate = new X509Certificate2(this.CertificatePath);
6370
}
6471

6572
internal void ApplyProxy(ILogger logger, ProcessStartInfo startInfo)
@@ -73,5 +80,13 @@ internal void ApplyProxy(ILogger logger, ProcessStartInfo startInfo)
7380
startInfo.EnvironmentVariables.Add("HTTPS_PROXY", this.Address);
7481
startInfo.EnvironmentVariables.Add("SSL_CERT_FILE", this.certFile?.FullName);
7582
}
83+
84+
public void Dispose()
85+
{
86+
if (this.Certificate != null)
87+
{
88+
this.Certificate.Dispose();
89+
}
90+
}
7691
}
7792
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ private void AnalyseProject(FileInfo project)
545545
public void Dispose()
546546
{
547547
nugetPackageRestorer?.Dispose();
548+
dependabotProxy.Dispose();
548549
if (cleanupTempWorkingDirectory)
549550
{
550551
tempWorkingDirectory?.Dispose();

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,12 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount,
600600
{
601601
httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address);
602602

603-
if (!String.IsNullOrEmpty(this.dependabotProxy.CertificatePath))
603+
if (this.dependabotProxy.Certificate != null)
604604
{
605-
X509Certificate2 proxyCert = new X509Certificate2(this.dependabotProxy.CertificatePath);
606605
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) =>
607606
{
608607
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
609-
chain.ChainPolicy.CustomTrustStore.Add(proxyCert);
608+
chain.ChainPolicy.CustomTrustStore.Add(this.dependabotProxy.Certificate);
610609
return chain.Build(cert);
611610
};
612611
}

0 commit comments

Comments
 (0)