Skip to content

Commit e8b61bb

Browse files
sapphi-redbluwy
andauthored
feat: show warning on 431 response (#9324)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
1 parent c868e64 commit e8b61bb

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

docs/guide/troubleshooting.md

+10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ To solve this:
4444
$ sudo sysctl fs.inotify.max_user_watches=524288
4545
```
4646

47+
### 431 Request Header Fields Too Large
48+
49+
When the server / WebSocket server receives a large HTTP header, the request will be dropped and the following warning will be shown.
50+
51+
> Server responded with status code 431. See https://vitejs.dev/guide/troubleshooting.html#_431-request-header-fields-too-large.
52+
53+
This is because Node.js limits request header size to mitigate [CVE-2018-12121](https://www.cve.org/CVERecord?id=CVE-2018-12121).
54+
55+
To avoid this, try to reduce your request header size. For example, if the cookie is long, delete it. Or you can use [`--max-http-header-size`](https://nodejs.org/api/cli.html#--max-http-header-sizesize) to change max header size.
56+
4757
## HMR
4858

4959
### Vite detects a file change but the HMR is not working

packages/vite/src/node/http.ts

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
} from 'node:http'
77
import type { ServerOptions as HttpsServerOptions } from 'node:https'
88
import type { Connect } from 'types/connect'
9+
import colors from 'picocolors'
910
import { isObject } from './utils'
1011
import type { ProxyOptions } from './server/middlewares/proxy'
1112
import type { Logger } from './logger'
@@ -183,3 +184,19 @@ export async function httpServerStart(
183184
})
184185
})
185186
}
187+
188+
export function setClientErrorHandler(
189+
server: HttpServer,
190+
logger: Logger
191+
): void {
192+
server.on('clientError', (err, socket) => {
193+
if ((err as any).code === 'HPE_HEADER_OVERFLOW') {
194+
logger.warn(
195+
colors.yellow(
196+
'Server responded with status code 431. ' +
197+
'See https://vitejs.dev/guide/troubleshooting.html#_431-request-header-fields-too-large.'
198+
)
199+
)
200+
}
201+
})
202+
}

packages/vite/src/node/preview.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import type { Connect } from 'types/connect'
66
import corsMiddleware from 'cors'
77
import type { ResolvedServerOptions, ResolvedServerUrls } from './server'
88
import type { CommonServerOptions } from './http'
9-
import { httpServerStart, resolveHttpServer, resolveHttpsConfig } from './http'
9+
import {
10+
httpServerStart,
11+
resolveHttpServer,
12+
resolveHttpsConfig,
13+
setClientErrorHandler
14+
} from './http'
1015
import { openBrowser } from './server/openBrowser'
1116
import compression from './server/middlewares/compression'
1217
import { proxyMiddleware } from './server/middlewares/proxy'
@@ -78,6 +83,7 @@ export async function preview(
7883
app,
7984
await resolveHttpsConfig(config.preview?.https, config.cacheDir)
8085
)
86+
setClientErrorHandler(httpServer, config.logger)
8187

8288
// apply server hooks from plugins
8389
const postHooks: ((() => void) | void)[] = []

packages/vite/src/node/server/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import type { Connect } from 'types/connect'
1212
import launchEditorMiddleware from 'launch-editor-middleware'
1313
import type { SourceMap } from 'rollup'
1414
import type { CommonServerOptions } from '../http'
15-
import { httpServerStart, resolveHttpServer, resolveHttpsConfig } from '../http'
15+
import {
16+
httpServerStart,
17+
resolveHttpServer,
18+
resolveHttpsConfig,
19+
setClientErrorHandler
20+
} from '../http'
1621
import type { InlineConfig, ResolvedConfig } from '../config'
1722
import { isDepsOptimizerEnabled, resolveConfig } from '../config'
1823
import {
@@ -301,6 +306,10 @@ export async function createServer(
301306
: await resolveHttpServer(serverConfig, middlewares, httpsOptions)
302307
const ws = createWebSocketServer(httpServer, config, httpsOptions)
303308

309+
if (httpServer) {
310+
setClientErrorHandler(httpServer, config.logger)
311+
}
312+
304313
const { ignored = [], ...watchOptions } = serverConfig.watch || {}
305314
const watcher = chokidar.watch(path.resolve(root), {
306315
ignored: [

0 commit comments

Comments
 (0)