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: parse port in x-forwarded-for (#827) #1205

Open
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,28 @@ module.exports = {
get ips() {
const proxy = this.app.proxy;
const val = this.get(this.app.proxyIpHeader);
let ips = proxy && val
? val.split(/\s*,\s*/)
: [];
let ips = [];

if (proxy && val) {
ips = val.split(/\s*,\s*/)
.map(host => {
let normalizedHost = host;

if (net.isIPv6(host)) {
normalizedHost = `[${host}]`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because since this already validates it is the IP only, I would suggest simply shortcutting the rest of the processing for this case:

Suggested change
normalizedHost = `[${host}]`;
return host;

}

const hostname = new URL(`http://${normalizedHost}`).hostname;

return hostname.replace(/(^\[|\]$)/g, '');
Comment on lines +447 to +449
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3imed-jaberi I don't think I can use parseurl module here as it expects a http.IncomingMessage object and here we're working on a header. I see url.parse is now deprecated so I swapped to using the URL constructor.

Problem is this doesn't strip the [] chars from IPv6 hosts so now we gotta do it manually. Regex to the rescue

})
.filter(ip => !!ip);
}

if (this.app.maxIpsCount > 0) {
ips = ips.slice(-this.app.maxIpsCount);
}

return ips;
},

Expand Down
18 changes: 18 additions & 0 deletions test/request/ips.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ describe('req.ips', () => {
assert.deepEqual(req.ips, ['127.0.0.1', '127.0.0.2']);
});
});

describe('and contains IPv4', () => {
it('should not return port', () => {
const req = request();
req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1:80,127.0.0.2';
assert.deepEqual(req.ips, ['127.0.0.1', '127.0.0.2']);
});
});

describe('and contains IPv6', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend adding a test for a IPv6 address with a port (i.e [::1]:80) as well as a dns name with a port (i.e. localhost:80).

it('should parse correctly', () => {
const req = request();
req.app.proxy = true;
req.header['x-forwarded-for'] = '::1';
assert.deepEqual(req.ips, ['::1']);
});
});
});

describe('when options.proxyIpHeader is present', () => {
Expand Down