Authenticate WebSocket connections and bind command results to their worker #41

Merged
jared merged 1 commits from ws-auth into main 2026-11-22 01:09:39 -05:00
Owner

Closes #38. Builds on #39 (ssoIdentity / TRUSTED_PROXIES).

Problem

  • Any client that could reach :8080 got a WebSocket connection and received every broadcast.
  • worker_connect accepted a missing api_key, so anyone could register under an existing worker's name and receive its commands.
  • command_result was accepted from any connection, so execution logs could be forged.

What changes

Upgrade authentication (lib/wsauth.js, run in verifyClient). Each connection is classified once, at the handshake:

Class Condition Can do
browser trusted proxy and Authelia identity in admin/employee receives the live event feed
worker Authorization: Bearer <WORKER_API_KEY> can register with worker_connect
pending neither, only while WORKER_AUTH_ENFORCE is unset may only send worker_connect; closed after 30 s if it doesn't; receives no events
rejected everything else, and always for a wrong Bearer key 401 at the handshake

Worker binding

  • worker_connect now requires the key, either at the handshake or as api_key in the message. While enforcement is off, a keyless worker is still accepted, with a [Security] warning naming it.
  • The server records which worker each command was sent to, in sendCommandToWorker(). All five execute_command send sites use it: quick command, scheduler, Gandalf internal command, workflow step, and workflow retry.
  • command_result is accepted only from the worker the command went to, matched by DB worker id so a reconnect mid-command still works. Each result is accepted only once.
  • Any message other than worker_connect from a connection that hasn't identified as a worker is ignored.
  • pong no longer trusts a client-supplied worker_id.

Other changes

  • Worker, heartbeat and Gandalf keys now use a constant-time compare.
  • The server logs an error at startup if WORKER_API_KEY is unset, plus the enforcement state.
  • worker/worker.js sends its key at the handshake and in worker_connect. Older servers ignore both, so a new worker works against an old server.
  • The README documents the setting and the rollout.

