Skip to content

Commit

Permalink
Reject domain parts longer than 63 bytes, fixes #787
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed May 3, 2018
1 parent f4a9433 commit bb3e542
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/isFQDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ function isFQDN(str, options) {
str = str.substring(0, str.length - 1);
}
var parts = str.split('.');
for (var i = 0; i < parts.length; i++) {
if (parts[i].length > 63) {
return false;
}
}
if (options.require_tld) {
var tld = parts.pop();
if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
Expand All @@ -40,8 +45,8 @@ function isFQDN(str, options) {
return false;
}
}
for (var part, i = 0; i < parts.length; i++) {
part = parts[i];
for (var part, _i = 0; _i < parts.length; _i++) {
part = parts[_i];
if (options.allow_underscores) {
part = part.replace(/_/g, '');
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/isFQDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default function isFQDN(str, options) {
str = str.substring(0, str.length - 1);
}
const parts = str.split('.');
for (let i = 0; i < parts.length; i++) {
if (parts[i].length > 63) {
return false;
}
}
if (options.require_tld) {
const tld = parts.pop();
if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
Expand Down
5 changes: 4 additions & 1 deletion test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('Validators', function () {
'"foobar"@example.com',
'" foo m端ller "@example.com',
'"foo\\@bar"@example.com',
`${repeat('a', 64)}@${repeat('a', 250)}.com`,
`${repeat('a', 64)}@${repeat('a', 63)}.com`,
`${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`,
`${repeat('a', 64)}@${repeat('a', 63)}.com`,
],
invalid: [
'invalidemail@',
Expand All @@ -70,6 +72,7 @@ describe('Validators', function () {
'gmailgmailgmailgmailgmail@gmail.com',
`${repeat('a', 64)}@${repeat('a', 251)}.com`,
`${repeat('a', 65)}@${repeat('a', 250)}.com`,
`${repeat('a', 64)}@${repeat('a', 64)}.com`,
'[email protected] m',
'[email protected] m',
'[email protected] m',
Expand Down
9 changes: 7 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ function isFQDN(str, options) {
str = str.substring(0, str.length - 1);
}
var parts = str.split('.');
for (var i = 0; i < parts.length; i++) {
if (parts[i].length > 63) {
return false;
}
}
if (options.require_tld) {
var tld = parts.pop();
if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
Expand All @@ -149,8 +154,8 @@ function isFQDN(str, options) {
return false;
}
}
for (var part, i = 0; i < parts.length; i++) {
part = parts[i];
for (var part, _i = 0; _i < parts.length; _i++) {
part = parts[_i];
if (options.allow_underscores) {
part = part.replace(/_/g, '');
}
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit bb3e542

Please sign in to comment.