A Go File Server Lists Your Directories Until You Stop It
Three lines of Go give you a static file server, and also a free map of your directory tree. http.FileServer will happily print the contents of any folder that lacks an index.html, and it answers differently for files that exist but can't be read. Both are information you probably didn't mean to publish.
Here is the server most of us have written at least once:
package main
import (
"log"
"net/http"
)
func main() {
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("./public"))))
}
What it gives away
Run that against a directory with a backups/ folder in it and ask for /backups/. If there is no index.html, you get an HTML page listing every file name. No guessing needed.
There are three separate leaks here:
- Directory listings for any folder without an index file.
- Dotfiles such as
/.git/configor/.env, served like anything else if they sit under the root. - A different status for "exists but unreadable" than for "doesn't exist".
The third one is the sneaky one, so let's look at it.
403 versus 404 is an oracle
When opening a file fails, the standard library maps the error to a status. A missing file becomes "404 page not found", a permission error becomes "403 Forbidden", and anything else becomes "500 Internal Server Error". That's reasonable behaviour in isolation.
Put together, it means a scanner can tell /secrets.tar (403, it's there, you just can't read it) from /nothing.tar (404). Any difference in response between "absent" and "present but denied" lets someone enumerate your layout without reading a single byte of content.
Quick detour: why a wrapper works at all
Hang on, why is this fixable without touching the file server? Because http.FileServer takes an http.FileSystem, which is just an interface with one method, Open(name string) (http.File, error). The server has no idea what is behind it.
So we hand it a filesystem that lies. Whatever we don't want shown, we report as fs.ErrNotExist, and the server dutifully produces its 404 path. It also means the directory-redirect logic never runs for hidden folders, since that happens only after a successful open.
A filesystem that says "not found" for everything unwanted
package main
import (
"io/fs"
"log"
"net/http"
"path"
"strings"
)
// hardenedFS reports dotfiles, unreadable paths and index-less
// directories as not existing.
type hardenedFS struct{ fsys http.FileSystem }
func (h hardenedFS) Open(name string) (http.File, error) {
if strings.Contains(name, "/.") {
return nil, fs.ErrNotExist
}
f, err := h.fsys.Open(name)
if err != nil {
log.Printf("open %q: %v", name, err) // real reason stays in the log
return nil, fs.ErrNotExist
}
st, err := f.Stat()
if err != nil {
f.Close()
return nil, fs.ErrNotExist
}
if st.IsDir() {
idx, err := h.fsys.Open(path.Join(name, "index.html"))
if err != nil {
f.Close()
return nil, fs.ErrNotExist
}
idx.Close()
}
return f, nil
}
Three things to notice. Every error collapses into not-exist, so the outside world sees one answer. The genuine cause goes to your log, where it's useful. And the directory check demands an index.html, so a folder is only reachable if you meant it to be.
The "/." test matches any path segment beginning with a dot. Names reach Open already slash-prefixed, so /.git/config and /a/.env are both caught. It also blocks /.well-known/, so if you need that, add an exception.
Giving the 404 your own face
Now every miss is the same, but it's still Go's stock plain-text "404 page not found". That is a fingerprint of its own, and it looks nothing like the rest of your site. The file server writes that body itself, so we intercept it with a small response writer wrapper.
var notFoundPage = []byte(`<!doctype html>
<title>Not found</title>
<h1>Nothing here</h1>
<p><a href="/">Back to the front page</a></p>
`)
type notFoundWriter struct {
http.ResponseWriter
swallow bool
}
func (w *notFoundWriter) WriteHeader(code int) {
if code == http.StatusNotFound {
w.swallow = true
h := w.Header()
h.Set("Content-Type", "text/html; charset=utf-8")
h.Del("Content-Length")
w.ResponseWriter.WriteHeader(code)
w.ResponseWriter.Write(notFoundPage)
return
}
w.ResponseWriter.WriteHeader(code)
}
func (w *notFoundWriter) Write(b []byte) (int, error) {
if w.swallow {
return len(b), nil // drop the stock body
}
return w.ResponseWriter.Write(b)
}
// Unwrap lets http.ResponseController reach the real writer.
func (w *notFoundWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
func withNotFound(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
next.ServeHTTP(¬FoundWriter{ResponseWriter: rw}, r)
})
}
func main() {
files := http.FileServer(hardenedFS{http.Dir("./public")})
log.Fatal(http.ListenAndServe(":8080", withNotFound(files)))
}
The trick is that the file server calls WriteHeader(404) before writing its text. We spot the code, send our page instead, and swallow whatever body follows. Any other status passes straight through untouched.
Check that it worked
Make a few awkward files and compare the responses:
mkdir -p public/backups
echo hi > public/index.html
echo x > public/backups/db.sql
echo x > public/.env
echo x > public/locked.txt && chmod 000 public/locked.txt
for p in / /backups/ /.env /locked.txt /nope.txt; do
curl -s -o /dev/null -w "$p %{http_code} %{size_download}\n" localhost:8080$p
done
You want / to return 200 and the other four to return identical 404s with identical sizes. If /locked.txt shows a 403, the wrapper isn't in place. Run as root, chmod 000 doesn't stop reads, so test the permission case as an ordinary user.
What this doesn't cover
Symlinks inside the root still get followed, because http.Dir follows them. If untrusted people can create files in that directory, a link to /etc is a bigger problem than a listing. Go 1.24 added os.Root, which is the better tool for confinement, but that deserves its own post.
Also, timing can still differ slightly between a hit and a miss. For a public static site that's not worth chasing; for anything sensitive, the real fix is not to put it under the web root in the first place.