server: a directory under /s/ answers 404, not a listing (closes #286)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
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:
@@ -19,6 +19,8 @@ milestone: https://git.eeqj.de/sneak/dnswatcher/milestone/7
|
|||||||
|
|
||||||
# Completed Steps
|
# 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
|
- 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).
|
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
|
- 2026-10-13: an IPv6 address is written in brackets before its port, as in
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||||
@@ -47,13 +49,7 @@ func (s *Server) SetupRoutes() {
|
|||||||
public.Get("/", s.handlers.HandleDashboard())
|
public.Get("/", s.handlers.HandleDashboard())
|
||||||
|
|
||||||
// Static assets (embedded CSS/JS)
|
// Static assets (embedded CSS/JS)
|
||||||
public.Mount(
|
public.Mount("/s", http.StripPrefix("/s", staticAssets()))
|
||||||
"/s",
|
|
||||||
http.StripPrefix(
|
|
||||||
"/s",
|
|
||||||
http.FileServer(http.FS(static.Static)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Health check (standard well-known path)
|
// Health check (standard well-known path)
|
||||||
public.Get(
|
public.Get(
|
||||||
@@ -86,3 +82,27 @@ func (s *Server) SetupRoutes() {
|
|||||||
s.router.Mount("/metrics", metrics)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package server_test
|
package server_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -288,3 +289,42 @@ func TestMetricsRateLimitComesBeforeAuth(t *testing.T) {
|
|||||||
t.Errorf("another address: status = %d, want 200", rec.Code)
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user