Skip to content

Commit

Permalink
fix(engine/ui): disable directory listing (#6191)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Jun 7, 2022
1 parent 4d04ca4 commit 515b469
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion engine/ui/ui_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *reverseProxyWithFilter) ServeHTTP(rw http.ResponseWriter, req *http.Req
}

func (s *Service) uiServe(fs http.FileSystem, dir string) http.Handler {
fsh := http.FileServer(fs)
fsh := http.FileServer(uiFileSystem{fs})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if dir == s.DocsDir {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/docs")
Expand All @@ -139,3 +139,32 @@ func (s *Service) uiServe(fs http.FileSystem, dir string) http.Handler {
fsh.ServeHTTP(w, r)
})
}

type uiFileSystem struct {
base http.FileSystem
}

func (uifs uiFileSystem) Open(path string) (http.File, error) {
f, err := uifs.base.Open(path)
if err != nil {
return nil, os.ErrNotExist
}

s, err := f.Stat()
if err != nil {
return nil, os.ErrNotExist
}
if s.IsDir() {
index := filepath.Join(path, "index.html")
if _, err := uifs.base.Open(index); err != nil {
closeErr := f.Close()
if closeErr != nil {
return nil, closeErr
}

return nil, os.ErrNotExist
}
}

return f, nil
}

0 comments on commit 515b469

Please sign in to comment.