Skip to main content
This page provides a deep dive into the Palyra Web Console’s state management architecture, the central API client implementation, and the security protocols governing session lifecycle and desktop handoffs.

Overview

The Web Console architecture is built around a centralized state container, useConsoleAppState, which orchestrates several domain-specific hooks. Communication with the palyrad daemon is handled by the ConsoleApiClient, which enforces CSRF protection and manages authentication headers.

System Data Flow

The following diagram illustrates the relationship between the React UI components, the state management layer, and the backend API. Console Data Flow & Entity Mapping Sources: apps/web/src/console/useConsoleAppState.tsx#164-220, apps/web/src/console/ConsoleSectionContent.tsx#26-107, apps/web/src/consoleApi.ts#1-100

Console State Management

The useConsoleAppState hook serves as the root of the console’s reactive state. It handles the initial “booting” sequence, session recovery, and section-based navigation.

Key State Properties

Domain Hooks

To prevent the root state from becoming bloated, logic is delegated to specialized domain hooks: Sources: apps/web/src/console/useConsoleAppState.tsx#164-230, apps/web/src/console/sections/UsageSection.tsx#34-40

Console API Client

The ConsoleApiClient is a class-based wrapper around the fetch API. It is responsible for standardizing request headers and handling response serialization.

Security Implementation

The client implements a “fail-closed” security model for mutating requests.
FeatureImplementation
CSRF ProtectionMutating methods (POST/PUT/DELETE) automatically inject the x-palyra-csrf-token header from the active session apps/web/src/consoleApi.test.ts#44-90.
CredentialsAll requests include credentials: "include" to support cookie-based session affinity where applicable apps/web/src/consoleApi.test.ts#89.
ValidationThe client throws a ControlPlaneApiError for non-2xx responses, which the UI uses to trigger error notices apps/web/src/consoleApi.ts#13-16.

Request Dispatch Logic

The client distinguishes between idempotent (GET) and non-idempotent requests. If a mutating request is attempted without a valid CSRF token in the session, the client throws an error before the request hits the wire apps/web/src/consoleApi.test.ts#92-106. Sources: apps/web/src/consoleApi.ts#1-100, apps/web/src/consoleApi.test.ts#44-106

Authentication & Desktop Handoff

Palyra supports a specialized “Desktop Handoff” flow, allowing a user to move from a CLI or Desktop app environment to the Web Console without re-authenticating.

Handoff Flow Sequence

Desktop Handoff Logic

Implementation Details

  1. Token Detection: On mount, useConsoleAppState checks for the desktop_handoff_token query parameter apps/web/src/console/useConsoleAppState.tsx#133-143.
  2. Consumption: The loadDesktopHandoffSession function attempts to exchange this one-time token for a full session apps/web/src/console/useConsoleAppState.tsx#109-123.
  3. Cleanup: Upon successful exchange, clearDesktopHandoffTokenFromAddressBar uses window.history.replaceState to remove the sensitive token from the URL without a page reload apps/web/src/console/useConsoleAppState.tsx#145-153.
  4. Retry Strategy: If the initial session check fails due to rate limiting (429) or transient errors, the console performs up to 5 retries with exponential backoff apps/web/src/console/useConsoleAppState.tsx#88-107.
Sources: apps/web/src/console/useConsoleAppState.tsx#88-153, apps/web/src/App.test.tsx#69-107

CSRF and Session Security

Security headers are strictly enforced by the daemon and verified by the client.

Admin vs Console Security

The daemon distinguishes between Admin and Console surfaces. The Console surface requires session-based authentication, whereas the Admin surface (used by CLI) relies on the Authorization: Bearer <ADMIN_TOKEN> header and specific context headers like x-palyra-principal crates/palyra-daemon/tests/admin_surface.rs#46-61.

Security Headers

Every response from the console API includes strict security headers to prevent common web vulnerabilities:
  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • Content-Security-Policy (Restricting script execution)
Sources: crates/palyra-daemon/tests/admin_surface.rs#40-72, apps/web/src/consoleApi.test.ts#44-90