1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-10-18 14:24:20 +00:00

Add http.NoListFS

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-06-16 11:28:23 +02:00
parent 8899d95437
commit 18ec694696
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5

42
http/nolistfs.go Normal file
View file

@ -0,0 +1,42 @@
package http
import (
"net/http"
"os"
"github.com/pkg/errors"
)
type (
// NoListFS wraps an http.FileSystem and ensures no directory
// listings are printed out in order not to expose the contents
// of the directory in a listing.
NoListFS struct{ Next http.FileSystem }
)
var _ http.FileSystem = NoListFS{}
// Open wraps the Open of the inner http.FileSystem and returns an
// os.ErrNotExist in case a directory should be opened. This will
// result in a HTTP 404 when trying to open a directory causing the
// contents to be conceiled in an effective manner (zero exposure
// of known paths)
func (n NoListFS) Open(name string) (http.File, error) {
f, err := n.Next.Open(name)
if err != nil {
return f, errors.Wrap(err, "opening file from inner fs")
}
info, err := f.Stat()
if err != nil {
f.Close()
return nil, errors.Wrap(err, "getting stat from opened file")
}
if info.IsDir() {
f.Close()
return nil, errors.Wrap(os.ErrNotExist, "refusing to open a directory")
}
return f, nil
}