Skip to content

Commit

Permalink
Merge pull request #60 from myndzi/url-parse
Browse files Browse the repository at this point in the history
Update cleanHostValue so it never returns invalid hostname characters
  • Loading branch information
Thomas Parisot committed Sep 17, 2015
2 parents 13877e1 + 3e40c07 commit 2593cd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,16 @@ tld.prototype.isValid = function isValid (host) {

// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
var hasPrefixRE = /^(([a-z][a-z0-9+.-]*)?:)?\/\//;
var invalidHostnameChars = /[^A-Za-z0-9.-]/;

tld.cleanHostValue = function cleanHostValue(value){
value = trim(value).toLowerCase();

var parts = URL.parse(hasPrefixRE.test(value) ? value : '//' + value, null, true);

return parts.hostname || value || '';
if (parts.hostname && !invalidHostnameChars.test(parts.hostname)) { return parts.hostname; }
if (!invalidHostnameChars.test(value)) { return value; }
return '';
};

/**
Expand Down
8 changes: 8 additions & 0 deletions test/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe('tld.js', function () {
//@see https://github.com/oncletom/tld.js/issues/53
tld.getDomain("http://('4drsteve.com', [], ['54.213.246.177'])/xmlrpc.php");
}).not.to.throwError();
expect(function (){
//@see https://github.com/oncletom/tld.js/issues/53
tld.getDomain("('4drsteve.com', [], ['54.213.246.177'])");
}).not.to.throwError();
});

//@see https://github.com/oncletom/tld.js/issues/53
Expand Down Expand Up @@ -252,6 +256,10 @@ describe('tld.js', function () {
//@see https://github.com/oncletom/tld.js/issues/53
tld.getSubdomain("http://('4drsteve.com', [], ['54.213.246.177'])/xmlrpc.php");
}).not.to.throwError();
expect(function (){
//@see https://github.com/oncletom/tld.js/issues/53
tld.getSubdomain("('4drsteve.com', [], ['54.213.246.177'])");
}).not.to.throwError();
});

//@see https://github.com/oncletom/tld.js/issues/53
Expand Down

0 comments on commit 2593cd0

Please sign in to comment.