Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

net.createConnection seems to assume IPv4 localhost address when no host given #8140

Closed
misterdjules opened this issue Aug 11, 2014 · 4 comments
Labels

Comments

@misterdjules
Copy link

It seems that Socket.prototype.connect assumes an IPv4 localhost address when no host is given. Here's the code in lib/net.js:

} else if (!options.host) {
    debug('connect: missing host');
    self._host = '127.0.0.1';
    connect(self, self._host, options.port, 4);

Should we instead set options.host to 'localhost' and fall through so that both use cases (host set in options or not) resolve to the same IP address?

There are other places where it seems IPv4 addresses are assumed:

  1. In Socket.prototype.listen, when no IP is passed, IPv4 is hardcoded:
} else if (util.isUndefined(arguments[1]) ||
             util.isFunction(arguments[1]) ||
             util.isNumber(arguments[1])) {
    // The first argument is the port, no IP given.
    listen(self, null, port, 4, backlog);
  1. There's another case in Socket.prototype.connect when we hard-code IPv4, I'm not sure what use case it covers:
} else {
        timers._unrefActive(self);

        addressType = addressType || 4;

        // node_net.cc handles null host names graciously but user land
        // expects remoteAddress to have a meaningful value
        ip = ip || (addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1');

        connect(self,
                ip,
                options.port,
                addressType,
                options.localAddress,
                options.localPort);
      }

/cc @cjihrig

@cjihrig
Copy link

cjihrig commented Aug 12, 2014

@misterdjules thanks for pointing this out. I'll look into removing hard coded IP addresses.

@misterdjules
Copy link
Author

@cjihrig Thanks!

@indutny
Copy link
Member

indutny commented Aug 27, 2014

@cjihrig ping ;)

cjihrig added a commit that referenced this issue Jan 4, 2015
Socket.prototype.connect() sometimes throws on bad inputs
after an asynchronous operation. This commit makes the input
validation synchronous. This commit also removes some hard
coded IP addresses.

PR-URL: #8180
Fixes: #8140
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
@cjihrig
Copy link

cjihrig commented Jan 4, 2015

Closed in b636ba8

@cjihrig cjihrig closed this as completed Jan 4, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants