Skip to content

Commit

Permalink
Introduce 'lenient' mode for hostname validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
remusao committed Mar 19, 2018
1 parent 1f257c4 commit 4d99d8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var extractHostname = require('./lib/clean-host.js');
var getDomain = require('./lib/domain.js');
var getPublicSuffix = require('./lib/public-suffix.js');
var getSubdomain = require('./lib/subdomain.js');
var isValidHostname = require('./lib/is-valid.js');
var isValid = require('./lib/is-valid.js');
var isIp = require('./lib/is-ip.js');
var tldExists = require('./lib/tld-exists.js');

Expand All @@ -33,8 +33,23 @@ var ALL = 5;
function factory(options) {
var rules = options.rules || allRules || {};
var validHosts = options.validHosts || [];
var lenientHostnameValidation = options.lenientHostnameValidation === true;
var _extractHostname = options.extractHostname || extractHostname;

// @see https://github.com/oncletom/tld.js/pull/122
var isValidHostname = isValid;
if (lenientHostnameValidation) {
// If 'lenient' mode is enabled, then underscores are allowed to appear in
// hostnames. By default, only alphanumeric characters as well as '-' is
// allowed.
isValidHostname = function (hostname) {
return isValid(hostname, function (code) {
return code === 45 || code === 95;
// ^ - (dash) ^ _ (underscore)
});
};
}

/**
* Process a given url and extract all information. This is a higher level API
* around private functions of `tld.js`. It allows to remove duplication (only
Expand Down
11 changes: 8 additions & 3 deletions lib/is-valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ function isAlpha(code) {
* @param {string} hostname
* @return {boolean}
*/
module.exports = function isValid(hostname) {
module.exports = function isValid(hostname, _extraValidCode) {
var extraValidCode = function (code) { return code === 45; };
if (_extraValidCode !== undefined) {
extraValidCode = _extraValidCode;
}

if (typeof hostname !== 'string') {
return false;
}
Expand Down Expand Up @@ -72,13 +77,13 @@ module.exports = function isValid(hostname) {
// Check that previous character was not already a '.'
lastCharCode === 46 ||
// Check that the previous label does not end with a '-'
lastCharCode === 45
extraValidCode(lastCharCode)
) {
return false;
}

lastDotIndex = i;
} else if (!(isAlpha(code) || isDigit(code) || code === 45)) {
} else if (!(isAlpha(code) || isDigit(code) || extraValidCode(code))) {
// Check if there is a forbidden character in the label: [^a-zA-Z0-9-]
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions test/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ describe('tld.js', function () {
expect(tld.isValid('example.' + repeat('a', 63) + '.')).to.be(true);
expect(tld.isValid('example.' + repeat('a', 63))).to.be(true);

// Rejects domains with '_'
expect(tld.isValid('foo.bar_baz.com')).to.be(false);

//@see https://github.com/oncletom/tld.js/issues/95
expect(tld.isValid('miam.miam.google.com.')).to.be(true);

Expand All @@ -92,6 +95,12 @@ describe('tld.js', function () {
expect(tld.isValid('.google.com')).to.be(false);
expect(tld.isValid('.com')).to.be(false);
});

it('should accept extra code points in domain with lenien mode', function () {
// @see https://github.com/oncletom/tld.js/pull/122
var customTld = tld.fromUserSettings({ lenientHostnameValidation: true });
expect(customTld.isValid('foo.bar_baz.com')).to.be(true);
});
});

describe('isIp method', function () {
Expand Down

0 comments on commit 4d99d8e

Please sign in to comment.