node-http-proxy
node-http-proxy copied to clipboard
node-http-proxy crashes on unhandled exception ERR_INVALID_HTTP_TOKEN from node http
node.js version: v20.10.0 node-http-proxy version: v1.18.1
The error
When a client sends a http request with invalid http header (for example "User Agent" without a dash in the middle), the node (built-in) http generates the exception ERR_INVALID_HTTP_TOKEN https://github.com/nodejs/node/blob/v20.x/lib/_http_outgoing.js#L629 which is not handled by node-http-proxy and causes the http proxy to crash. Having proxy.on('error', function(e) {})
does not help.
Test to reproduce the error
-
Start the main server https://github.com/moky80/node-http-proxy-handle-invalid-header/blob/8d460432ed1ee6b24c5985ccd353799dfeae219a/main-server.js with
npm run main-server
-
Start the proxy server https://github.com/moky80/node-http-proxy-handle-invalid-header/blob/8d460432ed1ee6b24c5985ccd353799dfeae219a/proxy-server.js with
npm run proxy-server
-
Send a request with valid header to the proxy server -> works
curl -v -H "User-Agent: pinkytinky" http://localhost:4444
-
Send a request with invalid header (- is missing in User Agent) to the proxy server -> crashes
curl -v -H "User Agent: pinkytinky" http://localhost:4444
Crash log
$ npm run proxy-server
> [email protected] proxy-server
> node proxy-server.js
Proxy server listening on port 4444
node:_http_outgoing:622
throw new ERR_INVALID_HTTP_TOKEN(label || 'Header name', name);
^
TypeError [ERR_INVALID_HTTP_TOKEN]: Header name must be a valid HTTP token ["user agent"]
at ClientRequest.setHeader (node:_http_outgoing:654:3)
at new ClientRequest (node:_http_client:286:14)
at Object.request (node:http:103:10)
at Array.stream (/home/tien/node-http-proxy-handle-invalid-header/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:126:74)
at ProxyServer.<anonymous> (/home/tien/node-http-proxy-handle-invalid-header/node_modules/http-proxy/lib/http-proxy/index.js:81:21)
at Server.<anonymous> (/home/tien/node-http-proxy-handle-invalid-header/proxy-server.js:8:9)
at Server.emit (node:events:514:28)
at parserOnIncoming (node:_http_server:1143:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17) {
code: 'ERR_INVALID_HTTP_TOKEN'
}
Node.js v20.10.0