Skip to content

net/url: unexpected url encoding for ! in path #40894

@mh47838704

Description

@mh47838704

What version of Go are you using (go version)?

$ go version

Does this issue reproduce with the latest release?

alway Exist

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

func TestEncodedUrlPathEncode(t *testing.T) {
	urlString := "http://www.xyz.com/!^test.jpg"
	parsedUrl, _ := url.Parse(urlString)
	rawUrl := parsedUrl.EscapedPath()
	fmt.Println("encodedUrl:",rawUrl)
}

func TestEncodedUrlPathEncode(t *testing.T) {
	urlString := "http://www.xyz.com/!test.jpg"
	parsedUrl, _ := url.Parse(urlString)
	rawUrl := parsedUrl.EscapedPath()
	fmt.Println("encodedUrl:",rawUrl)
}

What did you expect to see?

for example

originUrl:https://github.com/!test.jpg
encodedUrl: https://github.com/!test.jpg    --- result is expected

originUrl: https://github.com/!^test.jpg
encodedUrl: https://github.com/%21%5Etest.jpg
expected: https://github.com/!%5eds.jpg

What did you see instead?

func (u *URL) EscapedPath() string {
	if u.RawPath != "" && validEncoded(u.RawPath, encodePath) {
		p, err := unescape(u.RawPath, encodePath)
		if err == nil && p == u.Path {
			return u.RawPath
		}
	}
	if u.Path == "*" {
		return "*" // don't escape (Issue 11202)
	}
	return escape(u.Path, encodePath)
}

i found the code int sdk, "validEncoded" method treat the "!" as encoded, but in "escape" define "!" need encoed when mode is “encodePath”, where is judged in method "shouldEscape", logic conflict, so the result is not expected.

and i found other language do not encode "!" "(" ")" , such as python

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions