-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add TargetHostName to QuicConnection #84976
New issue
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
Merged
rzikm
merged 12 commits into
dotnet:main
from
rzikm:80508-API-Proposal-Add-TargetHostName-to-QuicConnection
Apr 25, 2023
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
40bce02
Add TargetHostName to QuicConnection
rzikm d6bd50e
Make TargetHostName not nullable
rzikm 40af6e3
Fix build
rzikm f34abe2
Fix build of tests
rzikm d76cb1e
Fix failing tests
rzikm 04dcff6
Code review feedback
rzikm a5c124b
Use unencoded hostname in user-facing properties/params
rzikm aae6f48
Fix failing tests
rzikm 2fb014a
Revert unwanted changes
rzikm 98b827b
Add test for IDN cert validation
rzikm 15aa758
Fix test again
rzikm 995949a
Fix trailing dot in hostname
rzikm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/libraries/Common/src/System/Net/Security/TargetHostNameHelper.cs
This file contains hidden or 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,82 @@ | ||
| // 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('.'); | ||
|
|
||
| // RFC 6066 forbids IP literals | ||
| if (IsValidAddress(targetHost)) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| 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. | ||
| private static unsafe bool IsValidAddress(ReadOnlySpan<char> ipSpan) | ||
| { | ||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.