-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Several WebProxy-related fixes (#50368)
- Split browser from non-browser to avoid CA1416 suppressions, UnsupportedOSPlatform attributes, and PNSEs when used from browser - Cache domain name and local addresses for use in IsLocal, along with clearing of the caches upon network change detection - Removing redundant IPAddress.IsLoopback check that's covered by previous Uri.IsLoopback check - Change IsMatchInBypassList to use string interpolation, such that the non-default port case will get better implicitly with C# 10 interpolated string improvements
- Loading branch information
1 parent
29e8a13
commit a221324
Showing
4 changed files
with
114 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.Browser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.Serialization; | ||
|
||
namespace System.Net | ||
{ | ||
public partial class WebProxy : IWebProxy, ISerializable | ||
{ | ||
private bool IsLocal(Uri host) | ||
{ | ||
if (host.IsLoopback) | ||
{ | ||
return true; | ||
} | ||
|
||
string hostString = host.Host; | ||
return | ||
!IPAddress.TryParse(hostString, out _) && | ||
!hostString.Contains('.'); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/libraries/System.Net.WebProxy/src/System/Net/WebProxy.NonBrowser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net.NetworkInformation; | ||
using System.Runtime.Serialization; | ||
using System.Threading; | ||
|
||
namespace System.Net | ||
{ | ||
public partial class WebProxy : IWebProxy, ISerializable | ||
{ | ||
private static volatile string? s_domainName; | ||
private static volatile IPAddress[]? s_localAddresses; | ||
private static int s_networkChangeRegistered; | ||
|
||
private bool IsLocal(Uri host) | ||
{ | ||
if (host.IsLoopback) | ||
{ | ||
return true; | ||
} | ||
|
||
string hostString = host.Host; | ||
|
||
if (IPAddress.TryParse(hostString, out IPAddress? hostAddress)) | ||
{ | ||
EnsureNetworkChangeRegistration(); | ||
IPAddress[] localAddresses = s_localAddresses ??= Dns.GetHostEntry(Dns.GetHostName()).AddressList; | ||
return Array.IndexOf(localAddresses, hostAddress) != -1; | ||
} | ||
|
||
// No dot? Local. | ||
int dot = hostString.IndexOf('.'); | ||
if (dot == -1) | ||
{ | ||
return true; | ||
} | ||
|
||
// If it matches the primary domain, it's local (whether or not the hostname matches). | ||
EnsureNetworkChangeRegistration(); | ||
string local = s_domainName ??= "." + IPGlobalProperties.GetIPGlobalProperties().DomainName; | ||
return | ||
local.Length == (hostString.Length - dot) && | ||
string.Compare(local, 0, hostString, dot, local.Length, StringComparison.OrdinalIgnoreCase) == 0; | ||
} | ||
|
||
/// <summary>Ensures we've registered with NetworkChange to clear out statically-cached state upon a network change notification.</summary> | ||
private static void EnsureNetworkChangeRegistration() | ||
{ | ||
if (s_networkChangeRegistered == 0) | ||
{ | ||
Register(); | ||
|
||
static void Register() | ||
{ | ||
if (Interlocked.Exchange(ref s_networkChangeRegistered, 1) != 0) | ||
{ | ||
return; | ||
} | ||
|
||
// Clear out cached state when we get notification of a network-related change. | ||
NetworkChange.NetworkAddressChanged += (s, e) => | ||
{ | ||
s_domainName = null; | ||
s_localAddresses = null; | ||
}; | ||
NetworkChange.NetworkAvailabilityChanged += (s, e) => | ||
{ | ||
s_domainName = null; | ||
s_localAddresses = null; | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters