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

Always allow requests with IP-address as host in checkHost() #1007

Merged
merged 3 commits into from
Jul 27, 2017

Conversation

usefulthink
Copy link
Contributor

What kind of change does this PR introduce?

enhancement

Did you add or update the examples/?

no.

Summary

This patch will allow any requests made using an IP-address to always pass the
checkHost-test.

IP-addresses are not susceptible to a dns-rebind like attack so it would make
sense to not block them to make local-network development possible without
needing to disable the host-checks entirely.

Does this PR introduce a breaking change?

no

Other information

fixes #931

This patch will allow any requests made using an IP-address to always pass the
checkHost-test.

IP-addresses are not susceptible to a dns-rebind like attack so it would make
sense to not block them to make local-network development possible without
needing to disable the host-checks entirely.

fixes webpack#931
@jsf-clabot
Copy link

jsf-clabot commented Jul 24, 2017

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Jul 24, 2017

Codecov Report

Merging #1007 into master will increase coverage by 0.11%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1007      +/-   ##
==========================================
+ Coverage   72.13%   72.25%   +0.11%     
==========================================
  Files           4        4              
  Lines         463      465       +2     
  Branches      139      140       +1     
==========================================
+ Hits          334      336       +2     
  Misses        129      129
Impacted Files Coverage Δ
lib/Server.js 79.81% <100%> (+0.12%) ⬆️

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 628f0a2...79219db. Read the comment docs.

@usefulthink
Copy link
Contributor Author

first-time contributor here. Please let me know if there are any docs I need to validate/update or anything else I should think of.

lib/Server.js Outdated
@@ -444,6 +444,9 @@ Server.prototype.checkHost = function(headers) {
// always allow localhost host, for convience
if(hostname === "127.0.0.1" || hostname === "localhost") return true;

// always allow requests with explicit IP-address
if(/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$/.test(hostname)) return true;

Choose a reason for hiding this comment

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

Should we add a few more testcases, for both the valid and invalid IP cases?

Also, I wonder if a simplified regex that just checked that each octet was numeric would be simpler to read/faster/still as secure? RFC3696 section 2 says that top level domain names aren't allowed to be all-numeric, so eg 999.999.999.999 still won't be treated as a domain, so can't be used for DNS rebinding.

For example:

// Requests to explicit IP-addresses can't be exploited by DNS rebinding.
// For simplicity the regex matches numeric ranges that aren't valid IPs, but
// this is still secure since top level domain names can never be all-numeric:
// https://tools.ietf.org/html/rfc3696#section-2
if(/^([0-9]{1,3}(\.|$)){4}$/.test(hostname)) return true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the simplicity and probably would want to do that. We don't validate other aspects of IP adress as well (reserved, multicast, etc) and I can't think of a way to have an invalid or unrouteable IP-address in the browser anyway.

lib/Server.js Outdated
@@ -444,6 +444,9 @@ Server.prototype.checkHost = function(headers) {
// always allow localhost host, for convience
if(hostname === "127.0.0.1" || hostname === "localhost") return true;

// always allow requests with explicit IP-address
if(/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$/.test(hostname)) return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see https://www.npmjs.com/package/ip and ip.isV4Format used here, rather than a regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that as well, but didn't want to introduce any new dependencies for something that could be put in a simple (ok, thats arguable) regex. Is there any policy regarding adding dependencies?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the dependencies were being packaged for a production environment, I'd agree with you. Since this is strictly a development tool, tried and true dependencies to perform utility functions are acceptable. You'll see that pattern throughout the commit history. (for example; loglevel over a custom implementation).

@shellscape shellscape merged commit 72efaab into webpack:master Jul 27, 2017
@usefulthink usefulthink deleted the check-hosts-allow-ips branch July 27, 2017 12:35
@usefulthink
Copy link
Contributor Author

Thanks! 🎊

@@ -441,8 +442,11 @@ Server.prototype.checkHost = function(headers) {
const idx = hostHeader.indexOf(":");
const hostname = idx >= 0 ? hostHeader.substr(0, idx) : hostHeader;

// always allow requests with explicit IP-address
if(ip.isV4Format(hostname)) return true;
Copy link

Choose a reason for hiding this comment

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

Shouldn't it allow IPv6-adresses as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

that's true, good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I do another PR for this? Did anyone ever use IPv6 in such a context?
Using http://[::1]:1234/ is absolutely possible, but that will already break in the lines before as a colon no longer works to split out the port-number from the host-header.

Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worthwhile to solve. I haven't heard of anyone using IPv6 in that way, but the call out by @trygveaa is valid. Totally up to you if you'd like to create a followup PR

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.

Whitelist all IP-address-like hosts names by default in checkHost
5 participants