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

Conversation

jasonmacgowan
Copy link
Contributor

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

@codecov
Copy link

codecov bot commented Jun 14, 2018

Codecov Report

Merging #1205 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1205   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          487       494    +7     
  Branches       136       135    -1     
=========================================
+ Hits           487       494    +7     
Impacted Files Coverage Δ
lib/request.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d1c9263...d2a2714. Read the comment docs.

Copy link
Member

@3imed-jaberi 3imed-jaberi left a 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.

Comment on lines +447 to +449
const hostname = new URL(`http://${normalizedHost}`).hostname;

return hostname.replace(/(^\[|\]$)/g, '');
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

@jasonmacgowan
Copy link
Contributor Author

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?

/cc @jonathanong @dougwilson

});
});

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).

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;

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 this pull request may close these issues.

request.ip via x-forwarded-for should strip out ports
3 participants