facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.62k stars 26.8k forks source link

Proxying WebSocket requests is broken #10878

Open minus7 opened 3 years ago

minus7 commented 3 years ago

Describe the bug

Setting the proxy field in package.json proxies normal HTTP requests and WebSocket requests.

For normal requests, the Origin header is patched to equal the target host (added in #1212).

For websocket requests, the onProxyReq hook is not executed and thus the original Origin is sent to the target server. The target server will likely reject the request because Host and Origin mismatch (Gorilla websockts in my case). To also patch Origin for websocket requests, the onProxyReqWs hook has to be specified additionally.

Steps to reproduce

  1. Add a breakpoint or console.log in the onProxyReq callback mentioned above.
  2. yarn start any created project with a configured proxy
  3. Open a websocket connection
  4. See nothing getting logged because only onProxyReqWs is called for websocket connections.

Expected behavior

Origin and Host are equal for proxied websocket requests (e.g. http://localhost:8080).

Actual behavior

Origin is the original header sent by the browser (e.g. http://localhost:3000) while Host is changed (e.g. http://localhost:8080).

--

Note: I'd have sent a patch, but I refuse to sign the CLA.

dedo1911 commented 2 years ago

Related to #5280