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

[fix] do not append XHeader values #1581

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -56,7 +56,7 @@ module.exports = {
},

/**
* Sets `x-forwarded-*` headers if specified in config.
* Sets `x-forwarded-*` headers if specified in config and there's none.
*
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
Expand All @@ -76,10 +76,10 @@ module.exports = {
};

['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header];
var headerName = 'x-forwarded-' + header;
if (!req.headers[headerName]) {
req.headers[headerName] = values[header];
}
});

req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
Expand Down
8 changes: 4 additions & 4 deletions lib/http-proxy/passes/ws-incoming.js
Expand Up @@ -59,10 +59,10 @@ module.exports = {
};

['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header];
var headerName = 'x-forwarded-' + header;
if (!req.headers[headerName]) {
req.headers[headerName] = values[header];
}
});
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "http-proxy",
"version": "1.18.1",
"version": "1.18.2",
"repository": {
"type": "git",
"url": "https://github.com/http-party/node-http-proxy.git"
Expand Down
21 changes: 21 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Expand Up @@ -69,6 +69,27 @@ describe('lib/http-proxy/passes/web.js', function() {
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
expect(stubRequest.headers['x-forwarded-proto']).to.be('http');
});

it('do not change the x-forwarded-* header values if already exist on req.headers', function () {
var stubRequest = {
connection: {
remoteAddress: '192.168.1.2',
remotePort: '8080'
},
headers: {
host: '192.168.1.2:8080',
'x-forwarded-host': '192.168.1.3:8081',
'x-forwarded-for': '192.168.1.3',
'x-forwarded-port': '8081',
'x-forwarded-proto': 'https'
}
};
webPasses.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3');
expect(stubRequest.headers['x-forwarded-port']).to.be('8081');
expect(stubRequest.headers['x-forwarded-proto']).to.be('https');
expect(stubRequest.headers['x-forwarded-host']).to.be('192.168.1.3:8081');
});
});
});

Expand Down
24 changes: 23 additions & 1 deletion test/lib-http-proxy-passes-ws-incoming-test.js
Expand Up @@ -9,7 +9,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
method: 'DELETE',
headers: {}
},
stubSocket = {
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
Expand Down Expand Up @@ -116,5 +116,27 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
expect(stubRequest.headers['x-forwarded-port']).to.be('8181');
expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');
});

it('do not change the x-forwarded-* header values if already exist on req.headers', function () {
var stubRequest = {
socket: {
remoteAddress: '192.168.1.3',
remotePort: '8181'
},
connection: {
pair: true
},
headers: {
host: '192.168.1.3:8181',
'x-forwarded-for': '192.168.1.2',
'x-forwarded-port': '8182',
'x-forwarded-proto': 'ws'
}
};
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
expect(stubRequest.headers['x-forwarded-port']).to.be('8182');
expect(stubRequest.headers['x-forwarded-proto']).to.be('ws');
});
});
});