Files
OmniRoute/docker/vnc-browser/README.md
CAPSLOCKB 813bea4184 feat(vnc-session): persistent noVNC browser login for web-cookie providers (#7892)
* chore(ci): add .mergify.yml to main — Mergify only reads config from the default branch (#7168)

* feat(vnc-session): persistent noVNC browser login for web cookie/token providers

## Why (the headless-install problem)

OmniRoute's web cookie/token providers (ChatGPT Web, Gemini Web, Claude
Web, DeepSeek Web, …) need a live browser session, but the gateway normally
runs **headless** — as a systemd service, inside Docker, or on a VPS with no
display. There is no desktop for the operator to log into the provider in.

Today the operator has to obtain the session cookie/token *out of band* (open a
real browser elsewhere, export cookies, paste them into the connection row). That
is fiddly, breaks on every provider UI change, and is a non-starter on a
headless box where you can't open a browser at all.

This PR adds an **on-demand interactive login**: OmniRoute boots a
containerized browser that exposes a noVNC web UI at the host. The operator
opens that URL in *their own* browser, logs in normally, and OmniRoute then
harvests the resulting cookies / localStorage back into the provider's
`provider_connections` row over the DevTools Protocol. No display required on
the host — the headless server renders the login into a container and the human
just drives it through a web page.

## How we ran into this

- The shipped `dist/` bundle has **no App Router source**, so the only visible
  seam was `dist/server-ws.mjs`'s `http.createServer` monkeypatch. That seam
  is **dead**: Next's standalone `startServer` creates its own http server in a
  way that bypasses the override, so a route registered there never fires
  (debug logs confirmed: zero requests reached it). The real seam is the Next
  **App Router** (`src/app/api/...`), which lives in the dev tree, not `dist/`.
- **Chromium ≥130 forces the remote-debugging port onto `127.0.0.1`** and
  ignores `--remote-debugging-address=0.0.0.0`. A plain published port can't
  reach it, so cookie harvest needs an in-container TCP bridge to republish the
  loopback CDP onto `0.0.0.0`. We shipped that bridge, but the cleaner
  default is **Firefox** (`jlesage/firefox`): its debugger binds `0.0.0.0` out
  of the box, so harvest works with no bridge at all.
- The CDP harvester **hung forever** on the first tries: the message handler was
  defined but never attached to the socket, so every `send()` promise stayed
  pending. We replaced Playwright's `connectOverCDP` (which stalls through the
  bridge) with a **raw `ws` client** and wired the handler — now resolves.

## What

New management API (scoped like the other admin endpoints via
`requireManagementAuth`):

| Method | Path | Purpose |
| --- | --- | --- |
| GET | `/api/vnc-session` | list active sessions + supported providers |
| GET | `/api/vnc-session/:provider` | session state |
| POST | `/api/vnc-session/:provider/start` | boot browser container → returns `vncUrl` |
| POST | `/api/vnc-session/:provider/harvest` | persist cookies into the provider row |
| POST | `/api/vnc-session/:provider/touch` | defer idle auto-stop |
| DELETE | `/api/vnc-session/:provider` | stop + remove the container |

## Implementation

- `src/lib/vncSession/manifest.ts` — provider → login URL + cookie/token map + config
- `src/lib/vncSession/harvest.ts` — raw-CDP cookie/localStorage harvester (`ws`)
- `src/lib/vncSession/service.ts` — docker lifecycle, port allocation, idle sweep, DB write
- `src/app/api/vnc-session/**` — App Router routes
- `src/lib/gracefulShutdown.ts` — tears down running login containers on exit

## Browser image choice

Default is **`jlesage/firefox`** (0.0.0.0-friendly CDP, no bridge). The
Chromium image + in-container bridge lives under `docker/vnc-browser/chromium`,
selectable via `OMNIROUTE_VNC_IMAGE`. See `docker/vnc-browser/README.md`.

## Config (env)

`OMNIROUTE_VNC_IMAGE`, `OMNIROUTE_VNC_CONTAINER_VNC_PORT`,
`OMNIROUTE_VNC_CONTAINER_CDP_PORT`, `OMNIROUTE_VNC_PROFILE_DIR`,
`OMNIROUTE_VNC_IDLE_MS`, `OMNIROUTE_VNC_MAX_MS`, `OMNIROUTE_VNC_MAX_SESSIONS`,
`OMNIROUTE_DOCKER_BIN` — all documented in the docker README.

## Tests

`tests/unit/vnc-session.test.ts` — manifest lookup + credential mapping
(cookie / token / whole-jar). All passing via the Node test runner.

## Notes

- Docker is the only external dependency; if the `docker` CLI is missing, `start`
  throws a clear error and shutdown is a no-op.
- No secrets are returned by any endpoint — only session metadata + ports.

Co-authored-by: Sora <138304505+Capslockb@users.noreply.github.com>
Co-authored-by: Bernardo <138304505+Capslockb@users.noreply.github.com>

* refactor(vnc-session): derive provider credentials from shared contract

* fix(vnc-session): harden CDP harvesting and credential filtering

* refactor(vnc-session): scope lifecycle to provider connections

* fix(vnc-session): sanitize and scope management routes

