Skip to content

Modern Docker Management, Designed for Everyone

fix(edge): prevent slog-gin panic on tunneled requests (#2467)

Juliane1c10174108c
diff --git a/backend/internal/bootstrap/router_bootstrap.go b/backend/internal/bootstrap/router_bootstrap.go
index ffb9f21bb..3635d94e7 100644
--- a/backend/internal/bootstrap/router_bootstrap.go
+++ b/backend/internal/bootstrap/router_bootstrap.go
@@ -62,6 +62,20 @@ func shouldLogRequest(c *gin.Context) bool {
return true
}
+func requestLoggerMiddlewareInternal() gin.HandlerFunc {
+ loggerMiddleware := sloggin.NewWithConfig(slog.Default(), sloggin.Config{
+ Filters: []sloggin.Filter{shouldLogRequest},
+ })
+
+ return func(c *gin.Context) {
+ if edge.IsInternalTunnelRequest(c.Request.Context()) {
+ c.Next()
+ return
+ }
+ loggerMiddleware(c)
+ }
+}
+
func createAuthValidator(appServices *Services) middleware.AuthValidator {
return func(ctx context.Context, c *gin.Context) bool {
// Check for API key authentication
@@ -104,9 +118,7 @@ func setupRouter(ctx context.Context, cfg *config.Config, appServices *Services)
router := gin.New()
router.Use(gin.Recovery())
- router.Use(sloggin.NewWithConfig(slog.Default(), sloggin.Config{
- Filters: []sloggin.Filter{shouldLogRequest},
- }))
+ router.Use(requestLoggerMiddlewareInternal()) //nolint:contextcheck
authMiddleware := middleware.NewAuthMiddleware(appServices.Auth, cfg).
WithApiKeyValidator(appServices.ApiKey).
@@ -128,12 +140,13 @@ func setupRouter(ctx context.Context, cfg *config.Config, appServices *Services)
// Register public webhook trigger endpoint before auth middleware (token in URL is the sole auth)
handlers.RegisterWebhookTrigger(apiGroup, appServices.Webhook) //nolint:contextcheck
- apiGroup.Use(middleware.NewEnvProxyMiddlewareWithParam(
+ envProxyMiddleware := middleware.NewEnvProxyMiddlewareWithParam(
types.LOCAL_DOCKER_ENVIRONMENT_ID,
"id",
envResolver,
createAuthValidator(appServices),
- ))
+ )
+ apiGroup.Use(envProxyMiddleware)
humaServices := &huma.Services{
User: appServices.User,
diff --git a/backend/pkg/libarcane/edge/client.go b/backend/pkg/libarcane/edge/client.go
index 3f258dc0c..ce48bdc62 100644
--- a/backend/pkg/libarcane/edge/client.go
+++ b/backend/pkg/libarcane/edge/client.go
@@ -366,6 +366,7 @@ func (c *TunnelClient) handleRequest(ctx context.Context, msg *TunnelMessage) {
reqCtx, cancel := context.WithTimeout(ctx, c.requestTimeout)
defer cancel()
+ reqCtx = withInternalTunnelRequestInternal(reqCtx)
slog.DebugContext(reqCtx, "Processing tunneled request", "id", msg.ID, "method", msg.Method, "path", msg.Path, "bodyLength", len(msg.Body))
@@ -447,6 +448,7 @@ func (c *TunnelClient) isGRPCConnectionInternal() bool {
func (c *TunnelClient) handleRequestStreaming(ctx context.Context, msg *TunnelMessage) {
reqCtx, cancel := context.WithTimeout(ctx, c.requestTimeout)
defer cancel()
+ reqCtx = withInternalTunnelRequestInternal(reqCtx)
slog.DebugContext(reqCtx, "Processing tunneled request (streaming)", "id", msg.ID, "method", msg.Method, "path", msg.Path, "bodyLength", len(msg.Body))
diff --git a/backend/pkg/libarcane/edge/internal_tunnel_request_test.go b/backend/pkg/libarcane/edge/internal_tunnel_request_test.go
new file mode 100644
index 000000000..13fd0afc0
--- /dev/null
+++ b/backend/pkg/libarcane/edge/internal_tunnel_request_test.go
@@ -0,0 +1,88 @@
+package edge
+
+import (
+ "context"
+ "log/slog"
+ "net/http"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ sloggin "github.com/samber/slog-gin"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestTunnelClient_InternalRequestSkipsSlogGin(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ tests := []struct {
+ name string
+ run func(t *testing.T, client *TunnelClient)
+ }{
+ {
+ name: "legacy response",
+ run: func(t *testing.T, client *TunnelClient) {
+ conn := &capturingTunnelConnForHandleRequest{}
+ client.conn = conn
+
+ client.handleRequest(context.Background(), &TunnelMessage{
+ ID: "req-legacy",
+ Type: MessageTypeRequest,
+ Method: http.MethodGet,
+ Path: "/local/api",
+ })
+
+ require.Len(t, conn.sent, 1)
+ assert.Equal(t, MessageTypeResponse, conn.sent[0].Type)
+ assert.Equal(t, http.StatusOK, conn.sent[0].Status)
+ assert.Equal(t, "local response", string(conn.sent[0].Body))
+ },
+ },
+ {
+ name: "streaming response",
+ run: func(t *testing.T, client *TunnelClient) {
+ conn := &fakeTunnelConn{}
+ client.conn = conn
+
+ client.handleRequestStreaming(context.Background(), &TunnelMessage{
+ ID: "req-stream",
+ Type: MessageTypeRequest,
+ Method: http.MethodGet,
+ Path: "/local/api",
+ })
+
+ require.Len(t, conn.msgs, 3)
+ assert.Equal(t, MessageTypeResponse, conn.msgs[0].Type)
+ assert.Equal(t, http.StatusOK, conn.msgs[0].Status)
+ assert.Equal(t, MessageTypeStreamData, conn.msgs[1].Type)
+ assert.Equal(t, "local response", string(conn.msgs[1].Body))
+ assert.Equal(t, MessageTypeStreamEnd, conn.msgs[2].Type)
+ },
+ },
+ }
+
+ for _, testCase := range tests {
+ t.Run(testCase.name, func(t *testing.T) {
+ var sawInternalTunnelRequest bool
+ loggerMiddleware := sloggin.NewWithConfig(slog.Default(), sloggin.Config{})
+
+ router := gin.New()
+ router.Use(func(c *gin.Context) {
+ if IsInternalTunnelRequest(c.Request.Context()) {
+ sawInternalTunnelRequest = true
+ c.Next()
+ return
+ }
+ loggerMiddleware(c)
+ })
+ router.GET("/local/api", func(c *gin.Context) {
+ c.String(http.StatusOK, "local response")
+ })
+
+ client := NewTunnelClient(&Config{}, router)
+ testCase.run(t, client)
+
+ assert.True(t, sawInternalTunnelRequest)
+ })
+ }
+}
diff --git a/backend/pkg/libarcane/edge/proxy.go b/backend/pkg/libarcane/edge/proxy.go
index 212a1a016..1b145ea05 100644
--- a/backend/pkg/libarcane/edge/proxy.go
+++ b/backend/pkg/libarcane/edge/proxy.go
@@ -125,6 +125,13 @@ func (s *grpcResponseState) handleResponse(method string, incoming *TunnelMessag
s.respHeaders = incoming.Headers
}
+ if s.respHeaders["X-Arcane-Tunnel-Stream"] == "1" {
+ if len(incoming.Body) > 0 {
+ s.respBody.Write(incoming.Body)
+ }
+ return false, 0, nil, nil
+ }
+
if len(incoming.Body) > 0 {
s.respBody.Write(incoming.Body)
return true, s.status, stripInternalTunnelHeaders(s.respHeaders), s.respBody.Bytes()
diff --git a/backend/pkg/libarcane/edge/request_context.go b/backend/pkg/libarcane/edge/request_context.go
new file mode 100644
index 000000000..c2e0dfdfb
--- /dev/null
+++ b/backend/pkg/libarcane/edge/request_context.go
@@ -0,0 +1,19 @@
+package edge
+
+import "context"
+
+type internalTunnelRequestContextKey struct{}
+
+// IsInternalTunnelRequest reports whether a request is being dispatched by the
+// in-process edge tunnel client instead of a real network listener.
+func IsInternalTunnelRequest(ctx context.Context) bool {
+ if ctx == nil {
+ return false
+ }
+ isInternal, _ := ctx.Value(internalTunnelRequestContextKey{}).(bool)
+ return isInternal
+}
+
+func withInternalTunnelRequestInternal(ctx context.Context) context.Context {
+ return context.WithValue(ctx, internalTunnelRequestContextKey{}, true)
+}