{"id":"43547d71dfb0d0bf","repo":"labstack/echo","slug":"static-middleware-failed-to-read-directory-for-lis","errorCode":null,"errorMessage":"static middleware failed to read directory for listing: %w","messagePattern":"static middleware failed to read directory for listing: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"middleware/static.go","lineNumber":328,"sourceCode":"\n\t\t\treturn serveFile(c, file, info)\n\t\t}\n\t}, nil\n}\n\nfunc serveFile(c *echo.Context, file fs.File, info os.FileInfo) error {\n\tff, ok := file.(io.ReadSeeker)\n\tif !ok {\n\t\treturn errors.New(\"file does not implement io.ReadSeeker\")\n\t}\n\thttp.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), ff)\n\treturn nil\n}\n\nfunc listDir(t *template.Template, pathInFs string, filesystem fs.FS, res http.ResponseWriter) error {\n\tfiles, err := fs.ReadDir(filesystem, pathInFs)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"static middleware failed to read directory for listing: %w\", err)\n\t}\n\n\t// Create directory index\n\tres.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)\n\tdata := struct {\n\t\tName  string\n\t\tFiles []any\n\t}{\n\t\tName: pathInFs,\n\t}\n\n\tfor _, f := range files {\n\t\tvar size int64\n\t\tif !f.IsDir() {\n\t\t\tinfo, err := f.Info()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"static middleware failed to get file info for listing: %w\", err)\n\t\t\t}","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/middleware/static.go#L310-L346","documentation":"Returned by listDir() (static.go:328) when fs.ReadDir(filesystem, pathInFs) fails while generating a directory listing. This runs at request time when directory listing is enabled (ShowDirectory true) and the requested path resolves to a directory. The wrapped error is the underlying fs.ReadDir failure.","triggerScenarios":"A request hits the static handler with directory listing enabled; the resolved path is a directory, but fs.ReadDir cannot enumerate it (permissions, broken symlink, or the directory was removed between Open and ReadDir).","commonSituations":"Filesystem permissions deny read on the directory; a symlink loop; using a custom fs.FS whose ReadDir is buggy; containerized deployments where the assets dir has restrictive perms.","solutions":["Check filesystem permissions on the served directory (read+execute for the process user).","Avoid broken or cyclic symlinks inside the served tree when using the OS filesystem.","Disable directory listing (StaticConfig.ShowList = false) if enumeration is not required.","If using embed.FS, ensure the directory was actually embedded (rebuild after adding files)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Ensure the directory is readable before enabling listing.\nfunc dirReadable(fsys fs.FS, dir string) error {\n    _, err := fs.ReadDir(fsys, dir)\n    return err\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set directory permissions to allow the process user to read+execute.","Disable ShowDirectory listing on directories you don't need to enumerate.","Avoid serving from directories with broken or cyclic symlinks."],"tags":["static","filesystem","directory-listing","runtime","permissions"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}