Skip to content

UriBuilder should percent-encode its UserName or Password properties #74662

@0xced

Description

@0xced

Description

Constructing a URI with the UriBuilder class should produce valid URIs when setting valid UserName and/or Password properties. Instead, an UriFormatException is thrown if the user name or password contains a character that should be percent-encoded.

It should be the responsibility of the UriBuilder to properly percent-encode characters in the UserName and/or Password properties. Note that the documentation of the properties does not mention anything about percent-escaping so by the principle of least astonishment I'd expect the UriBuilder to properly escape what needs to be escaped.

Reproduction Steps

Run the following code:

var builder = new UriBuilder("https://www.microsoft.com")
{
    UserName = "mallory@hackers.net",
    Password = "hunter2",
};
Console.WriteLine(builder.Uri);

Expected behavior

The following URI is written to the console.

https://mallory%40hackers.net:hunter2@www.microsoft.com/

Actual behavior

An UriFormatException is thrown with the following message.

Invalid URI: Invalid port specified.

Regression?

I don't know whether this is a regression or not.

Known Workarounds

Manually percent-escaping the UserName and Password but that should not be necessary.

Configuration

Version of .NET: .NET 6.0.8
OS: All
Architecture: All

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Netbuggood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions