Skip to content

Commit d4cca9a

Browse files
committed
Revert "fix(proxy): replace changeOrigin changes in 5.3.0 with new rewriteWsOrigin option (vitejs#17563)"
This reverts commit 14c3d49.
1 parent a10fc79 commit d4cca9a

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

docs/config/server-options.md

Lines changed: 1 addition & 3 deletions
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

@@ -123,11 +123,9 @@ export default defineConfig({
123123
},
124124
},
125125
// Proxying websockets or socket.io: ws://localhost:5173/socket.io -> ws://localhost:5174/socket.io
126-
// Exercise caution using `rewriteWsOrigin` as it can leave the proxying open to CSRF attacks.
127126
'/socket.io': {
128127
target: 'ws://localhost:5174',
129128
ws: true,
130-
rewriteWsOrigin: true,
131129
},
132130
},
133131
},

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,20 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
2727
res: http.ServerResponse,
2828
options: ProxyOptions,
2929
) => void | null | undefined | false | string
30-
/**
31-
* rewrite the Origin header of a WebSocket request to match the the target
32-
*
33-
* **Exercise caution as rewriting the Origin can leave the proxying open to [CSRF attacks](https://owasp.org/www-community/attacks/csrf).**
34-
*/
35-
rewriteWsOrigin?: boolean | undefined
3630
}
3731

38-
const rewriteOriginHeader = (
32+
const setOriginHeader = (
3933
proxyReq: http.ClientRequest,
40-
options: ProxyOptions,
41-
config: ResolvedConfig,
34+
options: HttpProxy.ServerOptions,
4235
) => {
4336
// Browsers may send Origin headers even with same-origin
4437
// requests. It is common for WebSocket servers to check the Origin
45-
// header, so if rewriteWsOrigin is true we change the Origin to match
38+
// header, so if changeOrigin is true we change the Origin to match
4639
// the target URL.
47-
if (options.rewriteWsOrigin) {
40+
// https://github.com/http-party/node-http-proxy/issues/1669
41+
if (options.changeOrigin) {
4842
const { target } = options
4943

50-
if (proxyReq.headersSent) {
51-
config.logger.warn(
52-
colors.yellow(
53-
`Unable to rewrite Origin header as headers are already sent.`,
54-
),
55-
)
56-
return
57-
}
58-
5944
if (proxyReq.getHeader('origin') && target) {
6045
const changedOrigin =
6146
typeof target === 'object'
@@ -127,8 +112,12 @@ export function proxyMiddleware(
127112
}
128113
})
129114

115+
proxy.on('proxyReq', (proxyReq, req, res, options) => {
116+
setOriginHeader(proxyReq, options)
117+
})
118+
130119
proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
131-
rewriteOriginHeader(proxyReq, options, config)
120+
setOriginHeader(proxyReq, options)
132121

133122
socket.on('error', (err) => {
134123
config.logger.error(

0 commit comments

Comments
 (0)