Skip to content

Confusing behavior of Uri.IsWellFormedUriString in .Net Core and .Net Framework #37634

Description

@t-bzhan

The behavior of Uri.IsWellFormedUriString is very confusing. Below code snippets:

            List<string> absoluteUrls = new List<string>()
            {
                "https://api.contoso.com/query?q=世界&accessKey=acd%3D",
                "https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D",
                "https://api.contoso.com/query?q=世界&accessKey=acd=",
                "https://api.contoso.com/query?q=world&accessKey=acd%3D"
            };

            foreach (var url in absoluteUrls)
            {
                Console.WriteLine($"{url}: {Uri.IsWellFormedUriString(url, UriKind.Absolute)}");
            }

            List<string> relativeUrls = new List<string>()
            {
                "/query?q=世界&accessKey=acd%3D",
                "/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D",
                "/query?q=世界&accessKey=acd=",
                "/query?q=world&accessKey=acd%3D"
            };

            foreach (var url in relativeUrls)
            {
                Console.WriteLine($"{url}: {Uri.IsWellFormedUriString(url, UriKind.Relative)}");
            }

will output following results in both .Net core 3.1 and .Net framework 4.7.2:

URL Result
https://api.contoso.com/query?q=世界&accessKey=acd%3D False
https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D False
https://api.contoso.com/query?q=世界&accessKey=acd= True
https://api.contoso.com/query?q=world&accessKey=acd%3D True
/query?q=世界&accessKey=acd%3D False
/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D True
/query?q=世界&accessKey=acd= False
/query?q=world&accessKey=acd%3D True

The "False" result of https://api.contoso.com/query?q=世界&accessKey=acd%3D gives me the impression that Uri.IsWellFormedUriString is not able to handle percent-encodings of reserved characters defined in RFC 3986, but I could not explain why https://api.contoso.com/query?q=world&accessKey=acd%3D return "True"

I also found that there are inconsistencies between the parsing result of UriKind.Absolute and UriKind.Relative for the same URI. /query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D return "True" while https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D return "False".

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions