feat: add API Key authentication for REST and WebSocket servers#53
feat: add API Key authentication for REST and WebSocket servers#53Vishnu-tppr wants to merge 1 commit into
Conversation
Code Review by Qodo
1. Docs leak API key
|
PR Summary by QodoAdd API key auth for local REST + WebSocket gateway (CLI + docs support) WalkthroughsDescription• Generate and persist a per-install API key in settings.json on first run. • Require Bearer auth for sensitive REST endpoints while keeping public status/docs readable. • Enforce API key validation during WebSocket upgrade and auto-configure CLI/docs clients. Diagramgraph TD
EM["Electron main"] -->|"load/save"| ST[("settings.json")]
EM -->|"init + getApiKey()"| RA["REST API server"] -->|"initWebSocket(getApiKey)"| WS["WebSocket server"]
CLI["CLI client"] -->|"Authorization: Bearer"| RA
RA -->|"inject apiKey into HTML"| DOC["Docs UI (/docs) "]
DOC -->|"ws://.../ws?apiKey="| WS
High-Level AssessmentThe following are alternative approaches to this PR: 1. Origin allowlist + remove permissive CORS
2. Session/CSRF-style token tied to a local UI login
3. Use OS-protected local transport (UDS / named pipe) or mTLS
Recommendation: The API-key approach in this PR is a pragmatic, low-friction fix that immediately blocks unauthenticated REST and WS access while keeping CLI/docs usable. Consider follow-up hardening to also reduce browser attack surface (e.g., tighten/disable wildcard CORS and/or implement Origin allowlisting), since embedding the key into docs HTML and passing it via WS query string improves usability but increases accidental disclosure risk (logs, screenshots, shared HTML). File ChangesEnhancement (2)
Bug fix (2)
|
eee592d to
688fced
Compare
This change plugs a major security flaw in the local gateway: currently, any website you visit in a web browser can make silent cross-origin (CORS) HTTP/WebSocket calls to localhost:3210 and abuse the local AI models. Even worse, since the completions endpoint supports a "file" reference parameter, malicious websites could read arbitrary local files from the host machine.
To fix this, we are adding API Key authentication: