ws: refuse an upgrade whose Origin is not the Host it was addressed to (#1527) #1629

Merged
jaylouisw merged 2 commits from fix/1116-0735 into main 2026-11-16 07:56:16 +00:00
Owner

Part of #1527 - the WebSocket item. The bind default and the bearer token are not here, so the issue stays open. Step 2 of docs/plans/server-exposure.md.

What was wrong. server/index.js passed every upgrade request straight to wss.handleUpgrade, which read sec-websocket-key and nothing else, and on success immediately pushed the whole default board. A WebSocket handshake is not subject to the same-origin policy, so any page Jay visited could open ws://127.0.0.1:8000/ and read every device, cable and CLI transcript on the board. RFC 6455 10.2 asks a server to compare Origin with Host for exactly this reason.

What changed.

  • server/ws.js: allowOrigin(req, extra) - absent Origin allowed (curl, native clients, every suite in test/); a present Origin allowed only when it names the same host and port as Host, or is listed in extra. file:, data: and the literal null that a sandboxed iframe sends are refused, because they name no host. handleUpgrade(req, socket, { allowOrigin }) asks it before the 101 and answers 403 (new refuseUpgrade()) with a body naming both headers and the remedy, then destroys the socket.
  • server/index.js: PF_ALLOWED_ORIGINS (comma list) is the way to admit a page served from somewhere else; empty unless asked for. The UI is served by this process, so its own page is same-origin and needs no entry.
  • Docs: docs/plans/server-exposure.md marks step 2 landed and corrects its shifted line citations; SECURITY.md states the new rule and says plainly that the wildcard CORS header still lets a page read a board over HTTP; docs/ARCHITECTURE.md gains the rule in the WebSocket section.

Test. New test/ws-origin.test.js, 16 assertions driving a raw socket against a real server: no Origin gives 101 and the board; same-origin gives 101; http://evil.example gives 403 with no 101 and no board frame; the same host on another port gives 403; Origin: null gives 403; a non-URL gives 403; the server is still answering afterwards; PF_ALLOWED_ORIGINS admits the origin it names, refuses another, and leaves same-origin working.

Totals. node scripts/syntax-check.js: 343 files OK. Full npm test: 273 suites, 9813 assertions, 0 failed.

Not here (one PR each, per the plan): the loopback bind default with a host opt-out (step 1), narrowing the wildcard Access-Control-Allow-Origin on topology data (step 3), dropping the unsolicited board push (step 4), and the bearer token for the credential surface, which needs a credential store to protect and needs Jay's approval. Until step 1 lands the socket is still reachable from the LAN; a web page can no longer reach it.

Part of #1527 - the WebSocket item. The bind default and the bearer token are not here, so the issue stays open. Step 2 of `docs/plans/server-exposure.md`. **What was wrong.** `server/index.js` passed every upgrade request straight to `wss.handleUpgrade`, which read `sec-websocket-key` and nothing else, and on success immediately pushed the whole `default` board. A WebSocket handshake is not subject to the same-origin policy, so any page Jay visited could open `ws://127.0.0.1:8000/` and read every device, cable and CLI transcript on the board. RFC 6455 10.2 asks a server to compare `Origin` with `Host` for exactly this reason. **What changed.** - `server/ws.js`: `allowOrigin(req, extra)` - absent `Origin` allowed (curl, native clients, every suite in `test/`); a present `Origin` allowed only when it names the same host and port as `Host`, or is listed in `extra`. `file:`, `data:` and the literal `null` that a sandboxed iframe sends are refused, because they name no host. `handleUpgrade(req, socket, { allowOrigin })` asks it **before** the `101` and answers `403` (new `refuseUpgrade()`) with a body naming both headers and the remedy, then destroys the socket. - `server/index.js`: `PF_ALLOWED_ORIGINS` (comma list) is the way to admit a page served from somewhere else; empty unless asked for. The UI is served by this process, so its own page is same-origin and needs no entry. - Docs: `docs/plans/server-exposure.md` marks step 2 landed and corrects its shifted line citations; `SECURITY.md` states the new rule and says plainly that the wildcard CORS header still lets a page *read* a board over HTTP; `docs/ARCHITECTURE.md` gains the rule in the WebSocket section. **Test.** New `test/ws-origin.test.js`, 16 assertions driving a raw socket against a real server: no `Origin` gives 101 and the board; same-origin gives 101; `http://evil.example` gives 403 with no `101` and no board frame; the same host on another port gives 403; `Origin: null` gives 403; a non-URL gives 403; the server is still answering afterwards; `PF_ALLOWED_ORIGINS` admits the origin it names, refuses another, and leaves same-origin working. **Totals.** `node scripts/syntax-check.js`: 343 files OK. Full `npm test`: 273 suites, 9813 assertions, 0 failed. **Not here** (one PR each, per the plan): the loopback bind default with a host opt-out (step 1), narrowing the wildcard `Access-Control-Allow-Origin` on topology data (step 3), dropping the unsolicited board push (step 4), and the bearer token for the credential surface, which needs a credential store to protect and needs Jay's approval. Until step 1 lands the socket is still reachable from the LAN; a web page can no longer reach it.
ws: refuse an upgrade whose Origin is not the Host it was addressed to (#1527)
All checks were successful
CI / Classify the change (pull_request) Successful in 16s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 20s
CI / Server smoke test (pull_request) Successful in 54s
CI / Test (Node 20) (pull_request) Successful in 1m22s
CI / Docker build (pull_request) Successful in 24s
CI / Test (Node 18) (pull_request) Successful in 1m29s
CI / Test (Node 22) (pull_request) Successful in 1m40s
dffb57cfe8
The upgrade handler handed every request to `wss.handleUpgrade` and, on success,
pushed the whole `default` board before the client had sent a byte. A WebSocket
handshake is not subject to the same-origin policy, so any page Jay visited could
open ws://127.0.0.1:8000/ and read every device, cable and CLI transcript on the
board. RFC 6455 10.2 asks a server to compare `Origin` with `Host` for this reason.

`allowOrigin()` in server/ws.js now decides, `handleUpgrade` asks it before the
101 and answers 403 with a line naming both headers, and server/index.js wires it
with `PF_ALLOWED_ORIGINS` as the way to admit a page served from elsewhere. Absent
`Origin` stays allowed, so curl, native clients and every existing suite are
unaffected; a same-origin page keeps working because its `Origin` names the
`Host:port` it connected to.

test/ws-origin.test.js: 16 assertions against a real server - absent, same-origin,
foreign, same host on another port, `Origin: null`, a non-URL, and the allow-list
admitting exactly what it names.

This is step 2 of docs/plans/server-exposure.md. The loopback bind default (step 1),
narrowing the wildcard CORS header (step 3) and the bearer token are not here.
jaylouisw changed title from probe: should not be created to ws: refuse an upgrade whose Origin is not the Host it was addressed to (#1527) 2026-11-16 07:56:06 +00:00
jaylouisw scheduled this pull request to auto merge when all checks succeed 2026-11-16 07:56:09 +00:00
jaylouisw force-pushed fix/1116-0735 from dffb57cfe8
All checks were successful
CI / Classify the change (pull_request) Successful in 16s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 20s
CI / Server smoke test (pull_request) Successful in 54s
CI / Test (Node 20) (pull_request) Successful in 1m22s
CI / Docker build (pull_request) Successful in 24s
CI / Test (Node 18) (pull_request) Successful in 1m29s
CI / Test (Node 22) (pull_request) Successful in 1m40s
to cc67f4618f
All checks were successful
CI / Classify the change (pull_request) Successful in 13s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 15s
CI / Test (Node 18) (pull_request) Has been skipped
CI / Test (Node 20) (pull_request) Has been skipped
CI / Test (Node 22) (pull_request) Has been skipped
CI / Server smoke test (pull_request) Has been skipped
CI / Docker build (pull_request) Has been skipped
2026-11-16 07:56:13 +00:00
Compare
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge!1629
No description provided.