Skip to content
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

Simplify invalid hostname check in DomainNameHelper #90088

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/libraries/System.Private.Uri/src/System/DomainNameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ internal static class DomainNameHelper
private static readonly SearchValues<char> s_validChars =
SearchValues.Create("-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz.");

// For IRI, we're accepting anything non-ascii, so invert the condition to just check for invalid ascii characters
private static readonly SearchValues<char> s_iriInvalidAsciiChars = SearchValues.Create(
// For IRI, we're accepting anything non-ascii (except 0x80-0x9F), so invert the condition to search for invalid ascii characters.
private static readonly SearchValues<char> s_iriInvalidChars = SearchValues.Create(
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F" +
"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F" +
" !\"#$%&'()*+,/:;<=>?@[\\]^`{|}~\u007F");
" !\"#$%&'()*+,/:;<=>?@[\\]^`{|}~\u007F" +
"\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F" +
"\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F");

private static readonly SearchValues<char> s_asciiLetterUpperOrColonChars =
SearchValues.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZ:");
Expand Down Expand Up @@ -94,7 +96,7 @@ internal static string ParseCanonicalName(string str, int start, int end, ref bo
public static bool IsValid(ReadOnlySpan<char> hostname, bool iri, bool notImplicitFile, out int length)
{
int invalidCharOrDelimiterIndex = iri
? hostname.IndexOfAny(s_iriInvalidAsciiChars)
? hostname.IndexOfAny(s_iriInvalidChars)
: hostname.IndexOfAnyExcept(s_validChars);

if (invalidCharOrDelimiterIndex >= 0)
Expand Down Expand Up @@ -146,13 +148,6 @@ public static bool IsValid(ReadOnlySpan<char> hostname, bool iri, bool notImplic
ReadOnlySpan<char> label = hostname.Slice(0, labelLength);
if (!Ascii.IsValid(label))
{
// s_iriInvalidAsciiChars confirmed everything in [0, 7F] range.
// Chars in [80, 9F] range are also invalid, check for them now.
if (hostname.ContainsAnyInRange('\u0080', '\u009F'))
{
return false;
}

// Account for the ACE prefix ("xn--")
labelLength += 4;

Expand Down
Loading