-
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.
Add TargetHostName to QuicConnection (#84976)
* Add TargetHostName to QuicConnection Fixes #80508 * Make TargetHostName not nullable * Fix build * Fix build of tests * Fix failing tests * Code review feedback * Use unencoded hostname in user-facing properties/params * Fix failing tests * Revert unwanted changes * Add test for IDN cert validation * Fix test again * Fix trailing dot in hostname
- Loading branch information
Showing
13 changed files
with
232 additions
and
78 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/libraries/Common/src/System/Net/Security/TargetHostNameHelper.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,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Net.Security | ||
{ | ||
internal static class TargetHostNameHelper | ||
{ | ||
private static readonly IdnMapping s_idnMapping = new IdnMapping(); | ||
private static readonly IndexOfAnyValues<char> s_safeDnsChars = | ||
IndexOfAnyValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"); | ||
|
||
private static bool IsSafeDnsString(ReadOnlySpan<char> name) => | ||
name.IndexOfAnyExcept(s_safeDnsChars) < 0; | ||
|
||
internal static string NormalizeHostName(string? targetHost) | ||
{ | ||
if (string.IsNullOrEmpty(targetHost)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
// RFC 6066 section 3 says to exclude trailing dot from fully qualified DNS hostname | ||
targetHost = targetHost.TrimEnd('.'); | ||
|
||
try | ||
{ | ||
return s_idnMapping.GetAscii(targetHost); | ||
} | ||
catch (ArgumentException) when (IsSafeDnsString(targetHost)) | ||
{ | ||
// Seems like name that does not confrom to IDN but apers somewhat valid according to original DNS rfc. | ||
} | ||
|
||
return targetHost; | ||
} | ||
|
||
// Simplified version of IPAddressParser.Parse to avoid allocations and dependencies. | ||
// It purposely ignores scopeId as we don't really use so we do not need to map it to actual interface id. | ||
internal static unsafe bool IsValidAddress(string? hostname) | ||
{ | ||
if (string.IsNullOrEmpty(hostname)) | ||
{ | ||
return false; | ||
} | ||
|
||
ReadOnlySpan<char> ipSpan = hostname.AsSpan(); | ||
|
||
int end = ipSpan.Length; | ||
|
||
if (ipSpan.Contains(':')) | ||
{ | ||
// The address is parsed as IPv6 if and only if it contains a colon. This is valid because | ||
// we don't support/parse a port specification at the end of an IPv4 address. | ||
Span<ushort> numbers = stackalloc ushort[IPAddressParserStatics.IPv6AddressShorts]; | ||
|
||
fixed (char* ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) | ||
{ | ||
return IPv6AddressHelper.IsValidStrict(ipStringPtr, 0, ref end); | ||
} | ||
} | ||
else if (char.IsDigit(ipSpan[0])) | ||
{ | ||
long tmpAddr; | ||
|
||
fixed (char* ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) | ||
{ | ||
tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true); | ||
} | ||
|
||
if (tmpAddr != IPv4AddressHelper.Invalid && end == ipSpan.Length) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.