serverapi

package
v0.36.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: Apache-2.0 Imports: 53 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHealthRoutes

func AddHealthRoutes(mux *http.ServeMux)

AddHealthRoutes registers GET /health for liveness checks.

func AddUIAuthRoutes added in v0.36.0

func AddUIAuthRoutes(mux *http.ServeMux, token string)

AddUIAuthRoutes registers the Beam login endpoints on mux. Wire it on the serve root mux only (outside the /api protection wrapper, so /ui/login is reachable without already holding the cookie it issues). token is the configured shared secret; when empty, login is not required and the endpoints report so while remaining harmless no-ops.

func AddVersionRoutes

func AddVersionRoutes(mux *http.ServeMux, version, nodeInstanceID, tenancy string)

AddVersionRoutes registers GET /version.

func AuthenticateCredential added in v0.36.0

func AuthenticateCredential(token, cred string) bool

AuthenticateCredential reports whether cred authenticates against the configured token. It accepts either the raw TOKEN itself (compared in constant time — the programmatic Bearer / X-API-Key / ?token= path) or a valid session JWT minted by /ui/login (the browser cookie path). Both the API protection middleware and the /acp WebSocket upgrade use this single gate, so one login satisfies every serve surface. Returns false when no token is configured (callers gate on that separately) or cred is empty.

func Handler added in v0.32.8

func Handler(mux *http.ServeMux, config *Config) http.Handler

Handler wraps a mux with the standard middleware chain: CORS, request ID, tracing, and local API request protection.

func IsLoopbackAddress

func IsLoopbackAddress(addr string) bool

func LoadConfig

func LoadConfig[T any](cfg *T) error

LoadConfig populates cfg from environment variables (lowercased keys mapped to json tags).

func New

func New(ctx context.Context, mux *http.ServeMux, nodeInstanceID, tenancy string, config *Config, deps ...Dependencies) (func() error, error)

New registers the spine routes (not-found shape, health, version) on mux and, when deps are supplied, the product API routes. Returns a cleanup function.

func ProtectAPI added in v0.36.0

func ProtectAPI(token, allowedOrigins string, next http.Handler) http.Handler

ProtectAPI is the primary /api/* gate. When a TOKEN is configured, EVERY request — all methods including GET/HEAD, same-origin or cross-site — must present a valid credential (a session-cookie JWT from /ui/login, or the raw TOKEN as a Bearer / X-API-Key header for programmatic clients); anything else is 401. This closes the same-origin-read hole where a LAN attacker's browser, or any local script, could read /api/state, /api/backends, /api/mcp-servers, etc. with no credential because only mutations were gated.

When NO token is configured (the loopback zero-friction case), the historical CSRF stance is preserved: reads pass, and browser-originated mutations must be same-origin (or an explicitly allowed origin) — a cross-site browser mutation is 403. Non-loopback binds already require a TOKEN (ValidateLocalServeSecurity), so the no-token branch only ever serves local development.

func ProtectMutatingAPI

func ProtectMutatingAPI(token string, next http.Handler) http.Handler

ProtectMutatingAPI wraps next with ProtectAPI and no explicit allowed origins.

func ProtectMutatingAPIWithAllowedOrigins added in v0.33.0

func ProtectMutatingAPIWithAllowedOrigins(token, allowedOrigins string, next http.Handler) http.Handler

ProtectMutatingAPIWithAllowedOrigins is retained as an alias for ProtectAPI so existing callers keep compiling; the name is historical (the wrapper no longer gates only mutations — see ProtectAPI).

func ValidateLocalServeSecurity

func ValidateLocalServeSecurity(addr, token string) error

Types

type Config

type Config struct {
	Addr                string `json:"addr"`
	Port                string `json:"port"`
	Token               string `json:"token"`
	UIBaseURL           string `json:"ui_base_url"`
	AllowedAPIOrigins   string `json:"allowed_api_origins"`
	ProxyOrigin         string `json:"proxy_origin"`
	BeamDevProxyURL     string `json:"beam_dev_proxy_url"`
	TerminalEnabled     string `json:"terminal_enabled"`
	TerminalAllowedRoot string `json:"terminal_allowed_root"`
	TerminalShell       string `json:"terminal_shell"`
	TerminalIdleTimeout string `json:"terminal_idle_timeout"`
	TerminalMaxSessions string `json:"terminal_max_sessions"`
	// WorkspaceRoots is the operator's allowlist of directories a browser client
	// may choose as a session workspace, separated by the OS path-list separator
	// (":" on POSIX). The serve directory is always the default root; these
	// extend the allowlist. Also settable via `--workspace-root` flags and the
	// `contenox serve [dir]` positional arguments.
	WorkspaceRoots string `json:"workspace_roots"`
}

Config holds the HTTP serving configuration for `contenox serve`.

type Dependencies

type Dependencies struct {
	DB                   libdb.DBManager
	PubSub               libbus.Messenger
	State                *runtimestate.State
	ToolsProviderService toolsproviderservice.Service
	Auth                 middleware.AuthZReader
	Agent                agentservice.Agent
	Chains               taskchainservice.Service
	WorkspaceID          string
	ProjectRoot          string
	ContenoxDir          string
	// WorkspaceRoots is the workspace-root allowlist. When set, the /files browse
	// API resolves each request against a client-supplied `root` (validated
	// through the allowlist) instead of the single fixed ProjectRoot.
	WorkspaceRoots  *vfs.Factory
	Defaults        stateservice.RuntimeDefaults
	TerminalService terminalservice.Service
	TerminalEnabled bool
	// HITLPolicySource and HITLDefaultPolicyName feed the /files `agent` view
	// filter: verdicts are computed by the same HITL policy engine the live agent
	// uses. When HITLPolicySource is nil the filter is unavailable (the raw tree
	// still serves). HITLDefaultPolicyName is the fallback policy when a request
	// omits `policy`; empty means the service's built-in default.
	HITLPolicySource      hitlservice.PolicySource
	HITLDefaultPolicyName string
}

Dependencies are the services the product routes are mounted on. All fields are optional: a route group is only registered when its dependencies are present, so a bare runtime still serves /health and /version.

type HealthResponse

type HealthResponse struct {
	Status string `json:"status"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL