Skip to content

Commit

Permalink
shared/api/url: Fix double path encoding issue
Browse files Browse the repository at this point in the history
Go's net.URL requires Path to be populated with unencoded path and
RawPath to be populated with the hint on how we want Path to be
encoded.

This avoids double path encoding when calling url.EscapedPath(),
which is used in url.String().

Fixes #12398

Signed-off-by: Thomas Parrott <[email protected]>
  • Loading branch information
tomponline committed Oct 18, 2023
1 parent b0099f3 commit bd3f2b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
14 changes: 10 additions & 4 deletions shared/api/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ func (u *URL) Host(host string) *URL {
// Path sets the path of the URL from one or more path parts.
// It appends each of the pathParts (escaped using url.PathEscape) prefixed with "/" to the URL path.
func (u *URL) Path(pathParts ...string) *URL {
var b strings.Builder
var path, rawPath strings.Builder

for _, pathPart := range pathParts {
b.WriteString("/") // Build an absolute URL.
b.WriteString(url.PathEscape(pathPart))
// Generate unencoded path.
path.WriteString("/") // Build an absolute URL.
path.WriteString(pathPart)

// Generate encoded path hint (this will be used by u.URL.EncodedPath() to decide its methodology).
rawPath.WriteString("/") // Build an absolute URL.
rawPath.WriteString(url.PathEscape(pathPart))
}

u.URL.Path = b.String()
u.URL.Path = path.String()
u.URL.RawPath = rawPath.String()

return u
}
Expand Down
16 changes: 8 additions & 8 deletions shared/api/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (

func ExampleURL() {
u := NewURL()
fmt.Println(u.Path("1.0", "networks", "name-with-/-in-it"))
fmt.Println(u.Path("1.0", "networks", "name-with-/-in-it").String())
fmt.Println(u.Project("default"))
fmt.Println(u.Project("project-with-%-in-it"))
fmt.Println(u.Target(""))
fmt.Println(u.Target("member-with-%-in-it"))
fmt.Println(u.Host("example.com"))
fmt.Println(u.Scheme("https"))

// Output: /1.0/networks/name-with-%252F-in-it
// /1.0/networks/name-with-%252F-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// //example.com/1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// https://example.com/1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// Output: /1.0/networks/name-with-%2F-in-it
// /1.0/networks/name-with-%2F-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// //example.com/1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// https://example.com/1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
}

0 comments on commit bd3f2b7

Please sign in to comment.