From 089c3e52cd665879ec8ac33a9f73952a051b08f7 Mon Sep 17 00:00:00 2001 From: Junshu Okamoto Date: Wed, 30 Nov 2016 22:21:00 -0600 Subject: [PATCH 1/4] url: stop truncating long hostname MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, around line 417 lib/url.js is truncating hostname and put the rest of hostname to the path if hostname length after `.` is equal or more than 63. This behavior is different from browser behavior. I changed the code so that it doesn’t truncate. I also added the test example which has more than 63 length in after `.` in hostname in test url. --- lib/url.js | 39 ++++++++++++++------------------------- test/parallel/test-url.js | 11 +++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/url.js b/lib/url.js index 0cc364686cc28e..9f92e1db7f2914 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 isVc = code === 46/*.*/ || + 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; + // Invalid host character - self.hostname = hostname.slice(0, i); - if (i < hostname.length) + if (!isVc && i !== hostname.length) { + 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', From 835a097b711d2e3cbc74b0e760784e13d2d60d25 Mon Sep 17 00:00:00 2001 From: Junshu Okamoto Date: Fri, 23 Dec 2016 09:49:29 -0800 Subject: [PATCH 2/4] test: refactor fix some typo, put parents for groups and correct for loop condition --- lib/url.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/url.js b/lib/url.js index 9f92e1db7f2914..99aa891159ae6e 100644 --- a/lib/url.js +++ b/lib/url.js @@ -408,19 +408,19 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { }; function validateHostname(self, rest, hostname) { - for (var i = 0; i <= hostname.length; ++i) { + for (var i = 0; i < hostname.length; ++i) { const code = hostname.charCodeAt(i); const isVc = code === 46/*.*/ || - code >= 48/*0*/ && code <= 57/*9*/ || - code >= 97/*a*/ && code <= 122/*\z*/ || + (code >= 48/*0*/ && code <= 57/*9*/) || + (code >= 97/*a*/ && code <= 122/*z*/) || code === 45/*-*/ || - code >= 65/*A*/ && code <= 90/*Z*/ || + (code >= 65/*A*/ && code <= 90/*Z*/) || code === 43/*+*/ || code === 95/*_*/|| code > 127; // Invalid host character - if (!isVc && i !== hostname.length) { + if (!isVc) { self.hostname = hostname.slice(0, i); return '/' + hostname.slice(i) + rest; } From ac7c6be434128b1c56026e314b5154fdc0966427 Mon Sep 17 00:00:00 2001 From: Junshu Okamoto Date: Sat, 24 Dec 2016 12:15:32 -0800 Subject: [PATCH 3/4] url: correct variable name, and re-prioritize conditionals to valid hostname --- lib/url.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/url.js b/lib/url.js index 99aa891159ae6e..a0efb2da1fcfcc 100644 --- a/lib/url.js +++ b/lib/url.js @@ -410,17 +410,17 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { function validateHostname(self, rest, hostname) { for (var i = 0; i < hostname.length; ++i) { const code = hostname.charCodeAt(i); - const isVc = code === 46/*.*/ || - (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; + 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 - if (!isVc) { + if (!isValid) { self.hostname = hostname.slice(0, i); return '/' + hostname.slice(i) + rest; } From 782e3517402dda6781a783dfe3a19f39a2d8bd5d Mon Sep 17 00:00:00 2001 From: Junshu Okamoto Date: Sat, 24 Dec 2016 12:32:24 -0800 Subject: [PATCH 4/4] url: add missing space --- lib/url.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/url.js b/lib/url.js index a0efb2da1fcfcc..49f2a30726837f 100644 --- a/lib/url.js +++ b/lib/url.js @@ -416,7 +416,7 @@ function validateHostname(self, rest, hostname) { (code >= 48/*0*/ && code <= 57/*9*/) || code === 45/*-*/ || code === 43/*+*/ || - code === 95/*_*/|| + code === 95/*_*/ || code > 127; // Invalid host character