Skip to content

Answer exactly the origin the server serves - #55

Open
lgnap wants to merge 2 commits into
antoinevalentinHA:masterfrom
lgnap:fix/cors-loopback-origin
Open

lgnap wants to merge 2 commits into
antoinevalentinHA:masterfrom
lgnap:fix/cors-loopback-origin

Conversation

@lgnap

@lgnap lgnap commented Sep 9, 2026 •

Copy link
Copy Markdown

What

Reworked along the lines settled in #46, after the review on #68.

The CORS policy was http://localhost:.*. It refused http://127.0.0.1:8080, the address the server binds: a user who types it gets reads that work and every state-changing call refused, because a browser sends Origin on a POST even when it is same-origin. And it trusted every port of the machine, so any page on a localhost origin could drive this unauthenticated API. (The first version of this PR said the application opens the browser at 127.0.0.1; it does not, MainVerticle opens http://localhost:8080. That premise was wrong and is gone.)

The allowed origin is now derived from what the server serves: MainVerticle.allowedOrigins() builds it from listenHost() and LISTEN_PORT, so exactly http://localhost:8080 and http://127.0.0.1:8080 are answered, and nothing else. [::1] is not listed: the server binds an IPv4 address, so nothing reaches it under that name.

The development server. No exception and no env=dev branch: src/setupProxy.js carries /eventbus to the backend, WebSocket included, and rewrites the Origin it forwards to the backend's own. The package.json proxy already rewrites Origin on plain HTTP requests, but not on the WebSocket upgrade, which is why the entry is needed. REACT_APP_EVENTBUS_ORIGIN is no longer used and is removed from App.js. http-proxy-middleware is the copy react-scripts 3.0.1 ships (0.19.2), not a new dependency.

Not done on purpose: x-requested-with is left as it is, as agreed in #46.

Tests

  • CorsOriginTest (4 tests, real MainVerticle): localhost:8080 and 127.0.0.1:8080 answered; foreign hosts, the near-miss spellings and https://localhost:8080 refused; the loopback on a foreign port refused — http://localhost:1, :3000, 127.0.0.1:3000, :8081, :80800. Against the old pattern, 2 of the 4 fail (127.0.0.1:8080 refused, localhost:1 answered).
  • setupProxy.test.js (4 tests): /eventbus only, to 127.0.0.1:8080, ws: true, the Origin rewrite on HTTP and WebSocket requests, no Origin added where there was none. JavaScript suite: 61 tests.

Measured (Linux, backend with env=dev, yarn start on :3000)

direct :8080  GET  Origin http://localhost:8080          -> 200
direct :8080  POST Origin http://127.0.0.1:8080          -> 200 (event bus xhr)
direct :8080  GET  Origin http://localhost:3000          -> 403
direct :8080  GET  Origin http://localhost:1             -> 403
direct :8080  WS   Origin http://localhost:3000          -> 403 CORS Rejected - Invalid origin
via :3000     GET  /eventbus/info                        -> 200
via :3000     POST /eventbus xhr, Origin localhost:3000  -> 200
via :3000     POST /api/device/reorder, Origin :3000     -> 500 from the handler (mock device, empty body), not 403
via :3000     WS   /eventbus, Origin localhost:3000      -> 101 Switching Protocols
same, without setupProxy.js: WS                          -> 403 CORS Rejected - Invalid origin

Not measured: Windows, and a browser session end to end (the table above is curl).

The CORS policy allowed `http://localhost:` and nothing else. It was written when
the server bound every interface and was reached by name; since it binds the
loopback, `listenHost()` is an address rather than a name, so the URL the
application opens in the browser is `http://127.0.0.1:8080`.

A browser sends an `Origin` header on a POST even when it is same-origin, so the
page the application had just opened was refused by the application that opened
it. The event bus handshake is a POST: every SockJS request answered `403 CORS
Rejected - Invalid origin`, the channel never opened, and the UI retried for as
long as it stayed open while showing the user that device monitoring was down.
Nothing about it was intermittent -- it could not work at all -- and it was
reported from a browser before it was reproduced with curl.

The rule is that the loopback is one host under several names, so all three are
accepted and the port stays open-ended, which the development server needs. It is
not widened to `*`: this API has no authentication, and whatever a page on another
origin could reach here, it could reach in the browser of anyone who visits it.
That refusal is asserted with the same weight as the acceptance, since a policy
that allowed everything would pass the nominal cases on its own.
…nevalentinHA#46

Reworked after the review on antoinevalentinHA#68. The previous commit widened the
any-port pattern to every spelling of the loopback; this replaces it
with the shape agreed in antoinevalentinHA#46: the allowed origin is derived from
listenHost() and LISTEN_PORT, so exactly http://localhost:8080 and
http://127.0.0.1:8080 are answered, and no other port.

That fixes the real failure, a user who types 127.0.0.1:8080 getting
reads that work and every state-changing call refused, and closes the
port wildcard that let any page on a localhost origin drive the API.

The development server needs no exception. src/setupProxy.js carries
/eventbus to the backend, WebSocket included, and rewrites the Origin
it forwards to the backend's own; the package.json proxy already does
that for plain HTTP requests, but not on the WebSocket upgrade.
REACT_APP_EVENTBUS_ORIGIN is no longer needed and is gone.

CorsOriginTest keeps its near-miss refusals and adds the loopback on a
foreign port (http://localhost:1, :3000, :8081, :80800) and https on
the right port. Two of its four tests fail against the old pattern.
@lgnap
lgnap force-pushed the fix/cors-loopback-origin branch from 36cc4f3 to 57b3f60 Compare November 19, 2026 15:31
@lgnap lgnap changed the title fix: answer the loopback under every one of its names Answer exactly the origin the server serves Nov 19, 2026
@lgnap

lgnap commented Nov 19, 2026

Copy link
Copy Markdown
Author

Reworked along #46 in 57b3f60: exact origin from listenHost()/LISTEN_PORT, /eventbus carried by setupProxy.js with the Origin rewritten, localhost:1 added to the refusals. Measurements in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant