0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

Do not url decode when baseURL is set.

This commit is contained in:
mattp 2022-03-14 16:48:40 +01:00
parent 4a408f9c62
commit 9a8c38e365
2 changed files with 13 additions and 10 deletions

15
data.go
View file

@ -331,7 +331,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
return nil, URLError{"too few path segments", r.URL}
}
req.URL, err = parseURL(parts[1])
req.URL, err = parseURL(parts[1], (baseURL == nil))
if err != nil {
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
}
@ -343,7 +343,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
}
if !req.URL.IsAbs() {
return nil, URLError{"must provide absolute remote URL", r.URL}
return nil, URLError{"must provide absolute remote URL", req.URL}
}
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
@ -367,15 +367,18 @@ var reCleanedURL = regexp.MustCompile(`^(https?):/+([^/])`)
// parseURL parses s as a URL, handling URLs that have been munged by
// path.Clean or a webserver that collapses multiple slashes.
func parseURL(s string) (*url.URL, error) {
func parseURL(s string, urlDecode bool) (*url.URL, error) {
var err error
// convert http:/example.com to http://example.com
s = reCleanedURL.ReplaceAllString(s, "$1://$2")
s, err = decodeURL(s)
if err != nil {
return nil, err
// only decode remote url param if baseURL is not set
if urlDecode {
s, err = decodeURL(s)
if err != nil {
return nil, err
}
}
return url.Parse(s)

View file

@ -247,12 +247,12 @@ func TestNewRequest_BaseURL(t *testing.T) {
"http://localhost/x/../foo/bar",
"http://example.com/foo/bar", emptyOptions, false,
},
// relative remote urls should not have URL Decoding even if
// they start with http... (dirname)
// relative remote urls (baseURL set) should not have URL Decoding even if
// they start with http
{
"http://example.com/hello/",
"http://localhost/x/httpdir/rela%20tive",
"http://example.com/hello/httpdir/rela%20tive", emptyOptions, false,
"http://localhost/x/https%3A%2F%2Fimg.example.com%2Fpic.jpg",
"http://example.com/hello/https%3A%2F%2Fimg.example.com%2Fpic.jpg", emptyOptions, false,
},
}