| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using Semmle.Util; | ||
| using Semmle.Util.Logging; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.DependencyFetching | ||
| { | ||
| public class DependabotProxy : IDisposable | ||
| { | ||
| private readonly string host; | ||
| private readonly string port; | ||
|
|
||
| /// <summary> | ||
| /// The full address of the Dependabot proxy, if available. | ||
| /// </summary> | ||
| internal string Address { get; } | ||
| /// <summary> | ||
| /// The path to the temporary file where the certificate is stored. | ||
| /// </summary> | ||
| internal string? CertificatePath { get; private set; } | ||
| /// <summary> | ||
| /// The certificate used for the Dependabot proxy. | ||
| /// </summary> | ||
| internal X509Certificate2? Certificate { get; private set; } | ||
|
|
||
| internal static DependabotProxy? GetDependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory) | ||
| { | ||
| // Setting HTTP(S)_PROXY and SSL_CERT_FILE have no effect on Windows or macOS, | ||
| // but we would still end up using the Dependabot proxy to check for feed reachability. | ||
| // This would result in us discovering that the feeds are reachable, but `dotnet` would | ||
| // fail to connect to them. To prevent this from happening, we do not initialise an | ||
| // instance of `DependabotProxy` on those platforms. | ||
| if (SystemBuildActions.Instance.IsWindows() || SystemBuildActions.Instance.IsMacOs()) return null; | ||
|
|
||
| // Obtain and store the address of the Dependabot proxy, if available. | ||
| var host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost); | ||
| var port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port)) | ||
| { | ||
| logger.LogInfo("No Dependabot proxy credentials are configured."); | ||
| return null; | ||
| } | ||
|
|
||
| var result = new DependabotProxy(host, port); | ||
| logger.LogInfo($"Dependabot proxy configured at {result.Address}"); | ||
|
|
||
| // Obtain and store the proxy's certificate, if available. | ||
| var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(cert)) | ||
| { | ||
| logger.LogInfo("No certificate configured for Dependabot proxy."); | ||
|
|
||
| var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy")); | ||
| Directory.CreateDirectory(certDirPath.FullName); | ||
|
|
||
| result.CertificatePath = Path.Join(certDirPath.FullName, "proxy.crt"); | ||
| var certFile = new FileInfo(result.CertificatePath); | ||
|
|
||
| using var writer = certFile.CreateText(); | ||
| writer.Write(cert); | ||
| writer.Close(); | ||
|
|
||
| logger.LogInfo($"Stored Dependabot proxy certificate at {result.CertificatePath}"); | ||
|
|
||
| result.Certificate = X509Certificate2.CreateFromPem(cert); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private DependabotProxy(string host, string port) | ||
| { | ||
| this.host = host; | ||
| this.port = port; | ||
| this.Address = $"http://{this.host}:{this.port}"; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.Certificate?.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -3,7 +3,9 @@ | |
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| Expand All | @@ -20,6 +22,7 @@ internal sealed partial class NugetPackageRestorer : IDisposable | |
| private readonly FileProvider fileProvider; | ||
| private readonly FileContent fileContent; | ||
| private readonly IDotNet dotnet; | ||
| private readonly DependabotProxy? dependabotProxy; | ||
| private readonly IDiagnosticsWriter diagnosticsWriter; | ||
| private readonly TemporaryDirectory legacyPackageDirectory; | ||
| private readonly TemporaryDirectory missingPackageDirectory; | ||
| Expand All | @@ -32,13 +35,15 @@ public NugetPackageRestorer( | |
| FileProvider fileProvider, | ||
| FileContent fileContent, | ||
| IDotNet dotnet, | ||
| DependabotProxy? dependabotProxy, | ||
| IDiagnosticsWriter diagnosticsWriter, | ||
| ILogger logger, | ||
| ICompilationInfoContainer compilationInfoContainer) | ||
| { | ||
| this.fileProvider = fileProvider; | ||
| this.fileContent = fileContent; | ||
| this.dotnet = dotnet; | ||
| this.dependabotProxy = dependabotProxy; | ||
| this.diagnosticsWriter = diagnosticsWriter; | ||
| this.logger = logger; | ||
| this.compilationInfoContainer = compilationInfoContainer; | ||
| Expand Down Expand Up | @@ -588,7 +593,25 @@ private static async Task ExecuteGetRequest(string address, HttpClient httpClien | |
| private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, bool allowExceptions = true) | ||
| { | ||
| logger.LogInfo($"Checking if Nuget feed '{feed}' is reachable..."); | ||
| using HttpClient client = new(); | ||
|
|
||
| // Configure the HttpClient to be aware of the Dependabot Proxy, if used. | ||
| HttpClientHandler httpClientHandler = new(); | ||
| if (this.dependabotProxy != null) | ||
| { | ||
| httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address); | ||
|
|
||
| if (this.dependabotProxy.Certificate != null) | ||
| { | ||
|
Comment thread
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityDo we need this, because we need to support a certificate that's not signed by a well-known trusted root cert authority?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityCorrect. The Dependabot Proxy uses a self-signed certificate that is generated when we initialise it in the Default Setup workflow. That's then passed to us in an environment variable, and we should trust it.
Sorry, something went wrong.
All reactions
|
||
| httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) => | ||
| { | ||
| chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; | ||
| chain.ChainPolicy.CustomTrustStore.Add(this.dependabotProxy.Certificate); | ||
| return chain.Build(cert); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| using HttpClient client = new(httpClientHandler); | ||
|
|
||
| for (var i = 0; i < tryCount; i++) | ||
| { | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'm somewhat surprised that this is needed. The writer is disposed at the end of this method, and we wouldn't access the file elsewhere before that. If you want to control the disposal a bit more fine grained, you could still use a using block, and you could get rid of the Close call.
using(var writer = certFile.CreateText()) { writer.Write(cert); }Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.