* fix(vnc-session): use canonical provider list in API

* test(vnc-session): align coverage with canonical manifest

* docs(vnc-session): align browser setup with current implementation

* fix(security): loopback-gate /api/vnc-session (Hard Rule #15/#17)

The new /api/vnc-session/* routes spawn Docker containers via
child_process.spawn (src/lib/vncSession/service.ts) but were never
registered in LOCAL_ONLY_API_PREFIXES or SPAWN_CAPABLE_PREFIXES, so
they were reachable from non-loopback callers (any manage-scope API
key or dashboard session over a tunnel) - the same CVE class
(GHSA-fhh6-4qxv-rpqj) those constants exist to close.

Register VNC_ROUTE_PREFIX (already exported but unused in
manifest.ts) in both prefix lists, and add a regression test
asserting isLocalOnlyPath()/isLocalOnlyBypassableByManageScope()
correctly classify the new prefix.

Co-authored-by: CAPSLOCKB <138304505+Capslockb@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouzapw@users.noreply.github.com>
2026-07-22 10:46:04 -03:00

70 lines
3.9 KiB
Markdown

# Browser-login container
OmniRoute can open an isolated browser for provider connections that require an interactive web login. The operator signs in through the browser UI, then OmniRoute reads only the credential fields declared for that provider through the Chrome DevTools Protocol (CDP) and writes them to the selected `provider_connections` row.
The feature is exposed through the management-authenticated `/api/vnc-session` routes. Browser and CDP ports are published on `127.0.0.1` only; they are not intended to be exposed directly to a network.
## Build the required image
The current implementation uses the Chromium image in this directory. Build it before starting a browser-login session:
```bash
docker build -t omniroute-vnc-chromium:local docker/vnc-browser/chromium
```
`omniroute-vnc-chromium:local` is the default image. Set `OMNIROUTE_VNC_IMAGE` only when using a compatible image that provides:
- a browser UI on the configured container VNC port;
- a reachable CDP endpoint on the configured container CDP port;
- support for the `CHROME_CLI` environment variable used to pass the provider login URL and Chromium arguments;
- persistent browser data under the configured profile directory.
The container image includes a local bridge because modern Chromium binds its debugger to loopback inside the container.
## Ports and access
Docker assigns ephemeral host ports and binds them to loopback:
| Purpose | Default container port | Host exposure |
| --- | ---: | --- |
| Browser web UI | `3000` | `127.0.0.1:<ephemeral>` |
| DevTools/CDP bridge | `9223` | `127.0.0.1:<ephemeral>` |
Remote operators must access the browser UI through an authenticated application proxy or an SSH tunnel. Do not publish either port on `0.0.0.0`.
## Configuration
| Variable | Default | Purpose |
| --- | --- | --- |
| `OMNIROUTE_VNC_IMAGE` | `omniroute-vnc-chromium:local` | Compatible browser image |
| `OMNIROUTE_VNC_CONTAINER_VNC_PORT` | `3000` | Browser UI port inside the container |
| `OMNIROUTE_VNC_CONTAINER_CDP_PORT` | `9223` | CDP bridge port inside the container |
| `OMNIROUTE_VNC_CONTAINER_PROFILE_DIR` | `/config` | Profile mount point inside the container |
| `OMNIROUTE_VNC_PROFILE_DIR` | `$HOME/.omniroute/browser-login-profiles` | Host profile root |
| `OMNIROUTE_VNC_PERSIST_PROFILES` | `false` | Reuse a connection profile across sessions |
| `OMNIROUTE_VNC_IDLE_MS` | `600000` | Idle-session timeout in milliseconds |
| `OMNIROUTE_VNC_MAX_MS` | `1800000` | Maximum session lifetime in milliseconds |
| `OMNIROUTE_VNC_MAX_SESSIONS` | `4` | Maximum concurrent sessions |
| `OMNIROUTE_VNC_READY_MS` | `45000` | Browser/CDP startup timeout in milliseconds |
| `OMNIROUTE_VNC_HARVEST_MS` | `20000` | Credential-harvest timeout in milliseconds |
| `OMNIROUTE_VNC_CHROMIUM_ARGS` | see `manifest.ts` | Chromium command-line arguments |
| `OMNIROUTE_DOCKER_BIN` | `docker` | Docker-compatible CLI executable |
## Security and lifecycle
- Sessions are scoped to a specific provider connection and use random session IDs.
- Container names and persistent-profile path segments are sanitized.
- Only declared cookie or storage keys are retained; arbitrary local or session storage is not copied into the database.
- Startup failures remove the in-memory session, container, and non-persistent profile.
- Concurrent stop requests are idempotent, and shutdown removes managed containers.
- Harvest and CDP commands have bounded timeouts.
- API responses must sanitize internal Docker, filesystem, CDP, and database errors.
## Basic verification
```bash
npm test -- tests/unit/vnc-session.test.ts
npm run typecheck
```
For an end-to-end check, build the image, start OmniRoute, create or select a supported web-provider connection, start a browser-login session through the management API, complete login through the returned UI URL, harvest credentials, and verify that the selected connection—not another account for the same provider—was updated.