Skip to content

net/url: URL.String turns Path into Host when Host is empty, User is non-nil, and Path has no leading slash #81665

Closed

Description

@neild

The following is an LLM-written summary. The summary seems accurate to me. This was reported to us as a potential security issue. We feel that this is a bug, but we are classifying this as security hardening rather than a vulnerability.

In net/url, (*URL).String writes the //userinfo@ prefix whenever u.User != nil, even if u.Host == "", and only prepends
a leading / to u.Path when u.Host != "" and u.Path does not already begin with /.

As a result, when u.Host == "", u.User != nil, and u.Path does not start with /, u.String() concatenates //userinfo@
directly with u.Path, causing the first segment of u.Path to be parsed as Host if the string is reparsed:

u, _ := url.Parse("example.com/path") // Scheme="", Host="", Path="example.com/path"
u.Scheme = "https"
u.User = url.User("user")

s := u.String() // "https://user@example.com/path"
u2, _ := url.Parse(s)
fmt.Println(u2.Host) // "example.com"

(Related to #78328.)

Activity

  1. gopherbot commented on Sep 22, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/837125 mentions this issue: net/url: avoid turning Path into Host under some circumstances

  2. added this to the Go1.28 milestone on Sep 29, 2026
  3. added a commit that references this issue on Oct 7, 2026
    a904b1c
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

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions