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

Proxy mode missing critical headers #430

Closed
cowwoc opened this issue Feb 6, 2015 · 15 comments · May be fixed by tobybellwood/govstrap#4
Closed

Proxy mode missing critical headers #430

cowwoc opened this issue Feb 6, 2015 · 15 comments · May be fixed by tobybellwood/govstrap#4

Comments

@cowwoc
Copy link

cowwoc commented Feb 6, 2015

Given:

When running in proxy mode, we need the following headers in order for the application to work properly:

  • X-Forwarded-For: The IP address of the client (because the incoming request is coming from the proxy, which masks the client's real IP address)
  • X-Forwarded-Host: The host requested by the client (i.e. the proxy address, not the existing server's address)
  • Host: The existing server's address (right now it is the proxy server's address, which is wrong). In Do not overwrite Host header in proxy #120 (comment) you confirmed the behavior I am asking for, but the implementation does not match what you said it should be.

The first header is needed for security reasons (ability to detect local vs remote clients). The second header is needed for redirecting clients (e.g. redirect the user to the login page).

@shakyShane
Copy link
Contributor

this will be landing in BrowserSync soon which will allow you to define your own headers for each request.

:)

@shakyShane
Copy link
Contributor

@cowwoc
Copy link
Author

cowwoc commented Feb 17, 2015

@shakyShane Hmm, I don't fully understand https://github.com/BrowserSync/browser-sync/blob/master/examples/proxy.headers.js#L27. Where is config.urlObj documented?

@shakyShane
Copy link
Contributor

@cowwoc sorry it's not documented yet, but it's just the foxy config object https://github.com/shakyShane/foxy/blob/master/lib/server.js#L16-L21

You could log it out for now to see what you have access to.

browserSync({
    files: ["app/css/*.css"],
    proxy: {
        target: "localhost:8000",
        reqHeaders: function (config) {
            console.log(config);
            return {
                "host":            config.urlObj.host,
                "accept-encoding": "identity",
                "agent":           false
            }
        }
    }
});

@generalov
Copy link

Thank you, @shakyShane, it works:

 browserSync({
            proxy: {
            target: '127.0.0.1:5000',
            reqHeaders: function (config) {
                return {
                    // prevent Host header overriding
                    //"host":            config.urlObj.host,
                    "accept-encoding": "identity", // disable any compression
                    "agent":           false
                };
            },
            middleware: function (req, res, next) {
                res.setHeader("X-Forwarded-For", req.ip);
                res.setHeader("X-Forwarded-Host", req.headers.host);
                next();
            }
        }
    });

It seems bit tricky because 'http-proxy' has xfwd option.

@shakyShane
Copy link
Contributor

Thanks @generalov .

@cowwoc - does the example from @generalov work for you?

If it does and this is a common use-case I will add to core behind a single option. :)

@shakyShane
Copy link
Contributor

version 2.7.0 allows any of the node-http-proxy options to be passed through, so in your case, xfwd is the one you want.

var bs = require('browser-sync').create();

bs.init({
    proxy: {
        target: "www.bbc.co.uk",
        proxyOptions: {
            xfwd: true
        }
    }
});

@generalov
Copy link

Thanks!

@ericfong
Copy link

ericfong commented Jul 1, 2015

Seems xfwd is missing "x-forwarded-host"

@levicook
Copy link

@ericfong I ran into the same issue -- X-Forwarded-Host wasn't populated by xfwd.
Here's how I ended up populating it:

#!/usr/bin/env node
var path = require('path')
var home = path.normalize(__dirname + '../../..')

var bs = require('browser-sync').create()

bs.init({
  files: [
    path.join(home, 'server/static/*.*')
  ],
  proxy: {
    target: 'localhost:8000',
    middleware: function (req, res, next) {
      req.headers['X-Forwarded-Host'] = req.headers.host
      next()
    }
  }
})

@quinncomendant
Copy link

quinncomendant commented May 13, 2016

How to enable Host: header forwarding using the command line browser-sync? Is this the function of the --host option (its description, Specify a hostname to use is not very clear)?

@levicook
Copy link

Good question -- I assumed that it wasn't because that flag would traditionally control what interface the server would accept requests on. Not sure my assumption holds in this case. If it is a way to control the X header, the flag should probably be renamed.

@heijmans
Copy link

heijmans commented Jun 9, 2017

If you want to pass the Host header to the backend unchanged, use the following config:

  proxy: {
    target: "...",
    proxyOptions: {
      changeOrigin: false
    }
  },

By default, browser-sync passes changeOriginal: true to node-http-proxy, and this will change the Host header.

@dancon
Copy link

dancon commented Sep 19, 2017

there is no proxy.proxyOptions description in API Documention

@chrisspiegl
Copy link

I think this is interesting. I found this by luck and some googleing and @dancon is right, I also did not find any mention of this proxy.proxyOptions nore the changeOrigin in the docs anywhere — despite finding it very useful.

So happy I found this here now.

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

Successfully merging a pull request may close this issue.

9 participants