diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-07 00:41:45 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-07 00:41:45 +0300 |
| commit | e98f0edb56f3d2355adf83a79edcc8ec2fa65a18 (patch) | |
| tree | 64c958a8d81633bbd92aaf39932ace60beeeca6a /internal/api | |
| parent | 2985cbc5d84d6841bed8a249308eeaeb70bb3c83 (diff) | |
Fix static file read seeker guard for s0
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/handlers.go | 7 | ||||
| -rw-r--r-- | internal/api/handlers_more_test.go | 30 |
2 files changed, 36 insertions, 1 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index b837a18..9fcb780 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -100,7 +100,12 @@ func (s *Server) serveFile(w http.ResponseWriter, r *http.Request, filename stri http.Error(w, "not found", http.StatusNotFound) return } - http.ServeContent(w, r, filename, stat.ModTime(), f.(io.ReadSeeker)) + rs, ok := f.(io.ReadSeeker) + if !ok { + http.Error(w, "internal error", http.StatusInternalServerError) + return + } + http.ServeContent(w, r, filename, stat.ModTime(), rs) } func (s *Server) serveIndex(w http.ResponseWriter, r *http.Request) { diff --git a/internal/api/handlers_more_test.go b/internal/api/handlers_more_test.go index 6a4353e..0403f25 100644 --- a/internal/api/handlers_more_test.go +++ b/internal/api/handlers_more_test.go @@ -196,6 +196,36 @@ func TestServer_ServeFile_notFound(t *testing.T) { } } +type statErrorFS struct{} + +func (statErrorFS) Open(string) (http.File, error) { + return statErrorFile{}, nil +} + +type statErrorFile struct{} + +func (statErrorFile) Close() error { return nil } + +func (statErrorFile) Read([]byte) (int, error) { return 0, io.EOF } + +func (statErrorFile) Seek(int64, int) (int64, error) { return 0, nil } + +func (statErrorFile) Readdir(int) ([]os.FileInfo, error) { return nil, nil } + +func (statErrorFile) Stat() (os.FileInfo, error) { return nil, errors.New("stat failed") } + +func TestServer_ServeFile_statError(t *testing.T) { + srv := &Server{staticFS: statErrorFS{}} + req := httptest.NewRequest(http.MethodGet, "/", nil) + rr := httptest.NewRecorder() + + srv.serveFile(rr, req, "index.html") + + if rr.Code != http.StatusNotFound { + t.Fatalf("expected %d, got %d", http.StatusNotFound, rr.Code) + } +} + // ------------------------------------------------------------------ // Bootstrap negative paths // ------------------------------------------------------------------ |
