-
-
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?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1205 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 487 494 +7
Branches 136 135 -1
=========================================
+ Hits 487 494 +7
Continue to review full report at Codecov.
|
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.
@jasonmacgowan, thank you for your PR 🙏🏻.
Plz, can you resolve the conflicts then use parseurl
module over the url
module.
2b59260
to
f860ab0
Compare
const hostname = new URL(`http://${normalizedHost}`).hostname; | ||
|
||
return hostname.replace(/(^\[|\]$)/g, ''); |
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.
@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
Looks like @3imed-jaberi is going to be unavailable for a while based on his profile. Anyone else around to take a look at this? |
}); | ||
}); | ||
|
||
describe('and contains IPv6', () => { |
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.
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
).
let normalizedHost = host; | ||
|
||
if (net.isIPv6(host)) { | ||
normalizedHost = `[${host}]`; |
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:
normalizedHost = `[${host}]`; | |
return host; |
Parses out IPs even if a port is sent in x-forwarded-for for IPv4. Probably fails if port is sent with an IPv6 address, but works otherwise.
fixes #827