Skip to content

Commit c65d55f

Browse files
jun-okaTrott
authored andcommitted
url: do not truncate long hostnames
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. PR-URL: #9292 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent c732bd1 commit c65d55f

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

lib/url.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -408,33 +408,22 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
408408
};
409409

410410
function validateHostname(self, rest, hostname) {
411-
for (var i = 0, lastPos; i <= hostname.length; ++i) {
412-
var code;
413-
if (i < hostname.length)
414-
code = hostname.charCodeAt(i);
415-
if (code === 46/*.*/ || i === hostname.length) {
416-
if (i - lastPos > 0) {
417-
if (i - lastPos > 63) {
418-
self.hostname = hostname.slice(0, lastPos + 63);
419-
return '/' + hostname.slice(lastPos + 63) + rest;
420-
}
421-
}
422-
lastPos = i + 1;
423-
continue;
424-
} else if ((code >= 48/*0*/ && code <= 57/*9*/) ||
425-
(code >= 97/*a*/ && code <= 122/*z*/) ||
426-
code === 45/*-*/ ||
427-
(code >= 65/*A*/ && code <= 90/*Z*/) ||
428-
code === 43/*+*/ ||
429-
code === 95/*_*/ ||
430-
code > 127) {
431-
continue;
432-
}
411+
for (var i = 0; i < hostname.length; ++i) {
412+
const code = hostname.charCodeAt(i);
413+
const isValid = (code >= 97/*a*/ && code <= 122/*z*/) ||
414+
code === 46/*.*/ ||
415+
(code >= 65/*A*/ && code <= 90/*Z*/) ||
416+
(code >= 48/*0*/ && code <= 57/*9*/) ||
417+
code === 45/*-*/ ||
418+
code === 43/*+*/ ||
419+
code === 95/*_*/ ||
420+
code > 127;
421+
433422
// Invalid host character
434-
self.hostname = hostname.slice(0, i);
435-
if (i < hostname.length)
423+
if (!isValid) {
424+
self.hostname = hostname.slice(0, i);
436425
return '/' + hostname.slice(i) + rest;
437-
break;
426+
}
438427
}
439428
}
440429

test/parallel/test-url.js

+11
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,17 @@ var formatTests = {
12251225
path: '/node'
12261226
},
12271227

1228+
// greater than or equal to 63 characters after `.` in hostname
1229+
[`http://www.${'z'.repeat(63)}example.com/node`]: {
1230+
href: `http://www.${'z'.repeat(63)}example.com/node`,
1231+
protocol: 'http:',
1232+
slashes: true,
1233+
host: `www.${'z'.repeat(63)}example.com`,
1234+
hostname: `www.${'z'.repeat(63)}example.com`,
1235+
pathname: '/node',
1236+
path: '/node'
1237+
},
1238+
12281239
// https://github.com/nodejs/node/issues/3361
12291240
'file:///home/user': {
12301241
href: 'file:///home/user',

0 commit comments

Comments
 (0)