Skip to content

Commit 515b469

Browse files
authored
fix(engine/ui): disable directory listing (#6191)
Signed-off-by: francois samin <[email protected]>
1 parent 4d04ca4 commit 515b469

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

engine/ui/ui_router.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (r *reverseProxyWithFilter) ServeHTTP(rw http.ResponseWriter, req *http.Req
123123
}
124124

125125
func (s *Service) uiServe(fs http.FileSystem, dir string) http.Handler {
126-
fsh := http.FileServer(fs)
126+
fsh := http.FileServer(uiFileSystem{fs})
127127
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
128128
if dir == s.DocsDir {
129129
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/docs")
@@ -139,3 +139,32 @@ func (s *Service) uiServe(fs http.FileSystem, dir string) http.Handler {
139139
fsh.ServeHTTP(w, r)
140140
})
141141
}
142+
143+
type uiFileSystem struct {
144+
base http.FileSystem
145+
}
146+
147+
func (uifs uiFileSystem) Open(path string) (http.File, error) {
148+
f, err := uifs.base.Open(path)
149+
if err != nil {
150+
return nil, os.ErrNotExist
151+
}
152+
153+
s, err := f.Stat()
154+
if err != nil {
155+
return nil, os.ErrNotExist
156+
}
157+
if s.IsDir() {
158+
index := filepath.Join(path, "index.html")
159+
if _, err := uifs.base.Open(index); err != nil {
160+
closeErr := f.Close()
161+
if closeErr != nil {
162+
return nil, closeErr
163+
}
164+
165+
return nil, os.ErrNotExist
166+
}
167+
}
168+
169+
return f, nil
170+
}

0 commit comments

Comments
 (0)