Skip to content

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

Closed

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 = "[email protected]",
    Password = "hunter2",
};
Console.WriteLine(builder.Uri);

Expected behavior

The following URI is written to the console.

https://mallory%40hackers.net:[email protected]/

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

Activity

  1. ghost added
    untriaged
    on Aug 26, 2022
  2. MihaZupan commented on Aug 29, 2022

    @MihaZupan
    Member

    I agree that throwing when accessing builder.Uri is odd and the exception message is not ideal.

    The spec defines user-info as unreserved / pct-encoded / sub-delims / ":".
    The problematic characters are "/" / "\" / "?" / "#" / "@" (gen-delims excluding [ and ]).

    Blindly encoding the input (Uri.EscapeDataString) would be a breaking change, as we would start double-encoding already-encoded values.

    The options here are:

    • Do nothing (users already can and do call Uri.EscapeDataString on the input)
    • Throw for invalid characters when UserName or Password are set
    • Only percent-encode the problematic characters

    Given that the implementation already encodes some characters (e.g. spaces or non-encoded non-ASCII), I am leaning towards the latter.

    Example replacement code that you could use (or that could live in UriBuilder)
    string Encode(string input)
    {
        if (input.AsSpan().IndexOfAny(@"/\?$@") < 0) return input;
    
        return input
            .Replace("/", "%2F", StringComparison.Ordinal)
            .Replace(@"\", "%5C", StringComparison.Ordinal)
            .Replace("?", "%3F", StringComparison.Ordinal)
            .Replace("#", "%23", StringComparison.Ordinal)
            .Replace("@", "%40", StringComparison.Ordinal);
    }
  3. added
    help wanted
    and removed
    untriaged
    on Aug 30, 2022
  4. added this to the Future milestone on Aug 30, 2022
  5. karelz commented on Aug 30, 2022

    @karelz
    Member

    Triage: Should be fairly straightforward (see code in #74662 (comment) - bunch of Replace calls), but not critical.

  6. added
    good first issue
    on Aug 30, 2022
  7. ghost added
    in-pr
    on Sep 1, 2022
  8. ghost removed
    in-pr
    on Sep 7, 2022
  9. ghost locked as resolved and limited conversation to collaborators on Oct 7, 2022
  10. modified the milestones: Future, 8.0.0 on Mar 22, 2023
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

    area-System.Netbuggood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions