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)
Merge. Enforcement is off, so existing workers keep connecting, with a warning.
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.
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)
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 main2026-11-22 01:09:39 -05:00
jared
deleted branch ws-auth2026-11-22 01:09:40 -05:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Closes #38. Builds on #39 (
ssoIdentity/TRUSTED_PROXIES).Problem
:8080got a WebSocket connection and received every broadcast.worker_connectaccepted a missingapi_key, so anyone could register under an existing worker's name and receive its commands.command_resultwas accepted from any connection, so execution logs could be forged.What changes
Upgrade authentication (
lib/wsauth.js, run inverifyClient). Each connection is classified once, at the handshake:browseradmin/employeeworkerAuthorization: Bearer <WORKER_API_KEY>worker_connectpendingWORKER_AUTH_ENFORCEis unsetworker_connect; closed after 30 s if it doesn't; receives no eventsWorker binding
worker_connectnow requires the key, either at the handshake or asapi_keyin the message. While enforcement is off, a keyless worker is still accepted, with a[Security]warning naming it.sendCommandToWorker(). All fiveexecute_commandsend sites use it: quick command, scheduler, Gandalf internal command, workflow step, and workflow retry.command_resultis 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.worker_connectfrom a connection that hasn't identified as a worker is ignored.pongno longer trusts a client-suppliedworker_id.Other changes
WORKER_API_KEYis unset, plus the enforcement state.worker/worker.jssends its key at the handshake and inworker_connect. Older servers ignore both, so a new worker works against an old server.Rollout (backward compatible)
WORKER_AUTH_ENFORCE=1in the server.envand restart.Testing
tests/wsauth.test.js. The full suite passes (61 tests), and eslint shows 0 errors.server.jsagainst a stubbed DB, with realwsclients, with enforcement off and on: 27/27 checks pass.worker_connectwith a wrongapi_keyis closed with 4001.command_resultfrom a browser socket is ignored. So is one for a command that was never sent, and a replay of a result already received.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