Skip to content

Modern Docker Management, Designed for Everyone

fix: inject agent token headers in edge tunnel proxy path (#1680)

Dat Hoangfac4e38dcf7e
diff --git a/backend/internal/middleware/environment_middleware.go b/backend/internal/middleware/environment_middleware.go
index aa6bc99c9..489878a6a 100644
--- a/backend/internal/middleware/environment_middleware.go
+++ b/backend/internal/middleware/environment_middleware.go
@@ -137,6 +137,17 @@ func (m *EnvironmentMiddleware) Handle(c *gin.Context) {
// Check if this environment has an active edge tunnel
if tunnel, ok := edge.GetRegistry().Get(envID); ok && !tunnel.Conn.IsClosed() {
slog.DebugContext(c.Request.Context(), "Routing request through edge tunnel", "environment_id", envID, "path", c.Request.URL.Path)
+
+ // Inject agent token into request headers before proxying through the tunnel.
+ // ProxyHTTPRequest and ProxyWebSocketRequest copy headers from c.Request.Header,
+ // so setting the token here ensures the agent receives proper authentication.
+ // Without this, the agent's agentAuth middleware rejects requests with 401
+ // because the browser's session cookies are not valid on the agent.
+ if accessToken != nil && *accessToken != "" {
+ c.Request.Header.Set(remenv.HeaderAgentToken, *accessToken)
+ c.Request.Header.Set(remenv.HeaderAPIKey, *accessToken)
+ }
+
proxyPath := m.buildProxyPath(c, envID)
if m.isWebSocketUpgrade(c) {
// Route WebSocket through the edge tunnel