Skip to content

Commit 7c081ac

Browse files
GRACENOBLEclaude
andcommitted
fix(observability): address CodeRabbit review findings
- NewServer now returns error so collector registration failures bubble up to main rather than being silently swallowed - /metrics is restricted to loopback/RFC-1918 IPs in staging/production via a new LocalNetworkOnly middleware; unrestricted in debug mode - metrics handler test uses &Handler{} zero value instead of nil deps, consistent with the existing hello_handler_test.go pattern Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 038d94b commit 7c081ac

5 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎backend/cmd/api/main.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func main() {
6060
hub := ws.NewHub()
6161
go hub.Run(hubCtx)
6262

63-
srv := server.NewServer(app, hub)
63+
srv, err := server.NewServer(app, hub)
64+
if err != nil {
65+
fmt.Fprintf(os.Stderr, "startup failed: %v\n", err)
66+
os.Exit(1)
67+
}
6468
slog.Info("API docs", "url", fmt.Sprintf("http://localhost%s/swagger/index.html", srv.Addr))
6569

6670
done := make(chan bool, 1)

‎backend/internal/server/server.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
// NewServer wires all layers and returns a configured *http.Server.
20-
func NewServer(app *bootstrap.App, hub *ws.Hub) *http.Server {
20+
func NewServer(app *bootstrap.App, hub *ws.Hub) (*http.Server, error) {
2121
switch app.Config.Env {
2222
case "staging", "production":
2323
gin.SetMode(gin.ReleaseMode)
@@ -40,7 +40,7 @@ func NewServer(app *bootstrap.App, hub *ws.Hub) *http.Server {
4040
if err := prometheus.Register(dbCollector); err != nil {
4141
var are prometheus.AlreadyRegisteredError
4242
if !errors.As(err, &are) {
43-
app.Log.Warn("server: failed to register db metrics collector", "err", err)
43+
return nil, fmt.Errorf("server: register db metrics collector: %w", err)
4444
}
4545
}
4646

@@ -50,5 +50,5 @@ func NewServer(app *bootstrap.App, hub *ws.Hub) *http.Server {
5050
IdleTimeout: time.Minute,
5151
ReadTimeout: 10 * time.Second,
5252
WriteTimeout: 30 * time.Second,
53-
}
53+
}, nil
5454
}

‎backend/internal/transport/handlers/metrics_handler_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestMetricsEndpoint_Returns200(t *testing.T) {
11-
h := NewHandler(nil, nil, nil)
11+
h := &Handler{}
1212
handler := h.RegisterRoutes(0, 0, "")
1313

1414
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)

‎backend/internal/transport/handlers/routes.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ func (h *Handler) RegisterRoutes(rps float64, burst int, sentryDSN string) http.
4242
r.GET("/", h.HelloWorldHandler)
4343
r.GET("/health", h.HealthHandler)
4444
r.GET("/ws", h.WsHandler)
45-
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
45+
46+
// In staging/production, restrict /metrics to loopback and RFC 1918 addresses
47+
// so Prometheus can scrape from the internal network but external clients cannot.
48+
if gin.Mode() == gin.ReleaseMode {
49+
r.GET("/metrics", middleware.LocalNetworkOnly(), gin.WrapH(promhttp.Handler()))
50+
} else {
51+
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
52+
}
4653

4754
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
4855

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package middleware
2+
3+
import (
4+
"net"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// LocalNetworkOnly rejects requests that originate outside loopback or RFC 1918
11+
// private address space. Use this to restrict internal-only endpoints (e.g.
12+
// /metrics) from being reachable by external clients in production.
13+
func LocalNetworkOnly() gin.HandlerFunc {
14+
return func(c *gin.Context) {
15+
ip := net.ParseIP(c.ClientIP())
16+
if ip == nil || (!ip.IsLoopback() && !ip.IsPrivate()) {
17+
c.AbortWithStatus(http.StatusForbidden)
18+
return
19+
}
20+
c.Next()
21+
}
22+
}

0 commit comments

Comments
 (0)