Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy/proxy.go: disable directory indexes #133

Merged
merged 1 commit into from Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one exception we need to make here: /

If the name is /, you can return the file descriptor normally. Otherwise, the UI will not work. 😄

Making this change causes the currently failing test (systemtestSuite.TestUIResponseHeaders) to pass, so we'll need to add another one which hits a different directory like /assets or something.

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
12 changes: 12 additions & 0 deletions systemtests/basic_test.go
Expand Up @@ -113,6 +113,18 @@ 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)

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