Skip to content

Commit e8f9c58

Browse files
committed
Add setting to force TLS 1.2
Per #711 (and #699 before it), some users need to force TLS 1.2 for libman to work. This change adds a new user setting, "forcetls12", which will set libman to use TLS1.2 for any HttpClient it creates. I verified via WireShark that the traffic to services (cdnjs, etc) that libman calls to switched from 1.3 (my system default) to 1.2 when this setting was in place, and returned to 1.3 by unsetting it. I could also see that other connections from within VS were still using TLS1.3 so we didn't affect other components on accident.
1 parent 4589d14 commit e8f9c58

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/LibraryManager/Cache/WebRequestHandler.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Microsoft.Web.LibraryManager.Configuration;
1111
using Microsoft.Web.LibraryManager.Contracts;
12+
using Microsoft.Web.LibraryManager.Contracts.Configuration;
1213
using Microsoft.Web.LibraryManager.Helpers;
1314

1415
namespace Microsoft.Web.LibraryManager.Cache
@@ -20,12 +21,14 @@ internal class WebRequestHandler : IWebRequestHandler, IDisposable
2021
{
2122
private readonly ConcurrentDictionary<string, HttpClient> _cachedHttpClients = new ConcurrentDictionary<string, HttpClient>();
2223

23-
public static IWebRequestHandler Instance { get; } = new WebRequestHandler(ProxySettings.Default);
24+
public static IWebRequestHandler Instance { get; } = new WebRequestHandler(ProxySettings.Default, Settings.DefaultSettings);
2425
private readonly ProxySettings _proxySettings;
26+
private readonly ISettings _settings;
2527

26-
public WebRequestHandler(ProxySettings proxySettings)
28+
public WebRequestHandler(ProxySettings proxySettings, ISettings settings)
2729
{
2830
_proxySettings = proxySettings;
31+
_settings = settings;
2932
}
3033

3134
public void Dispose()
@@ -55,9 +58,14 @@ public async Task<Stream> GetStreamAsync(string url, CancellationToken cancellat
5558

5659
private HttpClient CreateHttpClient(string url)
5760
{
61+
5862
#pragma warning disable CA2000 // Dispose objects before losing scope
5963
var httpMessageHandler = new HttpClientHandler();
6064
#pragma warning restore CA2000 // Dispose objects before losing scope
65+
if (_settings.TryGetValue("forcetls12", out string value) && value.Length > 0)
66+
{
67+
httpMessageHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
68+
}
6169
httpMessageHandler.Proxy = _proxySettings.GetProxy(new Uri(url));
6270
var httpClient = new HttpClient(httpMessageHandler);
6371
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd($"LibraryManager/{ThisAssembly.AssemblyFileVersion}");

0 commit comments

Comments
 (0)