From ee19f1749bda8135df9a15cb2dbcfab72a3731cd Mon Sep 17 00:00:00 2001 From: Vibhu Pandey Date: Mon, 25 Aug 2025 09:01:32 +0530 Subject: [PATCH] fix(web): fix panic on nil file info (#8907) fix panic on nil file info --- pkg/web/routerweb/provider.go | 21 +++++++++++++-------- pkg/web/routerweb/provider_test.go | 9 +++++++++ pkg/web/routerweb/testdata/assets/index.css | 3 +++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 pkg/web/routerweb/testdata/assets/index.css diff --git a/pkg/web/routerweb/provider.go b/pkg/web/routerweb/provider.go index ccc632652f69..9ec07da85bc3 100644 --- a/pkg/web/routerweb/provider.go +++ b/pkg/web/routerweb/provider.go @@ -72,17 +72,22 @@ func (provider *provider) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // check whether a file exists or is a directory at the given path fi, err := os.Stat(path) - if os.IsNotExist(err) || fi.IsDir() { - // file does not exist or path is a directory, serve index.html - http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName)) + if err != nil { + // if the file doesn't exist, serve index.html + if os.IsNotExist(err) { + http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName)) + return + } + + // if we got an error (that wasn't that the file doesn't exist) stating the + // file, return a 500 internal server error and stop + http.Error(rw, err.Error(), http.StatusInternalServerError) return } - if err != nil { - // if we got an error (that wasn't that the file doesn't exist) stating the - // file, return a 500 internal server error and stop - // TODO: Put down a crash html page here - http.Error(rw, err.Error(), http.StatusInternalServerError) + if fi.IsDir() { + // path is a directory, serve index.html + http.ServeFile(rw, req, filepath.Join(provider.config.Directory, indexFileName)) return } diff --git a/pkg/web/routerweb/provider_test.go b/pkg/web/routerweb/provider_test.go index 22c82f9b7a3c..96db1edb370d 100644 --- a/pkg/web/routerweb/provider_test.go +++ b/pkg/web/routerweb/provider_test.go @@ -61,6 +61,10 @@ func TestServeHttpWithoutPrefix(t *testing.T) { name: "DoesNotExist", path: "/does-not-exist", }, + { + name: "Directory", + path: "/assets", + }, } for _, tc := range testCases { @@ -130,6 +134,11 @@ func TestServeHttpWithPrefix(t *testing.T) { path: "/web/does-not-exist", found: true, }, + { + name: "Directory", + path: "/web/assets", + found: true, + }, { name: "DoesNotExist", path: "/does-not-exist", diff --git a/pkg/web/routerweb/testdata/assets/index.css b/pkg/web/routerweb/testdata/assets/index.css new file mode 100644 index 000000000000..f93ad5f421a2 --- /dev/null +++ b/pkg/web/routerweb/testdata/assets/index.css @@ -0,0 +1,3 @@ +#root { + background-color: red; +} \ No newline at end of file