server: a directory under /s/ answers 404, not a listing (closes #286)
check / check (push) Waiting to run

http.FileServer answered /s/ and /s/css/ with an HTML listing of the
embedded files, and /s/css with a redirect to that listing. The static
handler now looks the name up in the embedded files first and answers
404 when it names a directory; files are served as before.

Model: opus-5-5
This commit is contained in:
2026-10-15 11:34:54 +00:00
parent b79726acc6
commit b4d53102e5
3 changed files with 69 additions and 7 deletions
+2
View File
@@ -19,6 +19,8 @@ milestone: https://git.eeqj.de/sneak/dnswatcher/milestone/7
# Completed Steps
- 2026-10-15: a request under `/s/` that names a directory, such as `/s/` or
`/s/css`, answers 404 instead of a listing of its files (closes #286).
- 2026-10-13: a hostname or a domain whose zone's nameservers all stop answering
gets an NS Failure for each, then an NS Recovery (closes #284).
- 2026-10-13: an IPv6 address is written in brackets before its port, as in
+27 -7
View File
@@ -1,7 +1,9 @@
package server
import (
"io/fs"
"net/http"
"strings"
"time"
sentryhttp "github.com/getsentry/sentry-go/http"
@@ -47,13 +49,7 @@ func (s *Server) SetupRoutes() {
public.Get("/", s.handlers.HandleDashboard())
// Static assets (embedded CSS/JS)
public.Mount(
"/s",
http.StripPrefix(
"/s",
http.FileServer(http.FS(static.Static)),
),
)
public.Mount("/s", http.StripPrefix("/s", staticAssets()))
// Health check (standard well-known path)
public.Get(
@@ -86,3 +82,27 @@ func (s *Server) SetupRoutes() {
s.router.Mount("/metrics", metrics)
}
}
// staticAssets serves the embedded static assets, with the /s prefix
// already stripped from the path. A request that names a directory
// answers 404: http.FileServer would answer it with a listing of the
// directory, or with a redirect to that listing when the trailing
// slash is missing.
func staticAssets() http.Handler {
files := http.FileServer(http.FS(static.Static))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// fs.Stat refuses a name that is empty or ends in a slash,
// so /s/ and /s/css/ answer 404 here along with /s/css.
name := strings.TrimPrefix(r.URL.Path, "/")
info, err := fs.Stat(static.Static, name)
if err != nil || info.IsDir() {
http.NotFound(w, r)
return
}
files.ServeHTTP(w, r)
})
}
+40
View File
@@ -1,6 +1,7 @@
package server_test
import (
"mime"
"net/http"
"net/http/httptest"
"testing"
@@ -288,3 +289,42 @@ func TestMetricsRateLimitComesBeforeAuth(t *testing.T) {
t.Errorf("another address: status = %d, want 200", rec.Code)
}
}
// TestStaticDirectoriesAnswerNotFound checks that a request naming a
// directory of the static assets, with or without the trailing slash,
// answers 404 rather than a listing of it or a redirect to one, and
// that the stylesheet is still served as CSS.
func TestStaticDirectoriesAnswerNotFound(t *testing.T) {
viper.Reset()
t.Setenv("DNSWATCHER_TARGETS", "example.com")
srv := routedServer(t)
for _, path := range []string{"/s", "/s/", "/s/css", "/s/css/"} {
rec := serve(srv, httptest.NewRequestWithContext(
t.Context(), http.MethodGet, path, nil,
))
if rec.Code != http.StatusNotFound {
t.Errorf("GET %s: status = %d, want 404", path, rec.Code)
}
}
rec := serve(srv, httptest.NewRequestWithContext(
t.Context(), http.MethodGet, "/s/css/tailwind.min.css", nil,
))
if rec.Code != http.StatusOK {
t.Errorf("GET stylesheet: status = %d, want 200", rec.Code)
}
contentType := rec.Header().Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil || mediaType != "text/css" {
t.Errorf(
"GET stylesheet: Content-Type = %q, want text/css",
contentType,
)
}
}