Rollout (backward compatible)

  1. Merge. Enforcement is off, so existing workers keep connecting, with a warning.
  2. Update every worker (#40).
  3. Once the keyless-worker warnings stop, set WORKER_AUTH_ENFORCE=1 in the server .env and restart.

Testing

  • Added 16 jest tests in tests/wsauth.test.js. The full suite passes (61 tests), and eslint shows 0 errors.
  • I also ran an end-to-end check of the real server.js against a stubbed DB, with real ws clients, with enforcement off and on: 27/27 checks pass.
    • An anonymous connection gets 401 with enforcement on. With it off, it's held as pending and gets no broadcasts.
    • A wrong Bearer key gets 401. An SSO browser connection and a keyed worker are both accepted.
    • A worker_connect with a wrong api_key is closed with 4001.
    • A forged command_result from a browser socket is ignored. So is one for a command that was never sent, and a replay of a result already received.
    • A genuine result is recorded and broadcast.
    • The heartbeat route still accepts the right key and rejects a wrong one.

Watch after deploy

The browser live feed depends on NPM passing the Authelia Remote-* headers on the WebSocket upgrade, the same as on normal requests. If it doesn't, the UI loses live updates: the socket is held as pending and closed after 30 s. API calls keep working. Check that the dashboard still updates live after the deploy.

🤖 Generated with Claude Code

Closes #38. Builds on #39 (`ssoIdentity` / `TRUSTED_PROXIES`). ## Problem - Any client that could reach `:8080` got a WebSocket connection and received every broadcast. - `worker_connect` accepted a missing `api_key`, so anyone could register under an existing worker's name and receive its commands. - `command_result` was accepted from any connection, so execution logs could be forged. ## What changes **Upgrade authentication** (`lib/wsauth.js`, run in `verifyClient`). Each connection is classified once, at the handshake: | Class | Condition | Can do | |---|---|---| | `browser` | trusted proxy and Authelia identity in `admin`/`employee` | receives the live event feed | | `worker` | `Authorization: Bearer <WORKER_API_KEY>` | can register with `worker_connect` | | `pending` | neither, only while `WORKER_AUTH_ENFORCE` is unset | may only send `worker_connect`; closed after 30 s if it doesn't; receives **no** events | | rejected | everything else, and **always** for a wrong Bearer key | 401 at the handshake | **Worker binding** - `worker_connect` now requires the key, either at the handshake or as `api_key` in the message. While enforcement is off, a keyless worker is still accepted, with a `[Security]` warning naming it. - The server records which worker each command was sent to, in `sendCommandToWorker()`. All five `execute_command` send sites use it: quick command, scheduler, Gandalf internal command, workflow step, and workflow retry. - `command_result` is accepted only from the worker the command went to, matched by DB worker id so a reconnect mid-command still works. Each result is accepted only once. - Any message other than `worker_connect` from a connection that hasn't identified as a worker is ignored. - `pong` no longer trusts a client-supplied `worker_id`. **Other changes** - Worker, heartbeat and Gandalf keys now use a constant-time compare. - The server logs an error at startup if `WORKER_API_KEY` is unset, plus the enforcement state. - `worker/worker.js` sends its key at the handshake and in `worker_connect`. Older servers ignore both, so a new worker works against an old server. - The README documents the setting and the rollout. ## Rollout (backward compatible) 1. Merge. Enforcement is **off**, so existing workers keep connecting, with a warning. 2. Update every worker (#40). 3. Once the keyless-worker warnings stop, set `WORKER_AUTH_ENFORCE=1` in the server `.env` and restart. ## Testing - Added 16 jest tests in `tests/wsauth.test.js`. The full suite passes (61 tests), and eslint shows 0 errors. - I also ran an end-to-end check of the real `server.js` against a stubbed DB, with real `ws` clients, with enforcement off and on: 27/27 checks pass. - An anonymous connection gets 401 with enforcement on. With it off, it's held as pending and gets no broadcasts. - A wrong Bearer key gets 401. An SSO browser connection and a keyed worker are both accepted. - A `worker_connect` with a wrong `api_key` is closed with 4001. - A forged `command_result` from a browser socket is ignored. So is one for a command that was never sent, and a replay of a result already received. - A genuine result is recorded and broadcast. - The heartbeat route still accepts the right key and rejects a wrong one. ## Watch after deploy The browser live feed depends on NPM passing the Authelia `Remote-*` headers on the WebSocket upgrade, the same as on normal requests. If it doesn't, the UI loses live updates: the socket is held as pending and closed after 30 s. API calls keep working. Check that the dashboard still updates live after the deploy. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared added 1 commit 2026-11-22 01:07:41 -05:00
Authenticate WebSocket connections and bind command results to their worker
Security / JS Security (npm audit) (push) Failing after 16s
Lint / JS (eslint) (push) Successful in 18s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Test / JS Tests (jest) (push) Successful in 15s
Lint / JS (eslint) (pull_request) Successful in 13s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / JS Security (npm audit) (pull_request) Failing after 17s
Test / JS Tests (jest) (pull_request) Successful in 15s
ac6c6c72c5
Any client that could reach :8080 got a WebSocket connection. Every
connection started as a browser client and received every broadcast.
worker_connect accepted a missing api_key, so anyone could register under
an existing worker's name and receive its commands. command_result was
accepted from any connection, so execution logs could be forged.

- Classify each upgrade in verifyClient (lib/wsauth.js). A request is:
  - browser: a trusted-proxy Authelia identity in an allowed group;
  - worker: Authorization: Bearer <WORKER_API_KEY>;
  - pending: neither, allowed only while WORKER_AUTH_ENFORCE is off;
  - rejected (401): everything else, and always for a wrong key.
- Only browser connections receive broadcasts. A pending connection may
  send only worker_connect and is closed after 30 s if it doesn't identify.
- worker_connect requires the key. A keyless worker is allowed with a
  warning until WORKER_AUTH_ENFORCE=1, so existing workers keep working
  until they are updated.
- Record which worker each command was sent to. command_result is accepted
  only from that worker (matched by DB id, so a reconnect mid-command
  still works) and only once. pong no longer trusts a client-supplied id.
- Worker, heartbeat and Gandalf keys use a constant-time compare. Log an
  error at startup if WORKER_API_KEY is unset.
- The worker sends its key at the handshake and in worker_connect.

Closes #38

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared scheduled this pull request to auto merge when all checks succeed 2026-11-22 01:09:38 -05:00
jared merged commit 2e6467d388 into main 2026-11-22 01:09:39 -05:00
jared deleted branch ws-auth 2026-11-22 01:09:40 -05:00
Sign in to join this conversation.