agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

Terminal WebSocket Authentication and Ownership Gap

Open nikhilachale opened this issue 7 months ago • 1 comments

Summary

The terminal access path currently trusts a user-supplied session identifier and does not enforce authentication or session ownership before attaching to the underlying tmux session.

This affects both terminal implementations:

  • packages/web/server/terminal-websocket.ts
  • packages/web/server/direct-terminal-ws.ts

On the client side, the dashboard terminal entry points build terminal URLs from a session ID and connect without a per-connection proof of authorization:

  • packages/web/src/components/Terminal.tsx
  • packages/web/src/components/DirectTerminal.tsx

The issue is broader than URL construction. The server-side terminal endpoints accept a valid session ID, resolve the tmux session, and attach to it. There is currently no auth token, no ownership check against session metadata, and no terminal-specific rate limiting.

Files Involved

  • packages/web/server/terminal-websocket.ts
  • packages/web/src/components/Terminal.tsx
  • packages/web/server/direct-terminal-ws.ts
  • packages/core/src/metadata.ts
  • Session metadata storage under ~/.agent-orchestrator/{hash}-{projectId}/sessions/{sessionId}

Security Impact

  • Any caller that can reach the terminal HTTP/WS ports and guess or enumerate a valid session ID may be able to attach to a live agent terminal.
  • Terminal access is effectively equivalent to interactive shell access within the session runtime, including seeing prompts, outputs, secrets printed to the terminal, and potentially sending input.
  • Session ID validation only checks format. It does not establish who is connecting or whether they are allowed to access that session.
  • Current metadata storage contains session state and routing data, but it is not used as an authorization source for terminal attachment.
  • The issue exists in both the ttyd-backed path and the direct ws path, so fixing only one endpoint would leave the other exposed.

Reproduction or Risk

Example risk flow:

  1. Discover the terminal host/port or a proxied WS path.
  2. Obtain or guess a valid session ID such as ao-15.
  3. Connect to:
    • ttyd path via the terminal server flow that returns a session-specific URL, or
    • direct WebSocket path such as /ws?session=<id> or /ao-terminal-ws?session=<id>.
  4. The server validates the session ID format, resolves the tmux session, and attaches without verifying the caller's identity or ownership.

Even if session IDs are not trivially enumerable, they are user-facing identifiers that appear throughout the dashboard and logs. That makes "unguessable ID" an insufficient control here.

Recommended Fixes

1. Add a terminal auth token

Introduce a short-lived, signed terminal access token issued by the web app for a specific session and user context. The token should be required by both terminal server implementations before any tmux resolution or attach.

Recommended properties:

  • Bound to sessionId
  • Bound to the authenticated user or dashboard session
  • Short TTL
  • Single-purpose for terminal access
  • Verifiable by both terminal-websocket.ts and direct-terminal-ws.ts

2. Enforce ownership checks

Use session metadata as part of authorization, not just discovery.

Recommended changes:

  • Persist an ownership or access-control field in session metadata storage
  • Resolve the requested session from metadata first
  • Verify the authenticated caller is allowed to access that session before attaching
  • Reject access before spawning or reusing ttyd / PTY resources

packages/core/src/metadata.ts is the obvious place to extend the metadata contract, but the storage layout under ~/.agent-orchestrator/{hash}-{projectId}/sessions/{sessionId} also needs a clear ownership model.

3. Add terminal-specific rate limiting

Apply rate limits to:

  • Terminal URL/token issuance
  • WebSocket connection attempts
  • Invalid session/token failures
  • Repeated attach attempts to the same session

This reduces brute-force session enumeration, reconnect storms, and abuse against PTY / ttyd processes.

4. Integrate with the direct WS endpoint, not just ttyd

The direct xterm.js endpoint in packages/web/server/direct-terminal-ws.ts must use the same auth and ownership enforcement as the ttyd-backed server in packages/web/server/terminal-websocket.ts.

Recommended approach:

  • Centralize terminal authorization in shared server-side logic
  • Make both implementations consume the same verifier
  • Keep the client contract consistent so Terminal.tsx and DirectTerminal.tsx both request authorized terminal access the same way

If only the iframe/ttyd path is hardened, the direct WebSocket endpoint remains a bypass.

Observability Gaps

There is some connection/error telemetry already, but it is not sufficient for security response.

Add:

  • Auth success/failure events
  • Ownership check failure events
  • Rate-limit denials
  • User identity or actor ID in terminal audit events
  • Session ID plus project ID correlation for all terminal access attempts
  • Alerts or dashboards for repeated invalid session/token usage

Also consider distinguishing:

  • token issuance failures
  • websocket auth failures
  • ownership denials
  • attach/spawn failures after authorization succeeds

Next Steps

  1. Define the terminal authorization model: authenticated dashboard user, session owner, and optional shared-access cases.
  2. Extend session metadata and/or surrounding session state so ownership is explicit and queryable.
  3. Implement a shared terminal auth verifier used by both terminal-websocket.ts and direct-terminal-ws.ts.
  4. Update Terminal.tsx and DirectTerminal.tsx to request authorized terminal access instead of connecting with only sessionId.
  5. Add rate limiting and security telemetry before exposing terminal endpoints beyond trusted local-only workflows.

Notes

packages/web/server/terminal-websocket.ts already contains a TODO calling out authentication, ownership verification, and rate limiting. This document expands that TODO into a concrete security issue and makes clear that the same requirements apply to the direct WebSocket terminal path as well.

nikhilachale avatar Apr 07 '26 10:04 nikhilachale

Closing this as part of the migration cleanup for the ground-up rewrite of Agent Orchestrator.

This item was opened more than a month before the migration batch and the codebase has been replaced, so this thread no longer maps cleanly onto the current source tree.

If something here still needs tracking, please reopen this item or open a fresh one against the new tree. Thank you for contributing.

harshitsinghbhandari avatar Jun 24 '26 14:06 harshitsinghbhandari