diff --git a/lib/url.js b/lib/url.js index 0cc364686cc28e..49f2a30726837f 100644 --- a/lib/url.js +++ b/lib/url.js @@ -408,33 +408,22 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { }; function validateHostname(self, rest, hostname) { - for (var i = 0, lastPos; i <= hostname.length; ++i) { - var code; - if (i < hostname.length) - code = hostname.charCodeAt(i); - if (code === 46/*.*/ || i === hostname.length) { - if (i - lastPos > 0) { - if (i - lastPos > 63) { - self.hostname = hostname.slice(0, lastPos + 63); - return '/' + hostname.slice(lastPos + 63) + rest; - } - } - lastPos = i + 1; - continue; - } else if ((code >= 48/*0*/ && code <= 57/*9*/) || - (code >= 97/*a*/ && code <= 122/*z*/) || - code === 45/*-*/ || - (code >= 65/*A*/ && code <= 90/*Z*/) || - code === 43/*+*/ || - code === 95/*_*/ || - code > 127) { - continue; - } + for (var i = 0; i < hostname.length; ++i) { + const code = hostname.charCodeAt(i); + const isValid = (code >= 97/*a*/ && code <= 122/*z*/) || + code === 46/*.*/ || + (code >= 65/*A*/ && code <= 90/*Z*/) || + (code >= 48/*0*/ && code <= 57/*9*/) || + code === 45/*-*/ || + code === 43/*+*/ || + code === 95/*_*/ || + code > 127; + // Invalid host character - self.hostname = hostname.slice(0, i); - if (i < hostname.length) + if (!isValid) { + self.hostname = hostname.slice(0, i); return '/' + hostname.slice(i) + rest; - break; + } } } diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index c97caa36429a9c..75e790cf8d515f 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1225,6 +1225,17 @@ var formatTests = { path: '/node' }, + // greater than or equal to 63 characters after `.` in hostname + [`http://www.${'z'.repeat(63)}example.com/node`]: { + href: `http://www.${'z'.repeat(63)}example.com/node`, + protocol: 'http:', + slashes: true, + host: `www.${'z'.repeat(63)}example.com`, + hostname: `www.${'z'.repeat(63)}example.com`, + pathname: '/node', + path: '/node' + }, + // https://github.com/nodejs/node/issues/3361 'file:///home/user': { href: 'file:///home/user',