Skip to content

Commit 7b0a65e

Browse files
fix(proxy): rewrite the origin header to match the target for ws proxy (#16558)
Co-authored-by: John Hunter <[email protected]>
1 parent 9f02a9f commit 7b0a65e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

docs/config/server-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Configure custom proxy rules for the dev server. Expects an object of `{ key: op
9090

9191
Note that if you are using non-relative [`base`](/config/shared-options.md#base), you must prefix each key with that `base`.
9292

93-
Extends [`http-proxy`](https://github.com/http-party/node-http-proxy#options). Additional options are [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/proxy.ts#L13).
93+
Extends [`http-proxy`](https://github.com/http-party/node-http-proxy#options). Additional options are [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/middlewares/proxy.ts#L13). Note that [unlike http-proxy](https://github.com/http-party/node-http-proxy/issues/1669), the `changeOrigin` option will change both host and origin headers to match the target.
9494

9595
In some cases, you might also want to configure the underlying dev server (e.g. to add custom middlewares to the internal [connect](https://github.com/senchalabs/connect) app). In order to do that, you need to write your own [plugin](/guide/using-plugins.html) and use [configureServer](/guide/api-plugin.html#configureserver) function.
9696

packages/vite/src/node/server/middlewares/proxy.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
2929
) => void | null | undefined | false | string
3030
}
3131

32+
const setOriginHeader = (
33+
proxyReq: http.ClientRequest,
34+
options: HttpProxy.ServerOptions,
35+
) => {
36+
// Browsers may send Origin headers even with same-origin
37+
// requests. It is common for WebSocket servers to check the Origin
38+
// header, so if changeOrigin is true we change the Origin to match
39+
// the target URL.
40+
// https://github.com/http-party/node-http-proxy/issues/1669
41+
if (options.changeOrigin) {
42+
const { target } = options
43+
44+
if (proxyReq.getHeader('origin') && target) {
45+
const changedOrigin =
46+
typeof target === 'object'
47+
? `${target.protocol}//${target.host}`
48+
: target
49+
50+
proxyReq.setHeader('origin', changedOrigin)
51+
}
52+
}
53+
}
54+
3255
export function proxyMiddleware(
3356
httpServer: HttpServer | null,
3457
options: NonNullable<CommonServerOptions['proxy']>,
@@ -89,7 +112,13 @@ export function proxyMiddleware(
89112
}
90113
})
91114

115+
proxy.on('proxyReq', (proxyReq, req, res, options) => {
116+
setOriginHeader(proxyReq, options)
117+
})
118+
92119
proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
120+
setOriginHeader(proxyReq, options)
121+
93122
socket.on('error', (err) => {
94123
config.logger.error(
95124
`${colors.red(`ws proxy socket error:`)}\n${err.stack}`,

0 commit comments

Comments
 (0)