Skip to content

Commit

Permalink
proxy/proxy.go: disable directory indexes
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
  • Loading branch information
unclejack committed Apr 14, 2017
1 parent 53c548a commit 351b61b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
30 changes: 29 additions & 1 deletion proxy/proxy.go
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"sync"
"time"

Expand Down Expand Up @@ -239,6 +240,33 @@ func (s *Server) Stop() {
s.wg.Wait()
}

type dirWithoutIndex struct {
fs http.FileSystem
}

func (d dirWithoutIndex) Open(name string) (http.File, error) {
f, err := d.fs.Open(name)

if err != nil {
return nil, err
}

if name == "/" {
return f, nil
}

stat, _ := f.Stat()
if stat.IsDir() {
return nil, os.ErrNotExist
}

return f, nil
}

func newDirWithoutIndexes(dir string) dirWithoutIndex {
return dirWithoutIndex{fs: http.Dir(dir)}
}

//
// staticFileServer returns a staticFileHandler which serves the UI and its assets.
// this is necessary so that we can set response headers.
Expand Down Expand Up @@ -303,7 +331,7 @@ func addRoutes(s *Server, router *mux.Router) {
// UI: static files which are served from the root
//
root := "/"
staticHandler := staticFileServer(http.Dir(uiDirectory))
staticHandler := staticFileServer(newDirWithoutIndexes(uiDirectory))

router.PathPrefix(root).Handler(http.StripPrefix(root, staticHandler))
}
Expand Down
14 changes: 14 additions & 0 deletions systemtests/basic_test.go
Expand Up @@ -113,6 +113,20 @@ func (s *systemtestSuite) TestVersion(c *C) {
})
}

// TestNoDirectoryIndex tests that /assets returns 404 and that /
// returns a 200 status.
func (s *systemtestSuite) TestNoDirectoryIndex(c *C) {
runTest(func(ms *MockServer) {
resp, _ := proxyGet(c, noToken, "/")
c.Assert(resp.StatusCode, Equals, 200)
})

runTest(func(ms *MockServer) {
resp, _ := proxyGet(c, noToken, "/assets")
c.Assert(resp.StatusCode, Equals, 404)
})
}

// TestHealthCheck tests that /health endpoint responds properly.
func (s *systemtestSuite) TestHealthCheck(c *C) {
runTest(func(ms *MockServer) {
Expand Down

0 comments on commit 351b61b

Please sign in to comment.