diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index eb6928fda..b5ac68478 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -24,6 +24,11 @@ export default function isFQDN(str, options) { str = str.substring(2); } + /* Presentation-form FQDN max length is 253 (RFC 1035 / RFC 3696). */ + if (!options.ignore_max_length && str.length > 253) { + return false; + } + const parts = str.split('.'); const tld = parts[parts.length - 1]; diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..75fad5c44 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -1332,6 +1332,33 @@ describe('Validators', () => { ], }); }); + it('should reject FQDNs longer than 253 characters unless ignore_max_length is set', () => { + const label = 'a'.repeat(63); + // Each label stays within the 63-char limit; only the overall length differs. + const atLimit = [label, label, label, 'a'.repeat(57), 'com'].join('.'); // 253 + const tooLongByOne = [label, label, label, 'a'.repeat(58), 'com'].join('.'); // 254 + const tooLong = [label, label, label, label, 'comm'].join('.'); // 260 + test({ + validator: 'isFQDN', + valid: [ + atLimit, + ], + invalid: [ + tooLongByOne, + tooLong, + ], + }); + test({ + validator: 'isFQDN', + args: [ + { ignore_max_length: true }, + ], + valid: [ + tooLongByOne, + tooLong, + ], + }); + }); it('should validate FQDN with trailing dot option', () => { test({ validator: 'isFQDN', diff --git a/test/validators/isFQDN.test.js b/test/validators/isFQDN.test.js index 134bab005..7e13a4292 100644 --- a/test/validators/isFQDN.test.js +++ b/test/validators/isFQDN.test.js @@ -23,4 +23,26 @@ describe('isFQDN', () => { ], }); }); + + it('should enforce the 253-character domain length limit', () => { + const label = 'a'.repeat(63); + const atLimit = [label, label, label, 'a'.repeat(57), 'com'].join('.'); + const tooLong = [label, label, label, label, 'comm'].join('.'); + test({ + validator: 'isFQDN', + valid: [ + atLimit, + ], + invalid: [ + tooLong, + ], + }); + test({ + validator: 'isFQDN', + args: [{ ignore_max_length: true }], + valid: [ + tooLong, + ], + }); + }); });