-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}]`; | ||
} | ||
|
||
const hostname = new URL(`http://${normalizedHost}`).hostname; | ||
|
||
return hostname.replace(/(^\[|\]$)/g, ''); | ||
Comment on lines
+447
to
+449
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @3imed-jaberi I don't think I can use Problem is this doesn't strip the |
||
}) | ||
.filter(ip => !!ip); | ||
} | ||
|
||
if (this.app.maxIpsCount > 0) { | ||
ips = ips.slice(-this.app.maxIpsCount); | ||
} | ||
|
||
return ips; | ||
}, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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', () => { | ||
|
There was a problem hiding this comment.
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: