Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Origin header on proxied WebSocket requests #12583

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/react-dev-utils/WebpackDevServerUtils.js
Expand Up @@ -359,16 +359,19 @@ function prepareProxy(proxy, appPublicFolder, servedPathname) {
// We use a heuristic: We want to proxy all the requests that are not meant
// for static assets and as all the requests for static assets will be using
// `GET` method, we can proxy all non-`GET` requests.
// For `GET` requests, if request `accept`s text/html, we pick /index.html.
// For `GET` requests, if request `Accept`s text/html, we pick /index.html.
// Modern browsers include text/html into `accept` header when navigating.
// However API calls like `fetch()` won’t generally accept text/html.
// Some browsers don't set an `Accept` header on WebSocket upgrade requests,
// so we also have a check for those to make sure they are still proxied.
// If this heuristic doesn’t work well for you, use `src/setupProxy.js`.
context: function (pathname, req) {
return (
req.method !== 'GET' ||
(mayProxy(pathname) &&
req.headers.accept &&
req.headers.accept.indexOf('text/html') === -1)
((req.headers.accept &&
req.headers.accept.indexOf('text/html') === -1) ||
(req.headers.upgrade && req.headers.upgrade === 'websocket')))
);
},
onProxyReq: proxyReq => {
Expand All @@ -379,6 +382,15 @@ function prepareProxy(proxy, appPublicFolder, servedPathname) {
proxyReq.setHeader('origin', target);
}
},
onProxyReqWs: proxyReq => {
// Browsers may send Origin headers even with same-origin
// requests. It is common for WebSocket servers to check the Origin
// header, so we have to change the Origin to match
// the target URL.
if (proxyReq.getHeader('origin')) {
proxyReq.setHeader('origin', target);
}
},
onError: onProxyError(target),
secure: false,
changeOrigin: true,
Expand Down