server: the REST API stops handing the board to any page that asks (#1527) #1644

Merged
jaylouisw merged 1 commit from fix/1116-0905 into main 2026-11-16 09:30:20 +00:00
Owner

Fixes #1527 (items 1, 2, and the HTTP half of item 2). Step 3 of docs/plans/server-exposure.md, landed in its own commit so a broken Origin rule and a broken CORS rule stay distinguishable in a bisect.

What was wrong

The WebSocket upgrade refuses a page from another origin (step 2, test/ws-origin.test.js), and every JSON answer still carried Access-Control-Allow-Origin: *. So the page that had just been refused a socket could call fetch('/api/topologies/default') and read the whole board over HTTP - a cheaper door than the one the issue named. SECURITY.md admitted this in a sentence ("that is the next change").

What changed

  • allowedOriginHeader() asks the same allowOrigin() the upgrade uses and returns the caller's own Origin only when that check accepts it (same host and port as Host, or listed in PF_ALLOWED_ORIGINS).
  • sendJSON echoes that origin; when it refuses, the answer carries no Access-Control-Allow-Origin at all, so the browser withholds the body.
  • Both answer paths now say Vary: Origin, Accept-Encoding: an answer that depends on Origin must not be handed from one page's cache to another.
  • The OPTIONS preflight follows the same rule instead of naming methods for an answer the caller cannot read.
  • Unchanged on purpose: /api/catalog and /api/media keep the wildcard (sendImmutableJSON). Fixed hardware catalogues, no board, read from other origins deliberately.

Tests

test/server-cors.test.js (new, 23 assertions) against a real server: same-origin echo, Vary, foreign origin gets no header on two board routes, Origin: null, a non-URL origin, absent Origin unaffected, both catalogues still wildcard, preflight both ways, PF_ALLOWED_ORIGINS admitting exactly what it names, and the server still answering afterwards.

Before the fix: 10 passed / 13 failed. After: 23 passed / 0 failed.

Gates

  • node scripts/syntax-check.js - 347 files, all OK
  • node scripts/check-control-bytes.js - clean
  • node scripts/check-doc-citations.js - unchanged (the same 7 pre-existing suspects)
  • full npm test - 277 suites / 9902 assertions / 0 failed

For Jay

Anyone reading /api/topologies/* from another origin loses that path. That is the point of step 3, but it is a behaviour change for third-party consumers, not just a header nit. The bearer-token item of #1527 stays open on purpose: there is no credential surface to guard yet.

Fixes #1527 (items 1, 2, and the HTTP half of item 2). Step 3 of `docs/plans/server-exposure.md`, landed in its own commit so a broken Origin rule and a broken CORS rule stay distinguishable in a bisect. ## What was wrong The WebSocket upgrade refuses a page from another origin (step 2, `test/ws-origin.test.js`), and every JSON answer still carried `Access-Control-Allow-Origin: *`. So the page that had just been refused a socket could call `fetch('/api/topologies/default')` and read the whole board over HTTP - a cheaper door than the one the issue named. SECURITY.md admitted this in a sentence ("that is the next change"). ## What changed - `allowedOriginHeader()` asks the same `allowOrigin()` the upgrade uses and returns the caller's own `Origin` only when that check accepts it (same host and port as `Host`, or listed in `PF_ALLOWED_ORIGINS`). - `sendJSON` echoes that origin; when it refuses, the answer carries no `Access-Control-Allow-Origin` at all, so the browser withholds the body. - Both answer paths now say `Vary: Origin, Accept-Encoding`: an answer that depends on `Origin` must not be handed from one page's cache to another. - The `OPTIONS` preflight follows the same rule instead of naming methods for an answer the caller cannot read. - Unchanged on purpose: `/api/catalog` and `/api/media` keep the wildcard (`sendImmutableJSON`). Fixed hardware catalogues, no board, read from other origins deliberately. ## Tests `test/server-cors.test.js` (new, 23 assertions) against a real server: same-origin echo, `Vary`, foreign origin gets no header on two board routes, `Origin: null`, a non-URL origin, absent `Origin` unaffected, both catalogues still wildcard, preflight both ways, `PF_ALLOWED_ORIGINS` admitting exactly what it names, and the server still answering afterwards. Before the fix: 10 passed / 13 failed. After: 23 passed / 0 failed. ## Gates - `node scripts/syntax-check.js` - 347 files, all OK - `node scripts/check-control-bytes.js` - clean - `node scripts/check-doc-citations.js` - unchanged (the same 7 pre-existing suspects) - full `npm test` - **277 suites / 9902 assertions / 0 failed** ## For Jay Anyone reading `/api/topologies/*` from another origin loses that path. That is the point of step 3, but it is a behaviour change for third-party consumers, not just a header nit. The bearer-token item of #1527 stays open on purpose: there is no credential surface to guard yet.
server: the REST API stops handing the board to any page that asks (#1527)
All checks were successful
CI / Classify the change (pull_request) Successful in 15s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 16s
CI / Server smoke test (pull_request) Successful in 27s
CI / Docker build (pull_request) Successful in 26s
CI / Test (Node 20) (pull_request) Successful in 1m16s
CI / Test (Node 18) (pull_request) Successful in 1m31s
CI / Test (Node 22) (pull_request) Successful in 1m34s
12f2525211
The WebSocket upgrade refuses a page from another origin (#1527 step 2), and
every JSON answer still carried `Access-Control-Allow-Origin: *` - so the page
that had just been refused a socket could call
`fetch('/api/topologies/default')` and read every device, cable and CLI
transcript over HTTP. The wildcard is the one value a browser will not narrow
and the server cannot qualify: it says "any page" and means it.

`allowedOriginHeader()` asks the same `allowOrigin()` the upgrade uses and
returns the caller's own Origin only when that check accepts it (same host and
port as Host, or listed in PF_ALLOWED_ORIGINS). `sendJSON` echoes it and sends
no header otherwise, so a foreign page gets a body its browser withholds; both
answer paths now say `Vary: Origin, Accept-Encoding`, because an answer that
depends on Origin must not be handed from one page's cache to another. The
OPTIONS preflight follows the same rule rather than naming methods for an
answer the caller cannot read.

Deliberately unchanged: /api/catalog and /api/media keep the wildcard. They are
served by sendImmutableJSON, are fixed catalogues of hardware, carry no board,
and are read from other origins on purpose (the wiki, third-party tools).

test/server-cors.test.js asserts same-origin echo, foreign and `Origin: null`
and non-URL origins getting no header, absent Origin unaffected, both
catalogues still wildcard, the preflight both ways, and PF_ALLOWED_ORIGINS
admitting exactly what it names - against a real server, not a stub.

Docs: SECURITY.md no longer says the HTTP door is "the next change";
docs/ARCHITECTURE.md gains the paragraph; docs/plans/server-exposure.md marks
step 3 landed.

Gates: scripts/syntax-check.js 347 files OK, check-control-bytes clean,
check-doc-citations unchanged (7 pre-existing suspects), full npm test
277 suites / 9902 assertions / 0 failed.

Fixes #1527 (items 1, 2 and the HTTP half of 2; the bearer-token item stays
open on purpose - see docs/plans/server-exposure.md).
jaylouisw scheduled this pull request to auto merge when all checks succeed 2026-11-16 09:30:08 +00:00
jaylouisw force-pushed fix/1116-0905 from 12f2525211
All checks were successful
CI / Classify the change (pull_request) Successful in 15s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 16s
CI / Server smoke test (pull_request) Successful in 27s
CI / Docker build (pull_request) Successful in 26s
CI / Test (Node 20) (pull_request) Successful in 1m16s
CI / Test (Node 18) (pull_request) Successful in 1m31s
CI / Test (Node 22) (pull_request) Successful in 1m34s
to ad3c898d62
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 13s
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 09:30:11 +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!1644
No description provided.