summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2014-09-15 07:14:33 -0400
committerBrad Fitzpatrick <bradfitz@golang.org>2014-09-15 07:14:33 -0400
commit567d00d491fd6ba602b1a68ee3232eb60a30bfd2 (patch)
tree565597179d2a71119587a0b3cf55b7f595e65230 /src/net
parenta2176217a1f58cd2bf2f1afab39e0447807e7de1 (diff)
downloadgo-567d00d491fd6ba602b1a68ee3232eb60a30bfd2.tar.gz
net/http: don't call FileSystem.Open with unclean index.html path
Fixes Issue 8722 LGTM=adg R=adg CC=golang-codereviews https://codereview.appspot.com/142090043
Diffstat (limited to 'src/net')
-rw-r--r--src/net/http/fs.go2
-rw-r--r--src/net/http/fs_test.go37
2 files changed, 38 insertions, 1 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go
index bae902cd2..7bd777b71 100644
--- a/src/net/http/fs.go
+++ b/src/net/http/fs.go
@@ -381,7 +381,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
// use contents of index.html for directory, if present
if d.IsDir() {
- index := name + indexPage
+ index := strings.TrimSuffix(name, "/") + indexPage
ff, err := fs.Open(index)
if err == nil {
defer ff.Close()
diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
index a6f33cc42..8770d9b41 100644
--- a/src/net/http/fs_test.go
+++ b/src/net/http/fs_test.go
@@ -877,4 +877,41 @@ func TestLinuxSendfileChild(*testing.T) {
}
}
+func TestFileServerCleanPath(t *testing.T) {
+ tests := []struct {
+ path string
+ wantCode int
+ wantOpen []string
+ }{
+ {"/", 200, []string{"/", "/index.html"}},
+ {"/dir", 301, []string{"/dir"}},
+ {"/dir/", 200, []string{"/dir", "/dir/index.html"}},
+ }
+ for _, tt := range tests {
+ var log []string
+ rr := httptest.NewRecorder()
+ req, _ := NewRequest("GET", "http://foo.localhost"+tt.path, nil)
+ FileServer(fileServerCleanPathDir{&log}).ServeHTTP(rr, req)
+ if !reflect.DeepEqual(log, tt.wantOpen) {
+ t.Logf("For %s: Opens = %q; want %q", tt.path, log, tt.wantOpen)
+ }
+ if rr.Code != tt.wantCode {
+ t.Logf("For %s: Response code = %d; want %d", tt.path, rr.Code, tt.wantCode)
+ }
+ }
+}
+
+type fileServerCleanPathDir struct {
+ log *[]string
+}
+
+func (d fileServerCleanPathDir) Open(path string) (File, error) {
+ *(d.log) = append(*(d.log), path)
+ if path == "/" || path == "/dir" || path == "/dir/" {
+ // Just return back something that's a directory.
+ return Dir(".").Open(".")
+ }
+ return nil, os.ErrNotExist
+}
+
type panicOnSeek struct{ io.ReadSeeker }