Skip to content

Commit 6a40583

Browse files
TurbineJoshuah2non
andauthored
Add URL unescaping for file query parameter. (#414)
* Add URL unescaping for file query parameter. * Fix go.mod declared package. * Add the net/url dependency. * Fix return values. * Fix return values correctly. * Fix call site. * Update go.mod --------- Co-authored-by: Tom <[email protected]>
1 parent 0d241c8 commit 6a40583

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

source_fs.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"net/http"
7+
"net/url"
68
"path"
79
"strings"
810
)
@@ -18,16 +20,24 @@ func NewFileSystemImageSource(config *SourceConfig) ImageSource {
1820
}
1921

2022
func (s *FileSystemImageSource) Matches(r *http.Request) bool {
21-
return r.Method == http.MethodGet && s.getFileParam(r) != ""
23+
file, err := s.getFileParam(r)
24+
if err != nil {
25+
return false
26+
}
27+
return r.Method == http.MethodGet && file != ""
2228
}
2329

2430
func (s *FileSystemImageSource) GetImage(r *http.Request) ([]byte, error) {
25-
file := s.getFileParam(r)
31+
file, err := s.getFileParam(r)
32+
if err != nil {
33+
return nil, err
34+
}
35+
2636
if file == "" {
2737
return nil, ErrMissingParamFile
2838
}
2939

30-
file, err := s.buildPath(file)
40+
file, err = s.buildPath(file)
3141
if err != nil {
3242
return nil, err
3343
}
@@ -51,8 +61,13 @@ func (s *FileSystemImageSource) read(file string) ([]byte, error) {
5161
return buf, nil
5262
}
5363

54-
func (s *FileSystemImageSource) getFileParam(r *http.Request) string {
55-
return r.URL.Query().Get("file")
64+
func (s *FileSystemImageSource) getFileParam(r *http.Request) (string, error) {
65+
unescaped, err := url.QueryUnescape(r.URL.Query().Get("file"))
66+
if err != nil{
67+
return "", fmt.Errorf("failed to unescape file param: %w", err)
68+
}
69+
70+
return unescaped, nil
5671
}
5772

5873
func init() {

source_fs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestFileSystemImageSource(t *testing.T) {
1212
var body []byte
1313
var err error
14-
const fixtureFile = "testdata/large.jpg"
14+
const fixtureFile = "testdata/large image.jpg"
1515

1616
source := NewFileSystemImageSource(&SourceConfig{MountPath: "testdata"})
1717
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
@@ -27,7 +27,7 @@ func TestFileSystemImageSource(t *testing.T) {
2727
}
2828

2929
file, _ := os.Open(fixtureFile)
30-
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?file=large.jpg", file)
30+
r, _ := http.NewRequest(http.MethodGet, "http://foo/bar?file=large%20image.jpg", file)
3131
w := httptest.NewRecorder()
3232
fakeHandler(w, r)
3333

0 commit comments

Comments
 (0)