Skip to content

Uri.IsWellFormedOriginalString false negatives #72632

Description

@MihaZupan

IsWellFormedOriginalString (and therefore also IsWellFormedUriString) will report false negatives in the following cases:

  • Case 1
    • Occurs if the Uri contains both
    • This happens because EscapeUnescapeIri removes the escaping from these characters, but CheckCanonical later reports that they should have been escaped (which they were, but we unescaped them)
    • Example: http://a/%41%22 (%41 is A (unreserved) and %22 is ")
  • Case 2
    • Occurs if the Uri contains both
      • An (escaped or not) non-ASCII character (except those outside the IRI range)
      • An escaped character from the [0, 1F] range, the ;/?:@&=+$,#[]!'()*%\ set (reserved + %, \), the " <>`^ set or outside the IRI range
    • This happens because EscapeUnescapeIri removes some escaping and CheckCanonical then sees a mismatch of escaped, unescaped and non-ASCII characters
    • Example: http://a/ű%20 (ü is non-ASCII and %20 is )

Case 1. occurred in #70929.
Case 2. occurred in #21626, #34031, #37634, #64249, VS Feedback.

My suggestion as a workaround is to not use IsWellFormedOriginalString at all. Odds are that in most cases you don't care about the sort of validation that it does anyway.
Consider using something like this instead:

static bool IsValidHttpUri(string uriString, out Uri uri) =>
    Uri.TryCreate(uriString, UriKind.Absolute, out uri) &&
    (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions