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

node-http-proxy crashes on unhandled exception ERR_INVALID_HTTP_TOKEN from node http #1679

Open
moky80 opened this issue Sep 17, 2024 · 1 comment

Comments

@moky80
Copy link

moky80 commented Sep 17, 2024

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

  1. Start the main server https://github.com/moky80/node-http-proxy-handle-invalid-header/blob/8d460432ed1ee6b24c5985ccd353799dfeae219a/main-server.js with npm run main-server

  2. Start the proxy server https://github.com/moky80/node-http-proxy-handle-invalid-header/blob/8d460432ed1ee6b24c5985ccd353799dfeae219a/proxy-server.js with npm run proxy-server

  3. Send a request with valid header to the proxy server -> works
    curl -v -H "User-Agent: pinkytinky" http://localhost:4444

  4. 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
@ravin00
Copy link

ravin00 commented Oct 27, 2024

I think this occurs because of there is a invalid HTTP header name containing some space called user agent.
Screenshot 2024-10-27 at 17 28 30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants