server: limit wildcard CORS to the public routes (closes #100)
check / check (push) Successful in 1m13s
check / check (push) Successful in 1m13s
The CORS wildcard was global, so it also covered the Basic-Auth protected /metrics, which REPO_POLICIES.md forbids, and it allowed POST, PUT and DELETE, which no route serves, plus the Authorization and X-CSRF-Token headers. CORS now sits on a router holding only the public routes and allows GET and OPTIONS with the Accept and Content-Type headers. /metrics gets no CORS at all. Both are mounted routers rather than a Group: chi answers OPTIONS on a Group's route with 405 before its middleware runs, and any method /metrics does not register would otherwise fall through to the public router. So every method on /metrics now meets Basic Auth first, and /metrics/ is served like /metrics. Model: opus-5-5
This commit was merged in pull request #172.
This commit is contained in:
+23
-14
@@ -23,14 +23,20 @@ func (s *Server) SetupRoutes() {
|
||||
s.router.Use(chimw.RequestID)
|
||||
s.router.Use(s.mw.SecurityHeaders())
|
||||
s.router.Use(s.mw.Logging())
|
||||
s.router.Use(s.mw.CORS())
|
||||
s.router.Use(chimw.Timeout(requestTimeout))
|
||||
|
||||
// Public, unauthenticated, read-only routes, the only ones
|
||||
// REPO_POLICIES.md allows wildcard CORS on. CORS is middleware of
|
||||
// this whole router, not of a Group, so that it also answers
|
||||
// OPTIONS preflight requests, which no route here registers.
|
||||
public := chi.NewRouter()
|
||||
public.Use(s.mw.CORS())
|
||||
|
||||
// Dashboard (read-only web UI)
|
||||
s.router.Get("/", s.handlers.HandleDashboard())
|
||||
public.Get("/", s.handlers.HandleDashboard())
|
||||
|
||||
// Static assets (embedded CSS/JS)
|
||||
s.router.Mount(
|
||||
public.Mount(
|
||||
"/s",
|
||||
http.StripPrefix(
|
||||
"/s",
|
||||
@@ -39,27 +45,30 @@ func (s *Server) SetupRoutes() {
|
||||
)
|
||||
|
||||
// Health check (standard well-known path)
|
||||
s.router.Get(
|
||||
public.Get(
|
||||
"/.well-known/healthcheck",
|
||||
s.handlers.HandleHealthCheck(),
|
||||
)
|
||||
|
||||
// Legacy health check (keep for backward compatibility)
|
||||
s.router.Get("/health", s.handlers.HandleHealthCheck())
|
||||
public.Get("/health", s.handlers.HandleHealthCheck())
|
||||
|
||||
// API v1 routes
|
||||
s.router.Route("/api/v1", func(r chi.Router) {
|
||||
public.Route("/api/v1", func(r chi.Router) {
|
||||
r.Get("/status", s.handlers.HandleStatus())
|
||||
})
|
||||
|
||||
// Metrics endpoint (optional, with basic auth)
|
||||
s.router.Mount("/", public)
|
||||
|
||||
// Metrics endpoint (optional, with basic auth) and no CORS: a
|
||||
// Prometheus scraper is not a browser. It is mounted rather than
|
||||
// added with Get so that every method on /metrics, OPTIONS
|
||||
// included, ends here instead of falling through to the public
|
||||
// router and its CORS.
|
||||
if s.params.Config.MetricsUsername != "" {
|
||||
s.router.Group(func(r chi.Router) {
|
||||
r.Use(s.mw.MetricsAuth())
|
||||
r.Get(
|
||||
"/metrics",
|
||||
promhttp.Handler().ServeHTTP,
|
||||
)
|
||||
})
|
||||
metrics := chi.NewRouter()
|
||||
metrics.Use(s.mw.MetricsAuth())
|
||||
metrics.Get("/", promhttp.Handler().ServeHTTP)
|
||||
s.router.Mount("/metrics", metrics)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user