node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Changing headers should be an option

Open VanCoding opened this issue 15 years ago • 37 comments

changing the headers "location" & "origin" when proxying websocket request should be an option, because this is important to make the proxying behave like "not happened".

VanCoding avatar Apr 19 '11 15:04 VanCoding

What happens if you modify request.headers directly before they get proxied out?

Marak avatar Apr 19 '11 16:04 Marak

I think you understood me wrong. I mean the code in the file "node-http-proxy.js" at line 599 and 600. The headers are going to be changed there. It doesn´t matter what headers are incoming. They are changed. That´s the problem.

It would be better if i could specify if I want to keep the original headers or change them.

VanCoding avatar Apr 19 '11 16:04 VanCoding

Here? https://github.com/nodejitsu/node-http-proxy/blob/master/lib/node-http-proxy.js#L589

I'm not sure if this is by design due to the websocket protocol or not. I will reread that part of the spec and follow-up on this.

indexzero avatar Apr 19 '11 17:04 indexzero

@indexzero - I think I'm running into a related issue here without websockets.

I think the disconnect is in the fact that http-proxy acts as a proxy server by default. This means that the receiving server can tell the request has been proxied. In other words, it's not transparent. The proxy should act in this mode by default, but we should probably expose an API option to enable "transparent" proxying. I'm not sure if there is a better name for that.

Here is a more clear illustration of the problem:

 Start a proxy server on local host that points to blog.nodejitsu.com

 A request comes into [ proxy server ] for: http://localhost

 [ proxy server ] proxies this request to [ blog.nodejitsu.com ]

 [ blog.nodejitsu.com ] receives the request and attempts to route the request based on headers.host

 The headers.host files reads "localhost", [ blog.nodejitsu.com ] fails to respond with the correct request

Does that make any sense?

Marak avatar Apr 22 '11 02:04 Marak

I ran into an issue with this recently. In my client (Google Chrome 10), I was getting this while attempting to make a connection to a websocket server behind node-http-proxy:

Error during WebSocket handshake: origin mismatch: http://127.0.0.1:8071 != http://127.0.0.1

It turns out this was due to the issue mentioned here of changing the request headers. After this patch:

--- /tmp/orig +++ lib/node-http-proxy.js @@ -586,8 +586,8 @@ remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);

// Change headers

  • req.headers.host = remoteHost;
  • req.headers.origin = 'http://' + options.host;
  • //req.headers.host = remoteHost;
  • //req.headers.origin = 'http://' + options.host;

outgoing = { host: options.host,

the error stops and the websocket connections work fine.

andyichr avatar Apr 26 '11 16:04 andyichr

This is why I posted this issue. I had the same problem.

VanCoding avatar Apr 26 '11 18:04 VanCoding

The websocket support was originally written by @donnerjack13589 and later updated by @davglass. Maybe they can chime in on the reasoning behind changing the outgoing headers.

I will kill those lines in v0.5.1 (coming this week) if there are no objections since both @flashfan and @andyichr have seen this fix their problems.

indexzero avatar Apr 26 '11 21:04 indexzero

If you're going to kill those lines, please have a look a little further down. There is code to replace the host and origin which wouldn't need to happen. Also, this section currently does not support ports other than 80.

jfis avatar May 05 '11 07:05 jfis

Updates on this? I'm getting an origin mismatch when proxying https => http

frank06 avatar May 11 '11 19:05 frank06

@olauzon can you take a look at this ticket? I'd like to get this resolved this week if possible.

Marak avatar May 11 '11 20:05 Marak

@Marak definitely, will investigate.

olauzon avatar May 11 '11 20:05 olauzon

Thanks @olauzon! Feel free to post your status in this thread.

Marak avatar May 11 '11 20:05 Marak

Great! Should be a pretty simple fix in there. Maybe we can summon @miksago or @donnerjack13589 to help shed some light on the underlying websocket spec and why these headers need to be changed (or don't)

@donnerjack13589 wrote the original implementation here, it's like 4am in Russia tho so he might not be around

indexzero avatar May 11 '11 21:05 indexzero

So what's the issue? Most websocket implementations will be reading the headers of origin and host, as well as the various sec- ones. If you're proxying websockets and adding, say, a "x-" to the front of these headers, then the websocket implementations will break. I think it's probably best not to modify headers on websocket requests.

As for the specification, I don't think it defines as to how proxies should interact with headers, @donnerjack13589 may know otherwise. I might also ask the guys I work with as to whether they can shed light, but they're in BST / GMT timezone.

ThisIsMissEm avatar May 11 '11 21:05 ThisIsMissEm

@miksago Thanks. The problem is that currently node-http-proxy rewrites the origin and host headers for WebSocket requests. This is causing some unexpected behavior for users over HTTPS and in other cross-domain proxying scenarios.

Based on what you said, I guess we should just not be rewriting these headers at all? Code: https://github.com/nodejitsu/node-http-proxy/blob/master/lib/node-http-proxy.js#L589

indexzero avatar May 11 '11 21:05 indexzero

I still think it would be best, if ther was an option. In some cases, it is necessary to rewrite them.

For example when someone tries to forward wss:// to an internal ws:// server. Then we would have an origin mismatch.

VanCoding avatar May 13 '11 07:05 VanCoding

how would wss to ws work? is that common for a proxy to handle all the encoding/decoding?

that isn't to say changing headers is never necessary

the code to change headers and replace host and origin in the handshake is very deliberate the coder(s) must have thought it was necessary would be nice to hear why

code i'm referring to:

// Change headers
req.headers.host   = remoteHost;
req.headers.origin = 'http://' + options.host;

...

request.socket.on('data', handshake = function(data) {
  // Handshaking

  // Ok, kind of harmfull part of code
  // Socket.IO is sending hash at the end of handshake
  // If protocol = 76
  // But we need to replace 'host' and 'origin' in response
  // So we split data to printable data and to non-printable
  // (Non-printable will come after double-CRLF)
  var sdata = data.toString();

  // Get Printable
  sdata = sdata.substr(0, sdata.search(CRLF + CRLF));

  // Get Non-Printable
  data = data.slice(Buffer.byteLength(sdata), data.length);

  // Replace host and origin
  sdata = sdata.replace(remoteHost, options.host)
               .replace(remoteHost, options.host);

jfis avatar May 13 '11 20:05 jfis

@jfis Again, this is a symptom of a larger problem. I am working on something in the experimental branch, but I'm not sure if we can actually write a websocket proxy on top of the existing http.Agent APIs without event emitter leaks.

indexzero avatar May 13 '11 20:05 indexzero

@indexzero i don't see how changing the headers and replacing host and origin in the handshake relates to http.Agent but I'll take your word for it.

I do see the problem with http.Agent in the other thread (#50) though.

jfis avatar May 13 '11 20:05 jfis

@jfis They are only related in that they both have to do with websockets. If this gets fixed, that is still a problem and websockets is still broken.

indexzero avatar May 13 '11 21:05 indexzero