Files
kandev/apps/backend/internal/agentctl/client/workspace_stream.go
T
João Salavisa dccff23cc0 feat(backend): per-launch authentication for agentctl (#666)
* feat(backend): per-launch authentication for agentctl

Add nonce-based handshake protocol to secure all agentctl HTTP/WS
endpoints. On launch, the parent (backend) passes a one-time bootstrap
nonce via AGENTCTL_BOOTSTRAP_NONCE env var. agentctl generates its own
auth token, enables Bearer auth immediately, and returns the token to
the parent via POST /auth/handshake (validated by nonce).

Standalone, Docker, and Sprites executors all use the same handshake
flow. Removes the old AGENTCTL_AUTH_TOKEN env var approach. For
Docker/Sprites, the retrieved token is persisted encrypted in
SecretStore (AES-256-GCM) for crash recovery.

Security properties:
- Auth enabled from boot (no unprotected window)
- Token never in env vars (only the burned nonce)
- Nonce is one-shot (replay impossible)
- Constant-time token comparison (timing-safe)

* fix: check fmt.Sscanf return value in integration test

* fix: address review feedback - constant-time nonce comparison, safe ID truncation

* fix(backend): exempt MCP routes from agentctl auth middleware

The agent subprocess connects to agentctl's MCP endpoints (/sse, /message,
/mcp) to use kandev tools (create_task, ask_user_question, etc.). These
endpoints run on the same HTTP server as the authenticated API, but the
agent process doesn't possess the auth token — it was only exchanged
between the backend and agentctl during the handshake.

Exempt /sse, /message, and /mcp from the Bearer auth middleware since the
agent process runs in the same trust boundary as agentctl.

* refactor(backend): remove query param token fallback and document SetAuthToken constraint

Remove the ?token= query parameter fallback from extractBearerToken —
tokens in URLs leak via access logs, referer headers, and browser history.
All WS callers already use Authorization headers via wsAuthHeaders().

Document SetAuthToken's concurrency constraint: it must not be called
concurrently with HTTP requests on the same client.

* fix(backend): enforce mutex in ControlClient/Launcher auth, fix Start() lock scope

- Add sync.Mutex to ControlClient; protect SetAuthToken and AuthToken() reads
- Guard Launcher.AuthToken() with l.mu (written under lock in Start, read unprotected)
- Extract buildAndStartProcess and performHandshake from Start() (was 106 lines, over 80-line limit)
- Release l.mu before blocking health/handshake calls so monitorExit() can close
  l.exited — previously the fast-fail path in waitForHealthy was unreachable
- Update stale executor_backend.go comment (standalone no longer uses env var auth)

Co-authored-by: João Salavisa <undefined@users.noreply.github.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: João Salavisa <undefined@users.noreply.github.com>
2026-04-21 14:00:49 +01:00

190 lines
5.4 KiB
Go

package client
import (
"context"
"fmt"
"sync"
"github.com/gorilla/websocket"
"github.com/kandev/kandev/internal/agentctl/tracing"
"github.com/kandev/kandev/internal/agentctl/types"
"github.com/kandev/kandev/internal/common/logger"
"go.uber.org/zap"
)
// WorkspaceStreamCallbacks defines callbacks for workspace stream events
type WorkspaceStreamCallbacks struct {
OnShellOutput func(data string)
OnShellExit func(code int)
OnGitStatus func(update *GitStatusUpdate)
OnGitCommit func(notification *GitCommitNotification)
OnGitReset func(notification *GitResetNotification)
OnBranchSwitch func(notification *GitBranchSwitchNotification)
OnFileChange func(notification *FileChangeNotification)
OnProcessOutput func(output *types.ProcessOutput)
OnProcessStatus func(status *types.ProcessStatusUpdate)
OnConnected func()
OnError func(err string)
}
// WorkspaceStream represents an active workspace stream connection
type WorkspaceStream struct {
conn *websocket.Conn
inputCh chan types.WorkspaceStreamMessage
closeCh chan struct{}
closeOnce sync.Once
logger *logger.Logger
}
// StreamWorkspace opens a unified WebSocket connection for all workspace events
func (c *Client) StreamWorkspace(ctx context.Context, callbacks WorkspaceStreamCallbacks) (*WorkspaceStream, error) {
c.mu.Lock()
if c.workspaceStreamConn != nil {
c.mu.Unlock()
return nil, fmt.Errorf("workspace stream already connected")
}
c.mu.Unlock()
wsURL := "ws" + c.baseURL[4:] + "/api/v1/workspace/stream"
conn, _, err := websocket.DefaultDialer.DialContext(ctx, wsURL, c.wsAuthHeaders())
if err != nil {
return nil, fmt.Errorf("failed to connect to workspace stream: %w", err)
}
c.mu.Lock()
c.workspaceStreamConn = conn
c.mu.Unlock()
c.logger.Info("connected to workspace stream", zap.String("url", wsURL))
stream := &WorkspaceStream{
conn: conn,
inputCh: make(chan types.WorkspaceStreamMessage, 64),
closeCh: make(chan struct{}),
logger: c.logger,
}
go c.readWorkspaceStream(conn, stream, callbacks)
go stream.writeLoop(conn)
return stream, nil
}
// workspaceTracedTypes contains message types that are low-volume and worth tracing.
// High-volume types (shell_output, process_output, shell_input, ping, pong) are excluded.
var workspaceTracedTypes = map[types.WorkspaceMessageType]bool{
types.WorkspaceMessageTypeGitStatus: true,
types.WorkspaceMessageTypeGitCommit: true,
types.WorkspaceMessageTypeGitReset: true,
types.WorkspaceMessageTypeBranchSwitch: true,
types.WorkspaceMessageTypeFileChange: true,
types.WorkspaceMessageTypeProcessStatus: true,
types.WorkspaceMessageTypeConnected: true,
types.WorkspaceMessageTypeError: true,
}
// readWorkspaceStream is the read loop for the workspace WebSocket stream.
func (c *Client) readWorkspaceStream(conn *websocket.Conn, stream *WorkspaceStream, callbacks WorkspaceStreamCallbacks) {
defer func() {
c.mu.Lock()
c.workspaceStreamConn = nil
c.mu.Unlock()
stream.Close()
}()
for {
var msg types.WorkspaceStreamMessage
if err := conn.ReadJSON(&msg); err != nil {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
c.logger.Debug("workspace stream read error", zap.Error(err))
}
return
}
if workspaceTracedTypes[msg.Type] {
tracing.TraceWorkspaceEvent(c.getTraceCtx(), string(msg.Type), c.executionID, c.sessionID)
}
dispatchWorkspaceMessage(msg, callbacks)
}
}
// writeLoop reads from the inputCh and writes messages to the WebSocket connection.
func (ws *WorkspaceStream) writeLoop(conn *websocket.Conn) {
for {
select {
case <-ws.closeCh:
return
case msg, ok := <-ws.inputCh:
if !ok {
return
}
if err := conn.WriteJSON(msg); err != nil {
ws.logger.Debug("workspace stream write error", zap.Error(err))
return
}
}
}
}
// WriteShellInput sends input to the shell through the workspace stream
func (ws *WorkspaceStream) WriteShellInput(data string) error {
msg := types.NewWorkspaceShellInput(data)
select {
case ws.inputCh <- msg:
return nil
case <-ws.closeCh:
return fmt.Errorf("workspace stream closed")
}
}
// ResizeShell sends a shell resize command through the workspace stream
func (ws *WorkspaceStream) ResizeShell(cols, rows int) error {
msg := types.NewWorkspaceShellResize(cols, rows)
select {
case ws.inputCh <- msg:
return nil
case <-ws.closeCh:
return fmt.Errorf("workspace stream closed")
}
}
// Ping sends a ping message through the workspace stream
func (ws *WorkspaceStream) Ping() error {
msg := types.NewWorkspacePing()
select {
case ws.inputCh <- msg:
return nil
case <-ws.closeCh:
return fmt.Errorf("workspace stream closed")
}
}
// Close closes the workspace stream
func (ws *WorkspaceStream) Close() {
ws.closeOnce.Do(func() {
close(ws.closeCh)
if ws.conn != nil {
if err := ws.conn.Close(); err != nil {
ws.logger.Debug("failed to close workspace stream connection", zap.Error(err))
}
}
})
}
// Done returns a channel that is closed when the stream is closed
func (ws *WorkspaceStream) Done() <-chan struct{} {
return ws.closeCh
}
// CloseWorkspaceStream closes the workspace stream connection
func (c *Client) CloseWorkspaceStream() {
c.mu.Lock()
defer c.mu.Unlock()
if c.workspaceStreamConn != nil {
if err := c.workspaceStreamConn.Close(); err != nil {
c.logger.Debug("failed to close workspace stream", zap.Error(err))
}
c.workspaceStreamConn = nil
}
}